Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / examples / c / app-bittorrent / bittorrent-peer.c
index 05d4e2f..32f66ab 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2012-2021. 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
@@ -64,7 +68,7 @@ static void peer_free(peer_t peer)
   connection_t connection;
   xbt_dict_cursor_t cursor;
   xbt_dict_foreach (peer->connected_peers, cursor, key, connection)
-    connection_free(connection);
+    xbt_free(connection);
 
   xbt_dict_free(&peer->connected_peers);
   xbt_dict_free(&peer->active_peers);
@@ -79,14 +83,14 @@ void peer(int argc, char* argv[])
   xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
 
   // Build peer object
-  peer_t peer = peer_init(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);
@@ -135,11 +139,10 @@ int get_peers_from_tracker(const_peer_t peer)
   }
 
   void* message           = NULL;
-  tracker_answer_t ta     = NULL;
   sg_comm_t comm_received = sg_mailbox_get_async(peer->mailbox, &message);
   res                     = sg_comm_wait_for(comm_received, GET_PEERS_TIMEOUT);
   if (res == SG_OK) {
-    ta = (tracker_answer_t)message;
+    const_tracker_answer_t ta = (const_tracker_answer_t)message;
     // Add the peers the tracker gave us to our peer list.
     unsigned i;
     int peer_id;
@@ -174,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);
@@ -185,7 +187,7 @@ void send_message(const_peer_t peer, sg_mailbox_t mailbox, e_message_type type,
 void send_bitfield(const_peer_t peer, sg_mailbox_t mailbox)
 {
   XBT_DEBUG("Sending a BITFIELD to %s", sg_mailbox_get_name(mailbox));
-  message_t message = message_bitfield_new(peer->id, peer->mailbox, peer->bitfield);
+  message_t message = message_other_new(MESSAGE_BITFIELD, peer->id, peer->mailbox, peer->bitfield);
   sg_comm_t comm    = sg_mailbox_put_init(mailbox, message, MESSAGE_BITFIELD_SIZE + BITS_TO_BYTES(FILE_PIECES));
   sg_comm_detach(comm, NULL);
 }
@@ -231,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)
@@ -308,7 +310,7 @@ void leech(peer_t peer)
     if (sg_comm_test(peer->comm_received)) {
       peer->message = (message_t)data;
       handle_message(peer, peer->message);
-      message_free(peer->message);
+      xbt_free(peer->message);
       peer->comm_received = NULL;
     } else {
       // We don't execute the choke algorithm if we don't already have a piece
@@ -338,7 +340,7 @@ void seed(peer_t peer)
     if (sg_comm_test(peer->comm_received)) {
       peer->message = (message_t)data;
       handle_message(peer, peer->message);
-      message_free(data);
+      xbt_free(peer->message);
       peer->comm_received = NULL;
     } else {
       if (simgrid_get_clock() >= next_choked_update) {
@@ -365,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,
@@ -462,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
@@ -814,12 +815,6 @@ void connection_add_speed_value(connection_t connection, double speed)
   connection->peer_speed = connection->peer_speed * 0.6 + speed * 0.4;
 }
 
-void connection_free(void* data)
-{
-  connection_t co = (connection_t)data;
-  xbt_free(co);
-}
-
 int connection_has_piece(const_connection_t connection, unsigned int piece)
 {
   return (connection->bitfield & 1U << piece);
@@ -844,13 +839,6 @@ message_t message_index_new(e_message_type type, int peer_id, sg_mailbox_t retur
   return message;
 }
 
-message_t message_bitfield_new(int peer_id, sg_mailbox_t return_mailbox, unsigned int bitfield)
-{
-  message_t message = message_new(MESSAGE_BITFIELD, peer_id, return_mailbox);
-  message->bitfield = bitfield;
-  return message;
-}
-
 message_t message_other_new(e_message_type type, int peer_id, sg_mailbox_t return_mailbox, unsigned int bitfield)
 {
   message_t message = message_new(type, peer_id, return_mailbox);
@@ -873,9 +861,3 @@ message_t message_piece_new(int peer_id, sg_mailbox_t return_mailbox, int piece,
   message->block_length = block_length;
   return message;
 }
-
-void message_free(void* task)
-{
-  message_t message = (message_t)task;
-  xbt_free(message);
-}