Пример #1
0
        static void Main(string[] args)
        {
            if (args != null && args.Length > 0)
            {
                ParseArguments(args, out _Ip, out _Port, out _PeerIpPorts);
                _IpPort = _Ip + ":" + _Port;
            }
            else
            {
                _IpPort = InputString("Local IP:port:", "127.0.0.1:8000", false);
                ParseIpPort(_IpPort, out _Ip, out _Port);
            }

            _Settings = new MeshSettings();
            _Settings.AcceptInvalidCertificates = true;
            _Settings.AutomaticReconnect        = true;
            _Settings.MutuallyAuthenticate      = false;
            _Settings.PresharedKey        = null;
            _Settings.StreamBufferSize    = 65536;
            _Settings.ReconnectIntervalMs = 1000;

            _Mesh = new MeshNode(_Ip, _Port);
            _Mesh.PeerConnected      += PeerConnected;
            _Mesh.PeerDisconnected   += PeerDisconnected;
            _Mesh.MessageReceived    += MessageReceived;
            _Mesh.SyncMessageReceived = SyncMessageReceived;
            // _Mesh.Logger = Logger;

            _Mesh.Start();

            if (_PeerIpPorts != null && _PeerIpPorts.Count > 0)
            {
                Console.Write("Adding peers: ");
                foreach (string curr in _PeerIpPorts)
                {
                    Console.Write(curr + " ");
                    _Mesh.Add(new MeshPeer(curr, false));
                }
                Console.WriteLine("");
            }

            while (_RunForever)
            {
                string userInput = InputString("WatsonMesh [? for help] >", null, false);

                List <MeshPeer> 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 (MeshPeer 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 (MeshPeer currPeer in peers)
                        {
                            Console.WriteLine("  " + currPeer.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("None");
                    }
                    break;

                case "send":
                    Send();
                    break;

                case "send md":
                    SendMetadata();
                    break;

                case "sendsync":
                    SendSync();
                    break;

                case "sendsync md":
                    SendSyncMetadata();
                    break;

                case "bcast":
                    Broadcast();
                    break;

                case "add":
                    _Mesh.Add(
                        new MeshPeer(
                            InputString("IP:port:", "127.0.0.1:8000", false),
                            false));
                    break;

                case "del":
                    _Mesh.Remove(
                        new MeshPeer(
                            InputString("IP:port:", "127.0.0.1:8000", false),
                            false));
                    break;

                case "health":
                    Console.WriteLine("Healthy: " + _Mesh.IsHealthy);
                    break;

                case "nodehealth":
                    Console.WriteLine(
                        _Mesh.IsServerConnected(
                            InputString("IP:Port", "127.0.0.1:8000", false)));
                    break;
                }
            }
        }