/* 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. */
/* 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. */
case MESSAGE_UNCHOKE:
xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?");
XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox, message->issuer_host_name);
case MESSAGE_UNCHOKE:
xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?");
XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox, message->issuer_host_name);
remote_peer->choked_download = 0;
//Send requests to the peer, since it has unchoked us
if (remote_peer->am_interested)
remote_peer->choked_download = 0;
//Send requests to the peer, since it has unchoked us
if (remote_peer->am_interested)
case MESSAGE_CHOKE:
xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?");
XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox, message->issuer_host_name);
case MESSAGE_CHOKE:
xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?");
XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox, message->issuer_host_name);
remote_peer->choked_download = 1;
remove_current_piece(peer, remote_peer, remote_peer->current_piece);
break;
remote_peer->choked_download = 1;
remove_current_piece(peer, remote_peer, remote_peer->current_piece);
break;
xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong request received");
if (remote_peer->choked_upload == 0) {
xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong request received");
if (remote_peer->choked_upload == 0) {
case MESSAGE_PIECE:
XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index, message->block_index,
message->block_index + message->block_length, message->mailbox, message->issuer_host_name);
case MESSAGE_PIECE:
XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index, message->block_index,
message->block_index + message->block_length, message->mailbox, message->issuer_host_name);
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(%s) remote bitfield(%s)", message->index, peer->bitfield, remote_peer->bitfield);
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(%s) remote bitfield(%s)", message->index, peer->bitfield, remote_peer->bitfield);
// end game mode
if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces) &&
(is_interested(peer, remote_peer) != 0)) {
// end game mode
if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces) &&
(is_interested(peer, remote_peer) != 0)) {
// get a random interesting piece
int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
for (i = 0; i < FILE_PIECES; i++) {
// get a random interesting piece
int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
for (i = 0; i < FILE_PIECES; i++) {
// get a random interesting piece
int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
for (i = 0; i < FILE_PIECES; i++) {
// get a random interesting piece
int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
for (i = 0; i < FILE_PIECES; i++) {
// compute the number of rarest pieces
for (i = 0; i < FILE_PIECES; i++) {
if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' &&
// compute the number of rarest pieces
for (i = 0; i < FILE_PIECES; i++) {
if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' &&
// get a random rarest piece
int random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1);
for (i = 0; i < FILE_PIECES; i++) {
// get a random rarest piece
int random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1);
for (i = 0; i < FILE_PIECES; i++) {
if (peer_choked != NULL) {
xbt_assert((!peer_choked->choked_upload), "Tries to choked a choked peer");
peer_choked->choked_upload = 1;
if (peer_choked != NULL) {
xbt_assert((!peer_choked->choked_upload), "Tries to choked a choked peer");
peer_choked->choked_upload = 1;
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);
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);
{
remote_peer->current_piece = piece;
xbt_assert(remote_peer->bitfield, "bitfield not received");
{
remote_peer->current_piece = piece;
xbt_assert(remote_peer->bitfield, "bitfield not received");
int block_index = get_first_block(peer, piece);
if (block_index != -1) {
int block_length = PIECES_BLOCKS - block_index;
int block_index = get_first_block(peer, piece);
if (block_index != -1) {
int block_length = PIECES_BLOCKS - block_index;