Ejemplo n.º 1
0
        public void ParallelValueStoredGetsPropagatedTest()
        {
            VirtualProtocol vp1    = new VirtualProtocol();
            VirtualProtocol vp2    = new VirtualProtocol();
            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, vp1, new ParallelRouter(), store1, store1, new VirtualStorage());

            vp1.Node = dht.Router.Node;

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

            vp2.Node = otherNode;

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

            // We want an integer distance, not an XOR distance.
            ID     key = ID.Zero;
            string val = "Test";

            Assert.IsFalse(store1.Contains(key), "Obviously we don't have the key-value yet.");
            Assert.IsFalse(store2.Contains(key), "And equally obvious, the other peer doesn't have the key-value yet either.");

            dht.Store(key, val);

            Assert.IsTrue(store1.Contains(key), "Expected our peer to have stored the key-value.");
            Assert.IsTrue(store2.Contains(key), "Expected the other peer to have stored the key-value.");
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Ask the next peer the question.
        /// </summary>
        async Task AskAsync(int taskId)
        {
            int pass  = 0;
            int waits = 20;

            while (!runningQuery.IsCancellationRequested && waits > 0)
            {
                // Get the nearest peer that has not been visited.
                var peer = Dht.RoutingTable
                           .NearestPeers(QueryKey)
                           .Where(p => !visited.Contains(p))
                           .FirstOrDefault();
                if (peer == null)
                {
                    --waits;
                    await Task.Delay(100);

                    continue;
                }

                ++pass;
                visited.Add(peer);

                // Ask the nearest peer.
                await askCount.WaitAsync(runningQuery.Token).ConfigureAwait(false);

                var start = DateTime.Now;
                log.Debug($"Q{Id}.{taskId}.{pass} ask {peer}");
                try
                {
                    using (var timeout = new CancellationTokenSource(askTime))
                        using (var cts = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, runningQuery.Token))
                            using (var stream = await Dht.Swarm.DialAsync(peer, Dht.ToString(), cts.Token).ConfigureAwait(false))
                            {
                                // Send the KAD query and get a response.
                                ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, queryMessage, PrefixStyle.Base128);
                                await stream.FlushAsync(cts.Token).ConfigureAwait(false);

                                var response = await ProtoBufHelper.ReadMessageAsync <DhtMessage>(stream, cts.Token).ConfigureAwait(false);

                                // Process answer
                                ProcessProviders(response.ProviderPeers);
                                ProcessCloserPeers(response.CloserPeers);
                            }
                    var time = DateTime.Now - start;
                    log.Debug($"Q{Id}.{taskId}.{pass} ok {peer} ({time.TotalMilliseconds} ms)");
                }
                catch (Exception e)
                {
                    Interlocked.Increment(ref failedConnects);
                    var time = DateTime.Now - start;
                    log.Warn($"Q{Id}.{taskId}.{pass} failed ({time.TotalMilliseconds} ms) - {e.Message}");
                    // eat it
                }
                finally
                {
                    askCount.Release();
                }
            }
        }
Ejemplo n.º 3
0
        public void Init()
        {
            Console.WriteLine("Initializing...");
            ArrayList RemoteTA = new ArrayList();

            for (int i = 0; i < network_size; i++)
            {
                RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:" + (base_port + i)));
            }

            for (int i = 0; i < network_size; i++)
            {
                Address addr = (Address) new AHAddress(new RNGCryptoServiceProvider());
                Node    node = new StructuredNode((AHAddress)addr, brunet_namespace);
                nodes.Add(addr, node);
                node.AddEdgeListener(new UdpEdgeListener(base_port + i));
                node.RemoteTAs = RemoteTA;
                tables[addr]   = new TableServer(node);
                (new Thread(node.Connect)).Start();
//        if(i < network_size / ((Dht)dhts.GetByIndex(i)).DEGREE) {
//          ((Dht)dhts.GetByIndex(i)).debug = true;
//        }
            }
            default_dht = new Dht((Node)nodes.GetByIndex(0), degree);
        }
Ejemplo n.º 4
0
        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));
            });
        }
Ejemplo n.º 5
0
 protected void BootstrapWithAPeer(int peerBootstrappingIdx)
 {
     Dht dht = dhts[peerBootstrappingIdx];
     var peerList = knownPeers.ExceptBy(dht, c => c.ID).ToList();
     Dht bootstrapWith = peerList[rnd.Next(peerList.Count)];
     dht.Bootstrap(bootstrapWith.Contact);
 }
Ejemplo n.º 6
0
        public ControlPanel(Dht dht)
        {
            this.dht = dht;
            InitializeComponent();
            tbOurPeerUrl.Text  = dht.Protocol.Url;
            tbOurPeerPort.Text = dht.Protocol.Port.ToString();
            // tbOurPeerSubnet.Text = dht.Protocol.Subnet.ToString();
            nodePeers        = tvPeerState.Nodes.Add("Peers");
            nodePendingPeers = tvPeerState.Nodes.Add("Pending Peers");
            TreeNode storageNode = tvPeerState.Nodes.Add("Storage");

            nodeLocalStore     = storageNode.Nodes.Add("Local Store");
            nodeRepublishStore = storageNode.Nodes.Add("Republish Store");
            nodeCacheStore     = storageNode.Nodes.Add("Cache Store");
            tvPeerState.ExpandAll();
            PopulatePeerNodes();
            PopulateStorageNodes();
            Text         = "Peer " + dht.Protocol.Port.ToString();
            FormClosing += (sndr, args) => SaveDht();

            dht.PendingContactAdded   += (sndr, args) => PendingContactAdded(args.Contact);
            dht.PendingContactRemoved += (sndr, args) => PendingContactRemoved(args.Contact);
            dht.ContactAdded          += (sndr, args) => ContactAdded(args.Contact);
            dht.ContactRemoved        += (sndr, args) => ContactRemoved(args.Contact);

            dht.OriginatorStoreAdded  += (sndr, args) => OriginatorStoreAdded(args.Key);
            dht.RepublishStoreAdded   += (sndr, args) => RepublishStoreAdded(args.Key);
            dht.RepublishStoreRemoved += (sndr, args) => RepublishStoreRemoved(args.Key);
            dht.CacheStoreAdded       += (sndr, args) => CacheStoreAdded(args.Key);
            dht.CacheStoreRemoved     += (sndr, args) => CacheStoreRemoved(args.Key);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Method that runs the kademlia layer
        /// </summary>
        /// <param name="single">if true indicates that the kademlia layer have to do a single start</param>
        /// <param name="btpNode">if indicated, it represents the node suggested to do bootstrap</param>
        /// <param name="svcHost">a service host object where is stored newly initalized host.</param>
        private void runKademliaLayer(bool single, string btpNode, ref ServiceHost svcHost)
        {
            log.Info("Running Kademlia layer.");

            KademliaNode node    = new KademliaNode(new EndpointAddress(kademliaAddress), new EndpointAddress(transportAddress));
            ServiceHost  kadHost = new ServiceHost(node, kademliaAddress);

            try
            {
                kadHost.Open();
            }
            catch (AddressAlreadyInUseException aaiue)
            {
                log.Error("Unable to Connect as a Server because there is already one on this machine", aaiue);
                throw aaiue;
            }
            try
            {
                this.kademliaLayer = new Dht(node, single, btpNode);
            }
            catch (FileNotFoundException fnfe)
            {
                log.Error("Unable to load nodes file (nodes.xml)", fnfe);
                throw fnfe;
            }
            List <TrackModel.Track> list = new List <TrackModel.Track>();

            log.Debug("GetAll Response : " + this.trackRep.GetAll(list));
            Parallel.ForEach(list, t =>
            {
                this.kademliaLayer.Put(t.Filename);
            });
            svcHost = kadHost;
        }
        public void NonRespondingContactDelayedEvictionTest()
        {
            // Create a DHT so we have an eviction handler.
            Dht dht = new Dht(ID.Zero, new VirtualProtocol(), () => null, new Router());
            //Contact dummyContact = new Contact(new VirtualProtocol(), ID.Zero);
            //((VirtualProtocol)dummyContact.Protocol).Node = new Node(dummyContact, new VirtualStorage());

            IBucketList bucketList = SetupSplitFailure(dht.Node.BucketList);

            Assert.IsTrue(bucketList.Buckets.Count == 2, "Bucket split should have occurred.");
            Assert.IsTrue(bucketList.Buckets[0].Contacts.Count == 1, "Expected 1 contact in bucket 0.");
            Assert.IsTrue(bucketList.Buckets[1].Contacts.Count == 20, "Expected 20 contacts in bucket 1.");

            // The bucket is now full.  Pick the first contact, as it is last seen (they are added in chronological order.)
            Contact nonRespondingContact = bucketList.Buckets[1].Contacts[0];

            // Since the protocols are shared, we need to assign a unique protocol for this node for testing.
            VirtualProtocol vpUnresponding = new VirtualProtocol(((VirtualProtocol)nonRespondingContact.Protocol).Node, false);

            nonRespondingContact.Protocol = vpUnresponding;

            // Setup the next new contact (it can respond.)
            Contact nextNewContact = new Contact(dht.Contact.Protocol, ID.Zero.SetBit(159));

            bucketList.AddContact(nextNewContact);

            Assert.IsTrue(bucketList.Buckets[1].Contacts.Count == 20, "Expected 20 contacts in bucket 1.");

            // Verify CanSplit -> Evict happened.

            Assert.IsTrue(dht.PendingContacts.Count == 1, "Expected one pending contact.");
            Assert.IsTrue(dht.PendingContacts.Contains(nextNewContact), "Expected pending contact to be the 21st contact.");
            Assert.IsTrue(dht.EvictionCount.Count == 1, "Expected one contact to be pending eviction.");
        }
Ejemplo n.º 9
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.");
        }
Ejemplo n.º 10
0
        override public void Start()
        {
            Channel returns = new Channel();

            returns.EnqueueEvent += delegate(object o, EventArgs ea) {
                while (returns.Count > 0)
                {
                    Hashtable result = null;
                    try {
                        result = returns.Dequeue() as Hashtable;
                    } catch {
                        continue;
                    }

                    byte[] res = result["value"] as byte[];
                    if (res != null)
                    {
                        Results.Enqueue(MemBlock.Reference(res));
                    }
                }
                if (_enqueue != null)
                {
                    _enqueue(this, EventArgs.Empty);
                }
            };

            returns.CloseEvent += delegate(object o, EventArgs ea) {
                Finished();
            };

            Dht dht = new Dht(Node, 3, 20);

            dht.AsyncGet(Key, returns);
        }
Ejemplo n.º 11
0
        public void ParallelBootstrapOutsideBootstrappingBucketTest()
        {
            // We need 32 virtual protocols.  One for the bootstrap peer,
            // 20 for the nodes the bootstrap peer knows about, 10 for the nodes
            // one of those nodes knows about, and one for us to rule them all.
            VirtualProtocol[] vp = new VirtualProtocol[32];
            32.ForEach((i) => vp[i] = new VirtualProtocol());

            // Us, ID doesn't matter.
            Dht dhtUs = new Dht(ID.RandomID, vp[0], () => new VirtualStorage(), new ParallelRouter());

            vp[0].Node = dhtUs.Router.Node;

            // Our bootstrap peer
            // All ID's are < 2^159
            Dht dhtBootstrap = new Dht(ID.Zero.RandomizeBeyond(Constants.ID_LENGTH_BITS - 1), vp[1], () => new VirtualStorage(), new ParallelRouter());

            vp[1].Node = dhtBootstrap.Router.Node;
            Node n = null;

            // Our boostrapper knows 20 contacts
            20.ForEach((i) =>
            {
                ID id;

                // All ID's are < 2^159 except the last one, which is >= 2^159
                // which will force a bucket split for _us_
                if (i < 19)
                {
                    id = ID.Zero.RandomizeBeyond(Constants.ID_LENGTH_BITS - 1);
                }
                else
                {
                    id = ID.Max;
                }

                Contact c      = new Contact(vp[i + 2], id);
                n              = new Node(c, new VirtualStorage());
                vp[i + 2].Node = n;
                dhtBootstrap.Router.Node.BucketList.AddContact(c);
            });

            // One of those nodes, in this case specifically the last one we added to our bootstrapper
            // so that it isn't in the bucket of our bootstrapper, we add 10 contacts.  The ID's of
            // those contacts don't matter.
            10.ForEach((i) =>
            {
                Contact c       = new Contact(vp[i + 22], ID.RandomID);
                Node n2         = new Node(c, new VirtualStorage());
                vp[i + 22].Node = n;
                n.BucketList.AddContact(c);     // Note we're adding these contacts to the 10th node.
            });

            dhtUs.Bootstrap(dhtBootstrap.Router.Node.OurContact);

            Assert.IsTrue(dhtUs.Router.Node.BucketList.Buckets.Sum(c => c.Contacts.Count) == 31, "Expected our peer to have 31 contacts.");
        }
Ejemplo n.º 12
0
 public SocialNetworkProvider(Dht dht, SocialUser user)
 {
     _local_user = user;
     _dht        = dht;
     _provider   = _drupal;
     _network    = _drupal;
     _drupal     = new DrupalNetwork(user);
     _online     = false;
 }
Ejemplo n.º 13
0
        public IActionResult GetTemperature()
        {
            if (!_dhtOptions.Enabled)
            {
                return(StatusCode(501, "Датчик отключен в настройках."));
            }

            var temperature = Dht.GetTemperature(_dhtOptions.PinNumber, _dhtOptions.PinNumberingScheme);

            return(Ok($"Температура: {temperature}°C."));
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Color the originator with yellow
 /// the immediate peer we're storing the value to in blue
 /// and the peers to which the value is republished in orange:
 /// </summary>
 private void btnPublish_Click(object sender, EventArgs e)
 {
     firstContacts = new List<Dht>();
     storeKey = ID.RandomID;
     originatorDht = dhts[(int)nudPeerNumber.Value];
     originatorDht.Store(storeKey, "Test");
     System.Threading.Thread.Sleep(500);
     dhts.Where(d => d.RepublishStorage.Contains(storeKey)).ForEach(d => firstContacts.Add(d));
     UpdatePeerColors();
     DrawDhts();
 }
Ejemplo n.º 15
0
        public IActionResult GetHumidity()
        {
            if (!_dhtOptions.Enabled)
            {
                return(StatusCode(501, "Датчик отключен в настройках."));
            }

            var humidity = Dht.GetHumidity(_dhtOptions.PinNumber, _dhtOptions.PinNumberingScheme);

            return(Ok($"Влажность воздуха: {humidity}%."));
        }
Ejemplo n.º 16
0
        public void ParallelGetValuePropagatesToCloserNodeTest()
        {
            VirtualProtocol vp1    = new VirtualProtocol();
            VirtualProtocol vp2    = new VirtualProtocol();
            VirtualProtocol vp3    = new VirtualProtocol();
            VirtualStorage  store1 = new VirtualStorage();
            VirtualStorage  store2 = new VirtualStorage();
            VirtualStorage  store3 = new VirtualStorage();
            VirtualStorage  cache3 = new VirtualStorage();

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

            vp1.Node = dht.Router.Node;

            // Setup node 2:

            ID      contactID2    = ID.Mid; // a closer contact.
            Contact otherContact2 = new Contact(vp2, contactID2);
            Node    otherNode2    = new Node(otherContact2, store2);

            vp2.Node = otherNode2;

            // Add the second contact to our peer list.
            dht.Router.Node.BucketList.AddContact(otherContact2);

            // Node 2 has the value.
            // We want an integer distance, not an XOR distance.
            ID     key = ID.Zero;
            string val = "Test";

            otherNode2.Storage.Set(key, val);

            // Setup node 3:

            ID      contactID3    = ID.Zero.SetBit(158); // 01000.... -- a farther contact.
            Contact otherContact3 = new Contact(vp3, contactID3);
            Node    otherNode3    = new Node(otherContact3, store3, cache3);

            vp3.Node = otherNode3;

            // Add the third contact to our peer list.
            dht.Router.Node.BucketList.AddContact(otherContact3);

            Assert.IsFalse(store1.Contains(key), "Obviously we don't have the key-value yet.");
            Assert.IsFalse(store3.Contains(key), "And equally obvious, the third peer doesn't have the key-value yet either.");

            var ret = dht.FindValue(key);

            Assert.IsTrue(ret.found, "Expected value to be found.");
            Assert.IsFalse(store3.Contains(key), "Key should not be in the republish store.");
            Assert.IsTrue(cache3.Contains(key), "Key should be in the cache store.");
            Assert.IsTrue(cache3.GetExpirationTimeSec(key.Value) == Constants.EXPIRATION_TIME_SECONDS / 2, "Expected 12 hour expiration.");
        }
Ejemplo n.º 17
0
        public IActionResult GetData()
        {
            if (!_dhtOptions.Enabled)
            {
                return(StatusCode(501, "Датчик отключен в настройках."));
            }

            var data = Dht.GetData(_dhtOptions.PinNumber, _dhtOptions.PinNumberingScheme);

            return(Ok($"Температура: {data.temperature}°C.\n" +
                      $"Влажность воздуха: {data.humidity}%."));
        }
Ejemplo n.º 18
0
        protected void InitializeKnownPeers()
        {
            knownPeers = new List<Dht>();
            List<Dht> workingList = new List<Dht>(dhts);

            NUM_KNOWN_PEERS.ForEach(() =>
            {
                Dht knownPeer = workingList[rnd.Next(workingList.Count)];
                peerColor[knownPeer.ID.Value] = Color.Red;
                knownPeers.Add(knownPeer);
                workingList.Remove(knownPeer);
            });
        }
Ejemplo n.º 19
0
        /// <summary>
        ///   Ask the next peer the question.
        /// </summary>
        async Task AskAsync(int taskId)
        {
            int pass = 0;

            while (!runningQuery.IsCancellationRequested)
            {
                ++pass;

                // Get the nearest peer that has not been visited.
                var peer = Dht.RoutingTable
                           .NearestPeers(QueryKey)
                           .Where(p => !visited.Contains(p))
                           .FirstOrDefault();
                if (peer == null)
                {
                    return;
                }
                visited.Add(peer);

                // Ask the nearest peer.
                try
                {
                    await askCount.WaitAsync(runningQuery.Token);

                    log.Debug($"Q{Id}.{taskId}.{pass} ask {peer}");
                    using (var timeout = new CancellationTokenSource(askTime))
                        using (var cts = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, runningQuery.Token))
                            using (var stream = await Dht.Swarm.DialAsync(peer, Dht.ToString(), cts.Token))
                            {
                                // Send the KAD query and get a response.
                                ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, queryMessage, PrefixStyle.Base128);
                                await stream.FlushAsync(cts.Token);

                                var response = await ProtoBufHelper.ReadMessageAsync <DhtMessage>(stream, cts.Token);

                                // Process answer
                                ProcessProviders(response.ProviderPeers);
                                ProcessCloserPeers(response.CloserPeers);
                            }
                }
                catch (Exception e)
                {
                    log.Warn($"Q{Id}.{taskId}.{pass} ask failed {e.Message}");
                    // eat it
                }
                finally
                {
                    askCount.Release();
                }
            }
        }
Ejemplo n.º 20
0
        public void ParallelLocalStoreFoundValueTest()
        {
            VirtualProtocol vp  = new VirtualProtocol();
            Dht             dht = new Dht(ID.RandomID, vp, () => new VirtualStorage(), new ParallelRouter());

            vp.Node = dht.Router.Node;
            ID     key = ID.RandomID;
            string val = "Test";

            dht.Store(key, val);
            string retval = dht.FindValue(key).val;

            Assert.IsTrue(retval == val, "Expected to get back what we stored");
        }
Ejemplo n.º 21
0
        override public void Start()
        {
            Channel returns = new Channel();

            returns.CloseEvent += delegate(object o, EventArgs ea) {
                try {
                    bool success = (bool)returns.Dequeue();
                    _result = new DhtPutResult(success, null);
                } catch (Exception e) {
                    _result = new DhtPutResult(false, e);
                } finally {
                    Finished();
                }
            };
            Dht dht = new Dht(Node, 3, 20);

            dht.AsyncPut(Key, Value, Ttl, returns);
        }
Ejemplo n.º 22
0
        public void ParallelBootstrapWithinBootstrappingBucketTest()
        {
            // We need 22 virtual protocols.  One for the bootstrap peer,
            // 10 for the nodes the bootstrap peer knows about, and 10 for the nodes
            // one of those nodes knows about, and one for us to rule them all.
            VirtualProtocol[] vp = new VirtualProtocol[22];
            22.ForEach((i) => vp[i] = new VirtualProtocol());

            // Us
            Dht dhtUs = new Dht(ID.RandomID, vp[0], () => new VirtualStorage(), new ParallelRouter());

            vp[0].Node = dhtUs.Router.Node;

            // Our bootstrap peer
            Dht dhtBootstrap = new Dht(ID.RandomID, vp[1], () => new VirtualStorage(), new ParallelRouter());

            vp[1].Node = dhtBootstrap.Router.Node;
            Node n = null;

            // Our boostrapper knows 10 contacts
            10.ForEach((i) =>
            {
                Contact c      = new Contact(vp[i + 2], ID.RandomID);
                n              = new Node(c, new VirtualStorage());
                vp[i + 2].Node = n;
                dhtBootstrap.Router.Node.BucketList.AddContact(c);
            });

            // One of those nodes, in this case the last one we added to our bootstrapper
            // for convenience, knows about 10 other contacts.
            10.ForEach((i) =>
            {
                Contact c       = new Contact(vp[i + 12], ID.RandomID);
                Node n2         = new Node(c, new VirtualStorage());
                vp[i + 12].Node = n;
                n.BucketList.AddContact(c);     // Note we're adding these contacts to the 10th node.
            });

            dhtUs.Bootstrap(dhtBootstrap.Router.Node.OurContact);

            Assert.IsTrue(dhtUs.Router.Node.BucketList.Buckets.Sum(c => c.Contacts.Count) == 11, "Expected our peer to get 11 contacts.");
        }
Ejemplo n.º 23
0
            public void Start()
            {
                Channel returns = new Channel();

                returns.CloseEvent += delegate(object o, EventArgs ea) {
                    try {
                        _successful = (bool)returns.Dequeue();
                    } catch {
                    }

                    _done = true;
                    if (_callback != null)
                    {
                        _callback(this, EventArgs.Empty);
                    }
                };
                Dht dht = new Dht(_node, 3, 20);

                dht.AsyncPut(_key, _value, _ttl, returns);
            }
Ejemplo n.º 24
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);
            }
        }
Ejemplo n.º 25
0
        public void ParallelValueStoredInFartherNodeTest()
        {
            VirtualProtocol vp1    = new VirtualProtocol();
            VirtualProtocol vp2    = new VirtualProtocol();
            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.Zero, vp1, new ParallelRouter(), store1, store1, new VirtualStorage());

            vp1.Node = dht.Router.Node;

            ID      contactID    = ID.Max; // a farther contact.
            Contact otherContact = new Contact(vp2, contactID);
            Node    otherNode    = new Node(otherContact, store2);

            vp2.Node = otherNode;

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

            // We want an integer distance, not an XOR distance.
            ID key = ID.One;

            // Set the value in the other node, to be discovered by the lookup process.
            string val = "Test";

            otherNode.SimpleStore(key, val);

            Assert.IsFalse(store1.Contains(key), "Expected our peer to NOT have cached the key-value.");
            Assert.IsTrue(store2.Contains(key), "Expected other node to HAVE cached the key-value.");

            // Try and find the value, given our Dht knows about the other contact.
            string retval = dht.FindValue(key).val;

            Assert.IsTrue(retval == val, "Expected to get back what we stored");
        }
        public void NonRespondingContactEvictedTest()
        {
            // Create a DHT so we have an eviction handler.
            Dht dht = new Dht(ID.Zero, new VirtualProtocol(), () => null, new Router());
            //Contact dummyContact = new Contact(new VirtualProtocol(), ID.Zero);
            //((VirtualProtocol)dummyContact.Protocol).Node = new Node(dummyContact, new VirtualStorage());

            IBucketList bucketList = SetupSplitFailure(dht.Node.BucketList);

            Assert.IsTrue(bucketList.Buckets.Count == 2, "Bucket split should have occurred.");
            Assert.IsTrue(bucketList.Buckets[0].Contacts.Count == 1, "Expected 1 contact in bucket 0.");
            Assert.IsTrue(bucketList.Buckets[1].Contacts.Count == 20, "Expected 20 contacts in bucket 1.");

            // The bucket is now full.  Pick the first contact, as it is last seen (they are added in chronological order.)
            Contact nonRespondingContact = bucketList.Buckets[1].Contacts[0];

            // Since the protocols are shared, we need to assign a unique protocol for this node for testing.
            VirtualProtocol vpUnresponding = new VirtualProtocol(((VirtualProtocol)nonRespondingContact.Protocol).Node, false);

            nonRespondingContact.Protocol = vpUnresponding;

            // Setup the next new contact (it can respond.)
            Contact nextNewContact = new Contact(dht.Contact.Protocol, ID.Zero.SetBit(159));

            // Hit the non-responding contact EVICTION_LIMIT times, which will trigger the eviction algorithm.
            Constants.EVICTION_LIMIT.ForEach(() => bucketList.AddContact(nextNewContact));

            Assert.IsTrue(bucketList.Buckets[1].Contacts.Count == 20, "Expected 20 contacts in bucket 1.");

            // Verify CanSplit -> Pending eviction happened.

            Assert.IsTrue(dht.PendingContacts.Count == 0, "Pending contact list should now be empty.");
            Assert.IsFalse(bucketList.Buckets.SelectMany(b => b.Contacts).Contains(nonRespondingContact), "Expected bucket to NOT contain non-responding contact.");
            Assert.IsTrue(bucketList.Buckets.SelectMany(b => b.Contacts).Contains(nextNewContact), "Expected bucket to contain new contact.");
            Assert.IsTrue(dht.EvictionCount.Count == 0, "Expected no contacts to be pending eviction.");
        }
Ejemplo n.º 27
0
        public void Test14(ref int op)
        {
            Console.WriteLine("Test 14: Testing 1000 puts and 1 get with 1000 " +
                              "results with the same key.  Then we remove the main owner of the " +
                              "key.");
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            byte[] key   = new byte[10];
            byte[] value = new byte[value_size];
            rng.GetBytes(key);
            ArrayList al_results = new ArrayList();
            int       count      = 60;

            BlockingQueue[] results_queue = new BlockingQueue[count];

            for (int i = 0; i < count; i++)
            {
                value = new byte[value_size];
                rng.GetBytes(value);
                al_results.Add(value);
                results_queue[i] = new BlockingQueue();
                default_dht.AsyncPut(key, value, 3000, results_queue[i]);
            }
            for (int i = 0; i < count; i++)
            {
                try {
                    bool res = (bool)results_queue[i].Dequeue();
                    Console.WriteLine("success in put : " + i);
                }
                catch {
                    Console.WriteLine("Failure in put : " + i);
                }
            }
            Console.WriteLine("Insertion done...");
            Console.WriteLine("Disconnecting nodes...");
            MemBlock[]   b         = default_dht.MapToRing(key);
            BigInteger[] baddrs    = new BigInteger[default_dht.DEGREE];
            BigInteger[] addrs     = new BigInteger[default_dht.DEGREE];
            bool         first_run = true;

            foreach (DictionaryEntry de in nodes)
            {
                Address addr = (Address)de.Key;
                for (int j = 0; j < b.Length; j++)
                {
                    if (first_run)
                    {
                        addrs[j]  = addr.ToBigInteger();
                        baddrs[j] = (new AHAddress(b[j])).ToBigInteger();
                    }
                    else
                    {
                        BigInteger caddr    = addr.ToBigInteger();
                        BigInteger new_diff = baddrs[j] - caddr;
                        if (new_diff < 0)
                        {
                            new_diff *= -1;
                        }
                        BigInteger c_diff = baddrs[j] - addrs[j];
                        if (c_diff < 0)
                        {
                            c_diff *= -1;
                        }
                        if (c_diff > new_diff)
                        {
                            addrs[j] = caddr;
                        }
                    }
                }
                first_run = false;
            }

            for (int i = 0; i < addrs.Length; i++)
            {
                Console.WriteLine(new AHAddress(baddrs[i]) + " " + new AHAddress(addrs[i]));
                Address laddr = new AHAddress(addrs[i]);
                Node    node  = (Node)nodes[laddr];
                node.Disconnect();
                nodes.Remove(laddr);
                tables.Remove(laddr);
                network_size--;
            }

            default_dht = new Dht((Node)nodes.GetByIndex(0), degree);

            // Checking the ring every 5 seconds..
            do
            {
                Thread.Sleep(5000);
            }while(!CheckAllConnections());
            Console.WriteLine("Going to sleep now...");
            Thread.Sleep(15000);
            Console.WriteLine("Timeout done.... now attempting gets");
            this.SerialAsyncGet(key, (byte[][])al_results.ToArray(typeof(byte[])), op++);
            Thread.Sleep(5000);
            Console.WriteLine("This checks to make sure our follow up Puts succeeded");
            this.SerialAsyncGet(key, (byte[][])al_results.ToArray(typeof(byte[])), op++);
            Console.WriteLine("If no error messages successful up to: " + (op - 1));
            foreach (TableServer ts in tables.Values)
            {
                Console.WriteLine("Count ... " + ts.Count);
            }
        }
Ejemplo n.º 28
0
        /// <summary>Creates an ApplicationNode and prepares it for connection to
        /// the overlay.  For historical reasons it is linked to _node, _dht,
        /// _rpc_dht, and _bso.</summary>
        public virtual ApplicationNode CreateNode(NodeConfig node_config)
        {
            // Get a Node ID for the new Node
            AHAddress address = null;

            try {
                address = (AHAddress)AddressParser.Parse(node_config.NodeAddress);
            } catch {
                address = Utils.GenerateAHAddress();
            }

            // Create the Node state
            StructuredNode node = new StructuredNode(address, node_config.BrunetNamespace);

            _shutdown.OnExit += node.Disconnect;
            IEnumerable addresses = IPAddresses.GetIPAddresses(node_config.DevicesToBind);

            SecurityOverlord so = null;

            // Enable Security if requested
            if (node_config.Security.Enabled)
            {
                if (node_config.Security.SelfSignedCertificates)
                {
                    SecurityPolicy.SetDefaultSecurityPolicy(SecurityPolicy.DefaultEncryptor,
                                                            SecurityPolicy.DefaultAuthenticator, true);
                }

                byte[] blob = null;
                using (FileStream fs = File.Open(node_config.Security.KeyPath, FileMode.Open)) {
                    blob = new byte[fs.Length];
                    fs.Read(blob, 0, blob.Length);
                }

                RSACryptoServiceProvider rsa_private = new RSACryptoServiceProvider();
                rsa_private.ImportCspBlob(blob);

                CertificateHandler ch = null;
                if (node_config.Security.Dtls)
                {
                    ch = new OpenSslCertificateHandler(node_config.Security.CertificatePath,
                                                       address.ToString());
                }
                else
                {
                    ch = new CertificateHandler(node_config.Security.CertificatePath,
                                                address.ToString());
                }


                if (node_config.Security.SecureEdges)
                {
                    node.EdgeVerifyMethod = EdgeVerify.AddressInSubjectAltName;
                }

                // A hack to enable a test for security that doesn't require each peer
                // to exchange certificates
                if (node_config.Security.TestEnable)
                {
                    blob = rsa_private.ExportCspBlob(false);
                    RSACryptoServiceProvider rsa_pub = new RSACryptoServiceProvider();
                    rsa_pub.ImportCspBlob(blob);
                    CertificateMaker cm = new CertificateMaker("United States", "UFL",
                                                               "ACIS", "David Wolinsky", "*****@*****.**", rsa_pub,
                                                               "brunet:node:abcdefghijklmnopqrs");
                    Certificate cacert = cm.Sign(cm, rsa_private);

                    cm = new CertificateMaker("United States", "UFL",
                                              "ACIS", "David Wolinsky", "*****@*****.**", rsa_pub,
                                              address.ToString());
                    Certificate cert = cm.Sign(cacert, rsa_private);
                    ch.AddCACertificate(cacert.X509);
                    ch.AddSignedCertificate(cert.X509);
                }

                if (node_config.Security.Dtls)
                {
                    OpenSslCertificateHandler ssl_ch = ch as OpenSslCertificateHandler;
                    so = new DtlsOverlord(rsa_private, ssl_ch, new PType(20));
                    node.GetTypeSource(new PType(20)).Subscribe(so, null);
                }
                else
                {
                    so = new SymphonySecurityOverlord(node, rsa_private, ch, node.Rrm);
                    node.GetTypeSource(PeerSecOverlord.Security).Subscribe(so, null);
                }
                so.Subscribe(node, null);
            }

            // Add Dht
            new TableServer(node);
            IDht        dht       = new Dht(node, 3, 20);
            RpcDhtProxy dht_proxy = new RpcDhtProxy(dht, node);

            // Setup Vivaldi if requested
            IRelayOverlap ito       = null;
            NCService     ncservice = null;

            if (node_config.NCService.Enabled)
            {
                ncservice = new NCService(node, node_config.NCService.Checkpoint);

                if (node_config.NCService.OptimizeShortcuts)
                {
                    node.Ssco.TargetSelector = new VivaldiTargetSelector(node, ncservice);
                }
                ito = new NCRelayOverlap(ncservice);
            }
            else
            {
                ito = new SimpleRelayOverlap();
            }

            // Create the ApplicationNode
            ApplicationNode app_node = new ApplicationNode(node, dht, dht_proxy, ncservice, so);

            // Add Edge listeners
            EdgeListener el = null;

            foreach (NodeConfig.EdgeListener item in node_config.EdgeListeners)
            {
                el = CreateEdgeListener(item, app_node, addresses);
                if (node_config.Security.SecureEdgesEnabled)
                {
                    el = new SecureEdgeListener(el, so);
                }
                node.AddEdgeListener(el);
            }

            // Create the tunnel and potentially wrap it in a SecureEL
            el = new Relay.RelayEdgeListener(node, ito);
            if (node_config.Security.SecureEdgesEnabled)
            {
                el = new SecureEdgeListener(el, so);
            }
            node.AddEdgeListener(el);

            List <TransportAddress> RemoteTAs = null;

            if (node_config.RemoteTAs != null)
            {
                RemoteTAs = new List <TransportAddress>();
                foreach (String ta in node_config.RemoteTAs)
                {
                    RemoteTAs.Add(TransportAddressFactory.CreateInstance(ta));
                }
                node.RemoteTAs = RemoteTAs;
            }

            // Add XmlRpc
            if (node_config.XmlRpcManager.Enabled)
            {
                if (_xrm == null)
                {
                    _xrm = new XmlRpcManagerServer(node_config.XmlRpcManager.Port);
                }
                _xrm.Add(node, GetXmlRpcUri(app_node));
                new RpcDht(dht, node);
            }

            if (node_config.PrivateNodeConfig != null &&
                node_config.PrivateNodeConfig.Enabled)
            {
                CreatePrivateNode(app_node, NodeConfig.GetPrivateNodeConfig(node_config));
            }
            return(app_node);
        }
Ejemplo n.º 29
0
        public static List <Image> Decode(Stream stream)
        {
            var reader = new BinaryReader(stream);
            var images = new List <Image>();

            stream.Seek(0, SeekOrigin.Begin);

            var imgInfo = new ImgInfo();

            for (long length = stream.Length; ;)
            {
                int markerId;
                do
                {
                    if (stream.Position == length)
                    {
                        goto end;
                    }
                } while (reader.ReadByte() != 0xff);

                markerId = reader.ReadByte();

                switch ((Markers)markerId)
                {
                case Markers.App0:
                    App0.Read(reader, imgInfo);
                    break;

                case Markers.App14:
                    App14.Read(reader, imgInfo);
                    break;

                case Markers.Dqt:
                    Dqt.Read(reader, imgInfo);
                    break;

                case Markers.Sof0:
                    Sof0.Read(reader, imgInfo);
                    break;

                case Markers.Sof2:
                    Sof2.Read(reader, imgInfo);
                    break;

                case Markers.Dht:
                    Dht.Read(reader, imgInfo);
                    break;

                case Markers.Sos:
                    images.Add(Sos.Read(reader, imgInfo));
                    break;

                case Markers.Soi:
                    //Logger.Write("Start of Image " + image);
                    //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                    imgInfo = new ImgInfo
                    {
                        startOfImageFound = true
                    };
                    break;

                case Markers.Dri:
                    Dri.Read(reader, imgInfo);
                    break;

                case Markers.Eoi:
                    //Logger.Write("End of Image " + image);
                    //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                    //eof = true;
                    break;

                // Unknown markers, or markers used outside of their specified area
                default:
                    Default.Read(reader, imgInfo, (Markers)markerId);
                    break;
                }
            }
end:
            reader.Dispose();
            return(images);
        }
Ejemplo n.º 30
0
        public static List <Bitmap> Decode(MemoryStream stream)
        {
            var reader = new BinaryReader(stream);
            var images = new List <Bitmap>();

            stream.Seek(0, SeekOrigin.Begin);

            bool eof = false;

            for (int image = 1; ; image++)
            {
                try
                {
                    var imgInfo = new ImgInfo();

                    while (true)
                    {
                        while (reader.ReadByte() != 0xff)
                        {
                            ;
                        }
                        int markerId = reader.ReadByte();

                        switch ((Markers)markerId)
                        {
                        case Markers.App0:
                            App0.Read(reader, imgInfo);
                            break;

                        case Markers.App14:
                            App14.Read(reader, imgInfo);
                            break;

                        case Markers.Dqt:
                            Dqt.Read(reader, imgInfo);
                            break;

                        case Markers.Sof0:
                            Sof0.Read(reader, imgInfo);
                            break;

                        case Markers.Sof2:
                            Sof2.Read(reader, imgInfo);
                            break;

                        case Markers.Dht:
                            Dht.Read(reader, imgInfo);
                            break;

                        case Markers.Sos:
                            images.Add(Sos.Read(reader, imgInfo));
                            break;

                        case Markers.Soi:
                            imgInfo = new ImgInfo();
                            //Logger.Write("Start of Image " + image);
                            //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                            imgInfo.startOfImageFound = true;
                            break;

                        case Markers.Dri:
                            Dri.Read(reader, imgInfo);
                            break;

                        case Markers.Eoi:
                            //Logger.Write("End of Image " + image);
                            //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                            eof = true;
                            break;

                        // Unknown markers, or markers used outside of their specified area
                        default:
                            Default.Read(reader, imgInfo, (Markers)markerId);
                            break;
                        }

                        if (eof)
                        {
                            eof = false;
                            break;
                        }
                    }
                }
                catch (EndOfStreamException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    //Logger.WriteLine(ex.Message);
                }
            }

            reader.Close();
            //Logger.Flush();

            return(images);
        }