Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move actor-stacksize from teshsuite to examples now that it's of some interest to...
[simgrid.git] / examples / s4u / app-bittorrent / s4u-peer.cpp
index 2670cd7..994d7ac 100644 (file)
@@ -35,6 +35,7 @@ Peer::Peer(std::vector<std::string> args)
   } catch (const std::invalid_argument&) {
     throw std::invalid_argument("Invalid ID:" + args[1]);
   }
+  random.set_seed(id);
 
   try {
     deadline = std::stod(args[2]);
@@ -441,15 +442,15 @@ int Peer::selectPieceToDownload(const Connection* remote_peer)
     int nb_interesting_pieces = 0;
     // compute the number of interesting pieces
     for (unsigned int i = 0; i < FILE_PIECES; i++)
-      if (hasNotPiece(i) && remote_peer->hasPiece(i))
+      if (remotePeerHasMissingPiece(remote_peer, i))
         nb_interesting_pieces++;
 
     xbt_assert(nb_interesting_pieces != 0);
     // get a random interesting piece
-    int random_piece_index = simgrid::xbt::random::uniform_int(0, nb_interesting_pieces - 1);
+    int random_piece_index = random.uniform_int(0, nb_interesting_pieces - 1);
     int current_index      = 0;
     for (unsigned int i = 0; i < FILE_PIECES; i++) {
-      if (hasNotPiece(i) && remote_peer->hasPiece(i)) {
+      if (remotePeerHasMissingPiece(remote_peer, i)) {
         if (random_piece_index == current_index) {
           piece = i;
           break;
@@ -465,14 +466,14 @@ int Peer::selectPieceToDownload(const Connection* remote_peer)
     int nb_interesting_pieces = 0;
     // compute the number of interesting pieces
     for (unsigned int i = 0; i < FILE_PIECES; i++)
-      if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i))
+      if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i))
         nb_interesting_pieces++;
     xbt_assert(nb_interesting_pieces != 0);
     // get a random interesting piece
-    int random_piece_index = simgrid::xbt::random::uniform_int(0, nb_interesting_pieces - 1);
+    int random_piece_index = random.uniform_int(0, nb_interesting_pieces - 1);
     int current_index      = 0;
     for (unsigned int i = 0; i < FILE_PIECES; i++) {
-      if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) {
+      if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i)) {
         if (random_piece_index == current_index) {
           piece = i;
           break;
@@ -488,24 +489,24 @@ int Peer::selectPieceToDownload(const Connection* remote_peer)
     int current_index = 0;
     // compute the smallest number of copies of available pieces
     for (unsigned int i = 0; i < FILE_PIECES; i++) {
-      if (pieces_count[i] < min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i))
+      if (pieces_count[i] < min && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i))
         min = pieces_count[i];
     }
 
     xbt_assert(min != SHRT_MAX || not isInterestedByFree(remote_peer));
     // compute the number of rarest pieces
     for (unsigned int i = 0; i < FILE_PIECES; i++)
-      if (pieces_count[i] == min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i))
+      if (pieces_count[i] == min && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i))
         nb_min_pieces++;
 
     xbt_assert(nb_min_pieces != 0 || not isInterestedByFree(remote_peer));
     // get a random rarest piece
     int random_rarest_index = 0;
     if (nb_min_pieces > 0) {
-      random_rarest_index = simgrid::xbt::random::uniform_int(0, nb_min_pieces - 1);
+      random_rarest_index = random.uniform_int(0, nb_min_pieces - 1);
     }
     for (unsigned int i = 0; i < FILE_PIECES; i++)
-      if (pieces_count[i] == min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) {
+      if (pieces_count[i] == min && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i)) {
         if (random_rarest_index == current_index) {
           piece = i;
           break;
@@ -552,7 +553,7 @@ void Peer::updateChokedPeers()
       do {
         // We choose a random peer to unchoke.
         std::unordered_map<int, Connection>::iterator chosen_peer_it = connected_peers.begin();
-        std::advance(chosen_peer_it, simgrid::xbt::random::uniform_int(0, connected_peers.size() - 1));
+        std::advance(chosen_peer_it, random.uniform_int(0, connected_peers.size() - 1));
         chosen_peer = &chosen_peer_it->second;
         if (not chosen_peer->interested || not chosen_peer->choked_upload)
           chosen_peer = nullptr;
@@ -606,7 +607,7 @@ void Peer::updateInterestedAfterReceive()
       bool interested = false;
       // Check if the peer still has a piece we want.
       for (unsigned int i = 0; i < FILE_PIECES; i++)
-        if (hasNotPiece(i) && remote_peer.hasPiece(i)) {
+        if (remotePeerHasMissingPiece(&remote_peer, i)) {
           interested = true;
           break;
         }
@@ -648,7 +649,7 @@ int Peer::getFirstMissingBlockFrom(int piece)
 int Peer::partiallyDownloadedPiece(const Connection* remote_peer)
 {
   for (unsigned int i = 0; i < FILE_PIECES; i++)
-    if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i) && getFirstMissingBlockFrom(i) > 0)
+    if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i) && getFirstMissingBlockFrom(i) > 0)
       return i;
   return -1;
 }