Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sonar treats
[simgrid.git] / examples / msg / app-bittorrent / peer.c
index 5555e86..959e523 100644 (file)
@@ -17,8 +17,8 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers");
  * User parameters for transferred file data. For the test, the default values are :
  * File size: 10 pieces * 5 blocks/piece * 16384 bytes/block = 819200 bytes
  */
-#define FILE_PIECES  10
-#define PIECES_BLOCKS 5
+#define FILE_PIECES  10U
+#define PIECES_BLOCKS 5U
 #define BLOCK_SIZE  16384
 static const unsigned long int FILE_SIZE = FILE_PIECES * PIECES_BLOCKS * BLOCK_SIZE;
 
@@ -167,7 +167,7 @@ void seed_loop(peer_t peer, double deadline)
   }
 }
 
-/** @brief Retrieves the peer list from the tracker
+/** @brief Retrieves the peer list from the tracker
  *  @param peer current peer data
  */
 int get_peers_data(peer_t peer)
@@ -263,7 +263,7 @@ void peer_free(peer_t peer)
   xbt_free(peer);
 }
 
-/** @brief Returns if a peer has finished downloading the file
+/** @brief Returns if a peer has finished downloading the file
  *  @param bitfield peer bitfield
  */
 int has_finished(unsigned int bitfield)
@@ -353,7 +353,8 @@ void handle_message(peer_t peer, msg_task_t task)
     xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?");
     xbt_assert(!remote_peer->choked_download);
     remote_peer->choked_download = 1;
-    remove_current_piece(peer, remote_peer, remote_peer->current_piece);
+    if (remote_peer->current_piece != -1)
+      remove_current_piece(peer, remote_peer, remote_peer->current_piece);
     break;
   case MESSAGE_HAVE:
     XBT_DEBUG("\t for piece %d", message->index);
@@ -453,7 +454,7 @@ void remove_current_piece(peer_t peer, connection_t remote_peer, unsigned int cu
 void update_pieces_count_from_bitfield(peer_t peer, unsigned int bitfield)
 {
   for (int i = 0; i < FILE_PIECES; i++) {
-    if ((bitfield & (1U << i))) {
+    if (bitfield & (1U << i)) {
       peer->pieces_count[i]++;
     }
   }
@@ -671,10 +672,9 @@ void update_interested_after_receive(peer_t peer)
   char *key;
   xbt_dict_cursor_t cursor;
   connection_t connection;
-  int interested;
   xbt_dict_foreach(peer->peers, cursor, key, connection) {
-    interested = 0;
     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++) {
         if (peer_has_not_piece(peer, i) && connection_has_piece(connection,i)) {