コード例 #1
0
        private bool Add(Node node, bool raiseNodeAdded)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            Bucket bucket = buckets.Find(delegate(Bucket b) { return(b.CanContain(node)); });

            if (bucket.Nodes.Contains(node))
            {
                return(false);
            }

            bool added = bucket.Add(node);

            if (added && raiseNodeAdded)
            {
                RaiseNodeAdded(node);
            }

            if (!added && bucket.CanContain(LocalNode))
            {
                if (Split(bucket))
                {
                    return(Add(node, raiseNodeAdded));
                }
            }

            return(added);
        }
コード例 #2
0
        public void NodeReplaceTest()
        {
            engine.TimeOut = TimeSpan.FromMilliseconds(25);
            ManualResetEvent handle = new ManualResetEvent(false);
            Bucket           b      = new Bucket();

            for (int i = 0; i < Bucket.MaxCapacity; i++)
            {
                Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i));
                n.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))
                {
                    Node n = b.Nodes.Find(delegate(Node no) { return(no.EndPoint.Port == e.EndPoint.Port); });
                    n.Seen();
                    PingResponse 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++;
                }
            };

            ReplaceNodeTask 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 += delegate(object o, TaskCompleteEventArgs e) { handle.Set(); };
            task.Execute();

#if NETSTANDARD1_5
            Assert.True(handle.WaitOne(4000), "#10");
#else
            Assert.True(handle.WaitOne(4000, false), "#10");
#endif
        }