1 /* Copyright (c) 2012-2018. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef BITTORRENT_BITTORRENT_HPP_
8 #define BITTORRENT_BITTORRENT_HPP_
10 #include <simgrid/s4u.hpp>
11 #include <xbt/RngStream.h>
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 */
32 * Sizes based on report by A. Legout et al, Understanding BitTorrent: An Experimental Perspective
33 * http://hal.inria.fr/inria-00000156/en
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
46 /** Types of messages exchanged between two peers. */
52 MESSAGE_NOTINTERESTED,
64 simgrid::s4u::MailboxPtr return_mailbox;
65 unsigned int bitfield = 0U;
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,
77 , return_mailbox(return_mailbox)
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){};
86 class HostBittorrent {
88 simgrid::s4u::Host* host = nullptr;
91 static simgrid::xbt::Extension<simgrid::s4u::Host, HostBittorrent> EXTENSION_ID;
93 explicit HostBittorrent(simgrid::s4u::Host* ptr) : host(ptr)
95 std::string descr = std::string("RngSream<") + host->get_cname() + ">";
96 stream_ = RngStream_CreateStream(descr.c_str());
99 ~HostBittorrent() { RngStream_DeleteStream(&stream_); };
101 RngStream getStream() { return stream_; };
104 #endif /* BITTORRENT_BITTORRENT_HPP_ */