Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar
[simgrid.git] / examples / msg / app-bittorrent / peer.c
index 10153cd..09fd875 100644 (file)
@@ -156,7 +156,8 @@ void seed_loop(peer_t peer, double deadline)
  */
 int get_peers_data(peer_t peer)
 {
-  int success = 0, send_success = 0;
+  int success = 0;
+  int send_success = 0;
   double timeout = MSG_get_clock() + GET_PEERS_TIMEOUT;
   //Build the task to send to the tracker
   tracker_task_data_t data = tracker_task_data_new(MSG_host_get_name(MSG_host_self()), peer->mailbox_tracker,
@@ -408,6 +409,8 @@ void handle_message(peer_t peer, msg_task_t task)
   case MESSAGE_CANCEL:
     XBT_DEBUG("The received CANCEL from %s (%s)", message->mailbox, message->issuer_host_name);
     break;
+  default:
+    THROW_IMPOSSIBLE;
   }
   //Update the peer speed.
   if (remote_peer) {
@@ -431,14 +434,7 @@ void request_new_piece_to_peer(peer_t peer, connection_t remote_peer)
 /** remove current_piece from the list of currently downloaded pieces. */
 void remove_current_piece(peer_t peer, connection_t remote_peer, int current_piece)
 {
-  int piece_index = -1, piece;
-  unsigned int i;
-  xbt_dynar_foreach(peer->current_pieces, i, piece) {
-    if (piece == current_piece) {
-      piece_index = i;
-      break;
-    }
-  }
+  int piece_index = xbt_dynar_search_or_negative(peer->current_pieces, &current_piece);
   if (piece_index != -1)
     xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL);
   remote_peer->current_piece = -1;
@@ -484,7 +480,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
       return -1;
     int i;
     int nb_interesting_pieces = 0;
-    int random_piece_index, current_index = 0;
+    int current_index = 0;
     // compute the number of interesting pieces
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
@@ -493,7 +489,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
     }
     xbt_assert(nb_interesting_pieces != 0, "WTF !!!");
     // get a random interesting piece
-    random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
+    int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
         if (random_piece_index == current_index) {
@@ -510,7 +506,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
   if (peer->pieces < 4 && (is_interested_and_free(peer, remote_peer) != 0)) {
     int i;
     int nb_interesting_pieces = 0;
-    int random_piece_index, current_index = 0;
+    int current_index = 0;
     // compute the number of interesting pieces
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' &&
@@ -520,7 +516,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
     }
     xbt_assert(nb_interesting_pieces != 0, "WTF !!!");
     // get a random interesting piece
-    random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
+    int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' &&
           (in_current_pieces(peer, i) == 0)) {
@@ -537,7 +533,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
     int i;
     short min = SHRT_MAX;
     int nb_min_pieces = 0;
-    int random_rarest_index, current_index = 0;
+    int current_index = 0;
     // compute the smallest number of copies of available pieces
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->pieces_count[i] < min && peer->bitfield[i] == '0' &&
@@ -555,7 +551,7 @@ 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), "WTF !!!");
     // get a random rarest piece
-    random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1);
+    int random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1);
     for (i = 0; i < FILE_PIECES; i++) {
       if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' &&
           remote_peer->bitfield[i] == '1' && (in_current_pieces(peer, i)==0)) {
@@ -566,8 +562,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer)
         current_index++;
       }
     }
-    xbt_assert(piece != -1 ||
-               (is_interested_and_free(peer, remote_peer) == 0), "WTF !!!");
+    xbt_assert(piece != -1 || (is_interested_and_free(peer, remote_peer) == 0), "WTF !!!");
     return piece;
   }
 }
@@ -582,7 +577,8 @@ void update_choked_peers(peer_t peer)
   XBT_DEBUG("(%d) update_choked peers %d active peers", peer->id, xbt_dict_size(peer->active_peers));
   //update the current round
   peer->round = (peer->round + 1) % 3;
-  char *key, *key_choked=NULL;
+  char *key;
+  char *key_choked=NULL;
   connection_t peer_choosed = NULL;
   connection_t peer_choked = NULL;
   //remove a peer from the list
@@ -735,8 +731,7 @@ int get_first_block(peer_t peer, int piece)
 int is_interested(peer_t peer, connection_t remote_peer)
 {
   xbt_assert(remote_peer->bitfield, "Bitfield not received");
-  int i;
-  for (i = 0; i < FILE_PIECES; i++) {
+  for (int i = 0; i < FILE_PIECES; i++) {
     if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0') {
       return 1;
     }
@@ -748,8 +743,7 @@ int is_interested(peer_t peer, connection_t remote_peer)
 int is_interested_and_free(peer_t peer, connection_t remote_peer)
 {
   xbt_assert(remote_peer->bitfield, "Bitfield not received");
-  int i;
-  for (i = 0; i < FILE_PIECES; i++) {
+  for (int i = 0; i < FILE_PIECES; i++) {
     if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' &&
         (in_current_pieces(peer, i) == 0)) {
       return 1;
@@ -762,8 +756,7 @@ int is_interested_and_free(peer_t peer, connection_t remote_peer)
 int partially_downloaded_piece(peer_t peer, connection_t remote_peer)
 {
   xbt_assert(remote_peer->bitfield, "Bitfield not received");
-  int i;
-  for (i = 0; i < FILE_PIECES; i++) {
+  for (int i = 0; i < FILE_PIECES; i++) {
     if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' &&
         (in_current_pieces(peer, i) == 0)) {
       if (get_first_block(peer, i) > 0)
@@ -780,12 +773,11 @@ int partially_downloaded_piece(peer_t peer, connection_t remote_peer)
 void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece)
 {
   remote_peer->current_piece = piece;
-  int block_index, block_length;
   xbt_assert(remote_peer->bitfield, "bitfield not received");
   xbt_assert(remote_peer->bitfield[piece] == '1', "WTF !!!");
-  block_index = get_first_block(peer, piece);
+  int block_index = get_first_block(peer, piece);
   if (block_index != -1) {
-    block_length = PIECES_BLOCKS - block_index;
+    int block_length = PIECES_BLOCKS - block_index;
     block_length = MIN(BLOCKS_REQUESTED, block_length);
     send_request(peer, remote_peer->mailbox, piece, block_index, block_length);
   }
@@ -794,14 +786,7 @@ void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece)
 /** Indicates if a piece is currently being downloaded by the peer. */
 int in_current_pieces(peer_t peer, int piece)
 {
-  unsigned i;
-  int peer_piece;
-  xbt_dynar_foreach(peer->current_pieces, i, peer_piece) {
-    if (peer_piece == piece) {
-      return 1;
-    }
-  }
-  return 0;
+  return xbt_dynar_member(peer->current_pieces, &piece);
 }
 
 /***********************************************************