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
Merge branch 'xbt_random' into 'master'
[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
ff9a094
..
a5e59bb
100644
(file)
--- a/
examples/s4u/app-bittorrent/s4u-peer.cpp
+++ b/
examples/s4u/app-bittorrent/s4u-peer.cpp
@@
-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)
@@
-44,8
+43,6
@@
Peer::Peer(std::vector<std::string> args)
}
xbt_assert(deadline > 0, "Wrong deadline supplied");
}
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;
if (args.size() == 4 && args[3] == "1") {
bitfield_ = (1U << FILE_PIECES) - 1U;
bitfield_blocks = (1ULL << (FILE_PIECES * PIECES_BLOCKS)) - 1ULL;
@@
-371,10
+368,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");
@@
-396,7
+389,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;
@@
-446,8
+438,6
@@
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++)
@@
-456,7
+446,7
@@
int Peer::selectPieceToDownload(Connection* remote_peer)
xbt_assert(nb_interesting_pieces != 0);
// get a random interesting piece
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 =
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)) {
int current_index = 0;
for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (hasNotPiece(i) && remote_peer->hasPiece(i)) {
@@
-479,7
+469,7
@@
int Peer::selectPieceToDownload(Connection* remote_peer)
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
- int random_piece_index =
RngStream_RandInt(stream,
0, nb_interesting_pieces - 1);
+ 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)) {
int current_index = 0;
for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) {
@@
-510,7
+500,10
@@
int Peer::selectPieceToDownload(Connection* remote_peer)
xbt_assert(nb_min_pieces != 0 || not isInterestedByFree(remote_peer));
// get a random rarest piece
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 = 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)) {
if (random_rarest_index == current_index) {
for (unsigned int i = 0; i < FILE_PIECES; i++)
if (pieces_count[i] == min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) {
if (random_rarest_index == current_index) {
@@
-559,7
+552,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::advance(chosen_peer_it,
RngStream_RandInt(stream,
0, connected_peers.size() - 1));
+ 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;
chosen_peer = &chosen_peer_it->second;
if (not chosen_peer->interested || not chosen_peer->choked_upload)
chosen_peer = nullptr;