Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
public headers should include simgrid in a system-wide way, not a project-wide one
[simgrid.git] / teshsuite / msg / app-bittorrent / bittorrent-messages.c
1 /* Copyright (c) 2012-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "bittorrent-messages.h"
7 #include "bittorrent.h"
8
9 #include <stdio.h> /* snprintf */
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory");
12
13 #define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0)
14
15 /** @brief Build a new empty message
16  * @param type type of the message
17  * @param issuer_host_name hostname of the issuer, for debugging 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, const char* mailbox, int peer_id,
23                             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 /** Builds a message containing an index. */
36 msg_task_t task_message_index_new(e_message_type type, const char* issuer_host_name, const char* mailbox, int peer_id,
37                                   int index, int varsize)
38 {
39   msg_task_t task   = task_message_new(type, issuer_host_name, mailbox, peer_id, task_message_size(type) + varsize);
40   message_t message = MSG_task_get_data(task);
41   message->index    = index;
42   return task;
43 }
44
45 msg_task_t task_message_bitfield_new(const char* issuer_host_name, const char* mailbox, int peer_id,
46                                      unsigned int bitfield, int bitfield_size)
47 {
48   msg_task_t task = task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id,
49                                      task_message_size(MESSAGE_BITFIELD) + BITS_TO_BYTES(bitfield_size));
50   message_t message = MSG_task_get_data(task);
51   message->bitfield = bitfield;
52   return task;
53 }
54
55 msg_task_t task_message_request_new(const char* issuer_host_name, const char* mailbox, int peer_id, int index,
56                                     int block_index, int block_length)
57 {
58   msg_task_t task       = task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox, peer_id, index, 0);
59   message_t message     = MSG_task_get_data(task);
60   message->block_index  = block_index;
61   message->block_length = block_length;
62   return task;
63 }
64
65 msg_task_t task_message_piece_new(const char* issuer_host_name, const char* mailbox, int peer_id, int index,
66                                   int block_index, int block_length, int block_size)
67 {
68   msg_task_t task =
69       task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id, index, block_length * block_size);
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 void task_message_free(void* task)
77 {
78   message_t message = MSG_task_get_data(task);
79   xbt_free(message);
80   MSG_task_destroy(task);
81 }
82
83 int task_message_size(e_message_type type)
84 {
85   int size = 0;
86   switch (type) {
87     case MESSAGE_HANDSHAKE:
88       size = MESSAGE_HANDSHAKE_SIZE;
89       break;
90     case MESSAGE_CHOKE:
91       size = MESSAGE_CHOKE_SIZE;
92       break;
93     case MESSAGE_UNCHOKE:
94       size = MESSAGE_UNCHOKE_SIZE;
95       break;
96     case MESSAGE_INTERESTED:
97     case MESSAGE_NOTINTERESTED:
98       size = MESSAGE_INTERESTED_SIZE;
99       break;
100     case MESSAGE_HAVE:
101       size = MESSAGE_HAVE_SIZE;
102       break;
103     case MESSAGE_BITFIELD:
104       size = MESSAGE_BITFIELD_SIZE;
105       break;
106     case MESSAGE_REQUEST:
107       size = MESSAGE_REQUEST_SIZE;
108       break;
109     case MESSAGE_PIECE:
110       size = MESSAGE_PIECE_SIZE;
111       break;
112     case MESSAGE_CANCEL:
113       size = MESSAGE_CANCEL_SIZE;
114       break;
115     default:
116       THROW_IMPOSSIBLE;
117   }
118   return size;
119 }