- if (peer->bitfield[message->index] == '0') {
- update_bitfield_blocks(peer, message->index, message->block_index, message->block_length);
- if (piece_complete(peer, message->index)) {
- //Removing the piece from our piece list
- remove_current_piece(peer, remote_peer, message->index);
- //Setting the fact that we have the piece
- peer->bitfield[message->index] = '1';
- peer->pieces++;
- XBT_DEBUG("My status is now %s", peer->bitfield);
- //Sending the information to all the peers we are connected to
- send_have(peer, message->index);
- //sending UNINTERSTED to peers that doesn't have what we want.
- update_interested_after_receive(peer);
- } else { // piece not completed
- send_request_to_peer(peer, remote_peer, message->index); // ask for the next block
- }
- } 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);
+ if (peer_has_not_piece(peer,message->index)) {
+ update_bitfield_blocks(peer, message->index, message->block_index, message->block_length);
+ if (piece_complete(peer, message->index)) {
+ //Removing the piece from our piece list
+ remove_current_piece(peer, remote_peer, message->index);
+ //Setting the fact that we have the piece
+ peer->bitfield = peer->bitfield | (1U<<message->index);
+ char* status = xbt_malloc0(FILE_PIECES+1);
+ get_status(&status, peer->bitfield);
+ XBT_DEBUG("My status is now %s", status);
+ xbt_free(status);
+ //Sending the information to all the peers we are connected to
+ send_have(peer, message->index);
+ //sending UNINTERESTED to peers that do not have what we want.
+ update_interested_after_receive(peer);
+ } else { // piece not completed
+ send_request_to_peer(peer, remote_peer, message->index); // ask for the next block