} catch (const std::invalid_argument&) {
throw std::invalid_argument("Invalid ID:" + args[1]);
}
+ random.set_seed(id);
try {
deadline = std::stod(args[2]);
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;
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;
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;
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;
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;
}
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;
}