Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mv examples/s4u examples/cpp
[simgrid.git] / examples / cpp / app-bittorrent / s4u-bittorrent.hpp
1 /* Copyright (c) 2012-2021. 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/random.hpp>
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 /** Types of messages exchanged between two peers. */
27 enum class MessageType { HANDSHAKE, CHOKE, UNCHOKE, INTERESTED, NOTINTERESTED, HAVE, BITFIELD, REQUEST, PIECE, CANCEL };
28
29 class Message {
30 public:
31   MessageType type;
32   int peer_id;
33   simgrid::s4u::Mailbox* return_mailbox;
34   unsigned int bitfield = 0U;
35   int piece             = 0;
36   int block_index       = 0;
37   int block_length      = 0;
38   Message(MessageType type, int peer_id, simgrid::s4u::Mailbox* return_mailbox)
39       : type(type), peer_id(peer_id), return_mailbox(return_mailbox){};
40   Message(MessageType type, int peer_id, unsigned int bitfield, simgrid::s4u::Mailbox* return_mailbox)
41       : type(type), peer_id(peer_id), return_mailbox(return_mailbox), bitfield(bitfield){};
42   Message(MessageType type, int peer_id, simgrid::s4u::Mailbox* return_mailbox, int piece, int block_index,
43           int block_length)
44       : type(type)
45       , peer_id(peer_id)
46       , return_mailbox(return_mailbox)
47       , piece(piece)
48       , block_index(block_index)
49       , block_length(block_length){};
50   Message(MessageType type, int peer_id, simgrid::s4u::Mailbox* return_mailbox, int piece)
51       : type(type), peer_id(peer_id), return_mailbox(return_mailbox), piece(piece){};
52 };
53
54 #endif /* BITTORRENT_BITTORRENT_HPP_ */