X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/f89671e0bd7450461d70d5ced6079123e73c2a63..34b20c9b8fb4093575385be4579cad4193ca2a09:/examples/s4u/app-bittorrent/s4u-peer.cpp diff --git a/examples/s4u/app-bittorrent/s4u-peer.cpp b/examples/s4u/app-bittorrent/s4u-peer.cpp index e335d11b25..994d7ac6f9 100644 --- a/examples/s4u/app-bittorrent/s4u-peer.cpp +++ b/examples/s4u/app-bittorrent/s4u-peer.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -22,7 +22,6 @@ constexpr int BLOCK_SIZE = 16384; /** Number of blocks asked by each request */ constexpr unsigned long BLOCKS_REQUESTED = 2UL; -constexpr bool ENABLE_END_GAME_MODE = true; constexpr double SLEEP_DURATION = 1.0; #define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0) @@ -36,6 +35,7 @@ Peer::Peer(std::vector args) } catch (const std::invalid_argument&) { throw std::invalid_argument("Invalid ID:" + args[1]); } + random.set_seed(id); try { deadline = std::stod(args[2]); @@ -161,9 +161,9 @@ void Peer::sendRequestTo(Connection* remote_peer, unsigned int piece) std::string Peer::getStatus() { - std::string res = std::string(""); - for (int i = FILE_PIECES - 1; i >= 0; i--) - res = std::string((bitfield_ & (1U << i)) ? "1" : "0") + res; + std::string res; + for (unsigned i = 0; i < FILE_PIECES; i++) + res += (bitfield_ & (1U << i)) ? '1' : '0'; return res; } @@ -173,12 +173,12 @@ bool Peer::hasFinished() } /** Indicates if the remote peer has a piece not stored by the local peer */ -bool Peer::isInterestedBy(Connection* remote_peer) +bool Peer::isInterestedBy(const Connection* remote_peer) const { return remote_peer->bitfield & (bitfield_ ^ ((1 << FILE_PIECES) - 1)); } -bool Peer::isInterestedByFree(Connection* remote_peer) +bool Peer::isInterestedByFree(const Connection* remote_peer) const { for (unsigned int i = 0; i < FILE_PIECES; i++) if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) @@ -369,10 +369,6 @@ void Peer::handleMessage() XBT_DEBUG(" \t for piece %d (%d,%d)", message->piece, message->block_index, message->block_index + message->block_length); xbt_assert(not remote_peer->choked_download); - xbt_assert(remote_peer->am_interested || ENABLE_END_GAME_MODE, - "Can't received a piece if I'm not interested without end-game mode!" - "piece (%d) bitfield (%u) remote bitfield (%u)", - message->piece, bitfield_, remote_peer->bitfield); xbt_assert(not remote_peer->choked_download, "Can't received a piece if I'm choked !"); xbt_assert((message->piece >= 0 && static_cast(message->piece) < FILE_PIECES), "Wrong piece received"); @@ -394,7 +390,6 @@ void Peer::handleMessage() } } else { XBT_DEBUG("However, we already have it"); - xbt_assert(ENABLE_END_GAME_MODE, "Should not happen because we don't use end game mode !"); requestNewPieceTo(remote_peer); } break; @@ -435,7 +430,7 @@ void Peer::removeCurrentPiece(Connection* remote_peer, unsigned int current_piec * @param remote_peer: information about the connection * @return the piece to download if possible. -1 otherwise */ -int Peer::selectPieceToDownload(Connection* remote_peer) +int Peer::selectPieceToDownload(const Connection* remote_peer) { int piece = partiallyDownloadedPiece(remote_peer); // strict priority policy @@ -444,20 +439,18 @@ int Peer::selectPieceToDownload(Connection* remote_peer) // end game mode if (countPieces(current_pieces) >= (FILE_PIECES - countPieces(bitfield_)) && isInterestedBy(remote_peer)) { - if (not ENABLE_END_GAME_MODE) - return -1; 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; @@ -473,14 +466,14 @@ int Peer::selectPieceToDownload(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; @@ -496,24 +489,24 @@ int Peer::selectPieceToDownload(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; @@ -560,7 +553,7 @@ void Peer::updateChokedPeers() do { // We choose a random peer to unchoke. std::unordered_map::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; @@ -614,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; } @@ -653,10 +646,10 @@ 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(Connection* remote_peer) +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; }