public void ThreeNodeNetwork()
        {
            Func<DistributedRoutingTable, Contact> contactFactory = drt =>
            {
                return new LocalContact(drt);
            };

            Guid networkId = Guid.NewGuid();
            Configuration config = new Configuration();
            DistributedRoutingTable table1 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);
            DistributedRoutingTable table2 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);
            DistributedRoutingTable table3 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);

            table1.Bootstrap(table2.LocalContact);
            table2.Bootstrap(table3.LocalContact);

#if DEBUG
            config.UpdateRoutingTable = false;
#endif

            FindTable(table1, table1);
            FindTable(table1, table2);
            FindTable(table1, table3);

            FindTable(table2, table1);
            FindTable(table2, table2);
            FindTable(table2, table3);

            FindTable(table3, table1);
            FindTable(table3, table2);
            FindTable(table3, table3);

            TestUtilities.TestCallbackLeak(table1, table2, table3);
        }
示例#2
0
        static void Main(string[] args)
        {
            myDomainMappings = ReadDomainMappings("MyDomainMappings.txt");
            ReadSettings(ref dnsport, ref peerport);

            dnsServer = new DnsServer(new Dictionary<string, DomainMapping>()
                {
                    { "hellequin.p2p", new DomainMapping() { Address = IPAddress.Parse("78.105.97.103"), Name = "hellequin.p2p", TimeToLive = TimeSpan.FromSeconds(1234) }}
                });
            dnsServer.Start();

            Identifier512 myId = Identifier512.NewIdentifier();
            routingTable = new DistributedRoutingTable(Identifier512.NewIdentifier(), (a) => new UdpContact(a.LocalIdentifier, networkId, LocalIp, peerport), networkId, new Configuration());

            UdpContact.InitialiseUdp(routingTable, peerport);

            Console.WriteLine("Bootstrapping DHT");
            routingTable.Bootstrap(LoadBootstrapData());

            Console.WriteLine("Bootstrap finished");
            Console.WriteLine("There are " + routingTable.ContactCount + " Contacts");

            Console.WriteLine("Press any key to close");
            Console.ReadLine();

            UdpContact.Stop();
        }
        public void Begin(DistributedRoutingTable table)
        {
            this.table = table;
            callback = table.GetConsumer<Callback>(Callback.CONSUMER_ID);

            BeginReceive(CreateEatPacket(table));
        }
示例#4
0
        public static void InitialiseUdp(DistributedRoutingTable localTable, int port)
        {
            UdpContact.localTable = localTable;

            client = new UdpClient(port);

            listenThread = new Thread(() =>
            {
                while (listen)
                {
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
                    byte[] bytes       = client.Receive(ref groupEP);

                    using (MemoryStream m = new MemoryStream(bytes))
                    {
                        using (BinaryReader r = new BinaryReader(m))
                        {
                            PacketFlag f = (PacketFlag)r.ReadByte();

                            switch (f)
                            {
                            case PacketFlag.Ping: ParsePing(r); break;

                            case PacketFlag.Data: ParseData(r); break;

                            default: Console.WriteLine("Unknown packet type " + f); break;
                            }
                        }
                    }
                }
            });
            listenThread.Start();
        }
        public static void InitialiseUdp(DistributedRoutingTable localTable, int port)
        {
            UdpContact.localTable = localTable;

            client = new UdpClient(port);

            listenThread = new Thread(() =>
            {
                while (listen)
                {
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
                    byte[] bytes = client.Receive(ref groupEP);

                    using (MemoryStream m = new MemoryStream(bytes))
                    {
                        using (BinaryReader r = new BinaryReader(m))
                        {
                            PacketFlag f = (PacketFlag)r.ReadByte();

                            switch (f)
                            {
                                case PacketFlag.Ping: ParsePing(r); break;
                                case PacketFlag.Data: ParseData(r); break;
                                default: Console.WriteLine("Unknown packet type " + f); break;
                            }
                        }
                    }
                }
            });
            listenThread.Start();
        }
示例#6
0
        static void Main(string[] args)
        {
            myDomainMappings = ReadDomainMappings("MyDomainMappings.txt");
            ReadSettings(ref dnsport, ref peerport);

            dnsServer = new DnsServer(new Dictionary <string, DomainMapping>()
            {
                { "hellequin.p2p", new DomainMapping()
                  {
                      Address = IPAddress.Parse("78.105.97.103"), Name = "hellequin.p2p", TimeToLive = TimeSpan.FromSeconds(1234)
                  } }
            });
            dnsServer.Start();

            Identifier512 myId = Identifier512.NewIdentifier();

            routingTable = new DistributedRoutingTable(Identifier512.NewIdentifier(), (a) => new UdpContact(a.LocalIdentifier, networkId, LocalIp, peerport), networkId, new Configuration());

            UdpContact.InitialiseUdp(routingTable, peerport);

            Console.WriteLine("Bootstrapping DHT");
            routingTable.Bootstrap(LoadBootstrapData());

            Console.WriteLine("Bootstrap finished");
            Console.WriteLine("There are " + routingTable.ContactCount + " Contacts");

            Console.WriteLine("Press any key to close");
            Console.ReadLine();

            UdpContact.Stop();
        }
        public ContactCollection(DistributedRoutingTable drt)
        {
            LocalContact = drt.LocalContact;
            NetworkId = drt.NetworkId;
            Configuration = drt.Configuration;
            this.DistributedRoutingTable = drt;

            for (int i = 0; i < buckets.Length; i++)
                buckets[i] = new ContactBucket(drt, i);
        }
        public void LinkedConsumerAttributeConstructorTest()
        {
            Foo f = new Foo(Guid.NewGuid());

            Bar b = new Bar(new Guid(Foo.guidString));

            DistributedRoutingTable drt = new DistributedRoutingTable(Identifier512.NewIdentifier(), (a) => new LocalContact(a), Guid.NewGuid(), new Configuration());
            drt.RegisterConsumer(b);
            drt.RegisterConsumer(f);

            Assert.AreEqual(f.Bar, b);
            Assert.IsNotNull(f.Bar2);
        }
        private List<DistributedRoutingTable> CreateNetwork()
        {
            Func<DistributedRoutingTable, Contact> contactFactory = drt =>
            {
                return new LocalContact(drt);
            };

            Guid networkId = Guid.NewGuid();
            Configuration config = new Configuration();
            DistributedRoutingTable table1 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);
            DistributedRoutingTable table2 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);
            DistributedRoutingTable table3 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);
            DistributedRoutingTable table4 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);
            DistributedRoutingTable table5 = new DistributedRoutingTable(Identifier512.NewIdentifier(), contactFactory, networkId, config);

            table1.Bootstrap(table2.LocalContact);
            table2.Bootstrap(table3.LocalContact);
            table3.Bootstrap(table4.LocalContact);
            table4.Bootstrap(table5.LocalContact);
            table5.Bootstrap(table1.LocalContact);

            return new List<DistributedRoutingTable>(new[] { table1, table2, table3, table4, table5 });
        }
 private void InitialiseBroadcast(string name, DistributedRoutingTable routingTable)
 {
     broadcaster = new BroadcastPeer(routingTable.LocalIdentifier, true);
     routingTable.RegisterConsumer(broadcaster);
 }
 public LocalContact(DistributedRoutingTable table)
     : base(table.LocalIdentifier, table.NetworkId)
 {
     tables.GetOrAdd(table.LocalIdentifier, new ConcurrentDictionary<Guid, DistributedRoutingTable>())
         .AddOrUpdate(table.NetworkId, table, (a, b) => table);
 }
示例#12
0
 public LocalContact(DistributedRoutingTable table)
     : base(table.LocalIdentifier, table.NetworkId)
 {
     tables.GetOrAdd(table.LocalIdentifier, new ConcurrentDictionary <Guid, DistributedRoutingTable>())
     .AddOrUpdate(table.NetworkId, table, (a, b) => table);
 }
        private Action<IAsyncResult> CreateEatPacket(DistributedRoutingTable routingTable)
        {
            return a =>
            {
                try
                {
                    IPEndPoint ep = new IPEndPoint(IPAddress.Any, ListenPort);

                    var m = new MemoryStream(udpClient.EndReceive(a, ref ep));

                    ProxyContact source = Serializer.DeserializeWithLengthPrefix<ProxyContact>(m, PrefixStyle.Base128);

                    if (m.ReadByte() == 0)
                    {
                        routingTable.DeliverPing(source);

                        BinaryReader r = new BinaryReader(m);

                        callback.SendResponse(routingTable.LocalContact, source, r.ReadInt64(), new byte[] { 1 });
                    }
                    else
                    {
                        try
                        {
                            BinaryReader r = new BinaryReader(m);

                            Guid consumer = new Guid(r.ReadBytes(16));

                            byte[] buffer = new byte[BitConverter.ToInt32(r.ReadBytes(4), 0)];
                            m.Read(buffer, 0, buffer.Length);

                            ThreadPool.QueueUserWorkItem(_ => routingTable.Deliver(source, consumer, buffer));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e);
                }
            };
        }
        private static void AddPeer(Configuration config, Func<DistributedRoutingTable, Contact> contactFactory, Guid networkId, IList<DistributedRoutingTable> tables, Random r)
        {
            Identifier512 id = Identifier512.NewIdentifier();
            DistributedRoutingTable t = new DistributedRoutingTable(id, contactFactory, networkId, config);

            t.Bootstrap(
                tables[r.Next(tables.Count)].LocalContact,
                tables[r.Next(tables.Count)].LocalContact,
                tables[r.Next(tables.Count)].LocalContact);

            tables.Add(t);
        }
 private void FindTable(DistributedRoutingTable start, DistributedRoutingTable end)
 {
     var closest = start.GetConsumer<GetClosestNodes>(GetClosestNodes.GUID).GetClosestContacts(end.LocalIdentifier).First();
     if (closest.NetworkId != end.NetworkId) throw new Exception("Incorrect network GUID");
     if (closest.Identifier != end.LocalIdentifier) throw new Exception("Incorrect table");
 }
 protected internal virtual void OnUnregisteredFromTable(DistributedRoutingTable table)
 {
     RoutingTable = null;
 }
 protected internal virtual void OnRegisteredToTable(DistributedRoutingTable table)
 {
     RoutingTable = table;
 }
示例#18
0
 protected internal virtual void OnRegisteredToTable(DistributedRoutingTable table)
 {
     RoutingTable = table;
 }
示例#19
0
 protected internal virtual void OnUnregisteredFromTable(DistributedRoutingTable table)
 {
     RoutingTable = null;
 }