X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/fbba32bb0526857d6da05d6e6604bde84fc85321..5edbea104d3c5d61dc5a6dce20706a6fe5c89b2a:/teshsuite/msg/app-bittorrent/bittorrent-peer.c diff --git a/teshsuite/msg/app-bittorrent/bittorrent-peer.c b/teshsuite/msg/app-bittorrent/bittorrent-peer.c index 700d920c3a..7abd4e9531 100644 --- a/teshsuite/msg/app-bittorrent/bittorrent-peer.c +++ b/teshsuite/msg/app-bittorrent/bittorrent-peer.c @@ -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 -#include #include #include /* 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) @@ -205,7 +203,7 @@ int get_peers_data(peer_t peer) // Add the peers the tracker gave us to our peer list. xbt_dynar_foreach (data->peers, i, peer_id) { if (peer_id != peer->id) - xbt_dict_set_ext(peer->peers, (char*)&peer_id, sizeof(int), connection_new(peer_id), NULL); + xbt_dict_set_ext(peer->peers, (char*)&peer_id, sizeof(int), connection_new(peer_id)); } success = 1; // free the communication and the task @@ -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; @@ -294,7 +291,7 @@ void update_active_peers_set(peer_t peer, connection_t remote_peer) { if ((remote_peer->interested != 0) && (remote_peer->choked_upload == 0)) { // add in the active peers set - xbt_dict_set_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int), remote_peer, NULL); + xbt_dict_set_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int), remote_peer); } else if (xbt_dict_get_or_null_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int))) { xbt_dict_remove_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int)); } @@ -319,7 +316,7 @@ void handle_message(peer_t peer, msg_task_t task) case MESSAGE_HANDSHAKE: // Check if the peer is in our connection list. if (remote_peer == 0) { - xbt_dict_set_ext(peer->peers, (char*)&message->peer_id, sizeof(int), connection_new(message->peer_id), NULL); + xbt_dict_set_ext(peer->peers, (char*)&message->peer_id, sizeof(int), connection_new(message->peer_id)); send_handshake(peer, message->mailbox); } // Send our bitfield to the peer @@ -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,15 +577,15 @@ 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* choked_key = NULL; + connection_t chosen_peer = NULL; + connection_t choked_peer = NULL; // remove a peer from the list xbt_dict_cursor_t cursor = NULL; xbt_dict_cursor_first(peer->active_peers, &cursor); if (!xbt_dict_is_empty(peer->active_peers)) { - key_choked = xbt_dict_cursor_get_key(cursor); - peer_choked = xbt_dict_cursor_get_data(cursor); + choked_key = xbt_dict_cursor_get_key(cursor); + choked_peer = xbt_dict_cursor_get_data(cursor); } xbt_dict_cursor_free(&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; + chosen_peer = 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; + chosen_peer = 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(chosen_peer != NULL, "A peer should have been selected at this point"); + if ((chosen_peer->interested == 0) || (chosen_peer->choked_upload == 0)) + chosen_peer = NULL; else XBT_DEBUG("Nothing to do, keep going"); j++; - } while (peer_choosed == NULL && j < MAXIMUM_PEERS); + } while (chosen_peer == NULL && j < MAXIMUM_PEERS); } else { // Use the "fastest download" policy. connection_t connection; @@ -641,34 +635,34 @@ 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; + chosen_peer = 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_choked != peer_choosed) { - if (peer_choked != NULL) { - xbt_assert((!peer_choked->choked_upload), "Tries to choked a choked peer"); - peer_choked->choked_upload = 1; - xbt_assert((*((int*)key_choked) == peer_choked->id)); - update_active_peers_set(peer, peer_choked); - XBT_DEBUG("(%d) Sending a CHOKE to %d", peer->id, peer_choked->id); - send_choked(peer, peer_choked->mailbox); + if (chosen_peer != NULL) + XBT_DEBUG("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ", peer->id, chosen_peer->id, + chosen_peer->interested, chosen_peer->choked_upload); + + if (choked_peer != chosen_peer) { + if (choked_peer != NULL) { + xbt_assert((!choked_peer->choked_upload), "Tries to choked a choked peer"); + choked_peer->choked_upload = 1; + xbt_assert((*((int*)choked_key) == choked_peer->id)); + update_active_peers_set(peer, choked_peer); + XBT_DEBUG("(%d) Sending a CHOKE to %d", peer->id, choked_peer->id); + send_choked(peer, choked_peer->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 (chosen_peer != NULL) { + xbt_assert((chosen_peer->choked_upload), "Tries to unchoked an unchoked peer"); + chosen_peer->choked_upload = 0; + xbt_dict_set_ext(peer->active_peers, (char*)&chosen_peer->id, sizeof(int), chosen_peer); + chosen_peer->last_unchoke = MSG_get_clock(); + XBT_DEBUG("(%d) Sending a UNCHOKE to %d", peer->id, chosen_peer->id); + update_active_peers_set(peer, chosen_peer); + send_unchoked(peer, chosen_peer->mailbox); } } }