Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b5ecc8887e3b5704df3e151daf7e24eb1e765fbb
[simgrid.git] / examples / msg / bittorrent / messages.c
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 #include "messages.h"
7 #include "bittorrent.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory");
10
11 #define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0)
12
13 /**
14  * Build a new empty message
15  * @param type type of the message
16  * @param issuer_host_name hostname of the issuer, for debuging purposes
17  * @param mailbox mailbox where the peer should answer
18  * @param peer_id id of the issuer
19  * @param size message size in bytes
20  */
21 msg_task_t task_message_new(e_message_type type, const char *issuer_host_name,
22                             const char *mailbox, int peer_id, int size)
23 {
24   message_t message = xbt_new(s_message_t, 1);
25   message->issuer_host_name = issuer_host_name;
26   message->peer_id = peer_id;
27   message->mailbox = mailbox;
28   message->type = type;
29   msg_task_t task = MSG_task_create(NULL, 0, size, message);
30   XBT_DEBUG("type: %d size: %d", (int)type, size);
31   return task;
32 }
33
34 /**
35  * Builds a message containing an index.
36  */
37 msg_task_t task_message_index_new(e_message_type type,
38                                   const char *issuer_host_name,
39                                   const char *mailbox, int peer_id, 
40                                   int index, int varsize)
41 {
42   msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id,
43                                      task_message_size(type) + varsize);
44   message_t message = MSG_task_get_data(task);
45   message->index = index;
46   return task;
47 }
48
49 msg_task_t task_message_bitfield_new(const char *issuer_host_name,
50                                      const char *mailbox, int peer_id,
51                                      char *bitfield, int bitfield_size)
52 {
53   msg_task_t task =
54       task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id,
55                        task_message_size(MESSAGE_BITFIELD) + 
56                        /* Size of bitfield in bytes */ 
57                        BITS_TO_BYTES(bitfield_size));
58   message_t message = MSG_task_get_data(task);
59   message->bitfield = bitfield;
60   return task;
61 }
62
63 msg_task_t task_message_request_new(const char *issuer_host_name,
64                                     const char *mailbox, int peer_id, int index,
65                                     int block_index, int block_length)
66 {
67   msg_task_t task =
68       task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
69                              peer_id, index, 0);
70   message_t message = MSG_task_get_data(task);
71   message->block_index = block_index;
72   message->block_length = block_length;
73   return task;
74 }
75
76 msg_task_t task_message_piece_new(const char *issuer_host_name,
77                                   const char *mailbox, int peer_id, int index,
78                                   int stalled, int block_index,
79                                   int block_length, int block_size)
80 {
81   msg_task_t task =
82       task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id,
83                              index, block_length * block_size);
84   message_t message = MSG_task_get_data(task);
85   message->stalled = stalled;
86   message->block_index = block_index;
87   message->block_length = block_length;
88   return task;
89 }
90
91 void task_message_free(void *task)
92 {
93   message_t message = MSG_task_get_data(task);
94   xbt_free(message);
95   MSG_task_destroy(task);
96 }
97
98 int task_message_size(e_message_type type)
99 {
100   int size = 0;
101   switch (type) {
102     case MESSAGE_HANDSHAKE: size = MESSAGE_HANDSHAKE_SIZE; break;
103     case MESSAGE_CHOKE: size = MESSAGE_CHOKE_SIZE; break;
104     case MESSAGE_UNCHOKE: size = MESSAGE_UNCHOKE_SIZE; break;
105     case MESSAGE_INTERESTED: size = MESSAGE_INTERESTED_SIZE; break;
106     case MESSAGE_NOTINTERESTED: size = MESSAGE_INTERESTED_SIZE; break;
107     case MESSAGE_HAVE: size = MESSAGE_HAVE_SIZE; break;
108     case MESSAGE_BITFIELD: size = MESSAGE_BITFIELD_SIZE; break;
109     case MESSAGE_REQUEST: size = MESSAGE_REQUEST_SIZE; break;
110     case MESSAGE_PIECE: size = MESSAGE_PIECE_SIZE; break;
111     case MESSAGE_CANCEL: size = MESSAGE_CANCEL_SIZE; break;
112   }
113   return size;
114 }