Exemple #1
0
        static void Main(string[] args)
        {
            IPAddress  bootstrapIP  = IPAddress.Parse("67.215.246.10"); // router.bittorrent.com:6881
            IPEndPoint bootstrapAdr = new IPEndPoint(bootstrapIP, 6881);

            dhtBucketM = new DHTBucketManager(bootstrapAdr, 3245);

            // temporary
            DHTNode dhtNode1 = new DHTNode(3244);

            Console.Read();
        }
        public void Run()
        {
            // Get starting nodes from network
            dhtBucketM = new DHTBucketManager(Program.bootstrapAdr, localPort+1);

            // 1. Get oldest infohashes from DB
            /*
            Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Fetching " + Program.MAX_INFOHASHES_PER_NODE.ToString() + " new infohashes from DB!");
            InitInfoHashes(false);*/

            while(true)
            {
                // 1. Get oldest infohashes from DB
                Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Fetching " + Program.MAX_INFOHASHES_PER_NODE.ToString() + " new infohashes from DB!");
                linfoHHex.Clear();
                InitInfoHashes(false);

                infohashCount = linfoHHex.Count;
                //Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Getting " + Program.MAX_INFOHASHES_PER_NODE.ToString() +
                //    " new infohashes from DB (background thread)!");
                //Thread bg2 = new Thread(o => { InitInfoHashes(true); });
                //bg2.Start();

                // Clear old data            
                nodeIDHex = GetRandomID();
                nofPeersFound = 0;
                bucketIndices.Clear();
                lTorrentPeers.Clear();
                lContactNodesA.Clear();
                lContactNodesB.Clear();

                for (int k = 0; k < Program.MAX_INFOHASHES_PER_NODE; k++)
                {
                    List<IPEndPoint> lt1 = new List<IPEndPoint>();
                    lTorrentPeers.Add(lt1);
                    List<IPEndPoint> lt0 = new List<IPEndPoint>();
                    lTorrentPeersForQueries.Add(lt0);
                    List<Tuple<int, string, IPEndPoint>> lt2 = new List<Tuple<int, string, IPEndPoint>>();
                    lContactNodesA.Add(lt2);
                    List<Tuple<int, string, IPEndPoint>> lt3 = new List<Tuple<int, string, IPEndPoint>>();
                    lContactNodesB.Add(lt3);
                }

                receiveThread = new Thread(new ThreadStart(ReceivePackets));
                receiveThread.IsBackground = true;
                receiveThread.Start();

                // 2. Send get_peers request to all nodes in corresponding buckets for each infohash
                Stopwatch sw = new Stopwatch();

                for (int curr = -Program.MAX_BUCKET_RADIUS; curr < Program.MAX_BUCKET_RADIUS+1; curr++)
                {
                    for (int i = 0; i < infohashCount; i++)
                    {
                        string conversationID = GetConversatioID(i);
                        bucketIndices.Add(GetBucketIndexFromInfohash(linfoHHex[i]));
                        sw.Start();
                        int getPeerReqSentCurrentTimespan = 0;

                        if (bucketIndices[i] + curr < 0) break;
                        if (bucketIndices[i] + curr >= 65536) break;
                        lock (dhtBucketM.locks[bucketIndices[i] + curr])
                        {
                            foreach (Tuple<string, IPEndPoint> t in dhtBucketM.lDHTNodes[bucketIndices[i] + curr])
                            {
                                GetPeers(t.Item2, linfoHHex[i], conversationID);
                                countGetPeerReqSent++; getPeerReqSentCurrentTimespan++;
                            }
                        }

                        while ((1000 * getPeerReqSentCurrentTimespan) / (1 + (double)sw.ElapsedMilliseconds) > Program.MAX_PACKETS_PER_SECOND_DHT_NODE) { }
                        sw.Stop();

                        dhtBucketM.nofNodes = 0;
                        for (int u = 0; u < dhtBucketM.lDHTNodes.Count; u++)
                        {
                            dhtBucketM.nofNodes += dhtBucketM.lDHTNodes[u].Count;
                        }
                    }
                }

                useA = !useA;
                Wait(300);
                receiveThread.Abort();
                Merge();

                 //3. Send get peer requests to all new contact nodes
                for (int passes = 0; passes < 4; passes++)
                {
                    receiveThread = new Thread(new ThreadStart(ReceivePackets));
                    receiveThread.IsBackground = true;
                    receiveThread.Start();

                    // Wait for responses to be processed
                    for (int i = 0; i < infohashCount; i++)
                    {
                        string conversationID = GetConversatioID(i);
                        sw.Start();
                        int getPeerReqSentCurrentTimespan = 0;

                        if (!useA)
                        {
                            foreach (Tuple<int, string, IPEndPoint> t in lContactNodesA[i])
                            {
                                GetPeers(t.Item3, linfoHHex[i], conversationID);
                                countGetPeerReqSent++; getPeerReqSentCurrentTimespan++;
                            }
                            lContactNodesA[i].Clear();
                        }
                        else
                        {
                            foreach (Tuple<int, string, IPEndPoint> t in lContactNodesB[i])
                            {
                                GetPeers(t.Item3, linfoHHex[i], conversationID);
                                countGetPeerReqSent++; getPeerReqSentCurrentTimespan++;
                            }
                            lContactNodesB[i].Clear();
                        }

                        while ((1000 * getPeerReqSentCurrentTimespan) / (1 + (double)sw.ElapsedMilliseconds) > Program.MAX_PACKETS_PER_SECOND_DHT_NODE) { }
                        sw.Stop();

                        dhtBucketM.nofNodes = 0;
                        for (int u = 0; u < dhtBucketM.lDHTNodes.Count; u++)
                        {
                            dhtBucketM.nofNodes += dhtBucketM.lDHTNodes[u].Count;
                        }
                    }
                    useA = !useA;
                    Wait(300);
                    receiveThread.Abort();
                    Merge();
                }
                Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Nodes/peers found: {0}/{1} for {2} infohashes",
dhtBucketM.nofNodes, nofPeersFound, Program.MAX_INFOHASHES_PER_NODE);

                Wait(5000);
                receiveThread.Abort();
                // Wait for get infohash thread to finish
                //while (bg2.IsAlive) { }
                /*linfoHHexForMySQLInsert = new List<string>(linfoHHex);
                lTorrentPeersForQueries = new List<List<IPEndPoint>>(lTorrentPeers);
                linfoHHex.Clear();
                linfoHHex = new List<string>(linfoHHex2);
                linfoHHex2.Clear();*/
                Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Inserting new peers into DB !");
                Program.mySQL.InsertPeers(linfoHHex, lTorrentPeers);
                /*Thread bg = new Thread(o => { Program.mySQL.InsertPeers(linfoHHexForMySQLInsert, lTorrentPeersForQueries); });
                bg.Start();*/
            }

            //Log("Contact nodes: " + lContactNodes.Count);
            //Log("Peers found: " + lTorrentPeers.Count);
            //Log("Peer requests sent: " + countGetPeerReqSent);
            //Log("Received peer packets: " + countRecvPeerPackets);
            //Log("Received node packets: " + countRecvNodePackets);
        }
Exemple #3
0
        public void Run()
        {
            // Get starting nodes from network
            dhtBucketM = new DHTBucketManager(Program.bootstrapAdr, localPort + 1);

            // 1. Get oldest infohashes from DB

            /*
             * Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Fetching " + Program.MAX_INFOHASHES_PER_NODE.ToString() + " new infohashes from DB!");
             * InitInfoHashes(false);*/

            while (true)
            {
                // 1. Get oldest infohashes from DB
                Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Fetching " + Program.MAX_INFOHASHES_PER_NODE.ToString() + " new infohashes from DB!");
                linfoHHex.Clear();
                InitInfoHashes(false);

                infohashCount = linfoHHex.Count;
                //Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Getting " + Program.MAX_INFOHASHES_PER_NODE.ToString() +
                //    " new infohashes from DB (background thread)!");
                //Thread bg2 = new Thread(o => { InitInfoHashes(true); });
                //bg2.Start();

                // Clear old data
                nodeIDHex     = GetRandomID();
                nofPeersFound = 0;
                bucketIndices.Clear();
                lTorrentPeers.Clear();
                lContactNodesA.Clear();
                lContactNodesB.Clear();

                for (int k = 0; k < Program.MAX_INFOHASHES_PER_NODE; k++)
                {
                    List <IPEndPoint> lt1 = new List <IPEndPoint>();
                    lTorrentPeers.Add(lt1);
                    List <IPEndPoint> lt0 = new List <IPEndPoint>();
                    lTorrentPeersForQueries.Add(lt0);
                    List <Tuple <int, string, IPEndPoint> > lt2 = new List <Tuple <int, string, IPEndPoint> >();
                    lContactNodesA.Add(lt2);
                    List <Tuple <int, string, IPEndPoint> > lt3 = new List <Tuple <int, string, IPEndPoint> >();
                    lContactNodesB.Add(lt3);
                }

                receiveThread = new Thread(new ThreadStart(ReceivePackets));
                receiveThread.IsBackground = true;
                receiveThread.Start();

                // 2. Send get_peers request to all nodes in corresponding buckets for each infohash
                Stopwatch sw = new Stopwatch();

                for (int curr = -Program.MAX_BUCKET_RADIUS; curr < Program.MAX_BUCKET_RADIUS + 1; curr++)
                {
                    for (int i = 0; i < infohashCount; i++)
                    {
                        string conversationID = GetConversatioID(i);
                        bucketIndices.Add(GetBucketIndexFromInfohash(linfoHHex[i]));
                        sw.Start();
                        int getPeerReqSentCurrentTimespan = 0;

                        if (bucketIndices[i] + curr < 0)
                        {
                            break;
                        }
                        if (bucketIndices[i] + curr >= 65536)
                        {
                            break;
                        }
                        lock (dhtBucketM.locks[bucketIndices[i] + curr])
                        {
                            foreach (Tuple <string, IPEndPoint> t in dhtBucketM.lDHTNodes[bucketIndices[i] + curr])
                            {
                                GetPeers(t.Item2, linfoHHex[i], conversationID);
                                countGetPeerReqSent++; getPeerReqSentCurrentTimespan++;
                            }
                        }

                        while ((1000 * getPeerReqSentCurrentTimespan) / (1 + (double)sw.ElapsedMilliseconds) > Program.MAX_PACKETS_PER_SECOND_DHT_NODE)
                        {
                        }
                        sw.Stop();

                        dhtBucketM.nofNodes = 0;
                        for (int u = 0; u < dhtBucketM.lDHTNodes.Count; u++)
                        {
                            dhtBucketM.nofNodes += dhtBucketM.lDHTNodes[u].Count;
                        }
                    }
                }

                useA = !useA;
                Wait(300);
                receiveThread.Abort();
                Merge();

                //3. Send get peer requests to all new contact nodes
                for (int passes = 0; passes < 4; passes++)
                {
                    receiveThread = new Thread(new ThreadStart(ReceivePackets));
                    receiveThread.IsBackground = true;
                    receiveThread.Start();

                    // Wait for responses to be processed
                    for (int i = 0; i < infohashCount; i++)
                    {
                        string conversationID = GetConversatioID(i);
                        sw.Start();
                        int getPeerReqSentCurrentTimespan = 0;

                        if (!useA)
                        {
                            foreach (Tuple <int, string, IPEndPoint> t in lContactNodesA[i])
                            {
                                GetPeers(t.Item3, linfoHHex[i], conversationID);
                                countGetPeerReqSent++; getPeerReqSentCurrentTimespan++;
                            }
                            lContactNodesA[i].Clear();
                        }
                        else
                        {
                            foreach (Tuple <int, string, IPEndPoint> t in lContactNodesB[i])
                            {
                                GetPeers(t.Item3, linfoHHex[i], conversationID);
                                countGetPeerReqSent++; getPeerReqSentCurrentTimespan++;
                            }
                            lContactNodesB[i].Clear();
                        }

                        while ((1000 * getPeerReqSentCurrentTimespan) / (1 + (double)sw.ElapsedMilliseconds) > Program.MAX_PACKETS_PER_SECOND_DHT_NODE)
                        {
                        }
                        sw.Stop();

                        dhtBucketM.nofNodes = 0;
                        for (int u = 0; u < dhtBucketM.lDHTNodes.Count; u++)
                        {
                            dhtBucketM.nofNodes += dhtBucketM.lDHTNodes[u].Count;
                        }
                    }
                    useA = !useA;
                    Wait(300);
                    receiveThread.Abort();
                    Merge();
                }
                Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Nodes/peers found: {0}/{1} for {2} infohashes",
                                  dhtBucketM.nofNodes, nofPeersFound, Program.MAX_INFOHASHES_PER_NODE);

                Wait(5000);
                receiveThread.Abort();
                // Wait for get infohash thread to finish
                //while (bg2.IsAlive) { }

                /*linfoHHexForMySQLInsert = new List<string>(linfoHHex);
                 * lTorrentPeersForQueries = new List<List<IPEndPoint>>(lTorrentPeers);
                 * linfoHHex.Clear();
                 * linfoHHex = new List<string>(linfoHHex2);
                 * linfoHHex2.Clear();*/
                Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + ": Inserting new peers into DB !");
                Program.mySQL.InsertPeers(linfoHHex, lTorrentPeers);

                /*Thread bg = new Thread(o => { Program.mySQL.InsertPeers(linfoHHexForMySQLInsert, lTorrentPeersForQueries); });
                 * bg.Start();*/
            }

            //Log("Contact nodes: " + lContactNodes.Count);
            //Log("Peers found: " + lTorrentPeers.Count);
            //Log("Peer requests sent: " + countGetPeerReqSent);
            //Log("Received peer packets: " + countRecvPeerPackets);
            //Log("Received node packets: " + countRecvNodePackets);
        }