Ejemplo n.º 1
0
        public void HandlerEvent(UdpEvent udp_event)
        {
            Message    message = udp_event.Message;
            IPEndPoint sender  = udp_event.Address;

            Node node = new Node(message.From.Id, sender.Address.ToString(), sender.Port);

            if (this.is_inbound_known_node && !ContainNodeHandler(node))
            {
                Logger.Warning(
                    string.Format("Receive packet from unknown node {0}.", sender.Address.ToString()));

                return;
            }

            NodeHandler handler = GetNodeHandler(node);

            handler.NodeStatistics.MessageStatistics.AddUdpInMessage(message.Type);
            switch (message.Type)
            {
            case UdpMessageType.DISCOVER_PING:
            {
                handler.HandlePing((PingMessage)message);
            }
            break;

            case UdpMessageType.DISCOVER_PONG:
            {
                handler.HandlePong((PongMessage)message);
            }
            break;

            case UdpMessageType.DISCOVER_FIND_NODE:
            {
                handler.HandleFindNode((FindNodeMessage)message);
            }
            break;

            case UdpMessageType.DISCOVER_NEIGHBORS:
            {
                handler.HandleNeighbours((NeighborsMessage)message);
            }
            break;

            default:
                break;
            }
        }
Ejemplo n.º 2
0
        public NodeHandler GetNodeHandler(Node node)
        {
            string key = GetKey(node);

            this.node_handlers.TryGetValue(key, out NodeHandler result);

            if (result == null)
            {
                TrimTable();
                result = new NodeHandler(node, this);
                this.node_handlers.Add(key, result);
            }
            else if (result.Node.IsDiscoveryNode && !node.IsDiscoveryNode)
            {
                result.Node = node;
            }

            return(result);
        }
Ejemplo n.º 3
0
        public void ChangeState(NodeHandlerState new_state)
        {
            NodeHandlerState old_state = this.state;

            if (new_state == NodeHandlerState.Discovered)
            {
                if (this.source_node != null && this.source_node.Port != this.node.Port)
                {
                    ChangeState(NodeHandlerState.Dead);
                }
                else
                {
                    SendPing();
                }
            }
            if (!node.IsDiscoveryNode)
            {
                if (new_state == NodeHandlerState.Alive)
                {
                    Node evict_candidate = this.node_manager.Table.AddNode(this.node);
                    if (evict_candidate == null)
                    {
                        new_state = NodeHandlerState.Active;
                    }
                    else
                    {
                        NodeHandler evict_handler = this.node_manager.GetNodeHandler(evict_candidate);
                        if (evict_handler.State != NodeHandlerState.EvictCandidate)
                        {
                            evict_handler.ChallengeWith(this);
                        }
                    }
                }
                if (new_state == NodeHandlerState.Active)
                {
                    if (old_state == NodeHandlerState.Alive)
                    {
                        this.node_manager.Table.AddNode(node);
                    }
                    else if (old_state == NodeHandlerState.EvictCandidate)
                    {
                    }
                    else
                    {
                    }
                }

                if (new_state == NodeHandlerState.NonActive)
                {
                    if (old_state == NodeHandlerState.EvictCandidate)
                    {
                        this.node_manager.Table.DropNode(node);
                        this.replace_candidate.ChangeState(NodeHandlerState.Active);
                    }
                    else if (old_state == NodeHandlerState.Alive)
                    {
                    }
                    else
                    {
                    }
                }
            }

            if (new_state == NodeHandlerState.EvictCandidate)
            {
                SendPing();
            }
            state = new_state;
        }
Ejemplo n.º 4
0
 public bool IsNodeAlive(NodeHandler handler)
 {
     return(handler.State == NodeHandler.NodeHandlerState.Alive ||
            handler.State == NodeHandler.NodeHandlerState.Active ||
            handler.State == NodeHandler.NodeHandlerState.EvictCandidate);
 }
Ejemplo n.º 5
0
 private void ChallengeWith(NodeHandler replace_candidate)
 {
     this.replace_candidate = replace_candidate;
     ChangeState(NodeHandlerState.EvictCandidate);
 }