-/* Copyright (c) 2012-2019. The SimGrid Team.
+/* Copyright (c) 2012-2020. The SimGrid Team.
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
explicit Connection(int id) : id(id), mailbox_(simgrid::s4u::Mailbox::by_name(std::to_string(id))){};
void addSpeedValue(double speed) { peer_speed = peer_speed * 0.6 + speed * 0.4; }
- bool hasPiece(unsigned int piece) { return bitfield & 1U << piece; }
+ bool hasPiece(unsigned int piece) const { return bitfield & 1U << piece; }
};
class Peer {
int id;
double deadline;
+ simgrid::xbt::random::XbtRandom random;
simgrid::s4u::Mailbox* mailbox_;
std::unordered_map<int, Connection> connected_peers;
std::set<Connection*> active_peers; // active peers list
simgrid::s4u::CommPtr comm_received = nullptr; // current comm
Message* message = nullptr; // current message being received
+
public:
explicit Peer(std::vector<std::string> args);
Peer(const Peer&) = delete;
std::string getStatus();
bool hasFinished();
int nbInterestedPeers();
- bool isInterestedBy(Connection* remote_peer);
- bool isInterestedByFree(Connection* remote_peer);
+ bool isInterestedBy(const Connection* remote_peer) const;
+ bool isInterestedByFree(const Connection* remote_peer) const;
void updateActivePeersSet(Connection* remote_peer);
void updateInterestedAfterReceive();
void updateChokedPeers();
- bool hasNotPiece(unsigned int piece) { return not(bitfield_ & 1U << piece); }
+ bool hasNotPiece(unsigned int piece) const { return not(bitfield_ & 1U << piece); }
+ bool remotePeerHasMissingPiece(const Connection* remote_peer, unsigned int piece)
+ {
+ return hasNotPiece(piece) && remote_peer->hasPiece(piece);
+ }
bool hasCompletedPiece(unsigned int piece);
unsigned int countPieces(unsigned int bitfield);
/** Check that a piece is not currently being download by the peer. */
- bool isNotDownloadingPiece(unsigned int piece) { return not(current_pieces & 1U << piece); }
- int partiallyDownloadedPiece(Connection* remote_peer);
+ bool isNotDownloadingPiece(unsigned int piece) const { return not(current_pieces & 1U << piece); }
+ int partiallyDownloadedPiece(const Connection* remote_peer);
void updatePiecesCountFromBitfield(unsigned int bitfield);
void removeCurrentPiece(Connection* remote_peer, unsigned int current_piece);
void updateBitfieldBlocks(int piece, int block_index, int block_length);
int getFirstMissingBlockFrom(int piece);
- int selectPieceToDownload(Connection* remote_peer);
+ int selectPieceToDownload(const Connection* remote_peer);
void requestNewPieceTo(Connection* remote_peer);
bool getPeersFromTracker();