Logo AND Algorithmique Numérique Distribuée

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