예제 #1
0
        static public 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);
        }
예제 #2
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.");
            }
        }