1 /* Copyright (c) 2012-2019. 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 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;
27 * Sizes based on report by A. Legout et al, Understanding BitTorrent: An Experimental Perspective
28 * http://hal.inria.fr/inria-00000156/en
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;
41 /** Types of messages exchanged between two peers. */
47 MESSAGE_NOTINTERESTED,
59 simgrid::s4u::MailboxPtr return_mailbox;
60 unsigned int bitfield = 0U;
64 Message(e_message_type type, int peer_id, simgrid::s4u::MailboxPtr 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::MailboxPtr 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::MailboxPtr return_mailbox, int piece, int block_index,
72 , return_mailbox(return_mailbox)
74 , block_index(block_index)
75 , block_length(block_length){};
76 Message(e_message_type type, int peer_id, simgrid::s4u::MailboxPtr return_mailbox, int piece)
77 : type(type), peer_id(peer_id), return_mailbox(return_mailbox), piece(piece){};
81 class HostBittorrent {
83 simgrid::s4u::Host* host = nullptr;
86 static simgrid::xbt::Extension<simgrid::s4u::Host, HostBittorrent> EXTENSION_ID;
88 explicit HostBittorrent(simgrid::s4u::Host* ptr) : host(ptr)
90 std::string descr = std::string("RngSream<") + host->get_cname() + ">";
91 stream_ = RngStream_CreateStream(descr.c_str());
93 HostBittorrent(const HostBittorrent&) = delete;
94 HostBittorrent& operator=(const HostBittorrent&) = delete;
96 ~HostBittorrent() { RngStream_DeleteStream(&stream_); };
98 RngStream getStream() { return stream_; };
101 #endif /* BITTORRENT_BITTORRENT_HPP_ */