예제 #1
0
        public void SendQueryTaskSucceed()
        {
            engine.TimeOut = TimeSpan.FromMilliseconds(25);

            Ping ping = new Ping(engine.LocalId);
            ping.TransactionId = transactionId;
            engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e)
            {
                if (e.TimedOut)
                {
                    counter++;
                    PingResponse response = new PingResponse(node.Id, transactionId);
                    listener.RaiseMessageReceived(response, node.EndPoint);
                }
            };

            SendQueryTask task = new SendQueryTask(engine, ping, node);
            task.Completed += delegate { handle.Set(); };
            task.Execute();

            Assert.IsTrue(handle.WaitOne(3000, false), "#1");
            System.Threading.Thread.Sleep(200);
            Assert.AreEqual(1, counter, "#2");
            Node n = engine.RoutingTable.FindNode(this.node.Id);
            Assert.IsNotNull(n, "#3");
            Assert.IsTrue(n.LastSeen > DateTime.UtcNow.AddSeconds(-2));
        }
예제 #2
0
 private void QueryNode(Node node)
 {
     this.node = node;
     message = new FindNode(engine.LocalId, node.Id);
     task = new SendQueryTask(engine, message, node);
     task.Completed += TaskComplete;
     task.Execute();
 }
예제 #3
0
        private void SendGetPeers(Node n)
        {
            NodeId distance = n.Id.Xor(infoHash);
            queriedNodes.Add(distance, n);

            activeQueries++;
            GetPeers m = new GetPeers(engine.LocalId, infoHash);
            SendQueryTask task = new SendQueryTask(engine, m, n);
            task.Completed += GetPeersCompleted;
            task.Execute();
        }
예제 #4
0
 private void SendFindNode(IEnumerable <Node> newNodes)
 {
     foreach (Node node in Node.CloserNodes(engine.LocalId, nodes, newNodes, Bucket.MaxCapacity))
     {
         activeRequests++;
         FindNode      request = new FindNode(engine.LocalId, engine.LocalId);
         SendQueryTask task    = new SendQueryTask(engine, request, node);
         task.Completed += FindNodeComplete;
         task.Execute();
     }
 }
예제 #5
0
        private void SendGetPeers(Node n)
        {
            var distance = n.Id.Xor(infoHash);
            ClosestActiveNodes.Add(distance, n);

            activeQueries++;
            var m = new GetPeers(engine.LocalId, infoHash);
            var task = new SendQueryTask(engine, m, n);
            task.Completed += GetPeersCompleted;
            task.Execute();
        }
예제 #6
0
        private void SendGetPeers(Node n)
        {
            NodeId distance = n.Id.Xor(infoHash);

            queriedNodes.Add(distance, n);

            activeQueries++;
            GetPeers      m    = new GetPeers(engine.LocalId, infoHash);
            SendQueryTask task = new SendQueryTask(engine, m, n);

            task.Completed += GetPeersCompleted;
            task.Execute();
        }
예제 #7
0
        private void SendPingToOldest()
        {
            bucket.LastChanged = DateTime.UtcNow;
            bucket.SortBySeen();

            if ((DateTime.UtcNow - bucket.Nodes[0].LastSeen) < TimeSpan.FromMinutes(3))
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
            else
            {
                Node oldest = bucket.Nodes[0];
                SendQueryTask task = new SendQueryTask(engine, new Ping(engine.LocalId), oldest);
                task.Completed += TaskComplete;
                task.Execute();
            }
        }
예제 #8
0
        private void SendPingToOldest()
        {
            bucket.LastChanged = DateTime.UtcNow;
            bucket.SortBySeen();

            if ((DateTime.UtcNow - bucket.Nodes[0].LastSeen) < TimeSpan.FromMinutes(3))
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
            else
            {
                Node          oldest = bucket.Nodes[0];
                SendQueryTask task   = new SendQueryTask(engine, new Ping(engine.LocalId), oldest);
                task.Completed += TaskComplete;
                task.Execute();
            }
        }
예제 #9
0
        public void SendQueryTaskTimeout()
        {
            engine.TimeOut = TimeSpan.FromMilliseconds(25);

            Ping ping = new Ping(engine.LocalId);
            ping.TransactionId = transactionId;
            engine.MessageLoop.QuerySent += delegate (object o, SendQueryEventArgs e) {
                if(e.TimedOut)
                    counter++;
            };

            SendQueryTask task = new SendQueryTask(engine, ping, node);
            task.Completed += delegate { handle.Set(); };
            task.Execute();
            Assert.IsTrue(handle.WaitOne(3000, false), "#1");
            Assert.AreEqual(task.Retries, counter);
        }
예제 #10
0
        private void GotPeers(object o, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= GotPeers;
            GetPeersTask getpeers = (GetPeersTask)e.Task;
            foreach (Node n in getpeers.ClosestActiveNodes.Values)
            {
                if (n.Token == null)
                    continue;
                AnnouncePeer query = new AnnouncePeer(engine.LocalId, infoHash, port, n.Token);
                SendQueryTask task = new SendQueryTask(engine, query, n);
                task.Completed += SentAnnounce;
                task.Execute();
                activeAnnounces++;
            }

            if (activeAnnounces == 0)
                RaiseComplete(new TaskCompleteEventArgs(this));
        }
예제 #11
0
        public void PingTimeout()
        {
            engine.TimeOut = TimeSpan.FromHours(1);
            // Send ping
            Ping ping = new Ping(node.Id);
            ping.TransactionId = transactionId;

            ManualResetEvent handle = new ManualResetEvent(false);
            SendQueryTask task = new SendQueryTask(engine, ping, node);
            task.Completed += delegate {
                handle.Set();
            };
            task.Execute();

            // Receive response
            PingResponse response = new PingResponse(node.Id, transactionId);
            listener.RaiseMessageReceived(response, node.EndPoint);

            Assert.IsTrue(handle.WaitOne(1000, true), "#0");

            engine.TimeOut = TimeSpan.FromMilliseconds(75);
            DateTime lastSeen = node.LastSeen;

            // Time out a ping
            ping = new Ping(node.Id);
            ping.TransactionId = (BEncodedString)"ab";

            task = new SendQueryTask(engine, ping, node, 4);
            task.Completed += delegate { handle.Set(); };

            handle.Reset();
            task.Execute();
            handle.WaitOne();

            Assert.AreEqual(4, node.FailedCount, "#1");
            Assert.AreEqual(NodeState.Bad, node.State, "#2");
            Assert.AreEqual(lastSeen, node.LastSeen, "#3");
        }
예제 #12
0
        private void GotPeers(object o, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= GotPeers;
            GetPeersTask getpeers = (GetPeersTask)e.Task;

            foreach (Node n in getpeers.ClosestActiveNodes.Values)
            {
                if (n.Token == null)
                {
                    continue;
                }
                AnnouncePeer  query = new AnnouncePeer(engine.LocalId, infoHash, port, n.Token);
                SendQueryTask task  = new SendQueryTask(engine, query, n);
                task.Completed += SentAnnounce;
                task.Execute();
                activeAnnounces++;
            }

            if (activeAnnounces == 0)
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
        }
예제 #13
0
        internal void Add(Node node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            SendQueryTask task = new SendQueryTask(this, new Ping(RoutingTable.LocalNode.Id), node);
            task.Execute();
        }
 private void SendFindNode(IEnumerable<Node> newNodes)
 {
     foreach (var node in Node.CloserNodes(engine.LocalId, nodes, newNodes, Bucket.MaxCapacity))
     {
         activeRequests++;
         var request = new FindNode(engine.LocalId, engine.LocalId);
         var task = new SendQueryTask(engine, request, node);
         task.Completed += FindNodeComplete;
         task.Execute();
     }
 }