Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Refactorize the sthread interception of functions
[simgrid.git] / examples / c / app-bittorrent / bittorrent-peer.c
index 408d3f136dd57478fb2473ca4b274ec8fc775f17..55e2ed213d418012b65f6fe8b040238607ac952e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2012-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -6,6 +6,7 @@
 #include "bittorrent-peer.h"
 #include "tracker.h"
 #include <simgrid/forward.h>
+#include <xbt/ex.h>
 
 #include <limits.h>
 #include <stdio.h> /* snprintf */
@@ -26,6 +27,9 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(bittorrent_peers, "Messages specific for the peers"
 #define SLEEP_DURATION 1
 #define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0)
 
+const char* const message_type_names[10] = {"HANDSHAKE", "CHOKE",    "UNCHOKE", "INTERESTED", "NOTINTERESTED",
+                                            "HAVE",      "BITFIELD", "REQUEST", "PIECE",      "CANCEL"};
+
 #ifndef MIN
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #endif
@@ -73,20 +77,20 @@ static void peer_free(peer_t peer)
 }
 
 /** Peer main function */
-void peer(int argc, char* argv[])
+void peer_run(int argc, char* argv[])
 {
   // Check arguments
   xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
 
   // Build peer object
-  peer_t peer = peer_init((int)xbt_str_parse_int(argv[1], "Invalid ID: %s"), argc == 4 ? 1 : 0);
+  peer_t peer = peer_init((int)xbt_str_parse_int(argv[1], "Invalid ID"), argc == 4 ? 1 : 0);
 
   // Retrieve deadline
-  peer->deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
+  peer->deadline = xbt_str_parse_double(argv[2], "Invalid deadline");
   xbt_assert(peer->deadline > 0, "Wrong deadline supplied");
 
   char* status = xbt_malloc0(FILE_PIECES + 1);
-  get_status(&status, peer->bitfield);
+  get_status(status, peer->bitfield);
 
   XBT_INFO("Hi, I'm joining the network with id %d", peer->id);
 
@@ -106,7 +110,7 @@ void peer(int argc, char* argv[])
     XBT_INFO("Couldn't contact the tracker.");
   }
 
-  get_status(&status, peer->bitfield);
+  get_status(status, peer->bitfield);
   XBT_INFO("Here is my current status: %s", status);
   if (peer->comm_received) {
     sg_comm_unref(peer->comm_received);
@@ -173,8 +177,7 @@ void send_handshake_to_all_peers(const_peer_t peer)
 
 void send_message(const_peer_t peer, sg_mailbox_t mailbox, e_message_type type, uint64_t size)
 {
-  const char* type_names[6] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", "CANCEL"};
-  XBT_DEBUG("Sending %s to %s", type_names[type], sg_mailbox_get_name(mailbox));
+  XBT_DEBUG("Sending %s to %s", message_type_names[type], sg_mailbox_get_name(mailbox));
   message_t message = message_other_new(type, peer->id, peer->mailbox, peer->bitfield);
   sg_comm_t comm    = sg_mailbox_put_init(mailbox, message, size);
   sg_comm_detach(comm, NULL);
@@ -230,11 +233,11 @@ void send_request_to_peer(const_peer_t peer, connection_t remote_peer, int piece
   }
 }
 
-void get_status(char** status, unsigned int bitfield)
+void get_status(char* status, unsigned int bitfield)
 {
   for (int i = FILE_PIECES - 1; i >= 0; i--)
-    (*status)[i] = (bitfield & (1U << i)) ? '1' : '0';
-  (*status)[FILE_PIECES] = '\0';
+    status[i] = (bitfield & (1U << i)) ? '1' : '0';
+  status[FILE_PIECES] = '\0';
 }
 
 int has_finished(unsigned int bitfield)
@@ -251,7 +254,7 @@ int is_interested(const_peer_t peer, const_connection_t remote_peer)
 /** Indicates if the remote peer has a piece not stored by the local peer nor requested by the local peer */
 int is_interested_and_free(const_peer_t peer, const_connection_t remote_peer)
 {
-  for (int i = 0; i < FILE_PIECES; i++)
+  for (unsigned int i = 0; i < FILE_PIECES; i++)
     if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) && peer_is_not_downloading_piece(peer, i))
       return 1;
   return 0;
@@ -260,14 +263,14 @@ int is_interested_and_free(const_peer_t peer, const_connection_t remote_peer)
 /** @brief Updates the list of who has a piece from a bitfield */
 void update_pieces_count_from_bitfield(const_peer_t peer, unsigned int bitfield)
 {
-  for (int i = 0; i < FILE_PIECES; i++)
+  for (unsigned int i = 0; i < FILE_PIECES; i++)
     if (bitfield & (1U << i))
       peer->pieces_count[i]++;
 }
 
-int count_pieces(unsigned int bitfield)
+unsigned int count_pieces(unsigned int bitfield)
 {
-  int count      = 0;
+  unsigned int count      = 0;
   unsigned int n = bitfield;
   while (n) {
     count += n & 1U;
@@ -364,9 +367,8 @@ void update_active_peers_set(const s_peer_t* peer, connection_t remote_peer)
 /** @brief Handle a received message sent by another peer */
 void handle_message(peer_t peer, message_t message)
 {
-  const char* type_names[10] = {"HANDSHAKE", "CHOKE",    "UNCHOKE", "INTERESTED", "NOTINTERESTED",
-                                "HAVE",      "BITFIELD", "REQUEST", "PIECE",      "CANCEL"};
-  XBT_DEBUG("Received a %s message from %s", type_names[message->type], sg_mailbox_get_name(message->return_mailbox));
+  XBT_DEBUG("Received a %s message from %s", message_type_names[message->type],
+            sg_mailbox_get_name(message->return_mailbox));
 
   connection_t remote_peer = xbt_dict_get_or_null_ext(peer->connected_peers, (char*)&message->peer_id, sizeof(int));
   xbt_assert(remote_peer != NULL || message->type == MESSAGE_HANDSHAKE,
@@ -422,7 +424,7 @@ void handle_message(peer_t peer, message_t message)
       break;
     case MESSAGE_HAVE:
       XBT_DEBUG("\t for piece %d", message->piece);
-      xbt_assert((message->piece >= 0 && message->piece < FILE_PIECES), "Wrong HAVE message received");
+      xbt_assert((message->piece >= 0 && (unsigned)message->piece < FILE_PIECES), "Wrong HAVE message received");
       remote_peer->bitfield = remote_peer->bitfield | (1U << message->piece);
       peer->pieces_count[message->piece]++;
       // If the piece is in our pieces, we tell the peer that we are interested.
@@ -435,7 +437,7 @@ void handle_message(peer_t peer, message_t message)
       break;
     case MESSAGE_REQUEST:
       xbt_assert(remote_peer->interested);
-      xbt_assert((message->piece >= 0 && message->piece < FILE_PIECES), "Wrong request received");
+      xbt_assert((message->piece >= 0 && (unsigned)message->piece < FILE_PIECES), "Wrong request received");
       if (remote_peer->choked_upload == 0) {
         XBT_DEBUG("\t for piece %d (%d,%d)", message->piece, message->block_index,
                   message->block_index + message->block_length);
@@ -451,8 +453,8 @@ void handle_message(peer_t peer, message_t message)
                 message->block_index + message->block_length);
       xbt_assert(!remote_peer->choked_download);
       xbt_assert(remote_peer->choked_download != 1, "Can't received a piece if I'm choked !");
-      xbt_assert((message->piece >= 0 && message->piece < FILE_PIECES), "Wrong piece received");
-      // TODO: Execute à computation.
+      xbt_assert((message->piece >= 0 && (unsigned)message->piece < FILE_PIECES), "Wrong piece received");
+      // TODO: Execute a computation.
       if (peer_has_not_piece(peer, message->piece)) {
         update_bitfield_blocks(peer, message->piece, message->block_index, message->block_length);
         if (piece_complete(peer, message->piece)) {
@@ -461,7 +463,7 @@ void handle_message(peer_t peer, message_t message)
           // Setting the fact that we have the piece
           peer->bitfield = peer->bitfield | (1U << message->piece);
           char* status   = xbt_malloc0(FILE_PIECES + 1);
-          get_status(&status, peer->bitfield);
+          get_status(status, peer->bitfield);
           XBT_DEBUG("My status is now %s", status);
           xbt_free(status);
           // Sending the information to all the peers we are connected to
@@ -527,7 +529,7 @@ int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer)
       (is_interested(peer, remote_peer) != 0)) {
     int nb_interesting_pieces = 0;
     // compute the number of interesting pieces
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) {
         nb_interesting_pieces++;
       }
@@ -536,7 +538,7 @@ int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer)
     // get a random interesting piece
     int random_piece_index = rand() % nb_interesting_pieces;
     int current_index      = 0;
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) {
         if (random_piece_index == current_index) {
           piece = i;
@@ -552,7 +554,7 @@ int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer)
   if (count_pieces(peer->bitfield) < 4 && (is_interested_and_free(peer, remote_peer) != 0)) {
     int nb_interesting_pieces = 0;
     // compute the number of interesting pieces
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
           peer_is_not_downloading_piece(peer, i)) {
         nb_interesting_pieces++;
@@ -562,7 +564,7 @@ int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer)
     // get a random interesting piece
     int random_piece_index = rand() % nb_interesting_pieces;
     int current_index      = 0;
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
           peer_is_not_downloading_piece(peer, i)) {
         if (random_piece_index == current_index) {
@@ -579,14 +581,14 @@ int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer)
     int nb_min_pieces = 0;
     int current_index = 0;
     // compute the smallest number of copies of available pieces
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer->pieces_count[i] < min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
           peer_is_not_downloading_piece(peer, i))
         min = peer->pieces_count[i];
     }
     xbt_assert(min != SHRT_MAX || (is_interested_and_free(peer, remote_peer) == 0));
     // compute the number of rarest pieces
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer->pieces_count[i] == min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
           peer_is_not_downloading_piece(peer, i))
         nb_min_pieces++;
@@ -597,7 +599,7 @@ int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer)
     if (nb_min_pieces > 0) {
       random_rarest_index = rand() % nb_min_pieces;
     }
-    for (int i = 0; i < FILE_PIECES; i++) {
+    for (unsigned int i = 0; i < FILE_PIECES; i++) {
       if (peer->pieces_count[i] == min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
           peer_is_not_downloading_piece(peer, i)) {
         if (random_rarest_index == current_index) {
@@ -721,7 +723,7 @@ void update_interested_after_receive(const_peer_t peer)
     if (connection->am_interested != 0) {
       int interested = 0;
       // Check if the peer still has a piece we want.
-      for (int i = 0; i < FILE_PIECES; i++) {
+      for (unsigned int i = 0; i < FILE_PIECES; i++) {
         if (peer_has_not_piece(peer, i) && connection_has_piece(connection, i)) {
           interested = 1;
           break;
@@ -737,8 +739,8 @@ void update_interested_after_receive(const_peer_t peer)
 
 void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length)
 {
-  xbt_assert((index >= 0 && index <= FILE_PIECES), "Wrong piece.");
-  xbt_assert((block_index >= 0 && block_index <= PIECES_BLOCKS), "Wrong block : %d.", block_index);
+  xbt_assert((index >= 0 && (unsigned)index <= FILE_PIECES), "Wrong piece.");
+  xbt_assert((block_index >= 0 && (unsigned)block_index <= PIECES_BLOCKS), "Wrong block : %d.", block_index);
   for (int i = block_index; i < (block_index + block_length); i++) {
     peer->bitfield_blocks |= (1ULL << (unsigned int)(index * PIECES_BLOCKS + i));
   }
@@ -747,7 +749,7 @@ void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_l
 /** Returns if a peer has completed the download of a piece */
 int piece_complete(const_peer_t peer, int index)
 {
-  for (int i = 0; i < PIECES_BLOCKS; i++) {
+  for (unsigned int i = 0; i < PIECES_BLOCKS; i++) {
     if (!(peer->bitfield_blocks & 1ULL << (index * PIECES_BLOCKS + i))) {
       return 0;
     }
@@ -758,7 +760,7 @@ int piece_complete(const_peer_t peer, int index)
 /** Returns the first block that a peer doesn't have in a piece. If the peer has all blocks of the piece, returns -1. */
 int get_first_missing_block_from(const_peer_t peer, int piece)
 {
-  for (int i = 0; i < PIECES_BLOCKS; i++) {
+  for (unsigned int i = 0; i < PIECES_BLOCKS; i++) {
     if (!(peer->bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i))) {
       return i;
     }
@@ -769,7 +771,7 @@ int get_first_missing_block_from(const_peer_t peer, int piece)
 /** Returns a piece that is partially downloaded and stored by the remote peer if any -1 otherwise. */
 int partially_downloaded_piece(const_peer_t peer, const_connection_t remote_peer)
 {
-  for (int i = 0; i < FILE_PIECES; i++) {
+  for (unsigned int i = 0; i < FILE_PIECES; i++) {
     if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) && peer_is_not_downloading_piece(peer, i) &&
         get_first_missing_block_from(peer, i) > 0)
       return i;