Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removing RngStream
[simgrid.git] / teshsuite / msg / app-bittorrent / bittorrent-peer.c
index 3a6e22f..e1b6cba 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2017. 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. */
@@ -7,9 +7,10 @@
 #include "bittorrent-messages.h"
 #include "connection.h"
 #include "tracker.h"
-#include <limits.h>
 #include <simgrid/msg.h>
-#include <xbt/RngStream.h>
+
+#include <limits.h>
+#include <stdio.h> /* snprintf */
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers");
 
@@ -244,7 +245,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;
@@ -497,7 +497,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)) {
@@ -523,7 +523,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) &&
@@ -556,7 +556,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)) {
@@ -589,7 +592,7 @@ void update_choked_peers(peer_t peer)
   // remove a peer from the list
   xbt_dict_cursor_t cursor = NULL;
   xbt_dict_cursor_first(peer->active_peers, &cursor);
-  if (xbt_dict_length(peer->active_peers) > 0) {
+  if (!xbt_dict_is_empty(peer->active_peers)) {
     key_choked  = xbt_dict_cursor_get_key(cursor);
     peer_choked = xbt_dict_cursor_get_data(cursor);
   }
@@ -613,7 +616,10 @@ 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) {
@@ -624,9 +630,8 @@ void update_choked_peers(peer_t peer)
           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))
+        xbt_assert(peer_choosed != NULL, "A peer should have been selected at this point");
+        if ((peer_choosed->interested == 0) || (peer_choosed->choked_upload == 0))
           peer_choosed = NULL;
         else
           XBT_DEBUG("Nothing to do, keep going");