From: Samuel Lepetit Date: Mon, 2 Jul 2012 16:47:55 +0000 (+0200) Subject: Bugfix in Bittorrent example X-Git-Tag: v3_9_90~569^2~19^2~22 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/17ac42f75ce20ece3947836ebdd8efb3378b9dce Bugfix in Bittorrent example --- diff --git a/examples/bittorrent/Common.java b/examples/bittorrent/Common.java index 51cb01d77f..e69d3c8c9e 100644 --- a/examples/bittorrent/Common.java +++ b/examples/bittorrent/Common.java @@ -3,6 +3,7 @@ package bittorrent; * Common constants for use in the simulation */ public class Common { + public static String TRACKER_MAILBOX = "tracker_mailbox"; public static int FILE_SIZE = 5120; @@ -48,7 +49,7 @@ public class Common { /** * Interval between each update of the choked peers */ - public static int UPDATE_CHOKED_INTERVAL = 50; + public static int UPDATE_CHOKED_INTERVAL = 30; /** * Number of pieces the peer asks for simultaneously */ diff --git a/examples/bittorrent/Connection.java b/examples/bittorrent/Connection.java index 5cb1b62bba..87ecaccd3b 100644 --- a/examples/bittorrent/Connection.java +++ b/examples/bittorrent/Connection.java @@ -1,7 +1,7 @@ package bittorrent; import java.util.Arrays; - +import org.simgrid.msg.Msg; public class Connection { /** * Remote peer id @@ -31,7 +31,18 @@ public class Connection { * Indicates if the peer has choked the current peer */ public boolean chokedDownload = true; - + /** + * Number of messages we have received from the peer + */ + public int messagesCount = 0; + /** + * Peer speed. + */ + public double peerSpeed = 0; + /** + * Last time the peer was unchoked + */ + public double lastUnchoke = 0; /** * Constructor */ @@ -39,7 +50,14 @@ public class Connection { this.id = id; this.mailbox = Integer.toString(id); } - + /** + * Add a new value to the peer speed average + */ + public void addSpeedValue(double speed) { + peerSpeed = peerSpeed * 0.55 + speed * 0.45; + // peerSpeed = (peerSpeed * messagesCount + speed) / (++messagesCount); + } + @Override public String toString() { return "Connection [id=" + id + ", bitfield=" diff --git a/examples/bittorrent/Peer.java b/examples/bittorrent/Peer.java index 68c694e8e1..981596dec7 100644 --- a/examples/bittorrent/Peer.java +++ b/examples/bittorrent/Peer.java @@ -2,6 +2,7 @@ package bittorrent; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Map.Entry; @@ -13,12 +14,15 @@ import org.simgrid.msg.RngStream; import org.simgrid.msg.Process; import org.simgrid.msg.Task; +import bittorrent.Connection; + /** * Main class for peers execution */ public class Peer extends Process { protected int round = 0; + protected double beginReceiveTime; protected double deadline; protected static RngStream stream = new RngStream(); @@ -70,6 +74,7 @@ public class Peer extends Process { if (getPeersData()) { Msg.debug("Got " + peers.size() + " peers from the tracker"); Msg.debug("Here is my current status: " + getStatus()); + beginReceiveTime = Msg.getClock(); if (hasFinished()) { pieces = Common.FILE_PIECES; sendHandshakeAll(); @@ -351,6 +356,10 @@ public class Peer extends Process { } break; } + if (remotePeer != null) { + remotePeer.addSpeedValue(1 / (Msg.getClock() - beginReceiveTime)); + } + beginReceiveTime = Msg.getClock(); } /** * Wait for the node to receive interesting bitfield messages (ie: non empty) @@ -439,33 +448,57 @@ public class Peer extends Process { peerChoked.chokedUpload = true; activePeers.remove(e.getKey()); } - //Random optimistic unchoking - if (round == 0 || true) { - int j = 0, i; - Connection peerChoosed = null; - do { - i = 0; - int idChosen = stream.randInt(0,peers.size() - 1); - for (Connection connection : peers.values()) { - if (i == idChosen) { - peerChoosed = connection; - break; + Connection peerChoosed = null; + //Separate the case from when the peer is seeding. + if (pieces == Common.FILE_PIECES) { + //Find the last unchoked peer. + double unchokeTime = deadline + 1; + for (Connection connection : peers.values()) { + if (connection.lastUnchoke < unchokeTime && connection.chokedUpload && connection.interested) { + peerChoosed = connection; + unchokeTime = connection.lastUnchoke; + } + } + } + else { + //Random optimistic unchoking + if (round == 0) { + int j = 0, i; + do { + i = 0; + int idChosen = stream.randInt(0,peers.size() - 1); + for (Connection connection : peers.values()) { + if (i == idChosen) { + peerChoosed = connection; + break; + } + i++; + } //TODO: Not really the best way ever + if (!peerChoosed.interested) { + peerChoosed = null; + } + j++; + } while (peerChoosed == null && j < + Common.MAXIMUM_PEERS); + } + else { + Connection fastest = null; + double fastestSpeed = 0; + for (Connection c : peers.values()) { + if (c.peerSpeed > fastestSpeed && c.chokedUpload && c.interested) { + fastest = c; + fastestSpeed = c.peerSpeed; } - i++; - } //TODO: Not really the best way ever - if (!peerChoosed.interested) { - peerChoosed = null; } - j++; - } while (peerChoosed == null && j < -Common.MAXIMUM_PEERS); - if (peerChoosed != null) { - activePeers.put(peerChoosed.id,peerChoosed); - peerChoosed.chokedUpload = false; - sendUnchoked(peerChoosed.mailbox); + peerChoosed = fastest; } } - //TODO: Use the leecher choke algorithm. + if (peerChoosed != null) { + activePeers.put(peerChoosed.id,peerChoosed); + peerChoosed.chokedUpload = false; + peerChoosed.lastUnchoke = Msg.getClock(); + sendUnchoked(peerChoosed.mailbox); + } } /** * Updates our "interested" state about peers: send "not interested" to peers diff --git a/examples/kademlia/Node.java b/examples/kademlia/Node.java index e8e4e7088f..1655a7dbcd 100644 --- a/examples/kademlia/Node.java +++ b/examples/kademlia/Node.java @@ -257,7 +257,7 @@ public class Node extends Process { return destinationFound; } /** - * Sends a "PING" request to a node + * Sends a "PING" reque ast to a node * @param destination Ping destination id. */ public void ping(int destination) {