Example #1
0
        public void FindNodeResponseEncode()
        {
            FindNodeResponse m = new FindNodeResponse(id, transactionId);
            m.Nodes = "def456...";

            Compare(m, "d1:rd2:id20:abcdefghij01234567895:nodes9:def456...e1:t2:aa1:y1:re");
        }
Example #2
0
		public override void Handle(DhtEngine engine, Node node)
		{
			base.Handle(engine, node);

			var response = new FindNodeResponse(engine.RoutingTable.LocalNode.Id, TransactionId);

			Node targetNode = engine.RoutingTable.FindNode(Target);
			if (targetNode != null)
				response.Nodes = targetNode.CompactNode();
			else
				response.Nodes = Node.CompactNode(engine.RoutingTable.GetClosest(Target));

			engine.MessageLoop.EnqueueSend(response, node.EndPoint);
		}
Example #3
0
        public override void Handle(DhtEngine engine, Node node)
        {
            base.Handle(engine, node);

            var response = new FindNodeResponse(engine.RoutingTable.LocalNode.Id, TransactionId);

            Node targetNode = engine.RoutingTable.FindNode(Target);

            response.Nodes = targetNode != null
                ? targetNode.CompactNode()
                : Node.CompactNode(engine.RoutingTable.GetClosest(Target));

            engine.MessageLoop.EnqueueSend(response, node, node.EndPoint);
        }
Example #4
0
        public override async Task HandleAsync(DhtEngine engine, Node node)
        {
            await base.HandleAsync(engine, node);

            FindNodeResponse response = new FindNodeResponse(engine.RoutingTable.LocalNode.Id, TransactionId);

            Node targetNode = engine.RoutingTable.FindNode(Target);

            if (targetNode != null)
            {
                response.Nodes = targetNode.CompactNode();
            }
            else
            {
                response.Nodes = Node.CompactNode(engine.RoutingTable.GetClosest(Target));
            }

            engine.MessageLoop.EnqueueSend(response, node.EndPoint);
        }
Example #5
0
        public void BucketRefreshTest()
        {
            List<Node> nodes = new List<Node>();
            for (int i = 0; i < 5; i++)
                nodes.Add(new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)));

            engine.TimeOut = TimeSpan.FromMilliseconds(25);
            engine.BucketRefreshTimeout = TimeSpan.FromMilliseconds(75);
            engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e)
            {
                DhtEngine.MainLoop.Queue(delegate
                {
                    if (!e.TimedOut)
                        return;

                    Node current = nodes.Find(delegate(Node n) { return n.EndPoint.Port.Equals(e.EndPoint.Port); });
                    if (current == null)
                        return;

                    if (e.Query is Ping)
                    {
                        PingResponse r = new PingResponse(current.Id, e.Query.TransactionId);
                        listener.RaiseMessageReceived(r, current.EndPoint);
                    }
                    else if (e.Query is FindNode)
                    {
                        FindNodeResponse response = new FindNodeResponse(current.Id, e.Query.TransactionId);
                        response.Nodes = "";
                        listener.RaiseMessageReceived(response, current.EndPoint);
                    }
                });
            };

            engine.Add(nodes);
            engine.Start();

            System.Threading.Thread.Sleep(500);
            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(b.LastChanged > DateTime.UtcNow.AddSeconds(-2));
                Assert.IsTrue(b.Nodes.Exists(delegate(Node n) { return n.LastSeen > DateTime.UtcNow.AddMilliseconds(-900); }));
            }
        }