Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ee81f4f23b1a1ce30ede6d2cca9143568f9cb24f
[simgrid.git] / examples / c / app-bittorrent / app-bittorrent.h
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_H_
8 #define BITTORRENT_BITTORRENT_H_
9 #include <simgrid/actor.h>
10 #include <simgrid/engine.h>
11 #include <simgrid/host.h>
12 #include <simgrid/mailbox.h>
13 #include <xbt/log.h>
14 #include <xbt/str.h>
15 #include <xbt/sysdep.h>
16
17 #define MAILBOX_SIZE 40
18 #define TRACKER_MAILBOX "tracker_mailbox"
19 /** Max number of pairs sent by the tracker to clients */
20 #define MAXIMUM_PEERS 50
21 /** Interval of time where the peer should send a request to the tracker */
22 #define TRACKER_QUERY_INTERVAL 1000
23 /** Communication size for a task to the tracker */
24 #define TRACKER_COMM_SIZE 1
25 #define GET_PEERS_TIMEOUT 10000
26 /** Number of peers that can be unchocked at a given time */
27 #define MAX_UNCHOKED_PEERS 4
28 /** Interval between each update of the choked peers */
29 #define UPDATE_CHOKED_INTERVAL 30
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 /** Message data */
61 typedef struct s_message {
62   e_message_type type;
63   int peer_id;
64   sg_mailbox_t return_mailbox;
65   unsigned int bitfield;
66   int piece;
67   int block_index;
68   int block_length;
69 } s_message_t;
70 typedef s_message_t* message_t;
71
72 /** Builds a new value-less message */
73 message_t message_new(e_message_type type, int peer_id, sg_mailbox_t return_mailbox);
74 /** Builds a new "have/piece" message */
75 message_t message_index_new(e_message_type type, int peer_id, sg_mailbox_t return_mailbox, int index);
76 message_t message_other_new(e_message_type type, int peer_id, sg_mailbox_t return_mailbox, unsigned int bitfield);
77 /** Builds a new "request" message */
78 message_t message_request_new(int peer_id, sg_mailbox_t return_mailbox, int piece, int block_index, int block_length);
79 /** Build a new "piece" message */
80 message_t message_piece_new(int peer_id, sg_mailbox_t return_mailbox, int index, int block_index, int block_length);
81
82 #endif /* BITTORRENT_BITTORRENT_H_ */