Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix in kademlia example
[simgrid.git] / examples / bittorrent / Peer.java
index 7e3fb8c..1ad6edf 100644 (file)
@@ -9,6 +9,7 @@ import org.simgrid.msg.Comm;
 import org.simgrid.msg.Host;
 import org.simgrid.msg.Msg;
 import org.simgrid.msg.MsgException;
+import org.simgrid.msg.RngStream;
 import org.simgrid.msg.Process;
 import org.simgrid.msg.Task;
 
@@ -20,6 +21,8 @@ public class Peer extends Process {
        
        protected double deadline;
        
+       protected static RngStream stream = new RngStream();
+       
        protected int id;
        protected String mailbox;
        protected String mailboxTracker;
@@ -317,7 +320,6 @@ public class Peer extends Process {
                                        if (bitfield[message.index] == '0') {
                                                piecesRequested--;
                                                //Removing the piece from our piece list.
-                                               //TODO: It can not work, I should test it
                                                if (!currentPieces.remove((Object)Integer.valueOf(message.index))) {
                                                }
                                                //Setting the fact that we have the piece
@@ -326,7 +328,7 @@ public class Peer extends Process {
                                                Msg.debug("My status is now " + getStatus());
                                                //Sending the information to all the peers we are connected to
                                                sendHave(message.index);
-                                               //sending UNINTERSTED to peers that doesn't have what we want.
+                                               //sending UNINTERESTED to peers that doesn't have what we want.
                                                updateInterestedAfterReceive();
                                        }
                                        else {
@@ -393,7 +395,7 @@ public class Peer extends Process {
                if (true || pieces < 3) {
                        int i = 0, peerPiece;
                        do {
-                               currentPiece = ((int)Msg.getClock() + id + i) % Common.FILE_PIECES;
+                               currentPiece = stream.randInt(0,Common.FILE_PIECES - 1);
                                i++;
                        } while (!(bitfield[currentPiece] == '0' && !currentPieces.contains(currentPiece)));
                }
@@ -429,7 +431,7 @@ public class Peer extends Process {
                        Connection peerChoosed = null;
                        do {
                                i = 0;
-                               int idChosen = ((int)Msg.getClock() + j) % peers.size();
+                               int idChosen = stream.randInt(0,peers.size() - 1);
                                for (Connection connection : peers.values()) {
                                        if (i == idChosen) {
                                                peerChoosed = connection;
@@ -441,13 +443,15 @@ public class Peer extends Process {
                                        peerChoosed = null;
                                }
                                j++;
-                       } while (peerChoosed == null && j < Common.MAXIMUM_PAIRS);
+                       } while (peerChoosed == null && j < 
+Common.MAXIMUM_PEERS);
                        if (peerChoosed != null) {
                                activePeers.put(peerChoosed.id,peerChoosed);
                                peerChoosed.chokedUpload = false;
                                sendUnchoked(peerChoosed.mailbox);
                        }
                }
+               //TODO: Use the leecher choke algorithm.
        }
        /**     
         * Updates our "interested" state about peers: send "not interested" to peers