Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Endgame mode is always enabled, remove option.
[simgrid.git] / teshsuite / msg / app-bittorrent / bittorrent-peer.c
index 69818a5..e2eb994 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2018. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2012-2019. 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. */
@@ -8,7 +8,6 @@
 #include "connection.h"
 #include "tracker.h"
 #include <simgrid/msg.h>
-#include <xbt/RngStream.h>
 
 #include <limits.h>
 #include <stdio.h> /* snprintf */
@@ -27,7 +26,6 @@ static const unsigned long int FILE_SIZE = FILE_PIECES * PIECES_BLOCKS * BLOCK_S
 /** Number of blocks asked by each request */
 #define BLOCKS_REQUESTED 2
 
-#define ENABLE_END_GAME_MODE 1
 #define SLEEP_DURATION 1
 
 int count_pieces(unsigned int bitfield)
@@ -246,7 +244,6 @@ peer_t peer_init(int id, int seed)
 
   peer->pieces_count = xbt_new0(short, FILE_PIECES);
 
-  peer->stream        = (RngStream)MSG_host_get_data(MSG_host_self());
   peer->comm_received = NULL;
 
   peer->round = 0;
@@ -392,10 +389,6 @@ void handle_message(peer_t peer, msg_task_t task)
       XBT_DEBUG(" \t for piece %d (%d,%d)", message->index, message->block_index,
                 message->block_index + message->block_length);
       xbt_assert(!remote_peer->choked_download);
-      xbt_assert(remote_peer->am_interested || ENABLE_END_GAME_MODE,
-                 "Can't received a piece if I'm not interested wihtout end-game mode!"
-                 "piece (%d) bitfield(%u) remote bitfield(%u)",
-                 message->index, peer->bitfield, remote_peer->bitfield);
       xbt_assert(remote_peer->choked_download != 1, "Can't received a piece if I'm choked !");
       xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong piece received");
       // TODO: Execute à computation.
@@ -419,7 +412,6 @@ void handle_message(peer_t peer, msg_task_t task)
         }
       } else {
         XBT_DEBUG("However, we already have it");
-        xbt_assert(ENABLE_END_GAME_MODE, "Should not happen because we don't use end game mode !");
         request_new_piece_to_peer(peer, remote_peer);
       }
       break;
@@ -487,9 +479,6 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
   // end game mode
   if (count_pieces(peer->current_pieces) >= (FILE_PIECES - count_pieces(peer->bitfield)) &&
       (is_interested(peer, remote_peer) != 0)) {
-#if ENABLE_END_GAME_MODE == 0
-    return -1;
-#endif
     int nb_interesting_pieces = 0;
     // compute the number of interesting pieces
     for (int i = 0; i < FILE_PIECES; i++) {
@@ -499,7 +488,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
     }
     xbt_assert(nb_interesting_pieces != 0);
     // get a random interesting piece
-    int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
+    int random_piece_index = rand() % nb_interesting_pieces;
     int current_index      = 0;
     for (int i = 0; i < FILE_PIECES; i++) {
       if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) {
@@ -525,7 +514,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
     }
     xbt_assert(nb_interesting_pieces != 0);
     // get a random interesting piece
-    int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
+    int random_piece_index = rand() % nb_interesting_pieces;
     int current_index      = 0;
     for (int i = 0; i < FILE_PIECES; i++) {
       if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
@@ -558,7 +547,10 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
     }
     xbt_assert(nb_min_pieces != 0 || (is_interested_and_free(peer, remote_peer) == 0));
     // get a random rarest piece
-    int random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1);
+    int random_rarest_index = 0;
+    if (nb_min_pieces > 0) {
+      random_rarest_index = rand() % nb_min_pieces;
+    }
     for (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)) {
@@ -585,13 +577,13 @@ void update_choked_peers(peer_t peer)
   // update the current round
   peer->round = (peer->round + 1) % 3;
   char* key;
-  char* key_choked          = NULL;
-  connection_t peer_choosed = NULL;
-  connection_t peer_choked  = NULL;
+  char* key_choked         = NULL;
+  connection_t peer_chosen = NULL;
+  connection_t peer_choked = NULL;
   // remove a peer from the list
   xbt_dict_cursor_t cursor = NULL;
   xbt_dict_cursor_first(peer->active_peers, &cursor);
-  if (not xbt_dict_is_empty(peer->active_peers)) {
+  if (!xbt_dict_is_empty(peer->active_peers)) {
     key_choked  = xbt_dict_cursor_get_key(cursor);
     peer_choked = xbt_dict_cursor_get_data(cursor);
   }
@@ -606,7 +598,7 @@ void update_choked_peers(peer_t peer)
       if (connection->last_unchoke < unchoke_time && (connection->interested != 0) &&
           (connection->choked_upload != 0)) {
         unchoke_time = connection->last_unchoke;
-        peer_choosed = connection;
+        peer_chosen  = connection;
       }
     }
   } else {
@@ -615,25 +607,27 @@ void update_choked_peers(peer_t peer)
       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 id_chosen = 0;
+        if (xbt_dict_length(peer->peers) > 0) {
+          id_chosen = rand() % xbt_dict_length(peer->peers);
+        }
         int i         = 0;
         connection_t connection;
         xbt_dict_foreach (peer->peers, cursor, key, connection) {
           if (i == id_chosen) {
-            peer_choosed = connection;
+            peer_chosen = connection;
             break;
           }
           i++;
         }
         xbt_dict_cursor_free(&cursor);
-        if (peer_choosed == NULL)
-          THROWF(unknown_error, 0, "A peer should have be selected at this point");
-        else if ((peer_choosed->interested == 0) || (peer_choosed->choked_upload == 0))
-          peer_choosed = NULL;
+        xbt_assert(peer_chosen != NULL, "A peer should have been selected at this point");
+        if ((peer_chosen->interested == 0) || (peer_chosen->choked_upload == 0))
+          peer_chosen = NULL;
         else
           XBT_DEBUG("Nothing to do, keep going");
         j++;
-      } while (peer_choosed == NULL && j < MAXIMUM_PEERS);
+      } while (peer_chosen == NULL && j < MAXIMUM_PEERS);
     } else {
       // Use the "fastest download" policy.
       connection_t connection;
@@ -641,18 +635,18 @@ void update_choked_peers(peer_t peer)
       xbt_dict_foreach (peer->peers, cursor, key, connection) {
         if (connection->peer_speed > fastest_speed && (connection->choked_upload != 0) &&
             (connection->interested != 0)) {
-          peer_choosed  = connection;
+          peer_chosen   = connection;
           fastest_speed = connection->peer_speed;
         }
       }
     }
   }
 
-  if (peer_choosed != NULL)
-    XBT_DEBUG("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ", peer->id, peer_choosed->id,
-              peer_choosed->interested, peer_choosed->choked_upload);
+  if (peer_chosen != NULL)
+    XBT_DEBUG("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ", peer->id, peer_chosen->id,
+              peer_chosen->interested, peer_chosen->choked_upload);
 
-  if (peer_choked != peer_choosed) {
+  if (peer_choked != peer_chosen) {
     if (peer_choked != NULL) {
       xbt_assert((!peer_choked->choked_upload), "Tries to choked a choked peer");
       peer_choked->choked_upload = 1;
@@ -661,14 +655,14 @@ void update_choked_peers(peer_t peer)
       XBT_DEBUG("(%d) Sending a CHOKE to %d", peer->id, peer_choked->id);
       send_choked(peer, peer_choked->mailbox);
     }
-    if (peer_choosed != NULL) {
-      xbt_assert((peer_choosed->choked_upload), "Tries to unchoked an unchoked peer");
-      peer_choosed->choked_upload = 0;
-      xbt_dict_set_ext(peer->active_peers, (char*)&peer_choosed->id, sizeof(int), peer_choosed, NULL);
-      peer_choosed->last_unchoke = MSG_get_clock();
-      XBT_DEBUG("(%d) Sending a UNCHOKE to %d", peer->id, peer_choosed->id);
-      update_active_peers_set(peer, peer_choosed);
-      send_unchoked(peer, peer_choosed->mailbox);
+    if (peer_chosen != NULL) {
+      xbt_assert((peer_chosen->choked_upload), "Tries to unchoked an unchoked peer");
+      peer_chosen->choked_upload = 0;
+      xbt_dict_set_ext(peer->active_peers, (char*)&peer_chosen->id, sizeof(int), peer_chosen, NULL);
+      peer_chosen->last_unchoke = MSG_get_clock();
+      XBT_DEBUG("(%d) Sending a UNCHOKE to %d", peer->id, peer_chosen->id);
+      update_active_peers_set(peer, peer_chosen);
+      send_unchoked(peer, peer_chosen->mailbox);
     }
   }
 }