/* 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. */
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,
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,
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);
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);
- } else {
- //remove
- xbt_ex_t e;
- TRY {
- xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int));
- }
- CATCH(e) {
- xbt_ex_free(e);
- }
+ } 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));
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);
/** remove current_piece from the list of currently downloaded pieces. */
void remove_current_piece(peer_t peer, connection_t remote_peer, int current_piece)
{
/** 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;
if (piece_index != -1)
xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL);
remote_peer->current_piece = -1;
// 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)) {
// compute the number of interesting pieces
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
nb_interesting_pieces++;
}
}
// compute the number of interesting pieces
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
nb_interesting_pieces++;
}
}
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
if (random_piece_index == current_index) {
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
if (random_piece_index == current_index) {
return piece;
}
// Random first policy
if (peer->pieces < 4 && (is_interested_and_free(peer, remote_peer) != 0)) {
int i;
int nb_interesting_pieces = 0;
return piece;
}
// Random first policy
if (peer->pieces < 4 && (is_interested_and_free(peer, remote_peer) != 0)) {
int i;
int nb_interesting_pieces = 0;
// compute the number of interesting pieces
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' &&
// compute the number of interesting pieces
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' &&
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' &&
(in_current_pieces(peer, i) == 0)) {
for (i = 0; i < FILE_PIECES; i++) {
if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' &&
(in_current_pieces(peer, i) == 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' &&
// 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' &&
// 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' &&
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)) {
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)) {
- 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));
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;
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;
connection_t peer_choosed = NULL;
connection_t peer_choked = NULL;
//remove a peer from the list
connection_t peer_choosed = NULL;
connection_t peer_choked = NULL;
//remove a peer from the list
- if ((peer_choosed->interested == 0) ||
- (peer_choosed->choked_upload == 0)) {
+ 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))
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);
int is_interested(peer_t peer, connection_t remote_peer)
{
xbt_assert(remote_peer->bitfield, "Bitfield not received");
int is_interested(peer_t peer, connection_t remote_peer)
{
xbt_assert(remote_peer->bitfield, "Bitfield not received");
int is_interested_and_free(peer_t peer, connection_t remote_peer)
{
xbt_assert(remote_peer->bitfield, "Bitfield not received");
int is_interested_and_free(peer_t peer, connection_t remote_peer)
{
xbt_assert(remote_peer->bitfield, "Bitfield not received");
if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' &&
(in_current_pieces(peer, i) == 0)) {
return 1;
if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' &&
(in_current_pieces(peer, i) == 0)) {
return 1;
int partially_downloaded_piece(peer_t peer, connection_t remote_peer)
{
xbt_assert(remote_peer->bitfield, "Bitfield not received");
int partially_downloaded_piece(peer_t peer, connection_t remote_peer)
{
xbt_assert(remote_peer->bitfield, "Bitfield not received");
if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' &&
(in_current_pieces(peer, i) == 0)) {
if (get_first_block(peer, i) > 0)
if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' &&
(in_current_pieces(peer, i) == 0)) {
if (get_first_block(peer, i) > 0)
void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece)
{
remote_peer->current_piece = piece;
void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece)
{
remote_peer->current_piece = piece;
- xbt_assert(remote_peer->bitfield[piece] == '1', "WTF !!!");
- block_index = get_first_block(peer, piece);
+ xbt_assert(remote_peer->bitfield[piece] == '1');
+ int block_index = get_first_block(peer, piece);
block_length = MIN(BLOCKS_REQUESTED, block_length);
send_request(peer, remote_peer->mailbox, piece, block_index, block_length);
}
block_length = MIN(BLOCKS_REQUESTED, block_length);
send_request(peer, remote_peer->mailbox, piece, block_index, block_length);
}
/** Indicates if a piece is currently being downloaded by the peer. */
int in_current_pieces(peer_t 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);