Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a8e98c3d4f20928d252ef3147984b80c8ca4cf6c
[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  */
14 #define MESSAGE_HANDSHAKE_SIZE 68
15 #define MESSAGE_CHOKE_SIZE 5
16 #define MESSAGE_UNCHOKE_SIZE 5
17 #define MESSAGE_INTERESTED_SIZE 5
18 #define MESSAGE_NOTINTERESTED_SIZE 5
19 #define MESSAGE_HAVE_SIZE 9
20 #define MESSAGE_BITFIELD_SIZE 5
21 #define MESSAGE_REQUEST_SIZE 17
22 #define MESSAGE_PIECE_SIZE 13
23 #define MESSAGE_CANCEL_SIZE 17
24
25 /**
26  * Types of messages exchanged between two peers.
27  */
28 typedef enum {
29   MESSAGE_HANDSHAKE,
30   MESSAGE_CHOKE,
31   MESSAGE_UNCHOKE,
32   MESSAGE_INTERESTED,
33   MESSAGE_NOTINTERESTED,
34   MESSAGE_HAVE,
35   MESSAGE_BITFIELD,
36   MESSAGE_REQUEST,
37   MESSAGE_PIECE,
38   MESSAGE_CANCEL
39 } e_message_type;
40
41 /**
42  * Message data
43  */
44 typedef struct s_message {
45   e_message_type type;
46   const char *mailbox;
47   const char *issuer_host_name;
48   int peer_id;
49   char *bitfield;
50   int index;
51   int block_index;
52   int block_length;
53   int stalled:1;
54 } s_message_t, *message_t;
55 /**
56  * Builds a new value-less message
57  */
58 msg_task_t task_message_new(e_message_type type,
59                                        const char *issuer_host_name,
60                                        const char *mailbox, int peer_id, 
61                                        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, int index,
80                                     int block_index, int block_length);
81
82 /**
83  * Build a new "piece" message
84  */
85 msg_task_t task_message_piece_new(const char *issuer_host_name,
86                                   const char *mailbox, int peer_id, int index,
87                                   int stalled, int block_index,
88                                   int block_length, int block_size);
89 /**
90  * Free a message task
91  */
92 void task_message_free(void *);
93
94 int task_message_size(e_message_type type);
95 #endif                          /* BITTORRENT_MESSAGES_H_ */