Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
19551d01a783b68453818a56a2c6027d479b3f33
[simgrid.git] / examples / s4u / app-bittorrent / s4u-bittorrent.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_BITTORRENT_HPP_
8 #define BITTORRENT_BITTORRENT_HPP_
9
10 #include <simgrid/s4u.hpp>
11 #include <xbt/RngStream.h>
12
13 constexpr char TRACKER_MAILBOX[] = "tracker_mailbox";
14 /** Max number of peers sent by the tracker to clients */
15 constexpr int MAXIMUM_PEERS = 50;
16 /** Interval of time where the peer should send a request to the tracker */
17 constexpr int TRACKER_QUERY_INTERVAL = 1000;
18 /** Communication size for a task to the tracker */
19 constexpr unsigned TRACKER_COMM_SIZE = 1;
20 constexpr double GET_PEERS_TIMEOUT   = 10000.0;
21 /** Number of peers that can be unchocked at a given time */
22 constexpr int MAX_UNCHOKED_PEERS = 4;
23 /** Interval between each update of the choked peers */
24 constexpr int UPDATE_CHOKED_INTERVAL = 30;
25
26 /** Message sizes
27  * Sizes based on report by A. Legout et al, Understanding BitTorrent: An Experimental Perspective
28  * http://hal.inria.fr/inria-00000156/en
29  */
30 constexpr unsigned MESSAGE_HANDSHAKE_SIZE     = 68;
31 constexpr unsigned MESSAGE_CHOKE_SIZE         = 5;
32 constexpr unsigned MESSAGE_UNCHOKE_SIZE       = 5;
33 constexpr unsigned MESSAGE_INTERESTED_SIZE    = 5;
34 constexpr unsigned MESSAGE_NOTINTERESTED_SIZE = 5;
35 constexpr unsigned MESSAGE_HAVE_SIZE          = 9;
36 constexpr unsigned MESSAGE_BITFIELD_SIZE      = 5;
37 constexpr unsigned MESSAGE_REQUEST_SIZE       = 17;
38 constexpr unsigned MESSAGE_PIECE_SIZE         = 13;
39 constexpr unsigned MESSAGE_CANCEL_SIZE        = 17;
40
41 /** Types of messages exchanged between two peers. */
42 enum e_message_type {
43   MESSAGE_HANDSHAKE,
44   MESSAGE_CHOKE,
45   MESSAGE_UNCHOKE,
46   MESSAGE_INTERESTED,
47   MESSAGE_NOTINTERESTED,
48   MESSAGE_HAVE,
49   MESSAGE_BITFIELD,
50   MESSAGE_REQUEST,
51   MESSAGE_PIECE,
52   MESSAGE_CANCEL
53 };
54
55 class Message {
56 public:
57   e_message_type type;
58   int peer_id;
59   simgrid::s4u::Mailbox* return_mailbox;
60   unsigned int bitfield = 0U;
61   int piece             = 0;
62   int block_index       = 0;
63   int block_length      = 0;
64   Message(e_message_type type, int peer_id, simgrid::s4u::Mailbox* return_mailbox)
65       : type(type), peer_id(peer_id), return_mailbox(return_mailbox){};
66   Message(e_message_type type, int peer_id, unsigned int bitfield, simgrid::s4u::Mailbox* return_mailbox)
67       : type(type), peer_id(peer_id), return_mailbox(return_mailbox), bitfield(bitfield){};
68   Message(e_message_type type, int peer_id, simgrid::s4u::Mailbox* return_mailbox, int piece, int block_index,
69           int block_length)
70       : type(type)
71       , peer_id(peer_id)
72       , return_mailbox(return_mailbox)
73       , piece(piece)
74       , block_index(block_index)
75       , block_length(block_length){};
76   Message(e_message_type type, int peer_id, simgrid::s4u::Mailbox* return_mailbox, int piece)
77       : type(type), peer_id(peer_id), return_mailbox(return_mailbox), piece(piece){};
78   ~Message() = default;
79 };
80
81 class HostBittorrent {
82   RngStream stream_;
83   simgrid::s4u::Host* host = nullptr;
84
85 public:
86   static simgrid::xbt::Extension<simgrid::s4u::Host, HostBittorrent> EXTENSION_ID;
87
88   explicit HostBittorrent(simgrid::s4u::Host* ptr) : host(ptr)
89   {
90     std::string descr = std::string("RngSream<") + host->get_cname() + ">";
91     stream_           = RngStream_CreateStream(descr.c_str());
92   }
93   HostBittorrent(const HostBittorrent&) = delete;
94   HostBittorrent& operator=(const HostBittorrent&) = delete;
95
96   ~HostBittorrent() { RngStream_DeleteStream(&stream_); };
97
98   RngStream getStream() { return stream_; };
99 };
100
101 #endif /* BITTORRENT_BITTORRENT_HPP_ */