public DiscoverableMeshEthNetwork( string meshName, NetworkInterfaceType network = NetworkInterfaceType.Ethernet, AddressFamily addressFamily = AddressFamily.InterNetwork, int fromPortRange = 8048, int toPortRange = 12000) { _Port = r.Next(fromPortRange, toPortRange); _Ip = GetFirstIpAddress(network, addressFamily); _settings = new MeshSettings(); Self = new Peer(_Ip.ToString(), _Port, false); Log.Info($"I am {Self.IpPort}"); _mesh = new WatsonMesh(_settings, Self) { PeerConnected = PeerConnected, PeerDisconnected = PeerDisconnected, AsyncMessageReceived = AsyncMessageReceived, SyncMessageReceived = SyncMessageReceived }; _mesh.StartServer(); var b = new Beacon(meshName, (ushort)_Port) { BeaconData = _Ip.ToString() }; b.Start(); probe = new Probe(meshName); probe.BeaconsUpdated += delegate(IEnumerable <BeaconLocation> locations) { var connectedPeers = _mesh.GetPeers() .Select(p => p.IpPort) .ToList(); var newNodes = locations .Where(x => !connectedPeers.Contains($"{x.Data}:{x.Address.Port}")) .ToArray(); foreach (var beacon in newNodes) { var peer = new Peer(beacon.Data, beacon.Address.Port, ssl: false); Console.Write($"Mesh -> Adding peer {peer.Ip}:{peer.Port} to mesh ... "); _mesh.Add(peer); Log.Info("Connected."); } }; probe.Start(); }
static void Main(string[] args) { _Ip = Common.InputString("Listener IP:", "127.0.0.1", false); _Port = Common.InputInteger("Listener Port:", 8000, true, false); _Settings = new MeshSettings(); _Settings.AcceptInvalidCertificates = true; _Settings.AutomaticReconnect = true; _Settings.Debug = false; _Settings.MutuallyAuthenticate = false; _Settings.PresharedKey = null; _Settings.ReadDataStream = true; _Settings.ReadStreamBufferSize = 65536; _Settings.ReconnectIntervalMs = 1000; _Self = new Peer(_Ip, _Port); _Mesh = new WatsonMesh(_Settings, _Self); _Mesh.PeerConnected = PeerConnected; _Mesh.PeerDisconnected = PeerDisconnected; _Mesh.AsyncMessageReceived = AsyncMessageReceived; _Mesh.SyncMessageReceived = SyncMessageReceived; _Mesh.AsyncStreamReceived = AsyncStreamReceived; _Mesh.SyncStreamReceived = SyncStreamReceived; _Mesh.Start(); while (_RunForever) { string userInput = Common.InputString("WatsonMesh [? for help] >", null, false); List <Peer> peers; switch (userInput) { case "?": Menu(); break; case "q": case "quit": _RunForever = false; break; case "c": case "cls": Console.Clear(); break; case "list": peers = _Mesh.GetPeers(); if (peers != null && peers.Count > 0) { Console.WriteLine("Configured peers: " + peers.Count); foreach (Peer curr in peers) { Console.WriteLine(" " + curr.ToString()); } } else { Console.WriteLine("None"); } break; case "failed": peers = _Mesh.GetDisconnectedPeers(); if (peers != null && peers.Count > 0) { Console.WriteLine("Failed peers: " + peers.Count); foreach (Peer currPeer in peers) { Console.WriteLine(" " + currPeer.ToString()); } } else { Console.WriteLine("None"); } break; case "sendasync": userInput = Common.InputString("Data:", "some data", false); if (_Mesh.SendAsync( Common.InputString("Peer IP", "127.0.0.1", false), Common.InputInteger("Peer port:", 8000, true, false), Encoding.UTF8.GetBytes(userInput))) { Console.WriteLine("Success"); } else { Console.WriteLine("Failed"); } break; case "sendsync": SendSync(); break; case "bcast": if (_Mesh.Broadcast( Encoding.UTF8.GetBytes(Common.InputString("Data:", "some data", false)))) { Console.WriteLine("Success"); } else { Console.WriteLine("Failed"); } break; case "add": _Mesh.Add( new Peer( Common.InputString("Peer IP:", "127.0.0.1", false), Common.InputInteger("Peer port:", 8000, true, false), false)); break; case "del": _Mesh.Remove( new Peer( Common.InputString("Peer IP:", "127.0.0.1", false), Common.InputInteger("Peer port:", 8000, true, false), false)); break; case "health": Console.WriteLine("Healthy: " + _Mesh.IsHealthy()); break; case "nodehealth": Console.WriteLine( _Mesh.IsHealthy( Common.InputString("Peer IP:", "127.0.0.1", false), Common.InputInteger("Peer port:", 8000, true, false))); break; } } }