public void VerifyObject(Boolean forceInterfaceUpdateFromServer, RemoteNpcObject expectedObject) { if (expectedInterfaces == null) { throw new InvalidOperationException(String.Format( "Cannot call VerifyObject because there were no expectedInterfaces passed to the constructor")); } GetServerInterface(forceInterfaceUpdateFromServer); Boolean foundObject = false; for (int serverObjectIndex = 0; serverObjectIndex < cachedServerObjects.Count; serverObjectIndex++) { RemoteNpcObject serverObject = cachedServerObjects[serverObjectIndex]; if (expectedObject.name.Equals(serverObject.name)) { foundObject = true; // Check that the interfaces are the same for (int expectedInterfaceIndex = 0; expectedInterfaceIndex < expectedObject.interfaces.Length; expectedInterfaceIndex++) { RemoteNpcInterface expectedInterface = expectedObject.interfaces[expectedInterfaceIndex]; Boolean foundInterface = false; for (int serverInterfaceIndex = 0; serverInterfaceIndex < serverObject.interfaces.Length; serverInterfaceIndex++) { RemoteNpcInterface serverInterface = serverObject.interfaces[serverInterfaceIndex]; if (expectedInterface.name.Equals(serverInterface.name)) { foundInterface = true; // Note: the interface definition does not need to be checked because it was already checked break; } } if (!foundInterface) { throw new InvalidOperationException(String.Format("Server object '{0}' is missing the '{1}' interface", serverObject.name, expectedInterface.name)); } } break; } } if (!foundObject) { throw new InvalidOperationException(String.Format("Server missing '{0}' object", expectedObject.name)); } }
// returns false if interactive mode should stop Boolean ProcessInteractiveCommand(String command) { if (command.Equals("help", StringComparison.CurrentCultureIgnoreCase)) { // // Request methods from server // Console.WriteLine("Format: <Interface> <Method> [<Parameters>...]"); List <RemoteNpcObject> objects = client.GetServerInterface(false); for (int objectIndex = 0; objectIndex < objects.Count; objectIndex++) { RemoteNpcObject npcObject = objects[objectIndex]; Console.WriteLine(interfaceMapping.NpcToUserInterface(npcObject.name)); for (int interfaceIndex = 0; interfaceIndex < npcObject.interfaces.Length; interfaceIndex++) { RemoteNpcInterface npcInterface = npcObject.interfaces[interfaceIndex]; for (int i = 0; i < npcInterface.methods.Length; i++) { SosMethodDefinition method = npcInterface.methods[i]; Console.Write(" {0}", method.methodName); if (method.parameters != null) { foreach (SosMethodDefinition.Parameter parameter in method.parameters) { Console.Write(" {0}", parameter.name); } } Console.WriteLine(); } } } } else if (command.Equals("exit", StringComparison.CurrentCultureIgnoreCase) || command.Equals("q", StringComparison.CurrentCultureIgnoreCase) || command.Equals("quit", StringComparison.CurrentCultureIgnoreCase)) { return(false); } else { Console.WriteLine("Error: Unknown interface/command '{0}'", command); } return(true); }
public static List <RemoteNpcObject> GetServerInterface(SocketLineReader socketLineReader, out Dictionary <String, RemoteNpcInterface> serverInterfaces) { socketLineReader.socket.Send(Encoding.ASCII.GetBytes(":interface\n")); serverInterfaces = new Dictionary <String, RemoteNpcInterface>(); List <SosMethodDefinition> methodDefinitionList = new List <SosMethodDefinition>(); while (true) { String interfaceName = socketLineReader.ReadLine(); if (interfaceName == null) { throw UnexpectedClose(socketLineReader); } if (interfaceName.Length <= 0) { break; } // Get parent interfaces String[] parentInterfaceNames = null; Int32 spaceIndex = interfaceName.IndexOf(' '); if (spaceIndex >= 0) { parentInterfaceNames = interfaceName.Substring(spaceIndex + 1).Split(' '); interfaceName = interfaceName.Remove(spaceIndex); } while (true) { String methodDefinitionLine = socketLineReader.ReadLine(); if (methodDefinitionLine == null) { throw UnexpectedClose(socketLineReader); } if (methodDefinitionLine.Length <= 0) { break; } SosMethodDefinition methodDefinition = SosTypes.ParseMethodDefinition(methodDefinitionLine, 0); methodDefinitionList.Add(methodDefinition); } serverInterfaces.Add(interfaceName, new RemoteNpcInterface(interfaceName, parentInterfaceNames, methodDefinitionList.ToArray())); methodDefinitionList.Clear(); } List <RemoteNpcObject> serverObjects = new List <RemoteNpcObject>(); while (true) { String objectLine = socketLineReader.ReadLine(); if (objectLine == null) { throw UnexpectedClose(socketLineReader); } if (objectLine.Length <= 0) { break; } String objectName = objectLine.Peel(out objectLine); String[] interfaceNames = objectLine.Split(RemoteNpcObject.SplitChars, StringSplitOptions.RemoveEmptyEntries); RemoteNpcInterface[] interfaces = new RemoteNpcInterface[interfaceNames.Length]; for (int i = 0; i < interfaceNames.Length; i++) { String interfaceName = interfaceNames[i]; RemoteNpcInterface npcInterface; if (!serverInterfaces.TryGetValue(interfaceName, out npcInterface)) { throw new FormatException(String.Format("The NPC server returned interface '{0}' in the :objects command but not in the :interfaces command", interfaceName)); } interfaces[i] = npcInterface; } serverObjects.Add(new RemoteNpcObject(objectName, interfaces)); } return(serverObjects); }