Logo AND Algorithmique Numérique Distribuée

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