#ifndef BITTORRENT_BITTORRENT_H_
#define BITTORRENT_BITTORRENT_H_
-/**
- * Information message size
- */
-#define MESSAGE_SIZE 1
/**
* Size of mailboxes
*/
> [ 0.000000] (8:peer@McGee) Hi, I'm joining the network with id 8
> [ 3000.000000] (1:tracker@Jacquelin) Tracker is leaving
> [ 5000.005340] (5:peer@Geoff) Here is my current status: 1111111111
-> [ 5000.012427] (7:peer@iRMX) Here is my current status: 1111111111
+> [ 5000.012389] (7:peer@iRMX) Here is my current status: 1111111111
> [ 5000.048881] (2:peer@Boivin) Here is my current status: 1111111111
-> [ 5000.827216] (8:peer@McGee) Here is my current status: 1111111111
-> [ 5000.832407] (3:peer@Jean_Yves) Here is my current status: 1111111111
-> [ 5000.897053] (4:peer@TeX) Here is my current status: 1111111111
-> [ 5000.897053] (6:peer@Disney) Here is my current status: 1111111111
-
-
+> [ 5000.827195] (8:peer@McGee) Here is my current status: 1111111111
+> [ 5000.832370] (3:peer@Jean_Yves) Here is my current status: 1111111111
+> [ 5000.897047] (4:peer@TeX) Here is my current status: 1111111111
+> [ 5000.897047] (6:peer@Disney) Here is my current status: 1111111111
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "messages.h"
#include "bittorrent.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory");
+
+#define BYTES_TO_MB(x) ((long double)x/1048576.0)
+#define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0)
+
/**
* Build a new empty message
* @param type type of the message
* @param issuer_host_name hostname of the issuer, for debuging purposes
* @param mailbox mailbox where the peer should answer
* @param peer_id id of the issuer
+ * @param size message size in bytes
*/
msg_task_t task_message_new(e_message_type type, const char *issuer_host_name,
- const char *mailbox, int peer_id)
+ const char *mailbox, int peer_id, int size)
{
+ long double size_mb = BYTES_TO_MB(size);
message_t message = xbt_new(s_message_t, 1);
message->issuer_host_name = issuer_host_name;
message->peer_id = peer_id;
message->mailbox = mailbox;
message->type = type;
- msg_task_t task = MSG_task_create(NULL, 0, MESSAGE_SIZE, message);
+ msg_task_t task = MSG_task_create(NULL, 0, size_mb, message);
+ XBT_DEBUG("type: %d size: %.20Lg (%d)", type, size_mb, size);
return task;
}
*/
msg_task_t task_message_index_new(e_message_type type,
const char *issuer_host_name,
- const char *mailbox, int peer_id, int index)
+ const char *mailbox, int peer_id,
+ int index, int varsize)
{
- msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id);
+ msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id,
+ task_message_size(type) + varsize);
message_t message = MSG_task_get_data(task);
message->index = index;
return task;
msg_task_t task_message_bitfield_new(const char *issuer_host_name,
const char *mailbox, int peer_id,
- char *bitfield)
+ char *bitfield, int bitfield_size)
{
msg_task_t task =
- task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id);
+ task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id,
+ task_message_size(MESSAGE_BITFIELD) +
+ /* Size of bitfield in bytes */
+ BITS_TO_BYTES(bitfield_size));
message_t message = MSG_task_get_data(task);
message->bitfield = bitfield;
return task;
{
msg_task_t task =
task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
- peer_id, index);
+ peer_id, index, 0);
message_t message = MSG_task_get_data(task);
message->block_index = block_index;
message->block_length = block_length;
msg_task_t task_message_piece_new(const char *issuer_host_name,
const char *mailbox, int peer_id, int index,
int stalled, int block_index,
- int block_length)
+ int block_length, int block_size)
{
msg_task_t task =
task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id,
- index);
+ index, block_length * block_size);
message_t message = MSG_task_get_data(task);
message->stalled = stalled;
message->block_index = block_index;
xbt_free(message);
MSG_task_destroy(task);
}
+
+int task_message_size(e_message_type type)
+{
+ int size = 0;
+ switch (type) {
+ case MESSAGE_HANDSHAKE: size = MESSAGE_HANDSHAKE_SIZE; break;
+ case MESSAGE_CHOKE: size = MESSAGE_CHOKE_SIZE; break;
+ case MESSAGE_UNCHOKE: size = MESSAGE_UNCHOKE_SIZE; break;
+ case MESSAGE_INTERESTED: size = MESSAGE_INTERESTED_SIZE; break;
+ case MESSAGE_NOTINTERESTED: size = MESSAGE_INTERESTED_SIZE; break;
+ case MESSAGE_HAVE: size = MESSAGE_HAVE_SIZE; break;
+ case MESSAGE_BITFIELD: size = MESSAGE_BITFIELD_SIZE; break;
+ case MESSAGE_REQUEST: size = MESSAGE_REQUEST_SIZE; break;
+ case MESSAGE_PIECE: size = MESSAGE_PIECE_SIZE; break;
+ }
+ return size;
+}
#ifndef BITTORRENT_MESSAGES_H_
#define BITTORRENT_MESSAGES_H_
#include <msg/msg.h>
+
+/**
+ * Message sizes
+ */
+#define MESSAGE_HANDSHAKE_SIZE 68
+#define MESSAGE_CHOKE_SIZE 5
+#define MESSAGE_UNCHOKE_SIZE 5
+#define MESSAGE_INTERESTED_SIZE 5
+#define MESSAGE_NOTINTERESTED_SIZE 5
+#define MESSAGE_HAVE_SIZE 9
+#define MESSAGE_BITFIELD_SIZE 5
+#define MESSAGE_REQUEST_SIZE 17
+#define MESSAGE_PIECE_SIZE 13
+#define MESSAGE_CANCEL_SIZE 17
+
/**
* Types of messages exchanged between two peers.
*/
*/
msg_task_t task_message_new(e_message_type type,
const char *issuer_host_name,
- const char *mailbox, int peer_id);
+ const char *mailbox, int peer_id,
+ int size);
/**
* Builds a new "have/piece" message
*/
msg_task_t task_message_index_new(e_message_type type,
const char *issuer_host_name,
const char *mailbox, int peer_id,
- int index);
+ int index, int varsize);
/**
* Builds a new bitfield message
*/
msg_task_t task_message_bitfield_new(const char *issuer_host_name,
const char *mailbox, int peer_id,
- char *bitfield);
+ char *bitfield, int bitfield_size);
/**
* Builds a new "request" message
*/
msg_task_t task_message_piece_new(const char *issuer_host_name,
const char *mailbox, int peer_id, int index,
int stalled, int block_index,
- int block_length);
+ int block_length, int block_size);
/**
* Free a message task
*/
void task_message_free(void *);
+
+int task_message_size(e_message_type type);
#endif /* BITTORRENT_MESSAGES_H_ */
//TODO: Let users change this
/*
* File transfered data
+ *
+ * File size: 10 pieces * 5 blocks/piece * 16384 bytes/block = 819200 bytes
*/
-static int FILE_SIZE = 5120;
+static int FILE_SIZE = 10 * 5 * 16384;
static int FILE_PIECES = 10;
static int PIECES_BLOCKS = 5;
+static int BLOCK_SIZE = 16384;
static int BLOCKS_REQUESTED = 2;
/**
connection->am_interested = 1;
msg_task_t task =
task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
- peer->id);
+ peer->id, task_message_size(MESSAGE_INTERESTED));
MSG_task_dsend(task, connection->mailbox, task_message_free);
XBT_DEBUG("Send INTERESTED to %s", connection->mailbox);
}
{
msg_task_t task =
task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
- peer->id);
+ peer->id, task_message_size(MESSAGE_INTERESTED));
MSG_task_dsend(task, mailbox, task_message_free);
XBT_DEBUG("Sending INTERESTED to %s", mailbox);
{
msg_task_t task =
task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox,
- peer->id);
+ peer->id, task_message_size(MESSAGE_NOTINTERESTED));
MSG_task_dsend(task, mailbox, task_message_free);
XBT_DEBUG("Sending NOTINTERESTED to %s", mailbox);
xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
msg_task_t task =
task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
- peer->id);
+ peer->id, task_message_size(MESSAGE_HANDSHAKE));
MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
XBT_DEBUG("Sending a HANDSHAKE to %s", remote_peer->mailbox);
}
{
msg_task_t task =
task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
- peer->id);
+ peer->id, task_message_size(MESSAGE_HANDSHAKE));
MSG_task_dsend(task, mailbox, task_message_free);
XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox);
}
{
XBT_DEBUG("Sending a CHOKE to %s", mailbox);
msg_task_t task =
- task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox, peer->id);
+ task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox,
+ peer->id, task_message_size(MESSAGE_CHOKE));
MSG_task_dsend(task, mailbox, task_message_free);
}
XBT_DEBUG("Sending a UNCHOKE to %s", mailbox);
msg_task_t task =
task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox,
- peer->id);
+ peer->id, task_message_size(MESSAGE_UNCHOKE));
MSG_task_dsend(task, mailbox, task_message_free);
}
xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
msg_task_t task =
task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox,
- peer->id, piece);
+ peer->id, piece, task_message_size(MESSAGE_HAVE));
MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
}
}
XBT_DEBUG("Sending a BITFIELD to %s", mailbox);
msg_task_t task =
task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id,
- peer->bitfield);
+ peer->bitfield, FILE_PIECES);
MSG_task_dsend(task, mailbox, task_message_free);
}
"Tried to send a piece that we doesn't have.");
msg_task_t task =
task_message_piece_new(peer->hostname, peer->mailbox, peer->id, piece,
- stalled, block_index, block_length);
+ stalled, block_index, block_length, BLOCK_SIZE);
MSG_task_dsend(task, mailbox, task_message_free);
}