From: Frederic Suter Date: Wed, 22 Jun 2016 10:02:36 +0000 (+0200) Subject: few fixes and improvements in bittorrent X-Git-Tag: v3_14~892^2~9 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/eeec4714ded8d0a6be32567502577faaca925a18?hp=e6c84456d9cbd536404b8d75b1117aa7c4ecbfde;ds=inline few fixes and improvements in bittorrent --- diff --git a/examples/msg/app-bittorrent/connection.h b/examples/msg/app-bittorrent/connection.h index 057e3d8cbe..0bfcde0d95 100644 --- a/examples/msg/app-bittorrent/connection.h +++ b/examples/msg/app-bittorrent/connection.h @@ -16,10 +16,10 @@ typedef struct s_connection { double peer_speed; double last_unchoke; int current_piece; - int am_interested:1; //Indicates if we are interested in something the peer has - int interested:1; //Indicates if the peer is interested in one of our pieces - int choked_upload:1; //Indicates if the peer is choked for the current peer - int choked_download:1; //Indicates if the peer has choked the current peer + unsigned int am_interested:1; //Indicates if we are interested in something the peer has + unsigned int interested:1; //Indicates if the peer is interested in one of our pieces + unsigned int choked_upload:1; //Indicates if the peer is choked for the current peer + unsigned int choked_download:1; //Indicates if the peer has choked the current peer } s_connection_t, *connection_t; /** @brief Build a new connection object from the peer id. diff --git a/examples/msg/app-bittorrent/messages.c b/examples/msg/app-bittorrent/messages.c index ce8f93e2e8..5c8dff5599 100644 --- a/examples/msg/app-bittorrent/messages.c +++ b/examples/msg/app-bittorrent/messages.c @@ -112,6 +112,8 @@ int task_message_size(e_message_type type) case MESSAGE_CANCEL: size = MESSAGE_CANCEL_SIZE; break; + default: + THROW_IMPOSSIBLE; } return size; } diff --git a/examples/msg/app-bittorrent/peer.c b/examples/msg/app-bittorrent/peer.c index 10153cd178..4aaf7c018f 100644 --- a/examples/msg/app-bittorrent/peer.c +++ b/examples/msg/app-bittorrent/peer.c @@ -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, ¤t_piece); if (piece_index != -1) xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL); remote_peer->current_piece = -1; @@ -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) @@ -794,14 +787,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); } /*********************************************************** diff --git a/examples/msg/app-bittorrent/peer.h b/examples/msg/app-bittorrent/peer.h index 009de84f2c..4ba13ae84c 100644 --- a/examples/msg/app-bittorrent/peer.h +++ b/examples/msg/app-bittorrent/peer.h @@ -50,7 +50,7 @@ int get_peers_data(peer_t peer); void leech_loop(peer_t peer, double deadline); void seed_loop(peer_t peer, double deadline); -void peer_init(peer_t, int id, int seed); +void peer_init(peer_t peer, int id, int seed); void peer_free(peer_t peer); int has_finished(char *bitfield);