Example #1
0
        private static void FindNodeComplete(object sender, TaskCompleteEventArgs e)
        {
            SendQueryEventArgs args = (SendQueryEventArgs)e;

            lock (engine)
                activities--;
            if (!args.TimedOut)
            {
                logAdd("Visited");
                if (find_queue.Count < 10000)
                {
                    FindNodeResponse response = (FindNodeResponse)args.Response;
                    var ns = Node.FromCompactNode(response.Nodes);
                    foreach (var n in ns)
                    {
                        //Console.WriteLine("Find {1} Node: {0}", n.Id, nodes.Count);
                        lock (find_queue)
                            find_queue.Enqueue(n);
                    }
                }
            }
            else
            {
                nodes.RemoveWhere(n => ((FindNode)args.Query).Target == n.Id);
            }
        }
Example #2
0
        private async Task SendFindNode(IEnumerable <Node> newNodes)
        {
            foreach (Node node in Node.CloserNodes(engine.LocalId, nodes, newNodes, Bucket.MaxCapacity))
            {
                FindNode request = new FindNode(engine.LocalId, engine.LocalId);
                activeRequests++;
                var args = await engine.SendQueryAsync(request, node);

                activeRequests--;

                if (!args.TimedOut)
                {
                    FindNodeResponse response = (FindNodeResponse)args.Response;
                    await SendFindNode(Node.FromCompactNode(response.Nodes));
                }

                if (activeRequests == 0)
                {
                    if (initialNodes.Count > 0 && engine.RoutingTable.CountNodes() < 10)
                    {
                        await new InitialiseTask(engine).ExecuteAsync();
                    }
                }
            }
        }
Example #3
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 #4
0
        public void BucketRefreshTest()
        {
            var nodes = new List <Node>();

            for (var 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(() =>
                {
                    if (!e.TimedOut)
                    {
                        return;
                    }

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

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

            _engine.Add(nodes);
            _engine.Start();

            Thread.Sleep(500);

            foreach (var bucket in _engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(bucket.LastChanged > DateTime.UtcNow.AddSeconds(-2));
                Assert.IsTrue(bucket.Nodes.Exists(n => n.LastSeen > DateTime.UtcNow.AddMilliseconds(-900)));
            }
        }
Example #5
0
        public async Task 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)));
            }

            listener.MessageSent += (data, endpoint) => {
                engine.MessageLoop.DhtMessageFactory.TryDecodeMessage(BEncodedValue.Decode <BEncodedDictionary> (data), out DhtMessage message);

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

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

            foreach (var n in nodes)
            {
                engine.RoutingTable.Add(n);
            }

            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                b.Changed(TimeSpan.FromDays(1));
                foreach (var n in b.Nodes)
                {
                    n.Seen(TimeSpan.FromDays(1));
                }
            }

            await engine.RefreshBuckets();

            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(b.LastChanged < TimeSpan.FromHours(1));
                Assert.IsTrue(b.Nodes.Exists(n => n.LastSeen < TimeSpan.FromHours(1)));
            }
        }
Example #6
0
        public void FindNodeResponseDecode()
        {
            FindNodeEncode();
            MessageFactory.RegisterSend(message);
            string           text = "d1:rd2:id20:abcdefghij01234567895:nodes9:def456...e1:t2:aa1:y1:re";
            FindNodeResponse m    = (FindNodeResponse)Decode(text);

            Assert.AreEqual(id, m.Id, "#1");
            Assert.AreEqual((BEncodedString)"def456...", m.Nodes, "#2");
            Assert.AreEqual(transactionId, m.TransactionId, "#3");

            Compare(m, text);
        }
Example #7
0
        public async System.Threading.Tasks.Task BucketRefreshTestAsync()
        {
            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);
                    }
                });
            };

            await engine.AddAsync(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)); }));
            }
        }
Example #8
0
        public override void Handle(DhtEngine engine, Node node)
        {
            base.Handle(engine, node);

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

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

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

            engine.MessageLoop.EnqueueSend(response, node.EndPoint);
        }
Example #9
0
        public async Task 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.MessageLoop.Timeout  = TimeSpan.FromMilliseconds(25);
            engine.BucketRefreshTimeout = TimeSpan.FromMilliseconds(75);
            listener.MessageSent       += (message, endpoint) => {
                Node current = nodes.Find(delegate(Node n) { return(n.EndPoint.Port.Equals(endpoint.Port)); });
                if (current == null)
                {
                    return;
                }

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

            engine.Add(nodes);

            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                b.Changed(TimeSpan.FromDays(-1));
            }

            await engine.StartAsync();

            await engine.WaitForState(DhtState.Ready);

            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(b.LastChanged < TimeSpan.FromMinutes(1));
                Assert.IsTrue(b.Nodes.Exists(delegate(Node n) { return(n.LastSeen < TimeSpan.FromMilliseconds(900)); }));
            }
        }
        private void FindNodeComplete(object sender, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= FindNodeComplete;
            activeRequests--;

            SendQueryEventArgs args = (SendQueryEventArgs)e;

            if (!args.TimedOut)
            {
                FindNodeResponse response = (FindNodeResponse)args.Response;
                SendFindNode(Node.FromCompactNode(response.Nodes));
            }

            if (activeRequests == 0)
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
        }