Esempio n. 1
0
        // Handling the routing table request
        public void handleRequestClosest(IPEndPoint remotePoint, int sender, int target)
        {
            PeerInfo     targetInfo = askForClosestPeer(sender, target);
            UDPResponder responder  = new UDPResponder(remotePoint, remotePoint.Port);

            responder.sendClosest(target, targetInfo);
        }
Esempio n. 2
0
        private void sendLeaveRequest(PeerInfo pInfo)
        {
            listener.closeSocket();
            IPEndPoint   remotePoint = new IPEndPoint(IPAddress.Parse(pInfo.getIP()), pInfo.getPORT());
            UDPResponder responder   = new UDPResponder(remotePoint, myInfo.getPORT());

            responder.sendRequestLeave(myInfo.getGUID);
        }
Esempio n. 3
0
 public void replace(int targetGuid, PeerInfo p)
 {
     if (!containsValue(p))
     {
         PeerInfo temp = table[targetGuid];
         table.Remove(targetGuid);
         table.Add(targetGuid, p);
         addIfCloser(temp);
     }
 }
Esempio n. 4
0
        private void btnSendFile_Click(object sender, EventArgs e)
        {
            string guid;

            guid = listRoutingTable.SelectedValue.ToString();
            Console.WriteLine(guid);
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult   dr  = ofd.ShowDialog();

            if (dr != DialogResult.OK)
            {
                // no file was selected
                return;
            }

            string fileFullPath = ofd.FileName;
            string fileName     = Path.GetFileName(fileFullPath);

            // get ip and port from selected peer in listbox
            int index = listRoutingTable.SelectedIndex;

            // find the guid of the peer selected
            //string selectedPeer = listRoutingTable.Items[index].ToString();

            if (index == -1)
            {
                toolStatus.Text = "Error: No peer selected!";
                return;
            }

            Dictionary <int, PeerInfo> peerDictionary = peer.getRoutingTable.getPeers();


            // FIX FOR MONDAY
            // Will loop through peerDictonary and send to the first one it finds..

            for (int i = 0; i < 12; i++)
            {
                if (peerDictionary.ContainsKey(i) && peer.getGUID != i)
                {
                    PeerInfo receivingPeer = peerDictionary[i];
                    toolStatus.Text = String.Format("Sending file \"{0}\" to peer guid {1}, ip {2}:{3}", fileName, receivingPeer.getGUID, receivingPeer.getIP(), receivingPeer.getPORT());

                    IPAddress RecevingIp = IPAddress.Parse(receivingPeer.getIP());

                    //// Send file info
                    //// create end point
                    IPEndPoint   remotePoint = new IPEndPoint(RecevingIp, receivingPeer.getPORT());
                    UDPResponder udpResponse = new UDPResponder(remotePoint, listenPort);
                    udpResponse.sendFileInfo(fileFullPath);

                    return;
                }
            }
        }
Esempio n. 5
0
 public void add(PeerInfo p)
 {
     if (table.ContainsKey(p.getGUID))
     {
         replace(p.getGUID, p);
     }
     else
     {
         table.Add(p.getGUID, p);
     }
 }
Esempio n. 6
0
 // Handling receive routing table request
 public void handleReceiveClosest(int guid, PeerInfo currentClosest)
 {
     routingTable.cleanTable(myInfo.getGUID);
     foreach (SearchChannel c in channels)
     {
         if (guid == c.getTargetGUID())
         {
             c.onReceiveClosest(currentClosest);
         }
     }
 }
Esempio n. 7
0
 public void remove(int guid)
 {
     foreach (KeyValuePair <int, PeerInfo> entry in table)
     {
         PeerInfo p = entry.Value;
         if (p.getGUID == guid)
         {
             table.Remove(entry.Key);
             break;
         }
     }
 }
Esempio n. 8
0
        public string toString()
        {
            string result = "";
            int    count  = 0;

            foreach (KeyValuePair <int, PeerInfo> entry in table)
            {
                PeerInfo p = entry.Value;
                result += count + ": " + p.toString + "\n";
                count++;
            }
            return(result);
        }
Esempio n. 9
0
        public bool containsValue(PeerInfo peer)
        {
            List <PeerInfo> list = new List <PeerInfo>(table.Values);

            // Loop through list
            foreach (PeerInfo p in list)
            {
                if (p.getGUID == peer.getGUID)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 10
0
        public PeerInfo findClosestFor(int senderGUID, int Target)
        {
            PeerInfo closest = myInfo;
            int      target  = Target;

            foreach (KeyValuePair <int, PeerInfo> entry in table)
            {
                PeerInfo p = entry.Value;
                if (calculateXOR(p.getGUID, target) < calculateXOR(closest.getGUID, target) && senderGUID != p.getGUID)
                {
                    closest = p;
                }
            }
            return(closest);
        }
Esempio n. 11
0
        // Handling the routing table request
        public void handleJoinRequest(int targetGuid, PeerInfo newPeer)
        {
            List <int> targets = routingTable.getTargetGUIDs(myInfo.getGUID);

            if (targets.Contains(newPeer.getGUID))
            {
                routingTable.add(newPeer);
            }
            else if (!routingTable.containsValue(newPeer))
            {
                routingTable.addIfCloser(newPeer);
            }
            routingTable.cleanTable(myInfo.getGUID);
            updateTable();
        }
Esempio n. 12
0
        public bool addIfCloser(PeerInfo p)
        {
            List <int> targets       = getTargetGUIDs(myInfo.getGUID);
            List <int> closerToIndex = new List <int>();
            bool       added         = false;

            foreach (int tar in targets)
            {
                if (table.ContainsKey(tar))
                {
                    PeerInfo current = table[tar];
                    if (calculateXOR(p.getGUID, tar) < calculateXOR(current.getGUID, tar))
                    {
                        closerToIndex.Add(tar);
                    }
                }
                else
                {
                    closerToIndex.Add(tar);
                }
            }

            int index = -1;
            int dist  = -1;

            foreach (int closer in closerToIndex)
            {
                if (dist == -1 || calculateXOR(p.getGUID, closer) < dist)
                {
                    index = closer;
                    dist  = calculateXOR(p.getGUID, closer);
                }
            }

            if (index != -1)
            {
                added = true;
                if (table.ContainsKey(index))
                {
                    replace(index, p);
                }
                else
                {
                    table.Add(index, p);
                }
            }
            return(added);
        }
Esempio n. 13
0
        // Finds closest GUIDs and searches for closest peers to them in the network
        private void searchTargetPeers()
        {
            List <int> targetGUIDs = routingTable.getTargetGUIDs(myInfo.getGUID);

            foreach (int guid in targetGUIDs)
            {
                PeerInfo closest = routingTable.findClosest(guid);
                if (closest != null && myInfo.getGUID != closest.getGUID)
                {
                    //routingTable.remove(closest.getGUID);
                    SearchChannel channel = new SearchChannel(this, guid);
                    channel.onReceiveClosest(closest);
                    channels.Add(channel);
                }
            }
        }
Esempio n. 14
0
        public PeerInfo findClosest(int targetGUID)
        {
            PeerInfo closest = null;
            int      target  = targetGUID;

            foreach (KeyValuePair <int, PeerInfo> entry in table)
            {
                PeerInfo p = entry.Value;
                if (closest == null)
                {
                    closest = p;
                }
                else if (calculateXOR(p.getGUID, target) < calculateXOR(closest.getGUID, target))
                {
                    closest = p;
                }
            }
            return(closest);
        }
Esempio n. 15
0
        public void init(int guid, string ip, int port)
        {
            myInfo = new PeerInfo(guid, ip, port);

            // Create UDP listen and add events
            listener = new UDPListener(port);
            subscribeToListener(listener);

            // Create Routing Table and adding boot peer
            routingTable = new RoutingTable(myInfo);
            routingTable.add(bootPeer);

            listener.setRoutingtable = routingTable;

            // Create EndPoint
            IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse(bootPeer.getIP()), bootPeer.getPORT());
            // Sending routing table request
            UDPResponder responder = new UDPResponder(remotePoint, port);

            responder.sendRequestRoutingTable();
        }
Esempio n. 16
0
        public void addPeerInfo(PeerInfo pInfo)
        {
            List <int> targets = routingTable.getTargetGUIDs(myInfo.getGUID);

            if (targets.Contains(pInfo.getGUID))
            {
                routingTable.add(pInfo);
                IPEndPoint   remotePoint = new IPEndPoint(IPAddress.Parse(pInfo.getIP()), pInfo.getPORT());
                UDPResponder responder   = new UDPResponder(remotePoint, myInfo.getPORT());
                responder.sendRequestJoin(1, myInfo);
            }
            else if (!routingTable.containsValue(pInfo))
            {
                bool added = routingTable.addIfCloser(pInfo);
                if (added)
                {
                    IPEndPoint   remotePoint = new IPEndPoint(IPAddress.Parse(pInfo.getIP()), pInfo.getPORT());
                    UDPResponder responder   = new UDPResponder(remotePoint, myInfo.getPORT());
                    responder.sendRequestJoin(1, myInfo);
                }
            }
            routingTable.cleanTable(myInfo.getGUID);
            updateTable();
        }
Esempio n. 17
0
 public RoutingTable(PeerInfo myInfo)
 {
     table       = new Dictionary <int, PeerInfo>();
     this.myInfo = myInfo;
 }
Esempio n. 18
0
 // TODO: pass boot peer after retrieval of peers from servers
 public Peer()
 {
     channels = new List <SearchChannel>();
     bootPeer = new PeerInfo(0, "145.93.116.180", 8080);
 }