]> AND Public Git Repository - simgrid.git/blobdiff - examples/s4u/app-bittorrent/s4u-peer.cpp
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into xbt_random
[simgrid.git] / examples / s4u / app-bittorrent / s4u-peer.cpp
index ff9a09476c9f17012851f3f81f7c0df0204f9b68..e335d11b25bf26cf5e16856d821cc35ec6e6255d 100644 (file)
@@ -44,8 +44,6 @@ Peer::Peer(std::vector<std::string> args)
   }
   xbt_assert(deadline > 0, "Wrong deadline supplied");
 
-  stream = simgrid::s4u::this_actor::get_host()->extension<HostBittorrent>()->getStream();
-
   if (args.size() == 4 && args[3] == "1") {
     bitfield_       = (1U << FILE_PIECES) - 1U;
     bitfield_blocks = (1ULL << (FILE_PIECES * PIECES_BLOCKS)) - 1ULL;
@@ -456,7 +454,7 @@ int Peer::selectPieceToDownload(Connection* remote_peer)
 
     xbt_assert(nb_interesting_pieces != 0);
     // get a random interesting piece
-    int random_piece_index = RngStream_RandInt(stream, 0, nb_interesting_pieces - 1);
+    int random_piece_index = simgrid::xbt::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)) {
@@ -479,7 +477,7 @@ int Peer::selectPieceToDownload(Connection* remote_peer)
         nb_interesting_pieces++;
     xbt_assert(nb_interesting_pieces != 0);
     // get a random interesting piece
-    int random_piece_index = RngStream_RandInt(stream, 0, nb_interesting_pieces - 1);
+    int random_piece_index = simgrid::xbt::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)) {
@@ -510,7 +508,10 @@ int Peer::selectPieceToDownload(Connection* remote_peer)
 
     xbt_assert(nb_min_pieces != 0 || not isInterestedByFree(remote_peer));
     // get a random rarest piece
-    int random_rarest_index = RngStream_RandInt(stream, 0, nb_min_pieces - 1);
+    int random_rarest_index = 0;
+    if (nb_min_pieces > 0) {
+      random_rarest_index = simgrid::xbt::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 (random_rarest_index == current_index) {
@@ -559,7 +560,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, RngStream_RandInt(stream, 0, connected_peers.size() - 1));
+        std::advance(chosen_peer_it, simgrid::xbt::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;