Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
BitTorrent: fixed message sizes, based on the actual protocol sizes
authorMaximiliano Geier <maximiliano.geier@loria.fr>
Thu, 18 Oct 2012 13:27:07 +0000 (15:27 +0200)
committerMaximiliano Geier <maximiliano.geier@loria.fr>
Thu, 18 Oct 2012 13:27:07 +0000 (15:27 +0200)
Fixed tesh to match this change

examples/msg/bittorrent/bittorrent.h
examples/msg/bittorrent/bittorrent.tesh
examples/msg/bittorrent/messages.c
examples/msg/bittorrent/messages.h
examples/msg/bittorrent/peer.c

index 4c24b9d..87881f4 100644 (file)
@@ -7,10 +7,6 @@
 #ifndef BITTORRENT_BITTORRENT_H_
 #define BITTORRENT_BITTORRENT_H_
 
 #ifndef BITTORRENT_BITTORRENT_H_
 #define BITTORRENT_BITTORRENT_H_
 
-/**
- * Information message size
- */
-#define MESSAGE_SIZE 1
 /**
  * Size of mailboxes
  */
 /**
  * Size of mailboxes
  */
index 4bb9145..a94ad65 100644 (file)
@@ -15,11 +15,9 @@ $ $SG_TEST_EXENV ${bindir:=.}/bittorrent ${srcdir:=.}/../msg_platform.xml ${srcd
 > [    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
 > [    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.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
index bebb90c..8eac54f 100644 (file)
@@ -5,22 +5,31 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 #include "messages.h"
 #include "bittorrent.h"
  * 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
 /**
  * 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,
  */
 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;
   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;
 }
 
   return task;
 }
 
@@ -29,9 +38,11 @@ msg_task_t task_message_new(e_message_type type, const char *issuer_host_name,
  */
 msg_task_t task_message_index_new(e_message_type type,
                                   const char *issuer_host_name,
  */
 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;
   message_t message = MSG_task_get_data(task);
   message->index = index;
   return task;
@@ -39,10 +50,13 @@ msg_task_t task_message_index_new(e_message_type type,
 
 msg_task_t task_message_bitfield_new(const char *issuer_host_name,
                                      const char *mailbox, int peer_id,
 
 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 =
 {
   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;
   message_t message = MSG_task_get_data(task);
   message->bitfield = bitfield;
   return task;
@@ -54,7 +68,7 @@ msg_task_t task_message_request_new(const char *issuer_host_name,
 {
   msg_task_t task =
       task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
 {
   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;
   message_t message = MSG_task_get_data(task);
   message->block_index = block_index;
   message->block_length = block_length;
@@ -64,11 +78,11 @@ msg_task_t task_message_request_new(const char *issuer_host_name,
 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,
 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,
 {
   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;
   message_t message = MSG_task_get_data(task);
   message->stalled = stalled;
   message->block_index = block_index;
@@ -82,3 +96,20 @@ void task_message_free(void *task)
   xbt_free(message);
   MSG_task_destroy(task);
 }
   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;
+}
index 5073640..2a87b7c 100644 (file)
@@ -7,6 +7,21 @@
 #ifndef BITTORRENT_MESSAGES_H_
 #define BITTORRENT_MESSAGES_H_
 #include <msg/msg.h>
 #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.
  */
 /**
  * Types of messages exchanged between two peers.
  */
@@ -41,20 +56,21 @@ typedef struct s_message {
  */
 msg_task_t task_message_new(e_message_type type,
                                        const char *issuer_host_name,
  */
 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,
 /**
  * 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,
 /**
  * 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
  */
 /**
  * Builds a new "request" message
  */
@@ -68,9 +84,11 @@ msg_task_t task_message_request_new(const char *issuer_host_name,
 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,
 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 *);
 /**
  * Free a message task
  */
 void task_message_free(void *);
+
+int task_message_size(e_message_type type);
 #endif                          /* BITTORRENT_MESSAGES_H_ */
 #endif                          /* BITTORRENT_MESSAGES_H_ */
index acafbc7..baa8bc1 100644 (file)
@@ -15,11 +15,14 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers");
 //TODO: Let users change this
 /*
  * File transfered data
 //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 FILE_PIECES = 10;
 
 static int PIECES_BLOCKS = 5;
+static int BLOCK_SIZE = 16384;
 static int BLOCKS_REQUESTED = 2;
 
 /**
 static int BLOCKS_REQUESTED = 2;
 
 /**
@@ -726,7 +729,7 @@ void send_interested_to_peers(peer_t peer)
       connection->am_interested = 1;
       msg_task_t task =
           task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
       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_dsend(task, connection->mailbox, task_message_free);
       XBT_DEBUG("Send INTERESTED to %s", connection->mailbox);
     }
@@ -744,7 +747,7 @@ void send_interested(peer_t peer, const char *mailbox)
 {
   msg_task_t task =
       task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->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_dsend(task, mailbox, task_message_free);
   XBT_DEBUG("Sending INTERESTED to %s", mailbox);
 
@@ -759,7 +762,7 @@ void send_notinterested(peer_t peer, const char *mailbox)
 {
   msg_task_t task =
       task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->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);
 
   MSG_task_dsend(task, mailbox, task_message_free);
   XBT_DEBUG("Sending NOTINTERESTED to %s", mailbox);
 
@@ -777,7 +780,7 @@ void send_handshake_all(peer_t peer)
   xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
     msg_task_t task =
         task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->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_dsend(task, remote_peer->mailbox, task_message_free);
     XBT_DEBUG("Sending a HANDSHAKE to %s", remote_peer->mailbox);
   }
@@ -792,7 +795,7 @@ void send_handshake(peer_t peer, const char *mailbox)
 {
   msg_task_t task =
       task_message_new(MESSAGE_HANDSHAKE, peer->hostname, 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);
 }
   MSG_task_dsend(task, mailbox, task_message_free);
   XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox);
 }
@@ -804,7 +807,8 @@ void send_choked(peer_t peer, const char *mailbox)
 {
   XBT_DEBUG("Sending a CHOKE to %s", mailbox);
   msg_task_t task =
 {
   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);
 }
 
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
@@ -816,7 +820,7 @@ void send_unchoked(peer_t peer, const char *mailbox)
   XBT_DEBUG("Sending a UNCHOKE to %s", mailbox);
   msg_task_t task =
       task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox,
   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);
 }
 
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
@@ -832,7 +836,7 @@ void send_have(peer_t peer, int piece)
   xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
     msg_task_t task =
         task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox,
   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);
   }
 }
     MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
   }
 }
@@ -846,7 +850,7 @@ void send_bitfield(peer_t peer, const char *mailbox)
   XBT_DEBUG("Sending a BITFIELD to %s", mailbox);
   msg_task_t task =
       task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id,
   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);
 }
 
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
@@ -877,7 +881,7 @@ void send_piece(peer_t peer, const char *mailbox, int piece, int stalled,
              "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,
              "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);
 }
 
   MSG_task_dsend(task, mailbox, task_message_free);
 }