Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
97630d9d86c2de5938a2979b96bed1afa629593e
[simgrid.git] / examples / s4u / app-bittorrent / s4u_peer.hpp
1 /* Copyright (c) 2012-2017. 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::MailboxPtr mailbox_;
17   unsigned int bitfield = 0U; // Fields
18   //  int messages_count;
19   double peer_speed    = 0;
20   double last_unchoke  = 0;
21   int current_piece    = -1;
22   bool am_interested   = false; // Indicates if we are interested in something the peer has
23   bool interested      = false; // Indicates if the peer is interested in one of our pieces
24   bool choked_upload   = true;  // Indicates if the peer is choked for the current peer
25   bool choked_download = true;  // Indicates if the peer has choked the current peer
26
27   Connection(int id) : id(id), mailbox_(simgrid::s4u::Mailbox::byName(std::to_string(id))){};
28   ~Connection() = default;
29   void addSpeedValue(double speed) { peer_speed = peer_speed * 0.6 + speed * 0.4; }
30   bool hasPiece(unsigned int piece) { return bitfield & 1U << piece; }
31 };
32
33 class Peer {
34   int id;
35   double deadline;
36   RngStream stream;
37   simgrid::s4u::MailboxPtr mailbox_;
38   std::set<Connection*> active_peers; // active peers list
39
40   unsigned int bitfield_             = 0;       // list of pieces the peer has.
41   unsigned long long bitfield_blocks = 0;       // list of blocks the peer has.
42   short* pieces_count                = nullptr; // number of peers that have each piece.
43   unsigned int current_pieces        = 0;       // current pieces the peer is downloading
44   double begin_receive_time = 0; // time when the receiving communication has begun, useful for calculating host speed.
45   int round_                = 0; // current round for the chocking algorithm.
46
47   std::unordered_map<int, Connection*> connected_peers;
48   simgrid::s4u::CommPtr comm_received = nullptr; // current comm
49   Message* message                    = nullptr; // current message being received
50 public:
51   explicit Peer(std::vector<std::string> args);
52   ~Peer();
53   void operator()();
54
55   std::string getStatus();
56   bool hasFinished();
57   int nbInterestedPeers();
58   bool isInterestedBy(Connection* remote_peer);
59   bool isInterestedByFree(Connection* remote_peer);
60   void updateActivePeersSet(Connection* remote_peer);
61   void updateInterestedAfterReceive();
62   void updateChokedPeers();
63
64   bool hasNotPiece(unsigned int piece) { return !(bitfield_ & 1U << piece); }
65   bool hasCompletedPiece(unsigned int piece);
66   unsigned int countPieces(unsigned int bitfield);
67   /** Check that a piece is not currently being download by the peer. */
68   bool isNotDownloadingPiece(unsigned int piece) { return !(current_pieces & 1U << piece); }
69   int partiallyDownloadedPiece(Connection* remote_peer);
70   void updatePiecesCountFromBitfield(unsigned int bitfield);
71   void removeCurrentPiece(Connection* remote_peer, unsigned int current_piece);
72   void updateBitfieldBlocks(int piece, int block_index, int block_length);
73   int getFirstMissingBlockFrom(int piece);
74   int selectPieceToDownload(Connection* remote_peer);
75   void requestNewPieceTo(Connection* remote_peer);
76
77   bool getPeersFromTracker();
78   void sendHandshake(simgrid::s4u::MailboxPtr mailbox);
79   void sendBitfield(simgrid::s4u::MailboxPtr mailbox);
80   void sendPiece(simgrid::s4u::MailboxPtr mailbox, unsigned int piece, int block_index, int block_length);
81   void sendInterested(simgrid::s4u::MailboxPtr mailbox);
82   void sendChoked(simgrid::s4u::MailboxPtr mailbox);
83   void sendUnchoked(simgrid::s4u::MailboxPtr mailbox);
84   void sendNotInterested(simgrid::s4u::MailboxPtr mailbox);
85   void sendHandshakeToAllPeers();
86   void sendRequest(simgrid::s4u::MailboxPtr mailbox, unsigned int piece, int block_index, int block_length);
87   void sendHaveToAllPeers(unsigned int piece);
88   void sendRequestTo(Connection* remote_peer, unsigned int piece);
89
90   void handleMessage();
91   void leech();
92   void seed();
93 };
94
95 #endif /* BITTORRENT_PEER_HPP */