Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename two more files in msg/app-bittorrent/.
[simgrid.git] / teshsuite / msg / app-bittorrent / bittorrent-messages.c
1 /* Copyright (c) 2012-2014. 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 #include "bittorrent-messages.h"
8 #include "bittorrent.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory");
11
12 #define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0)
13
14 /** @brief Build a new empty message
15  * @param type type of the message
16  * @param issuer_host_name hostname of the issuer, for debugging 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, const char* mailbox, int peer_id,
22                             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 /** Builds a message containing an index. */
35 msg_task_t task_message_index_new(e_message_type type, const char* issuer_host_name, const char* mailbox, int peer_id,
36                                   int index, int varsize)
37 {
38   msg_task_t task   = task_message_new(type, issuer_host_name, mailbox, peer_id, task_message_size(type) + varsize);
39   message_t message = MSG_task_get_data(task);
40   message->index    = index;
41   return task;
42 }
43
44 msg_task_t task_message_bitfield_new(const char* issuer_host_name, const char* mailbox, int peer_id,
45                                      unsigned int bitfield, int bitfield_size)
46 {
47   msg_task_t task = task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id,
48                                      task_message_size(MESSAGE_BITFIELD) + BITS_TO_BYTES(bitfield_size));
49   message_t message = MSG_task_get_data(task);
50   message->bitfield = bitfield;
51   return task;
52 }
53
54 msg_task_t task_message_request_new(const char* issuer_host_name, const char* mailbox, int peer_id, int index,
55                                     int block_index, int block_length)
56 {
57   msg_task_t task       = task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox, peer_id, index, 0);
58   message_t message     = MSG_task_get_data(task);
59   message->block_index  = block_index;
60   message->block_length = block_length;
61   return task;
62 }
63
64 msg_task_t task_message_piece_new(const char* issuer_host_name, const char* mailbox, int peer_id, int index,
65                                   int block_index, int block_length, int block_size)
66 {
67   msg_task_t task =
68       task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id, index, block_length * block_size);
69   message_t message     = MSG_task_get_data(task);
70   message->block_index  = block_index;
71   message->block_length = block_length;
72   return task;
73 }
74
75 void task_message_free(void* task)
76 {
77   message_t message = MSG_task_get_data(task);
78   xbt_free(message);
79   MSG_task_destroy(task);
80 }
81
82 int task_message_size(e_message_type type)
83 {
84   int size = 0;
85   switch (type) {
86     case MESSAGE_HANDSHAKE:
87       size = MESSAGE_HANDSHAKE_SIZE;
88       break;
89     case MESSAGE_CHOKE:
90       size = MESSAGE_CHOKE_SIZE;
91       break;
92     case MESSAGE_UNCHOKE:
93       size = MESSAGE_UNCHOKE_SIZE;
94       break;
95     case MESSAGE_INTERESTED:
96     case MESSAGE_NOTINTERESTED:
97       size = MESSAGE_INTERESTED_SIZE;
98       break;
99     case MESSAGE_HAVE:
100       size = MESSAGE_HAVE_SIZE;
101       break;
102     case MESSAGE_BITFIELD:
103       size = MESSAGE_BITFIELD_SIZE;
104       break;
105     case MESSAGE_REQUEST:
106       size = MESSAGE_REQUEST_SIZE;
107       break;
108     case MESSAGE_PIECE:
109       size = MESSAGE_PIECE_SIZE;
110       break;
111     case MESSAGE_CANCEL:
112       size = MESSAGE_CANCEL_SIZE;
113       break;
114     default:
115       THROW_IMPOSSIBLE;
116   }
117   return size;
118 }