1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "bittorrent.h"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages,
11 "Messages specific for the message factory");
13 #define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0)
16 * Build a new empty message
17 * @param type type of the message
18 * @param issuer_host_name hostname of the issuer, for debuging purposes
19 * @param mailbox mailbox where the peer should answer
20 * @param peer_id id of the issuer
21 * @param size message size in bytes
23 msg_task_t task_message_new(e_message_type type, const char *issuer_host_name,
24 const char *mailbox, int peer_id, int size)
26 message_t message = xbt_new(s_message_t, 1);
27 message->issuer_host_name = issuer_host_name;
28 message->peer_id = peer_id;
29 message->mailbox = mailbox;
31 msg_task_t task = MSG_task_create(NULL, 0, size, message);
32 XBT_DEBUG("type: %d size: %d", (int) type, size);
37 * Builds a message containing an index.
39 msg_task_t task_message_index_new(e_message_type type,
40 const char *issuer_host_name,
41 const char *mailbox, int peer_id,
42 int index, int varsize)
44 msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id,
45 task_message_size(type) + varsize);
46 message_t message = MSG_task_get_data(task);
47 message->index = index;
51 msg_task_t task_message_bitfield_new(const char *issuer_host_name,
52 const char *mailbox, int peer_id,
53 char *bitfield, int bitfield_size)
56 task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id,
57 task_message_size(MESSAGE_BITFIELD) +
58 /* Size of bitfield in bytes */
59 BITS_TO_BYTES(bitfield_size));
60 message_t message = MSG_task_get_data(task);
61 message->bitfield = bitfield;
65 msg_task_t task_message_request_new(const char *issuer_host_name,
66 const char *mailbox, int peer_id,
67 int index, int block_index,
71 task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
73 message_t message = MSG_task_get_data(task);
74 message->block_index = block_index;
75 message->block_length = block_length;
79 msg_task_t task_message_piece_new(const char *issuer_host_name,
80 const char *mailbox, int peer_id, int index,
82 int block_length, int block_size)
85 task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id,
86 index, block_length * block_size);
87 message_t message = MSG_task_get_data(task);
88 message->block_index = block_index;
89 message->block_length = block_length;
93 void task_message_free(void *task)
95 message_t message = MSG_task_get_data(task);
97 MSG_task_destroy(task);
100 int task_message_size(e_message_type type)
104 case MESSAGE_HANDSHAKE:
105 size = MESSAGE_HANDSHAKE_SIZE;
108 size = MESSAGE_CHOKE_SIZE;
110 case MESSAGE_UNCHOKE:
111 size = MESSAGE_UNCHOKE_SIZE;
113 case MESSAGE_INTERESTED:
114 size = MESSAGE_INTERESTED_SIZE;
116 case MESSAGE_NOTINTERESTED:
117 size = MESSAGE_INTERESTED_SIZE;
120 size = MESSAGE_HAVE_SIZE;
122 case MESSAGE_BITFIELD:
123 size = MESSAGE_BITFIELD_SIZE;
125 case MESSAGE_REQUEST:
126 size = MESSAGE_REQUEST_SIZE;
129 size = MESSAGE_PIECE_SIZE;
132 size = MESSAGE_CANCEL_SIZE;