Beispiel #1
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;
        }
        private bool CreateKademliaNode(int id)
        {
            foreach (KademliaNode node in _kademliaNodes)
            {
                if (node.GetId() == id)
                {
                    return(false);
                }
            }

            KademliaNode newNode = new KademliaNode(id);

            if (id != 0)
            {
                newNode.Bootstrap(_kademliaNodes[0]);
            }
            _kademliaNodes.Add(newNode);
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Create a DHT using the given master server, and specify whether to publish our IP.
        /// PRECONDITION: Create one per app or you will have a node ID collision.
        /// </summary>
        /// <param name="dhtNode">The KademliaNode that is used to communicate using the protocol</param>
        /// <param name="alreadyBootstrapped">Checks if the node have or not to bootstrap</param>
        /// <param name="btpNode">The node to bootstrap with (can be leaved null)</param>
        public Dht(KademliaNode dhtNode = null, bool alreadyBootstrapped = false, string btpNode = "")
        {
            if (dhtNode != null)
            {
                this.dhtNode = dhtNode;
            }
            else
            {
                dhtNode = new KademliaNode();
            }
            if (!alreadyBootstrapped)
            {
                if (btpNode == "")
                {
                    int ourPort = dhtNode.GetPort();
                    log.Info("We are on UDP port " + ourPort.ToString());

                    log.Info("Getting bootstrap list...");

                    AppSettingsReader asr = new AppSettingsReader();

                    XDocument xmlDoc = XDocument.Load((string)asr.GetValue("KademliaNodesFile", typeof(string)));

                    List<EndpointAddress> nodes = new List<EndpointAddress>(from node in xmlDoc.Descendants("Node")
                                select new EndpointAddress("soap.udp://" + node.Element("Host").Value + ":" + node.Element("Port").Value + "/kademlia"));

                    foreach (var node in nodes)
                    {
                        if (dhtNode.AsyncBootstrap(nodes))
                        {
                            log.Debug("OK!");
                        }
                        else
                        {
                            log.Debug("Failed.");
                        }
                    }
                }
                else
                {
                    try
                    {
                        log.Debug("Bootstrapping with " + btpNode);
                        EndpointAddress bootstrapNode = new EndpointAddress(btpNode);
                        if (dhtNode.Bootstrap(bootstrapNode))
                        {
                            log.Debug("OK!");
                        }
                        else
                        {
                            log.Debug("Failed.");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Bad entry!", ex);
                    }
                }
            }
            else
            {
                log.Info("Self Bootstrapping");
                dhtNode.Bootstrap();
            }
            // Join the network officially
            log.Info("Trying to join network....");
            if(dhtNode.JoinNetwork()) {
                log.Info("Online");
            } else {
                log.Warn("Unable to connect to Kademlia overlay!\n"
                                   + "Check that nodes list has accessible nodes.");
            }
        }
Beispiel #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("ID tests");
            byte[] aData = new byte[20];
            aData[3] = 10;
            byte[] bData = new byte[20];
            bData[1] = 10;

            ID a = new ID(aData);
            ID b = new ID(bData);

            Debug.Assert(a != b);
            Debug.Assert(a == a);
            Debug.Assert(!(b == a));
            Debug.Assert(a < b);
            Debug.Assert(b > a);
            Debug.Assert(a.Equals(a));
            Debug.Assert(!a.Equals(b));
            Debug.Assert(a.GetHashCode() != b.GetHashCode());
            Debug.Assert(a.DifferingBit(b) == 4 * 8 - 2);             // next to last bit of 4th byte differs
            Console.WriteLine("Test complete");

            Console.WriteLine("Testing KademilaNode");
            KademliaNode node1 = new KademliaNode(new EndpointAddress("soap.udp://localhost:8002/kademlia"));
            ServiceHost  host1 = new ServiceHost(node1, new Uri("soap.udp://localhost:8002/kademlia"));
            KademliaNode node2 = new KademliaNode(new EndpointAddress("soap.udp://localhost:8001/kademlia"));
            ServiceHost  host2 = new ServiceHost(node2, new Uri("soap.udp://localhost:8001/kademlia"));

//			node1.Bootstrap();
            System.Threading.Thread.Sleep(50);             // Wait for the other node to process its bucket queue
            node1.JoinNetwork();
            node2.JoinNetwork();

            // Do a big test
            List <KademliaNode> nl       = new List <KademliaNode>();
            KademliaNode        lastNode = node1;

            for (int i = 0; i < 10; i++)
            {
                KademliaNode node = new KademliaNode();
                node.Bootstrap();
                lastNode = node;
                System.Threading.Thread.Sleep(50);                 // Wait for the other node to process its bucket queue
                node.JoinNetwork();
                nl.Add(node);
            }
            host1.Close();
            host2.Close();
            Console.WriteLine("Connectivity tests complete");

/*
 *                      // Do a store test
 *                      ID babiesID = ID.RandomID();
 *                      node1.Put(babiesID, "=====I eat babies=====");
 *                      Console.WriteLine("Store tests complete");
 *
 *                      // Get it back
 *                      IList<string> foundVals = node1.Get(babiesID);
 *                      foreach(string s in foundVals) {
 *                              Console.WriteLine("1 Found: " + s);
 *                      }
 *                      foundVals = node2.Get(babiesID);
 *                      foreach(string s in foundVals) {
 *                              Console.WriteLine("2 Found: " + s);
 *                      }
 *                      Console.WriteLine("Find tests complete");
 *
 *                      Console.WriteLine("Testing DHT");
 *                      Dht dht = new Dht("C:\\Users\\seby\\Documents\\progetto_malgeri\\p2p-player\\nodes"+Console.ReadLine()+".xml");
 *                      dht.Put("A", "The value for A");
 *                      Console.WriteLine("A = " + dht.Get("A"));
 *
 *                      Console.WriteLine("Multi-value test");
 *                      dht.Put("Animal", "cat");
 *                      dht.Put("Animal", "dog");
 *                      dht.Put("Animal", "double-beaver");
 *                      dht.Put("Animal", "wombat");
 *                      dht.Put("Animal", "alot");
 *                      Console.WriteLine("Animal entry count " + node1.Get(ID.Hash("Animal")).Count);
 *                      Console.WriteLine("Animal entry count " + node2.Get(ID.Hash("Animal")).Count);
 *                      Console.WriteLine("Arbitrary animal = " + dht.Get("Animal"));
 *
 */
            Console.WriteLine("Testing complete! Press any key to exit the program.");
            Console.ReadLine();
        }
Beispiel #5
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;
        }
Beispiel #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("ID tests");
            byte[] aData = new byte[20];
            aData[3] = 10;
            byte[] bData = new byte[20];
            bData[1] = 10;

            ID a = new ID(aData);
            ID b = new ID(bData);
            Debug.Assert(a != b);
            Debug.Assert(a == a);
            Debug.Assert(!(b == a));
            Debug.Assert(a < b);
            Debug.Assert(b > a);
            Debug.Assert(a.Equals(a));
            Debug.Assert(!a.Equals(b));
            Debug.Assert(a.GetHashCode() != b.GetHashCode());
            Debug.Assert(a.DifferingBit(b) == 4 * 8 - 2); // next to last bit of 4th byte differs
            Console.WriteLine("Test complete");

            Console.WriteLine("Testing KademilaNode");
            KademliaNode node1 = new KademliaNode(new EndpointAddress("soap.udp://localhost:8002/kademlia"));
            ServiceHost host1 = new ServiceHost(node1, new Uri("soap.udp://localhost:8002/kademlia"));
            KademliaNode node2 = new KademliaNode(new EndpointAddress("soap.udp://localhost:8001/kademlia"));
            ServiceHost host2 = new ServiceHost(node2, new Uri("soap.udp://localhost:8001/kademlia"));
            //			node1.Bootstrap();
            System.Threading.Thread.Sleep(50); // Wait for the other node to process its bucket queue
            node1.JoinNetwork();
            node2.JoinNetwork();

            // Do a big test
            List<KademliaNode> nl = new List<KademliaNode>();
            KademliaNode lastNode = node1;
            for(int i = 0; i < 10; i++) {
                KademliaNode node = new KademliaNode();
                node.Bootstrap();
                lastNode = node;
                System.Threading.Thread.Sleep(50); // Wait for the other node to process its bucket queue
                node.JoinNetwork();
                nl.Add(node);
            }
            host1.Close();
            host2.Close();
            Console.WriteLine("Connectivity tests complete");
            /*
            // Do a store test
            ID babiesID = ID.RandomID();
            node1.Put(babiesID, "=====I eat babies=====");
            Console.WriteLine("Store tests complete");

            // Get it back
            IList<string> foundVals = node1.Get(babiesID);
            foreach(string s in foundVals) {
                Console.WriteLine("1 Found: " + s);
            }
            foundVals = node2.Get(babiesID);
            foreach(string s in foundVals) {
                Console.WriteLine("2 Found: " + s);
            }
            Console.WriteLine("Find tests complete");

            Console.WriteLine("Testing DHT");
            Dht dht = new Dht("C:\\Users\\seby\\Documents\\progetto_malgeri\\p2p-player\\nodes"+Console.ReadLine()+".xml");
            dht.Put("A", "The value for A");
            Console.WriteLine("A = " + dht.Get("A"));

            Console.WriteLine("Multi-value test");
            dht.Put("Animal", "cat");
            dht.Put("Animal", "dog");
            dht.Put("Animal", "double-beaver");
            dht.Put("Animal", "wombat");
            dht.Put("Animal", "alot");
            Console.WriteLine("Animal entry count " + node1.Get(ID.Hash("Animal")).Count);
            Console.WriteLine("Animal entry count " + node2.Get(ID.Hash("Animal")).Count);
            Console.WriteLine("Arbitrary animal = " + dht.Get("Animal"));

            */
            Console.WriteLine("Testing complete! Press any key to exit the program.");
            Console.ReadLine();
        }