public void FindValueRouteTest() { TcpSubnetProtocol p1 = new TcpSubnetProtocol(localIP, port, 1); TcpSubnetProtocol p2 = new TcpSubnetProtocol(localIP, port, 2); ID ourID = ID.RandomID; Contact c1 = new Contact(p1, ourID); Node n1 = new Node(c1, new VirtualStorage()); Node n2 = new Node(new Contact(p2, ID.RandomID), new VirtualStorage()); server.RegisterProtocol(p1.Subnet, n1); server.RegisterProtocol(p2.Subnet, n2); server.Start(); ID testID = ID.RandomID; string testValue = "Test"; p2.Store(c1, testID, testValue); Assert.IsTrue(n2.Storage.Contains(testID), "Expected remote peer to have value."); Assert.IsTrue(n2.Storage.Get(testID) == testValue, "Expected remote peer to contain stored value."); var ret = p2.FindValue(c1, testID); Assert.IsTrue(ret.contacts == null, "Expected to find value."); Assert.IsTrue(ret.val == testValue, "Value does not match expected value from peer."); }
protected void InitializeDhts() { dhts = new List<Dht>(); dhtPos = new List<Rectangle>(); peerColor = new Dictionary<BigInteger, Color>(); NUM_DHT.ForEach((n) => { #if USE_TCP_SUBNET_PROTOCOL IProtocol protocol = new TcpSubnetProtocol("http://127.0.0.1", 2720, n); #else IProtocol protocol = new VirtualProtocol(); #endif Dht dht = new Dht(ID.RandomID, protocol, () => new VirtualStorage(), new Router()); peerColor[dht.ID.Value] = Color.Green; #if USE_TCP_SUBNET_PROTOCOL server.RegisterProtocol(n, dht.Node); #else ((VirtualProtocol)protocol).Node = dht.Node; #endif dhts.Add(dht); dhtPos.Add(new Rectangle(XOFFSET + rnd.Next(-JITTER, JITTER) + (n % ITEMS_PER_ROW) * XSPACING, YOFFSET + rnd.Next(-JITTER, JITTER) + (n / ITEMS_PER_ROW) * YSPACING, SIZE, SIZE)); }); }
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."); }
public void UnresponsiveNodeTest() { TcpSubnetProtocol p1 = new TcpSubnetProtocol(localIP, port, 1); TcpSubnetProtocol p2 = new TcpSubnetProtocol(localIP, port, 2); p2.Responds = false; ID ourID = ID.RandomID; Contact c1 = new Contact(p1, ourID); Node n1 = new Node(c1, new VirtualStorage()); Node n2 = new Node(new Contact(p2, ID.RandomID), new VirtualStorage()); server.RegisterProtocol(p1.Subnet, n1); server.RegisterProtocol(p2.Subnet, n2); server.Start(); ID testID = ID.RandomID; string testValue = "Test"; RestCall.RequestTimeout = 1; RpcError error = p2.Store(c1, testID, testValue); System.Threading.Thread.Sleep(10); Assert.IsTrue(error.TimeoutError, "Expected timeout."); }
public void PingRouteTest() { TcpSubnetProtocol p1 = new TcpSubnetProtocol(localIP, port, 1); TcpSubnetProtocol p2 = new TcpSubnetProtocol(localIP, port, 2); ID ourID = ID.RandomID; Contact c1 = new Contact(p1, ourID); Node n1 = new Node(c1, new VirtualStorage()); Node n2 = new Node(new Contact(p2, ID.RandomID), new VirtualStorage()); server.RegisterProtocol(p1.Subnet, n1); server.RegisterProtocol(p2.Subnet, n2); server.Start(); p2.Ping(c1); }
public void StoreRouteTest() { TcpSubnetProtocol p1 = new TcpSubnetProtocol(localIP, port, 1); TcpSubnetProtocol p2 = new TcpSubnetProtocol(localIP, port, 2); ID ourID = ID.RandomID; Contact c1 = new Contact(p1, ourID); Node n1 = new Node(c1, new VirtualStorage()); Node n2 = new Node(new Contact(p2, ID.RandomID), new VirtualStorage()); server.RegisterProtocol(p1.Subnet, n1); server.RegisterProtocol(p2.Subnet, n2); server.Start(); Contact sender = new Contact(p1, ID.RandomID); ID testID = ID.RandomID; string testValue = "Test"; p2.Store(sender, testID, testValue); Assert.IsTrue(n2.Storage.Contains(testID), "Expected remote peer to have value."); Assert.IsTrue(n2.Storage.Get(testID) == testValue, "Expected remote peer to contain stored value."); }
public void FindNodesRouteTest() { TcpSubnetProtocol p1 = new TcpSubnetProtocol(localIP, port, 1); TcpSubnetProtocol p2 = new TcpSubnetProtocol(localIP, port, 2); ID ourID = ID.RandomID; Contact c1 = new Contact(p1, ourID); Node n1 = new Node(c1, new VirtualStorage()); Node n2 = new Node(new Contact(p2, ID.RandomID), new VirtualStorage()); // Node 2 knows about another contact, that isn't us (because we're excluded.) ID otherPeer = ID.RandomID; n2.BucketList.Buckets[0].Contacts.Add(new Contact(new TcpSubnetProtocol(localIP, port, 3), otherPeer)); server.RegisterProtocol(p1.Subnet, n1); server.RegisterProtocol(p2.Subnet, n2); server.Start(); ID id = ID.RandomID; List <Contact> ret = p2.FindNode(c1, id).contacts; Assert.IsTrue(ret.Count == 1, "Expected 1 contact."); Assert.IsTrue(ret[0].ID == otherPeer, "Expected contact to the other peer (not us)."); }