A
lgorithmique
N
umérique
D
istribuée
Public GIT Repository
projects
/
simgrid.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
[sonar] Explicit cast when loosing precision (C++ examples).
[simgrid.git]
/
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 360bb203acdd70a687cb3b84e0d93ec70b5c3ac5..d653c2a0ee5e4c6aaa8fc58cd773cbcf5c8b643e 100644
(file)
--- a/
examples/s4u/app-bittorrent/s4u-peer.cpp
+++ b/
examples/s4u/app-bittorrent/s4u-peer.cpp
@@
-1,4
+1,4
@@
-/* Copyright (c) 2012-20
19
. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2012-20
20
. 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. */
/* 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;
/** 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)
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<std::string> args)
} catch (const std::invalid_argument&) {
throw std::invalid_argument("Invalid ID:" + args[1]);
}
} catch (const std::invalid_argument&) {
throw std::invalid_argument("Invalid ID:" + args[1]);
}
+ random.set_seed(id);
try {
deadline = std::stod(args[2]);
try {
deadline = std::stod(args[2]);
@@
-150,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) {
xbt_assert(remote_peer->hasPiece(piece));
int block_index = getFirstMissingBlockFrom(piece);
if (block_index != -1) {
- int block_length = st
d::min(BLOCKS_REQUESTED, PIECES_BLOCKS - block_index
);
+ int block_length = st
atic_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_
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_
@@
-159,26
+159,26
@@
void Peer::sendRequestTo(Connection* remote_peer, unsigned int piece)
}
}
}
}
-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;
}
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 */
{
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));
}
{
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))
{
for (unsigned int i = 0; i < FILE_PIECES; i++)
if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i))
@@
-193,7
+193,7
@@
void Peer::updatePiecesCountFromBitfield(unsigned int bitfield)
pieces_count[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;
{
unsigned int count = 0U;
unsigned int n = bitfield;
@@
-204,7
+204,7
@@
unsigned int Peer::countPieces(unsigned int bitfield)
return count;
}
return count;
}
-int Peer::nbInterestedPeers()
+int Peer::nbInterestedPeers()
const
{
int nb = 0;
for (auto const& kv : connected_peers)
{
int nb = 0;
for (auto const& kv : connected_peers)
@@
-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_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");
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");
@@
-394,7
+390,6
@@
void Peer::handleMessage()
}
} else {
XBT_DEBUG("However, we already have it");
}
} 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;
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
*/
* @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
{
int piece = partiallyDownloadedPiece(remote_peer);
// strict priority policy
@@
-444,21
+439,18
@@
int Peer::selectPieceToDownload(Connection* remote_peer)
// end game mode
if (countPieces(current_pieces) >= (FILE_PIECES - countPieces(bitfield_)) && isInterestedBy(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++)
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
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 = random.uniform_int(0, nb_interesting_pieces - 1);
int current_index = 0;
for (unsigned int i = 0; i < FILE_PIECES; i++) {
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;
if (random_piece_index == current_index) {
piece = i;
break;
@@
-474,15
+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++)
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
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 = random.uniform_int(0, nb_interesting_pieces - 1);
int current_index = 0;
for (unsigned int i = 0; i < FILE_PIECES; i++) {
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;
if (random_piece_index == current_index) {
piece = i;
break;
@@
-498,25
+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++) {
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++)
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) {
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) {
- std::uniform_int_distribution<int> dist(0, nb_min_pieces - 1);
- random_rarest_index = dist(generator);
+ random_rarest_index = random.uniform_int(0, nb_min_pieces - 1);
}
for (unsigned int i = 0; i < FILE_PIECES; i++)
}
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;
if (random_rarest_index == current_index) {
piece = i;
break;
@@
-563,8
+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();
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, 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;
chosen_peer = &chosen_peer_it->second;
if (not chosen_peer->interested || not chosen_peer->choked_upload)
chosen_peer = nullptr;
@@
-618,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++)
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;
}
interested = true;
break;
}
@@
-640,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));
}
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)))
{
for (unsigned int i = 0; i < PIECES_BLOCKS; i++)
if (not(bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i)))
@@
-648,7
+637,7
@@
bool Peer::hasCompletedPiece(unsigned int piece)
return true;
}
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)))
{
for (unsigned int i = 0; i < PIECES_BLOCKS; i++)
if (not(bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i)))
@@
-657,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. */
}
/** 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++)
{
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;
}
return i;
return -1;
}