Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Explicit cast when loosing precision (C++ examples).
[simgrid.git] / examples / s4u / app-bittorrent / s4u-peer.cpp
index 2962df6e18774a0ddf11461686f84d86bc1de6ae..d653c2a0ee5e4c6aaa8fc58cd773cbcf5c8b643e 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]);
@@ -149,7 +150,7 @@ void Peer::sendRequestTo(Connection* remote_peer, unsigned int piece)
   xbt_assert(remote_peer->hasPiece(piece));
   int block_index = getFirstMissingBlockFrom(piece);
   if (block_index != -1) {
-    int block_length = std::min(BLOCKS_REQUESTED, PIECES_BLOCKS - block_index);
+    int block_length = static_cast<int>(std::min(BLOCKS_REQUESTED, PIECES_BLOCKS - block_index));
     XBT_DEBUG("Sending a REQUEST to %s for piece %u (%d,%d)", remote_peer->mailbox_->get_cname(), piece, block_index,
               block_length);
     remote_peer->mailbox_
@@ -158,7 +159,7 @@ void Peer::sendRequestTo(Connection* remote_peer, unsigned int piece)
   }
 }
 
-std::string Peer::getStatus()
+std::string Peer::getStatus() const
 {
   std::string res;
   for (unsigned i = 0; i < FILE_PIECES; i++)
@@ -166,7 +167,7 @@ std::string Peer::getStatus()
   return res;
 }
 
-bool Peer::hasFinished()
+bool Peer::hasFinished() const
 {
   return bitfield_ == (1U << FILE_PIECES) - 1U;
 }
@@ -192,7 +193,7 @@ void Peer::updatePiecesCountFromBitfield(unsigned int bitfield)
       pieces_count[i]++;
 }
 
-unsigned int Peer::countPieces(unsigned int bitfield)
+unsigned int Peer::countPieces(unsigned int bitfield) const
 {
   unsigned int count = 0U;
   unsigned int n     = bitfield;
@@ -203,7 +204,7 @@ unsigned int Peer::countPieces(unsigned int bitfield)
   return count;
 }
 
-int Peer::nbInterestedPeers()
+int Peer::nbInterestedPeers() const
 {
   int nb = 0;
   for (auto const& kv : connected_peers)
@@ -446,7 +447,7 @@ int Peer::selectPieceToDownload(const Connection* remote_peer)
 
     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 (remotePeerHasMissingPiece(remote_peer, i)) {
@@ -469,7 +470,7 @@ int Peer::selectPieceToDownload(const Connection* remote_peer)
         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 (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i)) {
@@ -502,7 +503,7 @@ int Peer::selectPieceToDownload(const Connection* 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 && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i)) {
@@ -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, static_cast<int>(connected_peers.size() - 1)));
         chosen_peer = &chosen_peer_it->second;
         if (not chosen_peer->interested || not chosen_peer->choked_upload)
           chosen_peer = nullptr;
@@ -628,7 +629,7 @@ void Peer::updateBitfieldBlocks(int piece, int block_index, int block_length)
     bitfield_blocks |= (1ULL << static_cast<unsigned int>(piece * PIECES_BLOCKS + i));
 }
 
-bool Peer::hasCompletedPiece(unsigned int piece)
+bool Peer::hasCompletedPiece(unsigned int piece) const
 {
   for (unsigned int i = 0; i < PIECES_BLOCKS; i++)
     if (not(bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i)))
@@ -636,7 +637,7 @@ bool Peer::hasCompletedPiece(unsigned int piece)
   return true;
 }
 
-int Peer::getFirstMissingBlockFrom(int piece)
+int Peer::getFirstMissingBlockFrom(int piece) const
 {
   for (unsigned int i = 0; i < PIECES_BLOCKS; i++)
     if (not(bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i)))
@@ -645,7 +646,7 @@ int Peer::getFirstMissingBlockFrom(int piece)
 }
 
 /** Returns a piece that is partially downloaded and stored by the remote peer if any -1 otherwise. */
-int Peer::partiallyDownloadedPiece(const Connection* remote_peer)
+int Peer::partiallyDownloadedPiece(const Connection* remote_peer) const
 {
   for (unsigned int i = 0; i < FILE_PIECES; i++)
     if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i) && getFirstMissingBlockFrom(i) > 0)