private void ReceivedGraph(Endpoint remoteEndpoint, string graphjson) { Console.WriteLine("Got graph update from " + remoteEndpoint.Name); JsonSerializerSettings settings = new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, ContractResolver = new PrivateResolver() }; List <GraphUpdate> remoteupdates = JsonConvert.DeserializeObject <List <GraphUpdate> >(graphjson, settings); foreach (GraphUpdate nodeupdate in remoteupdates) { //Create or retrieve current node Node activenode = remoteEndpoint.CreateNode(nodeupdate.node); var missingremoteinputs = nodeupdate.node.Inputs.Where(i => i.destroyed); var missingremoteoutputs = nodeupdate.node.Outputs.Where(o => o.destroyed); var newremoteinputs = nodeupdate.node.Inputs.Where(p => !activenode.Inputs.Any(l => p.Path == l.Path) && !p.destroyed); var newremoteoutputs = nodeupdate.node.Outputs.Where(p => !activenode.Outputs.Any(l => p.Path == l.Path) && !p.destroyed); //Remove old plugs from the local node foreach (InputPlug input in missingremoteinputs) { InputPlug localinput = activenode.Inputs.Find(i => input.Name == i.Name); localinput.Dispose(); } foreach (OutputPlug output in missingremoteoutputs) { OutputPlug localoutput = activenode.Outputs.Find(o => output.Name == o.Name); localoutput.Dispose(); } //Add new plugs to nodes. foreach (InputPlug newinput in newremoteinputs) { activenode.CreateInputPlug(newinput); } foreach (OutputPlug newoutput in newremoteoutputs) { activenode.CreateOutputPlug(newoutput); } } //Remove missing nodes that have disappeared from the remote endpoint var missingnodes = remoteEndpoint.Nodes.Where(p => !remoteupdates.Any(l => p.Name == l.node.Name)); foreach (Node node in missingnodes) { node.Dispose(); } Nodes.RemoveAll(n => missingnodes.Any(o => o.Name == n.Name)); }
public static void Main(string[] args) { string guid = Guid.NewGuid().ToString(); string endpointid = guid.Substring(Math.Max(0, guid.Length - 4)); using (ZyreEndpoint local = new ZyreEndpoint(endpointid, (s) => { Console.WriteLine("LOCAL: " + s); })) { Console.WriteLine("Starting Showtime test"); System.Threading.Thread.Sleep(1000); Console.Clear(); Console.WriteLine("How many nodes to create?"); CreateNodes(local, 2); LinkNodesInChain(local); Console.WriteLine("Connect to local or remote? (l/r)"); Endpoint targetEndpoint = null; while (targetEndpoint == null) { char key = Console.ReadKey().KeyChar; Console.WriteLine(""); if (key == 'l') { targetEndpoint = local; Console.WriteLine("Test node chain?"); if ((Console.ReadKey().KeyChar == 'y') ? true : false) { LinkNodesInChain(targetEndpoint); TestNodeLink(targetEndpoint); } } else if (key == 'r') { local.StartZyre(); local.ListNodes(); Console.WriteLine("Waiting for remote nodes..."); while (local.RemoteEndpoints.Values.Count == 0) { System.Threading.Thread.Sleep(100); } foreach (Endpoint remoteEndpoint in local.RemoteEndpoints.Values) { targetEndpoint = remoteEndpoint; break; } } else { Console.WriteLine(string.Format("Didn't understand input \'{0}\'. Please enter l or r", key)); } } //Setup message test local.ListNodes(); Console.WriteLine("\nEnter index of source node"); Node outnode = local.Nodes[int.Parse(Console.ReadLine())]; outnode.ListOutputs(); Console.WriteLine("\nEnter index of source plug"); OutputPlug output = outnode.Outputs[int.Parse(Console.ReadLine())]; targetEndpoint.ListNodes(); Console.WriteLine("\nEnter index of destination node"); Node innode = targetEndpoint.Nodes[int.Parse(Console.ReadLine())]; innode.ListInputs(); Console.WriteLine("\nEnter index of destination plug"); InputPlug input = innode.Inputs[int.Parse(Console.ReadLine())]; Console.WriteLine(string.Format("About to connect {0} -> {1}", output.Path, input.Path)); input.Connect(output); System.Threading.Thread.Sleep(1000); do { Console.WriteLine("\nEnter message:"); output.Update(Console.ReadLine()); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); Console.WriteLine("Testing local output removal"); output.Dispose(); Console.WriteLine("\nRemaining outputs:"); outnode.ListOutputs(); System.Threading.Thread.Sleep(1000); Console.WriteLine("Testing local node removal"); outnode.Dispose(); Console.WriteLine("\nRemaining nodes:"); local.ListNodes(); System.Threading.Thread.Sleep(1000); //local.Dispose(); } }