-/* 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. */
}
/** 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))
* @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
xbt_assert(nb_interesting_pieces != 0);
// get a random interesting piece
- std::uniform_int_distribution<int> dist(0, nb_interesting_pieces - 1);
- int random_piece_index = dist(generator);
+ 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)) {
nb_interesting_pieces++;
xbt_assert(nb_interesting_pieces != 0);
// get a random interesting piece
- std::uniform_int_distribution<int> dist(0, nb_interesting_pieces - 1);
- int random_piece_index = dist(generator);
+ 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)) {
// get a random rarest piece
int random_rarest_index = 0;
if (nb_min_pieces > 0) {
- std::uniform_int_distribution<int> dist(0, nb_min_pieces - 1);
- random_rarest_index = dist(generator);
+ 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)) {
do {
// We choose a random peer to unchoke.
std::unordered_map<int, Connection>::iterator chosen_peer_it = connected_peers.begin();
- std::uniform_int_distribution<int> dist(0, connected_peers.size() - 1);
- std::advance(chosen_peer_it, dist(generator));
+ 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;
}
/** 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)