-/* Copyright (c) 2012-2018. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2012-2019. 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. */
* User parameters for transferred file data. For the test, the default values are :
* File size: 10 pieces * 5 blocks/piece * 16384 bytes/block = 819200 bytes
*/
-#define FILE_PIECES 10UL
-#define PIECES_BLOCKS 5UL
-#define BLOCK_SIZE 16384
+constexpr unsigned long FILE_PIECES = 10UL;
+constexpr unsigned long PIECES_BLOCKS = 5UL;
+constexpr int BLOCK_SIZE = 16384;
/** Number of blocks asked by each request */
-#define BLOCKS_REQUESTED 2UL
+constexpr unsigned long BLOCKS_REQUESTED = 2UL;
-#define ENABLE_END_GAME_MODE 1
-#define SLEEP_DURATION 1
+constexpr bool ENABLE_END_GAME_MODE = true;
+constexpr double SLEEP_DURATION = 1.0;
#define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0)
Peer::Peer(std::vector<std::string> args)
bool Peer::getPeersFromTracker()
{
- simgrid::s4u::MailboxPtr tracker_mailbox = simgrid::s4u::Mailbox::by_name(TRACKER_MAILBOX);
+ simgrid::s4u::Mailbox* tracker_mailbox = simgrid::s4u::Mailbox::by_name(TRACKER_MAILBOX);
// Build the task to send to the tracker
TrackerQuery* peer_request = new TrackerQuery(id, mailbox_);
try {
XBT_DEBUG("Sending a peer request to the tracker.");
tracker_mailbox->put(peer_request, TRACKER_COMM_SIZE, GET_PEERS_TIMEOUT);
- } catch (xbt_ex& e) {
- if (e.category == timeout_error) {
- XBT_DEBUG("Timeout expired when requesting peers to tracker");
- delete peer_request;
- return false;
- }
+ } catch (simgrid::TimeoutError& e) {
+ XBT_DEBUG("Timeout expired when requesting peers to tracker");
+ delete peer_request;
+ return false;
}
try {
if (id != peer_id)
connected_peers[peer_id] = new Connection(peer_id);
delete answer;
- } catch (xbt_ex& e) {
- if (e.category == timeout_error) {
- XBT_DEBUG("Timeout expired when requesting peers to tracker");
- return false;
- }
+ } catch (simgrid::TimeoutError& e) {
+ XBT_DEBUG("Timeout expired when requesting peers to tracker");
+ return false;
}
return true;
}
}
}
-void Peer::sendMessage(simgrid::s4u::MailboxPtr mailbox, e_message_type type, uint64_t size)
+void Peer::sendMessage(simgrid::s4u::Mailbox* mailbox, e_message_type type, uint64_t size)
{
const char* type_names[6] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", "CANCEL"};
XBT_DEBUG("Sending %s to %s", type_names[type], mailbox->get_cname());
mailbox->put_init(new Message(type, id, bitfield_, mailbox_), size)->detach();
}
-void Peer::sendBitfield(simgrid::s4u::MailboxPtr mailbox)
+void Peer::sendBitfield(simgrid::s4u::Mailbox* mailbox)
{
XBT_DEBUG("Sending a BITFIELD to %s", mailbox->get_cname());
mailbox
->detach();
}
-void Peer::sendPiece(simgrid::s4u::MailboxPtr mailbox, unsigned int piece, int block_index, int block_length)
+void Peer::sendPiece(simgrid::s4u::Mailbox* mailbox, unsigned int piece, int block_index, int block_length)
{
xbt_assert(not hasNotPiece(piece), "Tried to send a unavailable piece.");
XBT_DEBUG("Sending the PIECE %u (%d,%d) to %s", piece, block_index, block_length, mailbox->get_cname());
// end game mode
if (countPieces(current_pieces) >= (FILE_PIECES - countPieces(bitfield_)) && isInterestedBy(remote_peer)) {
-#if ENABLE_END_GAME_MODE == 0
- return -1;
-#endif
+ 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++)