Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into hypervisor
[simgrid.git] / examples / msg / bittorrent / messages.h
1 /* Copyright (c) 2012. 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_MESSAGES_H_
8 #define BITTORRENT_MESSAGES_H_
9 #include <msg/msg.h>
10
11 /**
12  * Message sizes
13  * Sizes based on report by A. Legout et al, Understanding BitTorrent: An Experimental Perspective
14  * http://hal.inria.fr/inria-00000156/en
15  */
16 #define MESSAGE_HANDSHAKE_SIZE 68
17 #define MESSAGE_CHOKE_SIZE 5
18 #define MESSAGE_UNCHOKE_SIZE 5
19 #define MESSAGE_INTERESTED_SIZE 5
20 #define MESSAGE_NOTINTERESTED_SIZE 5
21 #define MESSAGE_HAVE_SIZE 9
22 #define MESSAGE_BITFIELD_SIZE 5
23 #define MESSAGE_REQUEST_SIZE 17
24 #define MESSAGE_PIECE_SIZE 13
25 #define MESSAGE_CANCEL_SIZE 17
26
27 /**
28  * Types of messages exchanged between two peers.
29  */
30 typedef enum {
31   MESSAGE_HANDSHAKE,
32   MESSAGE_CHOKE,
33   MESSAGE_UNCHOKE,
34   MESSAGE_INTERESTED,
35   MESSAGE_NOTINTERESTED,
36   MESSAGE_HAVE,
37   MESSAGE_BITFIELD,
38   MESSAGE_REQUEST,
39   MESSAGE_PIECE,
40   MESSAGE_CANCEL
41 } e_message_type;
42
43 /**
44  * Message data
45  */
46 typedef struct s_message {
47   e_message_type type;
48   const char *mailbox;
49   const char *issuer_host_name;
50   int peer_id;
51   char *bitfield;
52   int index;
53   int block_index;
54   int block_length;
55 } s_message_t, *message_t;
56 /**
57  * Builds a new value-less message
58  */
59 msg_task_t task_message_new(e_message_type type,
60                             const char *issuer_host_name,
61                             const char *mailbox, int peer_id, int size);
62 /**
63  * Builds a new "have/piece" message
64  */
65 msg_task_t task_message_index_new(e_message_type type,
66                                   const char *issuer_host_name,
67                                   const char *mailbox, int peer_id,
68                                   int index, int varsize);
69 /**
70  * Builds a new bitfield message
71  */
72 msg_task_t task_message_bitfield_new(const char *issuer_host_name,
73                                      const char *mailbox, int peer_id,
74                                      char *bitfield, int bitfield_size);
75 /**
76  * Builds a new "request" message
77  */
78 msg_task_t task_message_request_new(const char *issuer_host_name,
79                                     const char *mailbox, int peer_id,
80                                     int index, int block_index,
81                                     int block_length);
82
83 /**
84  * Build a new "piece" message
85  */
86 msg_task_t task_message_piece_new(const char *issuer_host_name,
87                                   const char *mailbox, int peer_id, int index,
88                                   int block_index,
89                                   int block_length, int block_size);
90 /**
91  * Free a message task
92  */
93 void task_message_free(void *);
94
95 int task_message_size(e_message_type type);
96 #endif                          /* BITTORRENT_MESSAGES_H_ */