1 /* Copyright (c) 2012. 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. */
7 #include "bittorrent.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory");
11 #define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0)
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
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)
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;
29 msg_task_t task = MSG_task_create(NULL, 0, size, message);
30 XBT_DEBUG("type: %d size: %d", (int)type, size);
35 * Builds a message containing an index.
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)
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;
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)
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;
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)
68 task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
70 message_t message = MSG_task_get_data(task);
71 message->block_index = block_index;
72 message->block_length = block_length;
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)
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;
91 void task_message_free(void *task)
93 message_t message = MSG_task_get_data(task);
95 MSG_task_destroy(task);
98 int task_message_size(e_message_type 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;