Exemple #1
0
        public void DhtSerializationTest()
        {
            TcpSubnetProtocol p1     = new TcpSubnetProtocol("http://127.0.0.1", 2720, 1);
            TcpSubnetProtocol p2     = new TcpSubnetProtocol("http://127.0.0.1", 2720, 2);
            VirtualStorage    store1 = new VirtualStorage();
            VirtualStorage    store2 = new VirtualStorage();

            // Ensures that all nodes are closer, because ID.Max ^ n < ID.Max when n > 0.
            Dht dht = new Dht(ID.Max, p1, new Router(), store1, store1, new VirtualStorage());

            ID      contactID    = ID.Mid; // a closer contact.
            Contact otherContact = new Contact(p2, contactID);
            Node    otherNode    = new Node(otherContact, store2);

            // Add this other contact to our peer list.
            dht.Node.BucketList.AddContact(otherContact);

            string json = dht.Save();

            Dht newDht = Dht.Load(json);

            Assert.IsTrue(newDht.Node.BucketList.Buckets.Sum(b => b.Contacts.Count) == 1, "Expected our node to have 1 contact.");
            Assert.IsTrue(newDht.Node.BucketList.ContactExists(otherContact), "Expected our contact to have the other contact.");
            Assert.IsTrue(newDht.Router.Node == newDht.Node, "Router node not initialized.");
        }
Exemple #2
0
        private static void InitializeKademlia()
        {
            ID id = new ID(peerID);

            if (File.Exists(DHT_FILENAME))
            {
                dht = Dht.Load(File.ReadAllText(DHT_FILENAME));
                ((DBreezeStore)dht.OriginatorStorage).Open(fnLocalStore);
                ((DBreezeStore)dht.RepublishStorage).Open(fnRepublishStore);
                localStore       = dht.OriginatorStorage;
                republishStore   = dht.RepublishStorage;
                cacheStore       = new VirtualStorage();
                dht.CacheStorage = cacheStore;
                dht.FinishLoad();
            }
            else
            {
                localStore     = new DBreezeStore(fnLocalStore);
                republishStore = new DBreezeStore(fnRepublishStore);
                cacheStore     = new VirtualStorage();
                dht            = new Dht(id, new TcpProtocol(url, port), new ParallelRouter(), localStore, republishStore, cacheStore);
            }
        }