Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / c / app-bittorrent / bittorrent-peer.c
index 5e2926e..b08b01d 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. */
@@ -26,6 +26,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
@@ -79,7 +82,7 @@ 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: %s"), argc == 4 ? 1 : 0);
 
   // Retrieve deadline
   peer->deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
@@ -138,7 +141,7 @@ int get_peers_from_tracker(const_peer_t peer)
   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) {
-    const tracker_answer_t 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;
@@ -173,8 +176,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);
@@ -364,9 +366,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,