-/* 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. */
/** 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)
try {
id = std::stoi(args[1]);
mailbox_ = simgrid::s4u::Mailbox::by_name(std::to_string(id));
- } catch (std::invalid_argument& ia) {
- throw std::invalid_argument(std::string("Invalid ID:") + args[1].c_str());
+ } catch (const std::invalid_argument&) {
+ throw std::invalid_argument("Invalid ID:" + args[1]);
}
+ random.set_seed(id);
try {
deadline = std::stod(args[2]);
- } catch (std::invalid_argument& ia) {
- throw std::invalid_argument(std::string("Invalid deadline:") + args[2].c_str());
+ } catch (const std::invalid_argument&) {
+ throw std::invalid_argument("Invalid deadline:" + args[2]);
}
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;
try {
XBT_DEBUG("Sending a peer request to the tracker.");
tracker_mailbox->put(peer_request, TRACKER_COMM_SIZE, GET_PEERS_TIMEOUT);
- } catch (simgrid::TimeoutError& e) {
+ } catch (const simgrid::TimeoutException&) {
XBT_DEBUG("Timeout expired when requesting peers to tracker");
delete peer_request;
return false;
if (id != peer_id)
connected_peers.emplace(peer_id, Connection(peer_id));
delete answer;
- } catch (simgrid::TimeoutError& e) {
+ } catch (const simgrid::TimeoutException&) {
XBT_DEBUG("Timeout expired when requesting peers to tracker");
return false;
}
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_
}
}
-std::string Peer::getStatus()
+std::string Peer::getStatus() const
{
- 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;
}
-bool Peer::hasFinished()
+bool Peer::hasFinished() const
{
return bitfield_ == (1U << FILE_PIECES) - 1U;
}
/** 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))
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;
return count;
}
-int Peer::nbInterestedPeers()
+int Peer::nbInterestedPeers() const
{
int nb = 0;
for (auto const& kv : connected_peers)
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<unsigned int>(message->piece) < FILE_PIECES),
"Wrong piece received");
}
} 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;
* @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
// 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 = RngStream_RandInt(stream, 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;
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 = RngStream_RandInt(stream, 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;
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 = RngStream_RandInt(stream, 0, nb_min_pieces - 1);
+ int random_rarest_index = 0;
+ if (nb_min_pieces > 0) {
+ 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;
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, 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;
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;
}
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)))
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)))
}
/** 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) const
{
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;
}