示例#1
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(delegate
                {
                    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 = ""};
                        _listener.RaiseMessageReceived(response, current.EndPoint);
                    }
                });
            };

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

            Thread.Sleep(500);
            foreach (var b in _engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(b.LastChanged > DateTime.UtcNow.AddSeconds(-2));
                Assert.IsTrue(b.Nodes.Exists(n => n.LastSeen > DateTime.UtcNow.AddMilliseconds(-900)));
            }
        }
        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");
        }
示例#3
0
        public void NodeReplaceTest()
        {
            _engine.TimeOut = TimeSpan.FromMilliseconds(25);
            var handle = new ManualResetEvent(false);
            var b = new Bucket();
            for (var i = 0; i < Bucket.MaxCapacity; i++)
            {
                var n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i))
                            {
                                LastSeen = DateTime.UtcNow
                            };
                b.Add(n);
            }

            b.Nodes[3].LastSeen = DateTime.UtcNow.AddDays(-5);
            b.Nodes[1].LastSeen = DateTime.UtcNow.AddDays(-4);
            b.Nodes[5].LastSeen = DateTime.UtcNow.AddDays(-3);

            _engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e)
            {
                if (!e.TimedOut)
                    return;

                b.Nodes.Sort();
                if ((e.EndPoint.Port == 3 && _nodeCount == 0) ||
                     (e.EndPoint.Port == 1 && _nodeCount == 1) ||
                     (e.EndPoint.Port == 5 && _nodeCount == 2))
                {
                    var n = b.Nodes.Find(delegate(Node no) { return no.EndPoint.Port == e.EndPoint.Port; });
                    n.Seen();
                    var response = new PingResponse(n.Id, e.Query.TransactionId);
                    DhtEngine.MainLoop.Queue(delegate
                    {
                        //System.Threading.Thread.Sleep(100);
                        Console.WriteLine("Faking the receive");
                        _listener.RaiseMessageReceived(response, _node.EndPoint);
                    });
                    _nodeCount++;
                }

            };

            var task = new ReplaceNodeTask(_engine, b, null);
            // FIXME: Need to assert that node 0.0.0.0:0 is the one which failed - i.e. it should be replaced
            task.Completed += (o, e) => handle.Set();
            task.Execute();

            Assert.IsTrue(handle.WaitOne(4000, false), "#10");
        }
示例#4
0
        public void SendQueryTaskSucceed()
        {
            _engine.TimeOut = TimeSpan.FromMilliseconds(25);

            var ping = new Ping(_engine.LocalId) {TransactionId = _transactionId};
            _engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e)
            {
                if (!e.TimedOut)
                    return;

                _counter++;
                var response = new PingResponse(_node.Id, _transactionId);
                _listener.RaiseMessageReceived(response, _node.EndPoint);
            };

            var task = new SendQueryTask(_engine, ping, _node);
            task.Completed += delegate { _handle.Set(); };
            task.Execute();

            Assert.IsTrue(_handle.WaitOne(3000, false), "#1");
            Thread.Sleep(200);
            Assert.AreEqual(1, _counter, "#2");
            var n = _engine.RoutingTable.FindNode(_node.Id);
            Assert.IsNotNull(n, "#3");
            Assert.IsTrue(n.LastSeen > DateTime.UtcNow.AddSeconds(-2));
        }
示例#5
0
        public void PingResponseEncode()
        {
            var m = new PingResponse(_infohash, _transactionId);

            Compare(m, "d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re");
        }
        public void SendPing()
        {
            engine.Add(node);
            engine.TimeOut = TimeSpan.FromMilliseconds(75);
            ManualResetEvent handle = new ManualResetEvent(false);
            engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) {
                if (!e.TimedOut && e.Query is Ping)
                    handle.Set();

                if (!e.TimedOut || !(e.Query is Ping))
                    return;

                PingResponse response = new PingResponse(node.Id, e.Query.TransactionId);
                listener.RaiseMessageReceived(response, e.EndPoint);
            };

            Assert.AreEqual(NodeState.Unknown, node.State, "#1");

            DateTime lastSeen = node.LastSeen;
            Assert.IsTrue(handle.WaitOne(1000, false), "#1a");
            Node nnnn = node;
            node = engine.RoutingTable.FindNode(nnnn.Id);
            Assert.IsTrue (lastSeen < node.LastSeen, "#2");
            Assert.AreEqual(NodeState.Good, node.State, "#3");
        }