Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:Adrien.Gougeon/simgrid into master
[simgrid.git] / examples / s4u / app-bittorrent / s4u-peer.hpp
index 9a23ec5..4a3c84c 100644 (file)
@@ -31,6 +31,7 @@ public:
 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
@@ -44,15 +45,16 @@ class Peer {
 
   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;
   Peer& operator=(const Peer&) = delete;
   void operator()();
 
-  std::string getStatus();
-  bool hasFinished();
-  int nbInterestedPeers();
+  std::string getStatus() const;
+  bool hasFinished() const;
+  int nbInterestedPeers() const;
   bool isInterestedBy(const Connection* remote_peer) const;
   bool isInterestedByFree(const Connection* remote_peer) const;
   void updateActivePeersSet(Connection* remote_peer);
@@ -60,15 +62,19 @@ public:
   void updateChokedPeers();
 
   bool hasNotPiece(unsigned int piece) const { return not(bitfield_ & 1U << piece); }
-  bool hasCompletedPiece(unsigned int piece);
-  unsigned int countPieces(unsigned int bitfield);
+  bool remotePeerHasMissingPiece(const Connection* remote_peer, unsigned int piece) const
+  {
+    return hasNotPiece(piece) && remote_peer->hasPiece(piece);
+  }
+  bool hasCompletedPiece(unsigned int piece) const;
+  unsigned int countPieces(unsigned int bitfield) const;
   /** Check that a piece is not currently being download by the peer. */
   bool isNotDownloadingPiece(unsigned int piece) const { return not(current_pieces & 1U << piece); }
-  int partiallyDownloadedPiece(const Connection* remote_peer);
+  int partiallyDownloadedPiece(const Connection* remote_peer) const;
   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 getFirstMissingBlockFrom(int piece) const;
   int selectPieceToDownload(const Connection* remote_peer);
   void requestNewPieceTo(Connection* remote_peer);