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, "Messages specific for the message factory");
12 #define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0)
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
21 msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id,
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);
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)
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;
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)
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;
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)
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;
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)
67 msg_task_t task = task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id, index,
68 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;
75 void task_message_free(void *task)
77 message_t message = MSG_task_get_data(task);
79 MSG_task_destroy(task);
82 int task_message_size(e_message_type type)
86 case MESSAGE_HANDSHAKE:
87 size = MESSAGE_HANDSHAKE_SIZE;
90 size = MESSAGE_CHOKE_SIZE;
93 size = MESSAGE_UNCHOKE_SIZE;
95 case MESSAGE_INTERESTED:
96 case MESSAGE_NOTINTERESTED:
97 size = MESSAGE_INTERESTED_SIZE;
100 size = MESSAGE_HAVE_SIZE;
102 case MESSAGE_BITFIELD:
103 size = MESSAGE_BITFIELD_SIZE;
105 case MESSAGE_REQUEST:
106 size = MESSAGE_REQUEST_SIZE;
109 size = MESSAGE_PIECE_SIZE;
112 size = MESSAGE_CANCEL_SIZE;