Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reindent before changes
authorNicolas Bonichon <bonichon@labri.fr>
Thu, 25 Apr 2013 15:18:11 +0000 (17:18 +0200)
committerNicolas Bonichon <bonichon@labri.fr>
Thu, 25 Apr 2013 15:18:11 +0000 (17:18 +0200)
examples/msg/bittorrent/bittorrent.h
examples/msg/bittorrent/bittorrent_platfgen.c
examples/msg/bittorrent/connection.h
examples/msg/bittorrent/messages.c
examples/msg/bittorrent/messages.h
examples/msg/bittorrent/peer.c
examples/msg/bittorrent/peer.h
examples/msg/bittorrent/tracker.c
examples/msg/bittorrent/tracker.h

index 2cad1e4..e2c4e5b 100644 (file)
@@ -54,4 +54,4 @@
  */
 #define MAX_PIECES 1
 
-#endif                          /* BITTORRENT_BITTORRENT_H_ */
+#endif /* BITTORRENT_BITTORRENT_H_ */
index 62bbf49..8e97af6 100644 (file)
@@ -16,19 +16,22 @@ static RngStream rng_stream;
 void promoter(context_node_t node);
 void labeler(context_edge_t edge);
 void create_environment(int node_count);
-void dispatch_jobs(double tracker_deadline, double peer_deadline, double seed_percentage);
+void dispatch_jobs(double tracker_deadline, double peer_deadline,
+                  double seed_percentage);
 
-void promoter(context_node_t node) {
+void promoter(context_node_t node)
+{
   s_sg_platf_host_cbarg_t host_parameters;
 
-  if(node->degree == 1) {
+  if (node->degree == 1) {
     //We promote only the leaf; as we use a star topology, all the nodes
     //will be promoted except the first one, which will be a router with
     //every hosts connected on.
     host_parameters.id = NULL;
 
     //Power from 3,000,000 to 10,000,000
-    host_parameters.power_peak = 7000000 * RngStream_RandU01(rng_stream) + 3000000;
+    host_parameters.power_peak =
+      7000000 * RngStream_RandU01(rng_stream) + 3000000;
     host_parameters.core_amount = 1;
     host_parameters.power_scale = 1;
     host_parameters.power_trace = NULL;
@@ -41,13 +44,15 @@ void promoter(context_node_t node) {
   }
 }
 
-void labeler(context_edge_t edge) {
+void labeler(context_edge_t edge)
+{
 
   s_sg_platf_link_cbarg_t link_parameters;
   link_parameters.id = NULL;
 
   //bandwidth from 3,000,000 to 10,000,000
-  link_parameters.bandwidth = 7000000 * RngStream_RandU01(rng_stream) + 3000000;
+  link_parameters.bandwidth =
+    7000000 * RngStream_RandU01(rng_stream) + 3000000;
   link_parameters.bandwidth_trace = NULL;
 
   //Latency from 0ms to 100ms
@@ -61,7 +66,8 @@ void labeler(context_edge_t edge) {
   platf_graph_link_label(edge, &link_parameters);
 }
 
-void create_environment(int node_count) {
+void create_environment(int node_count)
+{
 
   platf_graph_uniform(node_count);
 
@@ -81,43 +87,48 @@ void create_environment(int node_count) {
   platf_generate();
 }
 
-void dispatch_jobs(double tracker_deadline, double peer_deadline, double seed_percentage) {
+void dispatch_jobs(double tracker_deadline, double peer_deadline,
+                  double seed_percentage)
+{
 
   xbt_dynar_t available_nodes = MSG_hosts_as_dynar();
   msg_host_t host;
   unsigned int i;
 
-  char** arguments_tracker;
-  char** arguments_peer;
+  char **arguments_tracker;
+  char **arguments_peer;
 
-  unsigned int seed_count = (seed_percentage/100.0) * xbt_dynar_length(available_nodes);
+  unsigned int seed_count =
+    (seed_percentage / 100.0) * xbt_dynar_length(available_nodes);
 
   xbt_dynar_foreach(available_nodes, i, host) {
-    if(i==0) {
+    if (i == 0) {
       //The fisrt node is the tracker
-      arguments_tracker = xbt_malloc0(sizeof(char*) * 2);
+      arguments_tracker = xbt_malloc0(sizeof(char *) * 2);
       arguments_tracker[0] = xbt_strdup("tracker");
       arguments_tracker[1] = bprintf("%f", tracker_deadline);
-      MSG_process_create_with_arguments("tracker", tracker, NULL, host, 2, arguments_tracker);
+      MSG_process_create_with_arguments("tracker", tracker, NULL, host, 2,
+                                       arguments_tracker);
     } else {
       //Other nodes are peers
       int argument_size;
-      arguments_peer = xbt_malloc0(sizeof(char*) * 4);
+      arguments_peer = xbt_malloc0(sizeof(char *) * 4);
       arguments_peer[0] = xbt_strdup("peer");
       arguments_peer[1] = bprintf("%d", i);
       arguments_peer[2] = bprintf("%f", peer_deadline);
 
       //The first peers will be seeders
-      if(seed_count > 0) {
-        seed_count--;
-        arguments_peer[3] = xbt_strdup("1");
-        argument_size = 4;
+      if (seed_count > 0) {
+       seed_count--;
+       arguments_peer[3] = xbt_strdup("1");
+       argument_size = 4;
       } else {
-        //Other ars leechers
-        arguments_peer[3] = NULL;
-        argument_size = 3;
+       //Other ars leechers
+       arguments_peer[3] = NULL;
+       argument_size = 3;
       }
-      MSG_process_create_with_arguments("peer", peer, NULL, host, argument_size, arguments_peer);
+      MSG_process_create_with_arguments("peer", peer, NULL, host,
+                                       argument_size, arguments_peer);
     }
   }
 }
index 807c709..6a31018 100644 (file)
@@ -9,16 +9,16 @@
  * Contains the connection data of a peer.
  */
 typedef struct s_connection {
-  int id;                       //Peer id
-  char *bitfield;               //Fields
+  int id;                      //Peer id
+  char *bitfield;              //Fields
   char *mailbox;
   int messages_count;
   double peer_speed;
   double last_unchoke;
-  int am_interested:1;          //Indicates if we are interested in something the peer has
-  int interested:1;             //Indicates if the peer is interested in one of our pieces
-  int choked_upload:1;          //Indicates if the peer is choked for the current peer
-  int choked_download:1;        //Indicates if the peer has choked the current peer
+  int am_interested:1;         //Indicates if we are interested in something the peer has
+  int interested:1;            //Indicates if the peer is interested in one of our pieces
+  int choked_upload:1;         //Indicates if the peer is choked for the current peer
+  int choked_download:1;       //Indicates if the peer has choked the current peer
 } s_connection_t, *connection_t;
 /**
  * Build a new connection object from the peer id.
@@ -35,4 +35,4 @@ void connection_add_speed_value(connection_t connection, double speed);
  * Frees a connection object
  */
 void connection_free(void *data);
-#endif                          /* BITTORRENT_CONNECTION_H_ */
+#endif /* BITTORRENT_CONNECTION_H_ */
index b5ecc88..d165051 100644 (file)
@@ -6,7 +6,8 @@
 #include "messages.h"
 #include "bittorrent.h"
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory");
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages,
+                            "Messages specific for the message factory");
 
 #define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0)
 
@@ -19,7 +20,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message fa
  * @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, int size)
+                           const char *mailbox, int peer_id, int size)
 {
   message_t message = xbt_new(s_message_t, 1);
   message->issuer_host_name = issuer_host_name;
@@ -27,7 +28,7 @@ msg_task_t task_message_new(e_message_type type, const char *issuer_host_name,
   message->mailbox = mailbox;
   message->type = type;
   msg_task_t task = MSG_task_create(NULL, 0, size, message);
-  XBT_DEBUG("type: %d size: %d", (int)type, size);
+  XBT_DEBUG("type: %d size: %d", (int) type, size);
   return task;
 }
 
@@ -35,38 +36,39 @@ msg_task_t task_message_new(e_message_type type, const char *issuer_host_name,
  * Builds a message containing an index.
  */
 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 varsize)
+                                 const char *issuer_host_name,
+                                 const char *mailbox, int peer_id,
+                                 int index, int varsize)
 {
   msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id,
-                                     task_message_size(type) + varsize);
+                                    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, int bitfield_size)
+                                    const char *mailbox, int peer_id,
+                                    char *bitfield, int bitfield_size)
 {
   msg_task_t task =
-      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));
+    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_message_request_new(const char *issuer_host_name,
-                                    const char *mailbox, int peer_id, int index,
-                                    int block_index, int block_length)
+                                   const char *mailbox, int peer_id,
+                                   int index, int block_index,
+                                   int block_length)
 {
   msg_task_t task =
-      task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
-                             peer_id, index, 0);
+    task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox,
+                          peer_id, index, 0);
   message_t message = MSG_task_get_data(task);
   message->block_index = block_index;
   message->block_length = block_length;
@@ -74,13 +76,13 @@ 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,
-                                  int block_length, int block_size)
+                                 const char *mailbox, int peer_id, int index,
+                                 int stalled, int block_index,
+                                 int block_length, int block_size)
 {
   msg_task_t task =
-      task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id,
-                             index, block_length * block_size);
+    task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id,
+                          index, block_length * block_size);
   message_t message = MSG_task_get_data(task);
   message->stalled = stalled;
   message->block_index = block_index;
@@ -99,16 +101,36 @@ 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;
-    case MESSAGE_CANCEL: size = MESSAGE_CANCEL_SIZE; break;
+  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;
+  case MESSAGE_CANCEL:
+    size = MESSAGE_CANCEL_SIZE;
+    break;
   }
   return size;
 }
index 8e29f04..26a8c7a 100644 (file)
@@ -58,40 +58,40 @@ typedef struct s_message {
  * Builds a new value-less message
  */
 msg_task_t task_message_new(e_message_type type,
-                                       const char *issuer_host_name,
-                                       const char *mailbox, int peer_id, 
-                                       int size);
+                           const char *issuer_host_name,
+                           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 varsize);
+                                 const char *issuer_host_name,
+                                 const char *mailbox, int peer_id,
+                                 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, int bitfield_size);
+                                    const char *mailbox, int peer_id,
+                                    char *bitfield, int bitfield_size);
 /**
  * Builds a new "request" message
  */
 msg_task_t task_message_request_new(const char *issuer_host_name,
-                                    const char *mailbox, int peer_id, int index,
-                                    int block_index, int block_length);
+                                   const char *mailbox, int peer_id,
+                                   int index, int block_index,
+                                   int block_length);
 
 /**
  * Build a new "piece" 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_size);
+                                 const char *mailbox, int peer_id, int index,
+                                 int stalled, int block_index,
+                                 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_ */
+#endif /* BITTORRENT_MESSAGES_H_ */
index 632b19e..9219ee1 100644 (file)
@@ -90,31 +90,32 @@ void leech_loop(peer_t peer, double deadline)
   while (MSG_get_clock() < deadline && peer->pieces < FILE_PIECES) {
     if (peer->comm_received == NULL) {
       peer->task_received = NULL;
-      peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
+      peer->comm_received =
+       MSG_task_irecv(&peer->task_received, peer->mailbox);
     }
     if (MSG_comm_test(peer->comm_received)) {
       msg_error_t status = MSG_comm_get_status(peer->comm_received);
       MSG_comm_destroy(peer->comm_received);
       peer->comm_received = NULL;
       if (status == MSG_OK) {
-        handle_message(peer, peer->task_received);
+       handle_message(peer, peer->task_received);
       }
     } else {
       handle_pending_sends(peer);
       if (peer->current_piece != -1) {
-        send_interested_to_peers(peer);
+       send_interested_to_peers(peer);
       } else {
-        //If the current interested pieces is < MAX
-        if (peer->pieces_requested < MAX_PIECES) {
-          update_current_piece(peer);
-        }
+       //If the current interested pieces is < MAX
+       if (peer->pieces_requested < MAX_PIECES) {
+         update_current_piece(peer);
+       }
       }
       //We don't execute the choke algorithm if we don't already have a piece
       if (MSG_get_clock() >= next_choked_update && peer->pieces > 0) {
-        update_choked_peers(peer);
-        next_choked_update += UPDATE_CHOKED_INTERVAL;
+       update_choked_peers(peer);
+       next_choked_update += UPDATE_CHOKED_INTERVAL;
       } else {
-        MSG_process_sleep(1);
+       MSG_process_sleep(1);
       }
     }
   }
@@ -133,22 +134,23 @@ void seed_loop(peer_t peer, double deadline)
   while (MSG_get_clock() < deadline) {
     if (peer->comm_received == NULL) {
       peer->task_received = NULL;
-      peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
+      peer->comm_received =
+       MSG_task_irecv(&peer->task_received, peer->mailbox);
     }
     if (MSG_comm_test(peer->comm_received)) {
       msg_error_t status = MSG_comm_get_status(peer->comm_received);
       MSG_comm_destroy(peer->comm_received);
       peer->comm_received = NULL;
       if (status == MSG_OK) {
-        handle_message(peer, peer->task_received);
+       handle_message(peer, peer->task_received);
       }
     } else {
       if (MSG_get_clock() >= next_choked_update) {
-        update_choked_peers(peer);
-        //TODO: Change the choked peer algorithm when seeding.
-        next_choked_update += UPDATE_CHOKED_INTERVAL;
+       update_choked_peers(peer);
+       //TODO: Change the choked peer algorithm when seeding.
+       next_choked_update += UPDATE_CHOKED_INTERVAL;
       } else {
-        MSG_process_sleep(1);
+       MSG_process_sleep(1);
       }
     }
   }
@@ -164,8 +166,8 @@ int get_peers_data(peer_t peer)
   double timeout = MSG_get_clock() + GET_PEERS_TIMEOUT;
   //Build the task to send to the tracker
   tracker_task_data_t data =
-      tracker_task_data_new(MSG_host_get_name(MSG_host_self()),
-                            peer->mailbox_tracker, peer->id, 0, 0, FILE_SIZE);
+    tracker_task_data_new(MSG_host_get_name(MSG_host_self()),
+                         peer->mailbox_tracker, peer->id, 0, 0, FILE_SIZE);
   //Build the task to send.
   msg_task_t task_send = MSG_task_create(NULL, 0, TRACKER_COMM_SIZE, data);
   msg_task_t task_received = NULL;
@@ -173,8 +175,8 @@ int get_peers_data(peer_t peer)
   while (!send_success && MSG_get_clock() < timeout) {
     XBT_DEBUG("Sending a peer request to the tracker.");
     msg_error_t status =
-        MSG_task_send_with_timeout(task_send, TRACKER_MAILBOX,
-                                   GET_PEERS_TIMEOUT);
+      MSG_task_send_with_timeout(task_send, TRACKER_MAILBOX,
+                                GET_PEERS_TIMEOUT);
     if (status == MSG_OK) {
       send_success = 1;
     }
@@ -188,9 +190,9 @@ int get_peers_data(peer_t peer)
       int peer_id;
       //Add the peers the tracker gave us to our peer list.
       xbt_dynar_foreach(data->peers, i, peer_id) {
-        if (peer_id != peer->id)
-          xbt_dict_set_ext(peer->peers, (char *) &peer_id, sizeof(int),
-                           connection_new(peer_id), NULL);
+       if (peer_id != peer->id)
+         xbt_dict_set_ext(peer->peers, (char *) &peer_id, sizeof(int),
+                          connection_new(peer_id), NULL);
       }
       success = 1;
       //free the communication and the task
@@ -224,11 +226,11 @@ void peer_init(peer_t peer, int id, int seed)
   if (seed) {
     memset(peer->bitfield, '1', sizeof(char) * (FILE_PIECES + 1));
     memset(peer->bitfield_blocks, '1',
-           sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
+          sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
   } else {
     memset(peer->bitfield, '0', sizeof(char) * (FILE_PIECES + 1));
     memset(peer->bitfield_blocks, '0',
-           sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
+          sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
   }
 
   peer->bitfield[FILE_PIECES] = '\0';
@@ -274,7 +276,8 @@ void peer_free(peer_t peer)
  */
 int has_finished(char *bitfield)
 {
-  return ((memchr(bitfield, '0', sizeof(char) * FILE_PIECES) == NULL) ? 1 : 0);
+  return ((memchr(bitfield, '0', sizeof(char) * FILE_PIECES) ==
+          NULL) ? 1 : 0);
 }
 
 /**
@@ -286,10 +289,13 @@ void handle_pending_sends(peer_t peer)
   int index;
 
   while ((index = MSG_comm_testany(peer->pending_sends)) != -1) {
-    msg_comm_t comm_send = xbt_dynar_get_as(peer->pending_sends, index, msg_comm_t);
+    msg_comm_t comm_send =
+      xbt_dynar_get_as(peer->pending_sends, index, msg_comm_t);
     int status = MSG_comm_get_status(comm_send);
     xbt_dynar_remove_at(peer->pending_sends, index, &comm_send);
-    XBT_DEBUG("Communication %p is finished with status %d, dynar size is now %lu", comm_send, status, xbt_dynar_length(peer->pending_sends));
+    XBT_DEBUG
+      ("Communication %p is finished with status %d, dynar size is now %lu",
+       comm_send, status, xbt_dynar_length(peer->pending_sends));
 
     msg_task_t task = MSG_comm_get_task(comm_send);
     MSG_comm_destroy(comm_send);
@@ -310,17 +316,17 @@ void handle_message(peer_t peer, msg_task_t task)
   message_t message = MSG_task_get_data(task);
   connection_t remote_peer;
   remote_peer =
-      xbt_dict_get_or_null_ext(peer->peers, (char *) &message->peer_id,
-                               sizeof(int));
+    xbt_dict_get_or_null_ext(peer->peers, (char *) &message->peer_id,
+                            sizeof(int));
   switch (message->type) {
 
   case MESSAGE_HANDSHAKE:
     XBT_DEBUG("Received a HANDSHAKE from %s (%s)", message->mailbox,
-              message->issuer_host_name);
+             message->issuer_host_name);
     //Check if the peer is in our connection list.
     if (!remote_peer) {
       xbt_dict_set_ext(peer->peers, (char *) &message->peer_id, sizeof(int),
-                       connection_new(message->peer_id), NULL);
+                      connection_new(message->peer_id), NULL);
       send_handshake(peer, message->mailbox);
     }
     //Send our bitfield to the peer
@@ -328,7 +334,7 @@ void handle_message(peer_t peer, msg_task_t task)
     break;
   case MESSAGE_BITFIELD:
     XBT_DEBUG("Recieved a BITFIELD message from %s (%s)", message->mailbox,
-              message->issuer_host_name);
+             message->issuer_host_name);
     //Update the pieces list
     update_pieces_count_from_bitfield(peer, message->bitfield);
     //Store the bitfield
@@ -340,40 +346,40 @@ void handle_message(peer_t peer, msg_task_t task)
     break;
   case MESSAGE_INTERESTED:
     XBT_DEBUG("Recieved an INTERESTED message from %s (%s)", message->mailbox,
-              message->issuer_host_name);
+             message->issuer_host_name);
     xbt_assert((remote_peer != NULL),
-               "A non-in-our-list peer has sent us a message. WTH ?");
+              "A non-in-our-list peer has sent us a message. WTH ?");
     //Update the interested state of the peer.
     remote_peer->interested = 1;
     break;
   case MESSAGE_NOTINTERESTED:
-    XBT_DEBUG("Received a NOTINTERESTED message from %s (%s)", message->mailbox,
-              message->issuer_host_name);
+    XBT_DEBUG("Received a NOTINTERESTED message from %s (%s)",
+             message->mailbox, message->issuer_host_name);
     xbt_assert((remote_peer != NULL),
-               "A non-in-our-list peer has sent us a message. WTH ?");
+              "A non-in-our-list peer has sent us a message. WTH ?");
     remote_peer->interested = 0;
     break;
   case MESSAGE_UNCHOKE:
     xbt_assert((remote_peer != NULL),
-               "A non-in-our-list peer has sent us a message. WTH ?");
+              "A non-in-our-list peer has sent us a message. WTH ?");
     XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox,
-              message->issuer_host_name);
+             message->issuer_host_name);
     remote_peer->choked_download = 0;
     xbt_dict_set_ext(peer->active_peers, (char *) &message->peer_id,
-                     sizeof(int), remote_peer, NULL);
+                    sizeof(int), remote_peer, NULL);
     //Send requests to the peer, since it has unchoked us
     send_requests_to_peer(peer, remote_peer);
     break;
   case MESSAGE_CHOKE:
     xbt_assert((remote_peer != NULL),
-               "A non-in-our-list peer has sent us a message. WTH ?");
+              "A non-in-our-list peer has sent us a message. WTH ?");
     XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox,
-              message->issuer_host_name);
+             message->issuer_host_name);
     remote_peer->choked_download = 1;
     xbt_ex_t e;
     TRY {
       xbt_dict_remove_ext(peer->active_peers, (char *) &message->peer_id,
-                          sizeof(int));
+                         sizeof(int));
     }
     CATCH(e) {
       xbt_ex_free(e);
@@ -381,76 +387,78 @@ void handle_message(peer_t peer, msg_task_t task)
     break;
   case MESSAGE_HAVE:
     XBT_DEBUG("Received a HAVE message from %s (%s) of piece %d",
-              message->mailbox, message->issuer_host_name, message->index);
+             message->mailbox, message->issuer_host_name, message->index);
     xbt_assert((message->index >= 0
-                && message->index < FILE_PIECES),
-               "Wrong HAVE message received");
+               && message->index < FILE_PIECES),
+              "Wrong HAVE message received");
     if (remote_peer->bitfield == NULL)
       return;
     remote_peer->bitfield[message->index] = '1';
     peer->pieces_count[message->index]++;
     //If the piece is in our pieces, we tell the peer that we are interested.
-    if (!remote_peer->am_interested && in_current_pieces(peer, message->index)) {
+    if (!remote_peer->am_interested
+       && in_current_pieces(peer, message->index)) {
       remote_peer->am_interested = 1;
       send_interested(peer, remote_peer->mailbox);
     }
     break;
   case MESSAGE_REQUEST:
     xbt_assert((message->index >= 0
-                && message->index < FILE_PIECES), "Wrong request received");
+               && message->index < FILE_PIECES), "Wrong request received");
     if (!remote_peer->choked_upload) {
       XBT_DEBUG("Received a REQUEST from %s (%s) for %d (%d,%d)",
-                message->mailbox, message->issuer_host_name, message->peer_id,
-                message->block_index,
-                message->block_index + message->block_length);
+               message->mailbox, message->issuer_host_name, message->peer_id,
+               message->block_index,
+               message->block_index + message->block_length);
       if (peer->bitfield[message->index] == '1') {
-        send_piece(peer, message->mailbox, message->index, 0,
-                   message->block_index, message->block_length);
+       send_piece(peer, message->mailbox, message->index, 0,
+                  message->block_index, message->block_length);
       }
     } else {
       XBT_DEBUG("Received a REQUEST from %s (%s) for %d but he is choked.",
-                message->mailbox, message->issuer_host_name, message->peer_id);
+               message->mailbox, message->issuer_host_name,
+               message->peer_id);
     }
     break;
   case MESSAGE_PIECE:
     xbt_assert((message->index >= 0
-                && message->index < FILE_PIECES), "Wrong piece received");
+               && message->index < FILE_PIECES), "Wrong piece received");
     //TODO: Execute à computation.
     if (message->stalled) {
-      XBT_DEBUG("The received piece %d from %s (%s) is STALLED", message->index,
-                message->mailbox, message->issuer_host_name);
+      XBT_DEBUG("The received piece %d from %s (%s) is STALLED",
+               message->index, message->mailbox, message->issuer_host_name);
     } else {
       XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index,
-                message->block_index,
-                message->block_index + message->block_length, message->mailbox,
-                message->issuer_host_name);
+               message->block_index,
+               message->block_index + message->block_length,
+               message->mailbox, message->issuer_host_name);
       if (peer->bitfield[message->index] == '0') {
-        update_bitfield_blocks(peer, message->index, message->block_index,
-                               message->block_length);
-        if (piece_complete(peer, message->index)) {
-          peer->pieces_requested--;
-          //Removing the piece from our piece list
-          unsigned i;
-          int piece_index = -1, piece;
-          xbt_dynar_foreach(peer->current_pieces, i, piece) {
-            if (piece == message->index) {
-              piece_index = i;
-              break;
-            }
-          }
-          xbt_assert(piece_index != -1, "Received an incorrect piece");
-          xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL);
-          //Setting the fact that we have the piece
-          peer->bitfield[message->index] = '1';
-          peer->pieces++;
-          XBT_DEBUG("My status is now %s", peer->bitfield);
-          //Sending the information to all the peers we are connected to
-          send_have(peer, message->index);
-          //sending UNINTERSTED to peers that doesn't have what we want.
-          update_interested_after_receive(peer);
-        }
+       update_bitfield_blocks(peer, message->index, message->block_index,
+                              message->block_length);
+       if (piece_complete(peer, message->index)) {
+         peer->pieces_requested--;
+         //Removing the piece from our piece list
+         unsigned i;
+         int piece_index = -1, piece;
+         xbt_dynar_foreach(peer->current_pieces, i, piece) {
+           if (piece == message->index) {
+             piece_index = i;
+             break;
+           }
+         }
+         xbt_assert(piece_index != -1, "Received an incorrect piece");
+         xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL);
+         //Setting the fact that we have the piece
+         peer->bitfield[message->index] = '1';
+         peer->pieces++;
+         XBT_DEBUG("My status is now %s", peer->bitfield);
+         //Sending the information to all the peers we are connected to
+         send_have(peer, message->index);
+         //sending UNINTERSTED to peers that doesn't have what we want.
+         update_interested_after_receive(peer);
+       }
       } else {
-        XBT_DEBUG("However, we already have it");
+       XBT_DEBUG("However, we already have it");
       }
     }
     break;
@@ -460,8 +468,8 @@ void handle_message(peer_t peer, msg_task_t task)
   //Update the peer speed.
   if (remote_peer) {
     connection_add_speed_value(remote_peer,
-                               1.0 / (MSG_get_clock() -
-                                      peer->begin_receive_time));
+                              1.0 / (MSG_get_clock() -
+                                     peer->begin_receive_time));
   }
   peer->begin_receive_time = MSG_get_clock();
 
@@ -480,7 +488,8 @@ void wait_for_pieces(peer_t peer, double deadline)
   while (MSG_get_clock() < deadline && !finished) {
     if (peer->comm_received == NULL) {
       peer->task_received = NULL;
-      peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
+      peer->comm_received =
+       MSG_task_irecv(&peer->task_received, peer->mailbox);
     }
     msg_error_t status = MSG_comm_wait(peer->comm_received, TIMEOUT_MESSAGE);
     //free the comm already, we don't need it anymore
@@ -490,7 +499,7 @@ void wait_for_pieces(peer_t peer, double deadline)
       MSG_task_get_data(peer->task_received);
       handle_message(peer, peer->task_received);
       if (peer->current_piece != -1) {
-        finished = 1;
+       finished = 1;
       }
     }
   }
@@ -527,27 +536,27 @@ void update_current_piece(peer_t peer)
     int i = 0;
     do {
       peer->current_piece =
-          RngStream_RandInt(peer->stream, 0, FILE_PIECES - 1);;
+       RngStream_RandInt(peer->stream, 0, FILE_PIECES - 1);;
       i++;
     } while (!
-             (peer->bitfield[peer->current_piece] == '0'
-              && !in_current_pieces(peer, peer->current_piece)));
+            (peer->bitfield[peer->current_piece] == '0'
+             && !in_current_pieces(peer, peer->current_piece)));
   } else {
     //Trivial min algorithm.
     int i, min_id = -1;
     short min = -1;
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->bitfield[i] == '0') {
-        min = peer->pieces_count[i];
-        min_id = i;
-        break;
+       min = peer->pieces_count[i];
+       min_id = i;
+       break;
       }
     }
     xbt_assert((min > -1), "Couldn't find a minimum");
     for (i = 1; i < FILE_PIECES; i++) {
       if (peer->pieces_count[i] < min && peer->bitfield[i] == '0') {
-        min = peer->pieces_count[i];
-        min_id = i;
+       min = peer->pieces_count[i];
+       min_id = i;
       }
     }
     peer->current_piece = min_id;
@@ -555,7 +564,7 @@ void update_current_piece(peer_t peer)
   xbt_dynar_push_as(peer->current_pieces, int, peer->current_piece);
   XBT_DEBUG("New interested piece: %d", peer->current_piece);
   xbt_assert((peer->current_piece >= 0 && peer->current_piece < FILE_PIECES),
-             "Peer want to retrieve a piece that doesn't exist.");
+            "Peer want to retrieve a piece that doesn't exist.");
 }
 
 /**
@@ -583,7 +592,7 @@ void update_choked_peers(peer_t peer)
   }
   xbt_dict_cursor_free(&cursor);
 
-        /**
+       /**
         * If we are currently seeding, we unchoke the peer which has
         * been unchoke the least time.
         */
@@ -593,8 +602,8 @@ void update_choked_peers(peer_t peer)
 
     xbt_dict_foreach(peer->peers, cursor, key, connection) {
       if (connection->last_unchoke < unchoke_time && connection->interested) {
-        unchoke_time = connection->last_unchoke;
-        peer_choosed = connection;
+       unchoke_time = connection->last_unchoke;
+       peer_choosed = connection;
       }
     }
   } else {
@@ -602,35 +611,34 @@ void update_choked_peers(peer_t peer)
     if (peer->round == 0) {
       int j = 0;
       do {
-        //We choose a random peer to unchoke.
-        int id_chosen =
-            RngStream_RandInt(peer->stream, 0,
-                              xbt_dict_length(peer->peers) - 1);
-        int i = 0;
-        connection_t connection;
-        xbt_dict_foreach(peer->peers, cursor, key, connection) {
-          if (i == id_chosen) {
-            peer_choosed = connection;
-            break;
-          }
-          i++;
-        }
-        xbt_dict_cursor_free(&cursor);
-        if (peer_choosed->interested == 0) {
-          peer_choosed = NULL;
-        }
-        j++;
+       //We choose a random peer to unchoke.
+       int id_chosen = RngStream_RandInt(peer->stream, 0,
+                                         xbt_dict_length(peer->peers) - 1);
+       int i = 0;
+       connection_t connection;
+       xbt_dict_foreach(peer->peers, cursor, key, connection) {
+         if (i == id_chosen) {
+           peer_choosed = connection;
+           break;
+         }
+         i++;
+       }
+       xbt_dict_cursor_free(&cursor);
+       if (peer_choosed->interested == 0) {
+         peer_choosed = NULL;
+       }
+       j++;
       } while (peer_choosed == NULL && j < MAXIMUM_PAIRS);
     } else {
       //Use the "fastest download" policy.
       connection_t connection;
       double fastest_speed = 0.0;
       xbt_dict_foreach(peer->peers, cursor, key, connection) {
-        if (connection->peer_speed > fastest_speed && connection->choked_upload
-            && connection->interested) {
-          peer_choosed = connection;
-          fastest_speed = connection->peer_speed;
-        }
+       if (connection->peer_speed > fastest_speed
+           && connection->choked_upload && connection->interested) {
+         peer_choosed = connection;
+         fastest_speed = connection->peer_speed;
+       }
       }
     }
 
@@ -638,7 +646,7 @@ void update_choked_peers(peer_t peer)
   if (peer_choosed != NULL) {
     peer_choosed->choked_upload = 0;
     xbt_dict_set_ext(peer->active_peers, (char *) &peer_choosed->id,
-                     sizeof(int), peer_choosed, NULL);
+                    sizeof(int), peer_choosed, NULL);
     peer_choosed->last_unchoke = MSG_get_clock();
     send_unchoked(peer, peer_choosed->mailbox);
   }
@@ -662,28 +670,28 @@ void update_interested_after_receive(peer_t peer)
     if (connection->am_interested) {
       //Check if the peer still has a piece we want.
       xbt_dynar_foreach(peer->current_pieces, cpt, piece) {
-        xbt_assert((piece >= 0), "Wrong piece.");
-        if (connection->bitfield && connection->bitfield[piece] == '1') {
-          interested = 1;
-          break;
-        }
+       xbt_assert((piece >= 0), "Wrong piece.");
+       if (connection->bitfield && connection->bitfield[piece] == '1') {
+         interested = 1;
+         break;
+       }
       }
       if (!interested) {
-        connection->am_interested = 0;
-        send_notinterested(peer, connection->mailbox);
+       connection->am_interested = 0;
+       send_notinterested(peer, connection->mailbox);
       }
     }
   }
 }
 
 void update_bitfield_blocks(peer_t peer, int index, int block_index,
-                            int block_length)
+                           int block_length)
 {
   int i;
   xbt_assert((index >= 0 && index <= FILE_PIECES), "Wrong piece.");
   xbt_assert((block_index >= 0
-              && block_index <= PIECES_BLOCKS), "Wrong block : %d.",
-             block_index);
+             && block_index <= PIECES_BLOCKS), "Wrong block : %d.",
+            block_index);
   for (i = block_index; i < (block_index + block_length); i++) {
     peer->bitfield_blocks[index * PIECES_BLOCKS + i] = '1';
   }
@@ -730,11 +738,11 @@ void send_requests_to_peer(peer_t peer, connection_t remote_peer)
     if (remote_peer->bitfield && remote_peer->bitfield[piece] == '1') {
       block_index = get_first_block(peer, piece);
       if (block_index != -1) {
-        block_length = PIECES_BLOCKS - block_index;
-        block_length = min(BLOCKS_REQUESTED, block_length);
-        send_request(peer, remote_peer->mailbox, piece, block_index,
-                     block_length);
-        break;
+       block_length = PIECES_BLOCKS - block_index;
+       block_length = min(BLOCKS_REQUESTED, block_length);
+       send_request(peer, remote_peer->mailbox, piece, block_index,
+                    block_length);
+       break;
       }
     }
   }
@@ -750,14 +758,14 @@ void send_interested_to_peers(peer_t peer)
   xbt_dict_cursor_t cursor = NULL;
   connection_t connection;
   xbt_assert((peer->current_piece != -1),
-             "Tried to send a interested message wheras the current_piece is -1");
+            "Tried to send a interested message wheras the current_piece is -1");
   xbt_dict_foreach(peer->peers, cursor, key, connection) {
     if (connection->bitfield
-        && connection->bitfield[peer->current_piece] == '1') {
+       && connection->bitfield[peer->current_piece] == '1') {
       connection->am_interested = 1;
       msg_task_t task =
-          task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
-                           peer->id, task_message_size(MESSAGE_INTERESTED));
+       task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
+                        peer->id, task_message_size(MESSAGE_INTERESTED));
       MSG_task_dsend(task, connection->mailbox, task_message_free);
       XBT_DEBUG("Send INTERESTED to %s", connection->mailbox);
     }
@@ -774,8 +782,8 @@ void send_interested_to_peers(peer_t peer)
 void send_interested(peer_t peer, const char *mailbox)
 {
   msg_task_t task =
-      task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
-                       peer->id, task_message_size(MESSAGE_INTERESTED));
+    task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
+                    peer->id, task_message_size(MESSAGE_INTERESTED));
   MSG_task_dsend(task, mailbox, task_message_free);
   XBT_DEBUG("Sending INTERESTED to %s", mailbox);
 
@@ -789,8 +797,8 @@ void send_interested(peer_t peer, const char *mailbox)
 void send_notinterested(peer_t peer, const char *mailbox)
 {
   msg_task_t task =
-      task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox,
-                       peer->id, task_message_size(MESSAGE_NOTINTERESTED));
+    task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox,
+                    peer->id, task_message_size(MESSAGE_NOTINTERESTED));
   MSG_task_dsend(task, mailbox, task_message_free);
   XBT_DEBUG("Sending NOTINTERESTED to %s", mailbox);
 
@@ -807,8 +815,8 @@ void send_handshake_all(peer_t peer)
   char *key;
   xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
     msg_task_t task =
-        task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
-                         peer->id, task_message_size(MESSAGE_HANDSHAKE));
+      task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
+                      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);
   }
@@ -822,8 +830,8 @@ void send_handshake_all(peer_t peer)
 void send_handshake(peer_t peer, const char *mailbox)
 {
   msg_task_t task =
-      task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
-                       peer->id, task_message_size(MESSAGE_HANDSHAKE));
+    task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
+                    peer->id, task_message_size(MESSAGE_HANDSHAKE));
   MSG_task_dsend(task, mailbox, task_message_free);
   XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox);
 }
@@ -835,8 +843,8 @@ void send_choked(peer_t peer, const char *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_size(MESSAGE_CHOKE));
+    task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox,
+                    peer->id, task_message_size(MESSAGE_CHOKE));
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
@@ -847,8 +855,8 @@ 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,
-                       peer->id, task_message_size(MESSAGE_UNCHOKE));
+    task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox,
+                    peer->id, task_message_size(MESSAGE_UNCHOKE));
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
@@ -863,8 +871,9 @@ void send_have(peer_t peer, int piece)
   char *key;
   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, task_message_size(MESSAGE_HAVE));
+      task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox,
+                            peer->id, piece,
+                            task_message_size(MESSAGE_HAVE));
     MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
   }
 }
@@ -877,8 +886,8 @@ 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,
-                                peer->bitfield, FILE_PIECES);
+    task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id,
+                             peer->bitfield, FILE_PIECES);
   //Async send and append to pending sends
   msg_comm_t comm = MSG_task_isend(task, mailbox);
   xbt_dynar_push(peer->pending_sends, &comm);
@@ -887,14 +896,14 @@ void send_bitfield(peer_t peer, const char *mailbox)
 /**
  * Send a "request" message to a pair, containing a request for a piece
  */
-void send_request(peer_t peer, const char *mailbox, int piece, int block_index,
-                  int block_length)
+void send_request(peer_t peer, const char *mailbox, int piece,
+                 int block_index, int block_length)
 {
   XBT_DEBUG("Sending a REQUEST to %s for piece %d (%d,%d)", mailbox, piece,
-            block_index, block_length);
+           block_index, block_length);
   msg_task_t task =
-      task_message_request_new(peer->hostname, peer->mailbox, peer->id, piece,
-                               block_index, block_length);
+    task_message_request_new(peer->hostname, peer->mailbox, peer->id, piece,
+                            block_index, block_length);
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
@@ -902,16 +911,16 @@ void send_request(peer_t peer, const char *mailbox, int piece, int block_index,
  * Send a "piece" message to a pair, containing a piece of the file
  */
 void send_piece(peer_t peer, const char *mailbox, int piece, int stalled,
-                int block_index, int block_length)
+               int block_index, int block_length)
 {
   XBT_DEBUG("Sending the PIECE %d (%d,%d) to %s", piece, block_index,
-            block_length, mailbox);
+           block_length, mailbox);
   xbt_assert(piece >= 0, "Tried to send a piece that doesn't exist.");
   xbt_assert((peer->bitfield[piece] == '1'),
-             "Tried to send a piece that we doesn't have.");
+            "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, BLOCK_SIZE);
+    task_message_piece_new(peer->hostname, peer->mailbox, peer->id, piece,
+                          stalled, block_index, block_length, BLOCK_SIZE);
   MSG_task_dsend(task, mailbox, task_message_free);
 }
 
index b468774..1edac8a 100644 (file)
  * Peer data
  */
 typedef struct s_peer {
-  int id;                       //peer id
+  int id;                      //peer id
 
-  int pieces;                   //number of pieces the peer has.
-  char *bitfield;               //list of pieces the peer has.
-  char *bitfield_blocks;        //list of blocks the peer has.
-  short *pieces_count;          //number of peers that have each piece.
+  int pieces;                  //number of pieces the peer has.
+  char *bitfield;              //list of pieces the peer has.
+  char *bitfield_blocks;       //list of blocks the peer has.
+  short *pieces_count;         //number of peers that have each piece.
 
-  xbt_dynar_t current_pieces;   //current pieces the peer is downloading
-  int current_piece;            //current pieces
-  int pieces_requested;         //number of pieces the peer has requested
+  xbt_dynar_t current_pieces;  //current pieces the peer is downloading
+  int current_piece;           //current pieces
+  int pieces_requested;                //number of pieces the peer has requested
 
-  xbt_dict_t peers;             //peers list
-  xbt_dict_t active_peers;      //active peers list
+  xbt_dict_t peers;            //peers list
+  xbt_dict_t active_peers;     //active peers list
 
-  char mailbox[MAILBOX_SIZE];   //peer mailbox.
-  char mailbox_tracker[MAILBOX_SIZE];   //pair mailbox while communicating with the tracker.
-  const char *hostname;         //peer hostname
+  char mailbox[MAILBOX_SIZE];  //peer mailbox.
+  char mailbox_tracker[MAILBOX_SIZE];  //pair mailbox while communicating with the tracker.
+  const char *hostname;                //peer hostname
 
-  msg_task_t task_received;     //current task being received
-  msg_comm_t comm_received;     //current comm
+  msg_task_t task_received;    //current task being received
+  msg_comm_t comm_received;    //current comm
 
-  int round;                    //current round for the chocking algortihm.
+  int round;                   //current round for the chocking algortihm.
 
-  RngStream stream;             //RngStream for
+  RngStream stream;            //RngStream for
 
-  double begin_receive_time;    //time when the receiving communication has begun, useful for calculating host speed.
+  double begin_receive_time;   //time when the receiving communication has begun, useful for calculating host speed.
 
-  xbt_dynar_t pending_sends;    // list of sends being delivered
+  xbt_dynar_t pending_sends;   // list of sends being delivered
 } s_peer_t, *peer_t;
 
 /**
@@ -72,7 +72,7 @@ void update_choked_peers(peer_t peer);
 void update_interested_after_receive(peer_t peer);
 
 void update_bitfield_blocks(peer_t peer, int index, int block_index,
-                            int block_length);
+                           int block_length);
 int piece_complete(peer_t peer, int index);
 int get_first_block(peer_t peer, int piece);
 
@@ -91,10 +91,10 @@ void send_choked(peer_t peer, const char *mailbox);
 void send_unchoked(peer_t peer, const char *mailbox);
 void send_have(peer_t peer, int piece);
 
-void send_request(peer_t peer, const char *mailbox, int piece, int block_index,
-                  int block_length);
+void send_request(peer_t peer, const char *mailbox, int piece,
+                 int block_index, int block_length);
 void send_piece(peer_t peer, const char *mailbox, int piece, int stalled,
-                int block_index, int block_length);
+               int block_index, int block_length);
 
 int in_current_pieces(peer_t peer, int piece);
-#endif                          /* BITTORRENT_PEER_H */
+#endif /* BITTORRENT_PEER_H */
index 169b5d6..740ede5 100644 (file)
@@ -9,7 +9,8 @@
 
 static void task_free(void *data);
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(msg_tracker, "Messages specific for the tracker");
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_tracker,
+                            "Messages specific for the tracker");
 /**
  * Tracker main function
  * @param argc number of arguments
@@ -41,29 +42,29 @@ int tracker(int argc, char *argv[])
     if (MSG_comm_test(comm_received)) {
       //Check for correct status
       if (MSG_comm_get_status(comm_received) == MSG_OK) {
-        //Retrieve the data sent by the peer.
-        tracker_task_data_t data = MSG_task_get_data(task_received);
-        //Add the peer to our peer list.
-        if (!is_in_list(peers_list, data->peer_id)) {
-          xbt_dynar_push_as(peers_list, int, data->peer_id);
-        }
-        //Sending peers to the peer
-        int next_peer;
-        int peers_length = xbt_dynar_length(peers_list);
-        for (i = 0; i < MAXIMUM_PAIRS && i < peers_length; i++) {
-          do {
-            next_peer =
-                xbt_dynar_get_as(peers_list,
-                                 RngStream_RandInt(stream, 0, peers_length - 1),
-                                 int);
-          } while (is_in_list(data->peers, next_peer));
-          xbt_dynar_push_as(data->peers, int, next_peer);
-        }
-        //setting the interval
-        data->interval = TRACKER_QUERY_INTERVAL;
-        //sending the task back to the peer.
-        MSG_task_dsend(task_received, data->mailbox, task_free);
-        //destroy the communication.
+       //Retrieve the data sent by the peer.
+       tracker_task_data_t data = MSG_task_get_data(task_received);
+       //Add the peer to our peer list.
+       if (!is_in_list(peers_list, data->peer_id)) {
+         xbt_dynar_push_as(peers_list, int, data->peer_id);
+       }
+       //Sending peers to the peer
+       int next_peer;
+       int peers_length = xbt_dynar_length(peers_list);
+       for (i = 0; i < MAXIMUM_PAIRS && i < peers_length; i++) {
+         do {
+           next_peer =
+             xbt_dynar_get_as(peers_list,
+                              RngStream_RandInt(stream, 0, peers_length - 1),
+                              int);
+         } while (is_in_list(data->peers, next_peer));
+         xbt_dynar_push_as(data->peers, int, next_peer);
+       }
+       //setting the interval
+       data->interval = TRACKER_QUERY_INTERVAL;
+       //sending the task back to the peer.
+       MSG_task_dsend(task_received, data->mailbox, task_free);
+       //destroy the communication.
       }
       MSG_comm_destroy(comm_received);
       comm_received = NULL;
@@ -89,9 +90,9 @@ int tracker(int argc, char *argv[])
  * @param issuer_host_name Hostname of the issuer. For debugging purposes
  */
 tracker_task_data_t tracker_task_data_new(const char *issuer_host_name,
-                                          const char *mailbox, int peer_id,
-                                          int uploaded, int downloaded,
-                                          int left)
+                                         const char *mailbox, int peer_id,
+                                         int uploaded, int downloaded,
+                                         int left)
 {
   tracker_task_data_t task = xbt_new(s_tracker_task_data_t, 1);
 
index b8752ad..f4922b7 100644 (file)
@@ -23,24 +23,24 @@ typedef enum {
  * Tasks exchanged between a tracker and peers.
  */
 typedef struct s_tracker_task_data {
-  e_tracker_task_type_t type;   //type of the task
-  const char *mailbox;          //mailbox where the tracker should answer
-  const char *issuer_host_name; //hostname, for debug purposes
+  e_tracker_task_type_t type;  //type of the task
+  const char *mailbox;         //mailbox where the tracker should answer
+  const char *issuer_host_name;        //hostname, for debug purposes
   //Query data
-  int peer_id;                  //peer id
-  int uploaded;                 //how much the peer has already uploaded
-  int downloaded;               //how much the peer has downloaded
-  int left;                     //how much the peer has left
+  int peer_id;                 //peer id
+  int uploaded;                        //how much the peer has already uploaded
+  int downloaded;              //how much the peer has downloaded
+  int left;                    //how much the peer has left
   //Answer data
-  int interval;                 //how often the peer should contact the tracker (unused for now)
-  xbt_dynar_t peers;            //the peer list the peer has asked for.
+  int interval;                        //how often the peer should contact the tracker (unused for now)
+  xbt_dynar_t peers;           //the peer list the peer has asked for.
 } s_tracker_task_data_t, *tracker_task_data_t;
 
 tracker_task_data_t tracker_task_data_new(const char *issuer_host_name,
-                                          const char *mailbox, int peer_id,
-                                          int uploaded, int downloaded,
-                                          int left);
+                                         const char *mailbox, int peer_id,
+                                         int uploaded, int downloaded,
+                                         int left);
 void tracker_task_data_free(tracker_task_data_t task);
 
 int is_in_list(xbt_dynar_t peers, int id);
-#endif                          /* BITTORRENT_TRACKER_H */
+#endif /* BITTORRENT_TRACKER_H */