-/* Copyright (c) 2012-2022. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2012-2023. 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. */
/* REQUEST */ 17,
/* PIECE */ 13,
/* CANCEL */ 17}};
- return sizes[static_cast<int>(type)];
+ return sizes.at(static_cast<int>(type));
}
constexpr const char* message_name(MessageType type)
{
constexpr std::array<const char*, 10> names{{"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", "HAVE",
"BITFIELD", "REQUEST", "PIECE", "CANCEL"}};
- return names[static_cast<int>(type)];
+ return names.at(static_cast<int>(type));
}
Peer::Peer(std::vector<std::string> args)
// Add the peers the tracker gave us to our peer list.
for (auto const& peer_id : answer->getPeers())
if (id != peer_id)
- connected_peers.try_emplace(peer_id, Connection(peer_id));
+ connected_peers.try_emplace(peer_id, peer_id);
} catch (const simgrid::TimeoutException&) {
XBT_DEBUG("Timeout expired when requesting peers to tracker");
return false;
void Peer::sendHandshakeToAllPeers()
{
- for (auto const& kv : connected_peers) {
- const Connection& remote_peer = kv.second;
+ for (auto const& [_, remote_peer] : connected_peers) {
auto* handshake = new Message(MessageType::HANDSHAKE, id, mailbox_);
remote_peer.mailbox_->put_init(handshake, message_size(MessageType::HANDSHAKE))->detach();
XBT_DEBUG("Sending a HANDSHAKE to %d", remote_peer.id);
void Peer::sendHaveToAllPeers(unsigned int piece)
{
XBT_DEBUG("Sending HAVE message to all my peers");
- for (auto const& kv : connected_peers) {
- const Connection& remote_peer = kv.second;
+ for (auto const& [_, remote_peer] : connected_peers) {
remote_peer.mailbox_->put_init(new Message(MessageType::HAVE, id, mailbox_, piece), message_size(MessageType::HAVE))
->detach();
}
// Check if the peer is in our connection list.
if (remote_peer == nullptr) {
XBT_DEBUG("This peer %d was unknown, answer to its handshake", message->peer_id);
- connected_peers.try_emplace(message->peer_id, Connection(message->peer_id));
+ connected_peers.try_emplace(message->peer_id, message->peer_id);
sendMessage(message->return_mailbox, MessageType::HANDSHAKE, message_size(MessageType::HANDSHAKE));
}
// Send our bitfield to the peer
/**If we are currently seeding, we unchoke the peer which has been unchoked the last time.*/
if (hasFinished()) {
double unchoke_time = sg4::Engine::get_clock() + 1;
- for (auto& kv : connected_peers) {
- Connection& remote_peer = kv.second;
+ for (auto& [_, remote_peer] : connected_peers) {
if (remote_peer.last_unchoke < unchoke_time && remote_peer.interested && remote_peer.choked_upload) {
unchoke_time = remote_peer.last_unchoke;
chosen_peer = &remote_peer;
} else {
// Use the "fastest download" policy.
double fastest_speed = 0.0;
- for (auto& kv : connected_peers) {
- Connection& remote_peer = kv.second;
+ for (auto& [_, remote_peer] : connected_peers) {
if (remote_peer.peer_speed > fastest_speed && remote_peer.choked_upload && remote_peer.interested) {
fastest_speed = remote_peer.peer_speed;
chosen_peer = &remote_peer;
/** @brief Update "interested" state of peers: send "not interested" to peers that don't have any more pieces we want.*/
void Peer::updateInterestedAfterReceive()
{
- for (auto& kv : connected_peers) {
- Connection& remote_peer = kv.second;
+ for (auto& [_, remote_peer] : connected_peers) {
if (remote_peer.am_interested) {
bool interested = false;
// Check if the peer still has a piece we want.