X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/82230d737404aa70c3c2b5824d97109f1f129d58..26f77b7dd18750482fccb9c88c437ccccfa91d66:/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 8f9bc799cc..0a1ac9f587 100644 --- a/examples/s4u/app-bittorrent/s4u-peer.cpp +++ b/examples/s4u/app-bittorrent/s4u-peer.cpp @@ -1,11 +1,10 @@ -/* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2012-2018. 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. */ #include #include -#include #include "s4u-peer.hpp" #include "s4u-tracker.hpp" @@ -33,7 +32,7 @@ Peer::Peer(std::vector args) xbt_assert(args.size() == 3 || args.size() == 4, "Wrong number of arguments"); try { id = std::stoi(args[1]); - mailbox_ = simgrid::s4u::Mailbox::byName(std::to_string(id)); + mailbox_ = simgrid::s4u::Mailbox::by_name(std::to_string(id)); } catch (std::invalid_argument& ia) { throw std::invalid_argument(std::string("Invalid ID:") + args[1].c_str()); } @@ -45,7 +44,7 @@ Peer::Peer(std::vector args) } xbt_assert(deadline > 0, "Wrong deadline supplied"); - stream = simgrid::s4u::this_actor::getHost()->extension()->getStream(); + stream = simgrid::s4u::this_actor::get_host()->extension()->getStream(); if (args.size() == 4 && args[3] == "1") { bitfield_ = (1U << FILE_PIECES) - 1U; @@ -69,8 +68,8 @@ void Peer::operator()() // Getting peer data from the tracker. if (getPeersFromTracker()) { XBT_DEBUG("Got %zu peers from the tracker. Current status is: %s", connected_peers.size(), getStatus().c_str()); - begin_receive_time = simgrid::s4u::Engine::getClock(); - mailbox_->setReceiver(simgrid::s4u::Actor::self()); + begin_receive_time = simgrid::s4u::Engine::get_clock(); + mailbox_->set_receiver(simgrid::s4u::Actor::self()); if (hasFinished()) { sendHandshakeToAllPeers(); } else { @@ -86,18 +85,16 @@ void Peer::operator()() bool Peer::getPeersFromTracker() { - simgrid::s4u::MailboxPtr tracker_mailbox = simgrid::s4u::Mailbox::byName(TRACKER_MAILBOX); + simgrid::s4u::MailboxPtr 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 { @@ -107,11 +104,9 @@ bool Peer::getPeersFromTracker() 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; } @@ -129,13 +124,13 @@ void Peer::sendHandshakeToAllPeers() void Peer::sendMessage(simgrid::s4u::MailboxPtr 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->getCname()); + 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) { - XBT_DEBUG("Sending a BITFIELD to %s", mailbox->getCname()); + XBT_DEBUG("Sending a BITFIELD to %s", mailbox->get_cname()); mailbox ->put_init(new Message(MESSAGE_BITFIELD, id, bitfield_, mailbox_), MESSAGE_BITFIELD_SIZE + BITS_TO_BYTES(FILE_PIECES)) @@ -145,7 +140,7 @@ void Peer::sendBitfield(simgrid::s4u::MailboxPtr mailbox) void Peer::sendPiece(simgrid::s4u::MailboxPtr 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->getCname()); + XBT_DEBUG("Sending the PIECE %u (%d,%d) to %s", piece, block_index, block_length, mailbox->get_cname()); mailbox->put_init(new Message(MESSAGE_PIECE, id, mailbox_, piece, block_index, block_length), BLOCK_SIZE)->detach(); } @@ -165,7 +160,7 @@ void Peer::sendRequestTo(Connection* remote_peer, unsigned int piece) int block_index = getFirstMissingBlockFrom(piece); if (block_index != -1) { int block_length = std::min(BLOCKS_REQUESTED, PIECES_BLOCKS - block_index); - XBT_DEBUG("Sending a REQUEST to %s for piece %u (%d,%d)", remote_peer->mailbox_->getCname(), piece, 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_ ->put_init(new Message(MESSAGE_REQUEST, id, mailbox_, piece, block_index, block_length), MESSAGE_REQUEST_SIZE) @@ -229,15 +224,15 @@ int Peer::nbInterestedPeers() void Peer::leech() { - double next_choked_update = simgrid::s4u::Engine::getClock() + UPDATE_CHOKED_INTERVAL; + double next_choked_update = simgrid::s4u::Engine::get_clock() + UPDATE_CHOKED_INTERVAL; XBT_DEBUG("Start downloading."); /* Send a "handshake" message to all the peers it got (since it couldn't have gotten more than 50 peers) */ sendHandshakeToAllPeers(); - XBT_DEBUG("Starting main leech loop listening on mailbox: %s", mailbox_->getCname()); + XBT_DEBUG("Starting main leech loop listening on mailbox: %s", mailbox_->get_cname()); void* data = nullptr; - while (simgrid::s4u::Engine::getClock() < deadline && countPieces(bitfield_) < FILE_PIECES) { + while (simgrid::s4u::Engine::get_clock() < deadline && countPieces(bitfield_) < FILE_PIECES) { if (comm_received == nullptr) { comm_received = mailbox_->get_async(&data); } @@ -248,7 +243,7 @@ void Peer::leech() comm_received = nullptr; } else { // We don't execute the choke algorithm if we don't already have a piece - if (simgrid::s4u::Engine::getClock() >= next_choked_update && countPieces(bitfield_) > 0) { + if (simgrid::s4u::Engine::get_clock() >= next_choked_update && countPieces(bitfield_) > 0) { updateChokedPeers(); next_choked_update += UPDATE_CHOKED_INTERVAL; } else { @@ -262,11 +257,11 @@ void Peer::leech() void Peer::seed() { - double next_choked_update = simgrid::s4u::Engine::getClock() + UPDATE_CHOKED_INTERVAL; + double next_choked_update = simgrid::s4u::Engine::get_clock() + UPDATE_CHOKED_INTERVAL; XBT_DEBUG("Start seeding."); // start the main seed loop void* data = nullptr; - while (simgrid::s4u::Engine::getClock() < deadline) { + while (simgrid::s4u::Engine::get_clock() < deadline) { if (comm_received == nullptr) { comm_received = mailbox_->get_async(&data); } @@ -276,7 +271,7 @@ void Peer::seed() delete message; comm_received = nullptr; } else { - if (simgrid::s4u::Engine::getClock() >= next_choked_update) { + if (simgrid::s4u::Engine::get_clock() >= next_choked_update) { updateChokedPeers(); // TODO: Change the choked peer algorithm when seeding. next_choked_update += UPDATE_CHOKED_INTERVAL; @@ -300,7 +295,7 @@ void Peer::handleMessage() const char* type_names[10] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", "HAVE", "BITFIELD", "REQUEST", "PIECE", "CANCEL"}; - XBT_DEBUG("Received a %s message from %s", type_names[message->type], message->return_mailbox->getCname()); + XBT_DEBUG("Received a %s message from %s", type_names[message->type], message->return_mailbox->get_cname()); auto known_peer = connected_peers.find(message->peer_id); Connection* remote_peer = (known_peer == connected_peers.end()) ? nullptr : known_peer->second; @@ -419,9 +414,9 @@ void Peer::handleMessage() } // Update the peer speed. if (remote_peer) { - remote_peer->addSpeedValue(1.0 / (simgrid::s4u::Engine::getClock() - begin_receive_time)); + remote_peer->addSpeedValue(1.0 / (simgrid::s4u::Engine::get_clock() - begin_receive_time)); } - begin_receive_time = simgrid::s4u::Engine::getClock(); + begin_receive_time = simgrid::s4u::Engine::get_clock(); } /** Selects the appropriate piece to download and requests it to the remote_peer */ @@ -558,7 +553,7 @@ void Peer::updateChokedPeers() /**If we are currently seeding, we unchoke the peer which has been unchoked the last time.*/ if (hasFinished()) { Connection* remote_peer; - double unchoke_time = simgrid::s4u::Engine::getClock() + 1; + double unchoke_time = simgrid::s4u::Engine::get_clock() + 1; for (auto const& kv : connected_peers) { remote_peer = kv.second; if (remote_peer->last_unchoke < unchoke_time && remote_peer->interested && remote_peer->choked_upload) { @@ -612,7 +607,7 @@ void Peer::updateChokedPeers() xbt_assert((chosen_peer->choked_upload), "Tries to unchoked an unchoked peer"); chosen_peer->choked_upload = false; active_peers.insert(chosen_peer); - chosen_peer->last_unchoke = simgrid::s4u::Engine::getClock(); + chosen_peer->last_unchoke = simgrid::s4u::Engine::get_clock(); XBT_DEBUG("(%d) Sending a UNCHOKE to %d", id, chosen_peer->id); updateActivePeersSet(chosen_peer); sendMessage(chosen_peer->mailbox_, MESSAGE_UNCHOKE, MESSAGE_UNCHOKE_SIZE);