private void OnTcpMessageUnregister(ITcpSession session, Message message)
        {
            // unregister requested enode
            ElasticAddress address = message.Value <string>("enode");

            // verify message
            PrivateKey nodeKey = MasterKey.CKD($"{address.Host}:{address.Port}");

            if (!message.Verify(nodeKey.Address))
            {
                Logger.warning("Unverified message");
                return;
            }

            // valid address?
            if (nodeKey.Address != address.Address)
            {
                Logger.warning("Invalid address");
                return;
            }

            // remove peer
            Peers.Remove(address);

            // sync all peers
            SyncAllPeers();
        }
        private void OnTcpMessagePeek(ITcpSession session, Message message)
        {
            ElasticAddress address = message.Value <string>("enode");

            // verify message
            PrivateKey nodeKey = MasterKey.CKD($"{address.Host}:{address.Port}");

            if (!message.Verify(nodeKey.Address))
            {
                Logger.warning("Unverified message");
                return;
            }

            // valid address?
            if (nodeKey.Address != address.Address)
            {
                Logger.warning("Invalid address");
                return;
            }

            Message msg = new Message.Builder()
                          .Body(ACTION, ACTION_PEEK)
                          .Body("peers", Peers.Count)
                          .Build(MasterKey);

            session?.Write(msg.ToBytes());
        }
        public void Start(int port, int acceptWorkers, CancellationTokenSource cts)
        {
            this.cts = cts;

            string host = Configuration.GetValue("Host", TcpHelper.ANY);

            if (port <= 0)
            {
                port = Configuration.GetValue("Port", 9000);
            }

            NodeKey  = PeerListClient.MasterKey.CKD($"{host}:{port}");
            EndPoint = new ElasticAddress(NodeKey.Address, host, port);

            // start catalog service
            PeerListClient.Start(NodeKey, EndPoint);

            // Elastic Node Host start
            ElasticNode = new ElasticNode(Configuration, Logger, Peers, NodeKey, EndPoint)
            {
                OnMessage = OnElasticMessage
            };
            ElasticNode.Start(cts);

            // ElasticGridNetworkTests
            Test = new ElasticGridNetworkTests(Logger, ElasticNode, Peers, NodeKey);
        }
Beispiel #4
0
        public void Start(PrivateKey nodeKey, ElasticAddress nodeUrl)
        {
            NodeKey = nodeKey;
            NodeUrl = nodeUrl;

            Start();
        }
        public void OnMessagePing(ElasticAddress sender, Message ping)
        {
            Message pong = new Message.Builder()
                           .Action("pong")
                           .Body("msgHash", ping.ID)
                           .Build(NodeKey);

            SendTo(pong, sender);
        }
 public void AddItem(string messageId, ElasticAddress peer)
 {
     lock (_reports)
     {
         if (_reports.ContainsKey(messageId))
         {
             _reports[messageId].Append(peer);
         }
     }
 }
        public void OnElasticMessage(ElasticAddress sender, Message message)
        {
            switch (message.Action())
            {
            case "ping": OnTcpMessagePing(sender, message); break;

            case "pong": OnTcpMessagePong(sender, message); break;

            default: break;
            }
        }
Beispiel #8
0
        public PeerListClient(IConfiguration configuration, ILoggable logger, IPeerList peers)
        {
            Configuration = configuration;
            Logger        = logger;
            Peers         = peers;

            ServerUrl = Configuration.GetValue <string>("ServerUrl");
            MasterKey = new PrivateKey(Configuration.GetValue <string>("MasterKey"));

            Client = new TcpClient()
            {
                OnConnect     = OnTcpConnect,
                OnConnectFail = OnTcpConnectFail,
                OnDisconnect  = OnTcpClose,
                OnMessage     = OnTcpMessage
            };
        }
        private void OnTcpMessageUpdate(ITcpSession session, Message message)
        {
            ElasticAddress address = message.Value <string>("enode");

            // verify message
            PrivateKey nodeKey = MasterKey.CKD($"{address.Host}:{address.Port}");

            if (!message.Verify(nodeKey.Address))
            {
                Logger.warning("Unverified message");
                return;
            }

            // valid address?
            if (nodeKey.Address != address.Address)
            {
                Logger.warning("Invalid address");
                return;
            }

            SyncPeers(session, Peers.Peers);
        }
        private void OnTcpMessageRegister(ITcpSession session, Message message)
        {
            // register requested enode
            ElasticAddress address = message.Value <string>("enode");

            // verify message
            PrivateKey nodeKey = MasterKey.CKD($"{address.Host}:{address.Port}");

            if (!message.Verify(nodeKey.Address))
            {
                Logger.warning("Unverified message!");
                return;
            }

            // valid address?
            if (nodeKey.Address != address.Address)
            {
                Logger.warning("Invalid address");
                return;
            }

            // register peer
            Peers.Append(address);

            // session
            lock (_sessionMap)
                _sessionMap[session.SID] = address.Address;

            lock (BConsole.Lock)
            {
                BConsole.Write(ConsoleColor.DarkCyan, address.ToString());
                BConsole.WriteLine(" registered!");
            }

            SyncAllPeers();
        }
 public void Append(ElasticAddress peer)
 {
     _received[peer] = (DateTime.Now.Ticks - MessageTime) / 10000;
 }
        public void OnMessagePong(ElasticAddress sender, Message pong)
        {
            string messageId = pong.Value <string>("msgHash");

            Reports.AddItem(messageId, sender);
        }
 public void SendTo(Message message, ElasticAddress peer)
 {
     Node.SendTo(message, peer);
 }
 private int OnTcpMessagePong(ElasticAddress sender, Message message)
 {
     Test?.OnMessagePong(sender, message);
     return(0);
 }