Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into CRTP
[simgrid.git] / examples / s4u / app-bittorrent / s4u-peer.hpp
1 /* Copyright (c) 2012-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef BITTORRENT_PEER_HPP
8 #define BITTORRENT_PEER_HPP
9 #include "s4u-bittorrent.hpp"
10 #include <set>
11 #include <unordered_map>
12
13 class Connection {
14 public:
15   int id; // Peer id
16   simgrid::s4u::Mailbox* mailbox_;
17   unsigned int bitfield = 0U; // Fields
18   double peer_speed    = 0;
19   double last_unchoke  = 0;
20   int current_piece    = -1;
21   bool am_interested   = false; // Indicates if we are interested in something the peer has
22   bool interested      = false; // Indicates if the peer is interested in one of our pieces
23   bool choked_upload   = true;  // Indicates if the peer is choked for the current peer
24   bool choked_download = true;  // Indicates if the peer has choked the current peer
25
26   explicit Connection(int id) : id(id), mailbox_(simgrid::s4u::Mailbox::by_name(std::to_string(id))){};
27   void addSpeedValue(double speed) { peer_speed = peer_speed * 0.6 + speed * 0.4; }
28   bool hasPiece(unsigned int piece) { return bitfield & 1U << piece; }
29 };
30
31 class Peer {
32   int id;
33   double deadline;
34   RngStream stream;
35   simgrid::s4u::Mailbox* mailbox_;
36   std::unordered_map<int, Connection> connected_peers;
37   std::set<Connection*> active_peers; // active peers list
38
39   unsigned int bitfield_             = 0;       // list of pieces the peer has.
40   unsigned long long bitfield_blocks = 0;       // list of blocks the peer has.
41   std::vector<short> pieces_count;              // number of peers that have each piece.
42   unsigned int current_pieces        = 0;       // current pieces the peer is downloading
43   double begin_receive_time = 0; // time when the receiving communication has begun, useful for calculating host speed.
44   int round_                = 0; // current round for the chocking algorithm.
45
46   simgrid::s4u::CommPtr comm_received = nullptr; // current comm
47   Message* message                    = nullptr; // current message being received
48 public:
49   explicit Peer(std::vector<std::string> args);
50   Peer(const Peer&) = delete;
51   Peer& operator=(const Peer&) = delete;
52   void operator()();
53
54   std::string getStatus();
55   bool hasFinished();
56   int nbInterestedPeers();
57   bool isInterestedBy(Connection* remote_peer);
58   bool isInterestedByFree(Connection* remote_peer);
59   void updateActivePeersSet(Connection* remote_peer);
60   void updateInterestedAfterReceive();
61   void updateChokedPeers();
62
63   bool hasNotPiece(unsigned int piece) { return not(bitfield_ & 1U << piece); }
64   bool hasCompletedPiece(unsigned int piece);
65   unsigned int countPieces(unsigned int bitfield);
66   /** Check that a piece is not currently being download by the peer. */
67   bool isNotDownloadingPiece(unsigned int piece) { return not(current_pieces & 1U << piece); }
68   int partiallyDownloadedPiece(Connection* remote_peer);
69   void updatePiecesCountFromBitfield(unsigned int bitfield);
70   void removeCurrentPiece(Connection* remote_peer, unsigned int current_piece);
71   void updateBitfieldBlocks(int piece, int block_index, int block_length);
72   int getFirstMissingBlockFrom(int piece);
73   int selectPieceToDownload(Connection* remote_peer);
74   void requestNewPieceTo(Connection* remote_peer);
75
76   bool getPeersFromTracker();
77   void sendMessage(simgrid::s4u::Mailbox* mailbox, e_message_type type, uint64_t size);
78   void sendBitfield(simgrid::s4u::Mailbox* mailbox);
79   void sendPiece(simgrid::s4u::Mailbox* mailbox, unsigned int piece, int block_index, int block_length);
80   void sendHandshakeToAllPeers();
81   void sendHaveToAllPeers(unsigned int piece);
82   void sendRequestTo(Connection* remote_peer, unsigned int piece);
83
84   void handleMessage();
85   void leech();
86   void seed();
87 };
88
89 #endif /* BITTORRENT_PEER_HPP */