1 /* Copyright (c) 2012. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "connection.h"
11 #include <xbt/RngStream.h>
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers");
16 //TODO: Let users change this
18 * File transfered data
19 * For the test, default values are :
20 * File size: 10 pieces * 5 blocks/piece * 16384 bytes/block = 819200 bytes
23 #define FILE_PIECES 10
24 #define PIECES_BLOCKS 5
25 #define BLOCK_SIZE 16384
28 * Number of blocks asked by each request
30 #define BLOCKS_REQUESTED 2
33 static const int FILE_SIZE = FILE_PIECES * PIECES_BLOCKS * BLOCK_SIZE;
35 void request_new_piece_to_peer(peer_t peer, connection_t remote_peer);
36 void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece);
37 void remove_current_piece(peer_t peer, connection_t remote_peer,
44 int peer(int argc, char *argv[])
48 xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
51 peer_init(&peer, atoi(argv[1]), 1);
53 peer_init(&peer, atoi(argv[1]), 0);
56 double deadline = atof(argv[2]);
57 xbt_assert(deadline > 0, "Wrong deadline supplied");
58 XBT_INFO("Hi, I'm joining the network with id %d", peer.id);
59 //Getting peer data from the tracker.
60 if (get_peers_data(&peer)) {
61 XBT_DEBUG("Got %d peers from the tracker", xbt_dict_length(peer.peers));
62 XBT_DEBUG("Here is my current status: %s", peer.bitfield);
63 peer.begin_receive_time = MSG_get_clock();
64 MSG_mailbox_set_async(peer.mailbox);
65 if (has_finished(peer.bitfield)) {
66 peer.pieces = FILE_PIECES;
67 send_handshake_all(&peer);
68 seed_loop(&peer, deadline);
70 leech_loop(&peer, deadline);
71 // XBT_INFO("%d becomes a seeder", peer.id);
72 seed_loop(&peer, deadline);
75 XBT_INFO("Couldn't contact the tracker.");
78 XBT_INFO("Here is my current status: %s", peer.bitfield);
79 if (peer.comm_received) {
80 MSG_comm_destroy(peer.comm_received);
88 * Peer main loop when it is leeching.
89 * @param peer peer data
90 * @param deadline time at which the peer has to leave
92 void leech_loop(peer_t peer, double deadline)
94 double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL;
95 XBT_DEBUG("Start downloading.");
97 * Send a "handshake" message to all the peers it got
98 * (since it couldn't have gotten more than 50 peers)
100 send_handshake_all(peer);
101 //Wait for at least one "bitfield" message.
102 // wait_for_pieces(peer, deadline);
103 XBT_DEBUG("Starting main leech loop");
105 while (MSG_get_clock() < deadline && peer->pieces < FILE_PIECES) {
106 if (peer->comm_received == NULL) {
107 // XBT_INFO("irecv");
108 peer->task_received = NULL;
109 peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
111 if (MSG_comm_test(peer->comm_received)) {
112 // XBT_INFO("comm_test OK");
113 msg_error_t status = MSG_comm_get_status(peer->comm_received);
114 MSG_comm_destroy(peer->comm_received);
115 peer->comm_received = NULL;
116 if (status == MSG_OK) {
117 handle_message(peer, peer->task_received);
120 handle_pending_sends(peer);
121 //We don't execute the choke algorithm if we don't already have a piece
122 if (MSG_get_clock() >= next_choked_update && peer->pieces > 0) {
123 update_choked_peers(peer);
124 next_choked_update += UPDATE_CHOKED_INTERVAL;
126 MSG_process_sleep(1);
133 * Peer main loop when it is seeding
134 * @param peer peer data
135 * @param deadline time when the peer will leave
137 void seed_loop(peer_t peer, double deadline)
139 double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL;
140 XBT_DEBUG("Start seeding.");
141 //start the main seed loop
142 while (MSG_get_clock() < deadline) {
143 if (peer->comm_received == NULL) {
144 peer->task_received = NULL;
145 peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
147 if (MSG_comm_test(peer->comm_received)) {
148 msg_error_t status = MSG_comm_get_status(peer->comm_received);
149 MSG_comm_destroy(peer->comm_received);
150 peer->comm_received = NULL;
151 if (status == MSG_OK) {
152 handle_message(peer, peer->task_received);
155 if (MSG_get_clock() >= next_choked_update) {
156 update_choked_peers(peer);
157 //TODO: Change the choked peer algorithm when seeding.
158 next_choked_update += UPDATE_CHOKED_INTERVAL;
160 MSG_process_sleep(1);
167 * Retrieves the peer list from the tracker
168 * @param peer current peer data
170 int get_peers_data(peer_t peer)
172 int success = 0, send_success = 0;
173 double timeout = MSG_get_clock() + GET_PEERS_TIMEOUT;
174 //Build the task to send to the tracker
175 tracker_task_data_t data =
176 tracker_task_data_new(MSG_host_get_name(MSG_host_self()),
177 peer->mailbox_tracker, peer->id, 0, 0, FILE_SIZE);
178 //Build the task to send.
179 msg_task_t task_send = MSG_task_create(NULL, 0, TRACKER_COMM_SIZE, data);
180 msg_task_t task_received = NULL;
181 msg_comm_t comm_received;
182 while (!send_success && MSG_get_clock() < timeout) {
183 XBT_DEBUG("Sending a peer request to the tracker.");
184 msg_error_t status = MSG_task_send_with_timeout(task_send, TRACKER_MAILBOX,
186 if (status == MSG_OK) {
190 while (!success && MSG_get_clock() < timeout) {
191 comm_received = MSG_task_irecv(&task_received, peer->mailbox_tracker);
192 msg_error_t status = MSG_comm_wait(comm_received, GET_PEERS_TIMEOUT);
193 if (status == MSG_OK) {
194 tracker_task_data_t data = MSG_task_get_data(task_received);
197 //Add the peers the tracker gave us to our peer list.
198 xbt_dynar_foreach(data->peers, i, peer_id) {
199 if (peer_id != peer->id)
200 xbt_dict_set_ext(peer->peers, (char *) &peer_id, sizeof(int),
201 connection_new(peer_id), NULL);
204 //free the communication and the task
205 MSG_comm_destroy(comm_received);
206 tracker_task_data_free(data);
207 MSG_task_destroy(task_received);
208 comm_received = NULL;
216 * Initialize the peer data.
217 * @param peer peer data
218 * @param id id of the peer to take in the network
219 * @param seed indicates if the peer is a seed.
221 void peer_init(peer_t peer, int id, int seed)
224 sprintf(peer->mailbox, "%d", id);
225 sprintf(peer->mailbox_tracker, "tracker_%d", id);
226 peer->peers = xbt_dict_new();
227 peer->active_peers = xbt_dict_new();
228 peer->hostname = MSG_host_get_name(MSG_host_self());
230 peer->bitfield = xbt_new(char, FILE_PIECES + 1);
231 peer->bitfield_blocks = xbt_new(char, (FILE_PIECES) * (PIECES_BLOCKS) + 1);
233 memset(peer->bitfield, '1', sizeof(char) * (FILE_PIECES + 1));
234 memset(peer->bitfield_blocks, '1',
235 sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
237 memset(peer->bitfield, '0', sizeof(char) * (FILE_PIECES + 1));
238 memset(peer->bitfield_blocks, '0',
239 sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
242 peer->bitfield[FILE_PIECES] = '\0';
245 peer->pieces_count = xbt_new0(short, FILE_PIECES);
247 peer->current_pieces = xbt_dynar_new(sizeof(int), NULL);
249 peer->stream = RngStream_CreateStream("");
250 peer->comm_received = NULL;
254 peer->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
258 * Destroys a poor peer object.
260 void peer_free(peer_t peer)
263 connection_t connection;
264 xbt_dict_cursor_t cursor;
265 xbt_dict_foreach(peer->peers, cursor, key, connection) {
266 connection_free(connection);
268 xbt_dict_free(&peer->peers);
269 xbt_dict_free(&peer->active_peers);
270 xbt_dynar_free(&peer->current_pieces);
271 xbt_dynar_free(&peer->pending_sends);
272 xbt_free(peer->pieces_count);
273 xbt_free(peer->bitfield);
274 xbt_free(peer->bitfield_blocks);
276 RngStream_DeleteStream(&peer->stream);
280 * Returns if a peer has finished downloading the file
281 * @param bitfield peer bitfield
283 int has_finished(char *bitfield)
285 return ((memchr(bitfield, '0', sizeof(char) * FILE_PIECES) == NULL) ? 1 : 0);
288 int nb_interested_peers(peer_t peer)
290 xbt_dict_cursor_t cursor = NULL;
292 connection_t connection;
294 xbt_dict_foreach(peer->peers, cursor, key, connection) {
295 if (connection->interested)
302 * Handle pending sends and remove those which are done
303 * @param peer Peer data
305 void handle_pending_sends(peer_t peer)
309 while ((index = MSG_comm_testany(peer->pending_sends)) != -1) {
310 msg_comm_t comm_send =
311 xbt_dynar_get_as(peer->pending_sends, index, msg_comm_t);
312 int status = MSG_comm_get_status(comm_send);
313 xbt_dynar_remove_at(peer->pending_sends, index, &comm_send);
315 ("Communication %p is finished with status %d, dynar size is now %lu",
316 comm_send, status, xbt_dynar_length(peer->pending_sends));
318 msg_task_t task = MSG_comm_get_task(comm_send);
319 MSG_comm_destroy(comm_send);
321 if (status != MSG_OK) {
322 task_message_free(task);
327 void update_active_peers_set(peer_t peer, connection_t remote_peer)
330 if (remote_peer->interested && !remote_peer->choked_upload) {
331 //add in the active peers set
332 xbt_dict_set_ext(peer->active_peers, (char *) &remote_peer->id,
333 sizeof(int), remote_peer, NULL);
338 xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id,
349 * Handle a received message sent by another peer
350 * @param peer Peer data
351 * @param task task received.
353 void handle_message(peer_t peer, msg_task_t task)
355 message_t message = MSG_task_get_data(task);
356 connection_t remote_peer;
358 xbt_dict_get_or_null_ext(peer->peers, (char *) &message->peer_id,
360 switch (message->type) {
362 case MESSAGE_HANDSHAKE:
363 XBT_DEBUG("Received a HANDSHAKE from %s (%s)", message->mailbox,
364 message->issuer_host_name);
365 //Check if the peer is in our connection list.
367 xbt_dict_set_ext(peer->peers, (char *) &message->peer_id, sizeof(int),
368 connection_new(message->peer_id), NULL);
369 send_handshake(peer, message->mailbox);
371 //Send our bitfield to the peer
372 send_bitfield(peer, message->mailbox);
374 case MESSAGE_BITFIELD:
375 XBT_DEBUG("Recieved a BITFIELD message from %s (%s)", message->mailbox,
376 message->issuer_host_name);
377 //Update the pieces list
378 update_pieces_count_from_bitfield(peer, message->bitfield);
380 remote_peer->bitfield = xbt_strdup(message->bitfield);
381 xbt_assert(!remote_peer->am_interested,
382 "Should not be interested at first");
383 if (is_interested(peer, remote_peer)) {
384 remote_peer->am_interested = 1;
385 send_interested(peer, message->mailbox);
388 case MESSAGE_INTERESTED:
389 XBT_DEBUG("Recieved an INTERESTED message from %s (%s)", message->mailbox,
390 message->issuer_host_name);
391 xbt_assert((remote_peer != NULL),
392 "A non-in-our-list peer has sent us a message. WTH ?");
393 //Update the interested state of the peer.
394 remote_peer->interested = 1;
395 update_active_peers_set(peer, remote_peer);
397 case MESSAGE_NOTINTERESTED:
398 XBT_DEBUG("Received a NOTINTERESTED message from %s (%s)",
399 message->mailbox, message->issuer_host_name);
400 xbt_assert((remote_peer != NULL),
401 "A non-in-our-list peer has sent us a message. WTH ?");
402 remote_peer->interested = 0;
403 update_active_peers_set(peer, remote_peer);
405 case MESSAGE_UNCHOKE:
406 xbt_assert((remote_peer != NULL),
407 "A non-in-our-list peer has sent us a message. WTH ?");
408 XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox,
409 message->issuer_host_name);
410 remote_peer->choked_download = 0;
411 //Send requests to the peer, since it has unchoked us
412 if (remote_peer->am_interested)
413 request_new_piece_to_peer(peer, remote_peer);
416 xbt_assert((remote_peer != NULL),
417 "A non-in-our-list peer has sent us a message. WTH ?");
418 XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox,
419 message->issuer_host_name);
420 remote_peer->choked_download = 1;
421 remove_current_piece(peer, remote_peer, remote_peer->current_piece);
424 XBT_DEBUG("Received a HAVE message from %s (%s) of piece %d",
425 message->mailbox, message->issuer_host_name, message->index);
426 xbt_assert(remote_peer->bitfield, "bitfield not received");
427 xbt_assert((message->index >= 0
428 && message->index < FILE_PIECES),
429 "Wrong HAVE message received");
430 remote_peer->bitfield[message->index] = '1';
431 peer->pieces_count[message->index]++;
432 //If the piece is in our pieces, we tell the peer that we are interested.
433 if (!remote_peer->am_interested && peer->bitfield[message->index] == '0') {
434 remote_peer->am_interested = 1;
435 send_interested(peer, message->mailbox);
436 if (!remote_peer->choked_download)
437 request_new_piece_to_peer(peer, remote_peer);
440 case MESSAGE_REQUEST:
441 xbt_assert((message->index >= 0
442 && message->index < FILE_PIECES), "Wrong request received");
443 if (!remote_peer->choked_upload) {
444 XBT_DEBUG("Received a REQUEST from %s (%s) for %d (%d,%d)",
445 message->mailbox, message->issuer_host_name, message->peer_id,
446 message->block_index,
447 message->block_index + message->block_length);
448 if (peer->bitfield[message->index] == '1') {
449 send_piece(peer, message->mailbox, message->index, 0,
450 message->block_index, message->block_length);
453 XBT_DEBUG("Received a REQUEST from %s (%s) for %d but he is choked.",
454 message->mailbox, message->issuer_host_name, message->peer_id);
458 xbt_assert((message->index >= 0
459 && message->index < FILE_PIECES), "Wrong piece received");
460 //TODO: Execute à computation.
461 if (message->stalled) {
462 XBT_DEBUG("The received piece %d from %s (%s) is STALLED",
463 message->index, message->mailbox, message->issuer_host_name);
465 XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index,
466 message->block_index,
467 message->block_index + message->block_length,
468 message->mailbox, message->issuer_host_name);
469 if (peer->bitfield[message->index] == '0') {
470 update_bitfield_blocks(peer, message->index, message->block_index,
471 message->block_length);
472 if (piece_complete(peer, message->index)) {
473 //Removing the piece from our piece list
474 remove_current_piece(peer, remote_peer, message->index);
475 //Setting the fact that we have the piece
476 peer->bitfield[message->index] = '1';
478 XBT_DEBUG("My status is now %s", peer->bitfield);
479 //Sending the information to all the peers we are connected to
480 send_have(peer, message->index);
481 //sending UNINTERSTED to peers that doesn't have what we want.
482 update_interested_after_receive(peer);
483 } else { // piece not completed
484 send_request_to_peer(peer, remote_peer, message->index); // ask for the next block
487 XBT_DEBUG("However, we already have it");
488 request_new_piece_to_peer(peer, remote_peer);
493 XBT_DEBUG("The received CANCEL from %s (%s)",
494 message->mailbox, message->issuer_host_name);
497 //Update the peer speed.
499 connection_add_speed_value(remote_peer,
500 1.0 / (MSG_get_clock() -
501 peer->begin_receive_time));
503 peer->begin_receive_time = MSG_get_clock();
505 task_message_free(task);
508 void request_new_piece_to_peer(peer_t peer, connection_t remote_peer)
510 int piece = select_piece_to_download(peer, remote_peer);
512 xbt_dynar_push_as(peer->current_pieces, int, piece);
513 send_request_to_peer(peer, remote_peer, piece);
517 void remove_current_piece(peer_t peer, connection_t remote_peer,
520 int piece_index = -1, piece, i;
521 xbt_dynar_foreach(peer->current_pieces, i, piece) {
522 if (piece == current_piece) {
527 if (piece_index != -1)
528 xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL);
529 remote_peer->current_piece = -1;
533 * Updates the list of who has a piece from a bitfield
534 * @param peer peer we want to update the list
535 * @param bitfield bitfield
537 void update_pieces_count_from_bitfield(peer_t peer, char *bitfield)
540 for (i = 0; i < FILE_PIECES; i++) {
541 if (bitfield[i] == '1') {
542 peer->pieces_count[i]++;
550 * Return the piece to be downloaded
551 * There are two cases (as described in "Bittorrent Architecture Protocol", Ryan Toole :
552 * If a piece is partially downloaded, this piece will be selected prioritarily
553 * If the peer has strictly less than 4 pieces, he chooses a piece at random.
554 * If the peer has more than pieces, he downloads the pieces that are the less
555 * replicated (rarest policy).
556 * If all pieces have been downloaded or requested, we select a random requested piece (endgame mode).
558 * @param peer: local peer
559 * @param remote_peer: information about the connection
560 * @return the piece to download if possible. -1 otherwise
562 int select_piece_to_download(peer_t peer, connection_t remote_peer)
566 piece = partially_downloaded_piece(peer, remote_peer);
567 // strict priority policy
572 if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces)
573 && is_interested(peer, remote_peer)) {
575 int nb_interesting_pieces = 0;
576 int random_piece_index, current_index = 0;
577 // compute the number of interesting pieces
578 for (i = 0; i < FILE_PIECES; i++) {
579 if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
580 nb_interesting_pieces++;
583 xbt_assert(nb_interesting_pieces != 0, "WTF !!!");
584 // get a random interesting piece
586 RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
587 for (i = 0; i < FILE_PIECES; i++) {
588 if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') {
589 if (random_piece_index == current_index) {
596 xbt_assert(piece != -1, "WTF !!!");
599 // Random first policy
600 if (peer->pieces < 4 && is_interested_and_free(peer, remote_peer)) {
602 int nb_interesting_pieces = 0;
603 int random_piece_index, current_index = 0;
604 // compute the number of interesting pieces
605 for (i = 0; i < FILE_PIECES; i++) {
606 if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1'
607 && !in_current_pieces(peer, i)) {
608 nb_interesting_pieces++;
611 xbt_assert(nb_interesting_pieces != 0, "WTF !!!");
612 // get a random interesting piece
614 RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1);
615 for (i = 0; i < FILE_PIECES; i++) {
616 if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1'
617 && !in_current_pieces(peer, i)) {
618 if (random_piece_index == current_index) {
625 xbt_assert(piece != -1, "WTF !!!");
627 } else { // Rarest first policy
629 short min = SHRT_MAX;
630 int nb_min_pieces = 0;
631 int random_rarest_index, current_index = 0;
632 // compute the smallest number of copies of available pieces
633 for (i = 0; i < FILE_PIECES; i++) {
634 if (peer->pieces_count[i] < min && peer->bitfield[i] == '0'
635 && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i))
636 min = peer->pieces_count[i];
638 xbt_assert(min != SHRT_MAX
639 || !is_interested_and_free(peer, remote_peer), "WTF !!!");
640 // compute the number of rarest pieces
641 for (i = 0; i < FILE_PIECES; i++) {
642 if (peer->pieces_count[i] == min && peer->bitfield[i] == '0'
643 && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i))
646 xbt_assert(nb_min_pieces != 0
647 || !is_interested_and_free(peer, remote_peer), "WTF !!!");
648 // get a random rarest piece
649 random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1);
650 for (i = 0; i < FILE_PIECES; i++) {
651 if (peer->pieces_count[i] == min && peer->bitfield[i] == '0'
652 && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i)) {
653 if (random_rarest_index == current_index) {
660 xbt_assert(piece != -1
661 || !is_interested_and_free(peer, remote_peer), "WTF !!!");
668 * Update the list of current choked and unchoked peers, using the
670 * @param peer the current peer
672 void update_choked_peers(peer_t peer)
674 if (nb_interested_peers(peer) == 0)
676 // if(xbt_dict_size(peer->active_peers) > 0)
678 XBT_DEBUG("(%d) update_choked peers %d active peers", peer->id,
679 xbt_dict_size(peer->active_peers));
680 //update the current round
681 peer->round = (peer->round + 1) % 3;
682 char *key, *key_choked;
683 connection_t peer_choosed = NULL;
684 connection_t peer_choked = NULL;
685 //remove a peer from the list
686 xbt_dict_cursor_t cursor = NULL;
687 xbt_dict_cursor_first(peer->active_peers, &cursor);
688 if (xbt_dict_length(peer->active_peers) > 0) {
689 key_choked = xbt_dict_cursor_get_key(cursor);
690 peer_choked = xbt_dict_cursor_get_data(cursor);
692 xbt_dict_cursor_free(&cursor);
695 * If we are currently seeding, we unchoke the peer which has
696 * been unchoke the least time.
698 if (peer->pieces == FILE_PIECES) {
699 connection_t connection;
700 double unchoke_time = MSG_get_clock() + 1;
702 xbt_dict_foreach(peer->peers, cursor, key, connection) {
703 if (connection->last_unchoke < unchoke_time && connection->interested
704 && connection->choked_upload) {
705 unchoke_time = connection->last_unchoke;
706 peer_choosed = connection;
710 //Random optimistic unchoking
711 if (peer->round == 0) {
714 //We choose a random peer to unchoke.
715 int id_chosen = RngStream_RandInt(peer->stream, 0,
716 xbt_dict_length(peer->peers) - 1);
718 connection_t connection;
719 xbt_dict_foreach(peer->peers, cursor, key, connection) {
720 if (i == id_chosen) {
721 peer_choosed = connection;
726 xbt_dict_cursor_free(&cursor);
727 if (!peer_choosed->interested || !peer_choosed->choked_upload) {
731 } while (peer_choosed == NULL && j < MAXIMUM_PAIRS);
733 //Use the "fastest download" policy.
734 connection_t connection;
735 double fastest_speed = 0.0;
736 xbt_dict_foreach(peer->peers, cursor, key, connection) {
737 if (connection->peer_speed > fastest_speed
738 && connection->choked_upload && connection->interested) {
739 peer_choosed = connection;
740 fastest_speed = connection->peer_speed;
746 if (peer_choosed != NULL)
748 ("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ",
749 peer->id, peer_choosed->id, peer_choosed->interested,
750 peer_choosed->choked_upload);
752 // if (xbt_dict_size(peer->peers) > 0)
753 // xbt_assert((xbt_dict_size(peer->active_peers) != 0),
754 // "No more active peers !");
757 // if (peer_choked != NULL && peer_choked->choked_upload != 0)
758 // peer_choked = NULL;
759 // if (peer_choosed != NULL && peer_choosed->choked_upload == 0)
760 // peer_choosed = NULL;
762 if (peer_choked != peer_choosed) {
763 if (peer_choked != NULL) {
764 xbt_assert((!peer_choked->choked_upload),
765 "Tries to choked a choked peer");
766 peer_choked->choked_upload = 1;
767 xbt_assert((*((int *) key_choked) == peer_choked->id), "WTF !!!");
768 update_active_peers_set(peer, peer_choked);
769 // xbt_dict_remove_ext(peer->active_peers, key_choked, sizeof(int));
770 XBT_DEBUG("(%d) Sending a CHOKE to %d", peer->id, peer_choked->id);
771 send_choked(peer, peer_choked->mailbox);
773 if (peer_choosed != NULL) {
774 xbt_assert((peer_choosed->choked_upload),
775 "Tries to unchoked an unchoked peer");
776 peer_choosed->choked_upload = 0;
777 xbt_dict_set_ext(peer->active_peers, (char *) &peer_choosed->id,
778 sizeof(int), peer_choosed, NULL);
779 peer_choosed->last_unchoke = MSG_get_clock();
780 XBT_DEBUG("(%d) Sending a UNCHOKE to %d", peer->id, peer_choosed->id);
781 update_active_peers_set(peer, peer_choosed);
782 send_unchoked(peer, peer_choosed->mailbox);
788 * Updates our "interested" state about peers: send "not interested" to peers
789 * that don't have any more pieces we want.
790 * @param peer our peer data
792 void update_interested_after_receive(peer_t peer)
795 xbt_dict_cursor_t cursor;
796 connection_t connection;
798 int interested, piece;
799 xbt_dict_foreach(peer->peers, cursor, key, connection) {
801 if (connection->am_interested) {
802 xbt_assert(connection->bitfield, "Bitfield not received");
803 //Check if the peer still has a piece we want.
805 for (i = 0; i < FILE_PIECES; i++) {
806 if (connection->bitfield[i] == '1' && peer->bitfield[i] == '0') {
811 if (!interested) { //no more piece to download from connection
812 connection->am_interested = 0;
813 send_notinterested(peer, connection->mailbox);
819 void update_bitfield_blocks(peer_t peer, int index, int block_index,
823 xbt_assert((index >= 0 && index <= FILE_PIECES), "Wrong piece.");
824 xbt_assert((block_index >= 0
825 && block_index <= PIECES_BLOCKS), "Wrong block : %d.",
827 for (i = block_index; i < (block_index + block_length); i++) {
828 peer->bitfield_blocks[index * PIECES_BLOCKS + i] = '1';
833 * Returns if a peer has completed the download of a piece
835 int piece_complete(peer_t peer, int index)
838 for (i = 0; i < PIECES_BLOCKS; i++) {
839 if (peer->bitfield_blocks[index * PIECES_BLOCKS + i] == '0') {
847 * Returns the first block that a peer doesn't have in a piece.
848 * If the peer has all blocks of the piece, returns -1.
850 int get_first_block(peer_t peer, int piece)
853 for (i = 0; i < PIECES_BLOCKS; i++) {
854 if (peer->bitfield_blocks[piece * PIECES_BLOCKS + i] == '0') {
862 * Indicates if the remote peer has a piece not stored by the local peer
864 int is_interested(peer_t peer, connection_t remote_peer)
866 xbt_assert(remote_peer->bitfield, "Bitfield not received");
868 for (i = 0; i < FILE_PIECES; i++) {
869 if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0') {
877 * Indicates if the remote peer has a piece not stored by the local peer nor requested by the local peer
879 int is_interested_and_free(peer_t peer, connection_t remote_peer)
881 xbt_assert(remote_peer->bitfield, "Bitfield not received");
883 for (i = 0; i < FILE_PIECES; i++) {
884 if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0'
885 && !in_current_pieces(peer, i)) {
894 * Returns a piece that is partially downloaded and stored by the remote peer if any
897 int partially_downloaded_piece(peer_t peer, connection_t remote_peer)
899 xbt_assert(remote_peer->bitfield, "Bitfield not received");
901 for (i = 0; i < FILE_PIECES; i++) {
902 if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0'
903 && !in_current_pieces(peer, i)) {
904 if (get_first_block(peer, i) > 0)
913 * Send request messages to a peer that have unchoked us
915 * @param remote_peer peer data to the peer we want to send the request
917 void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece)
919 remote_peer->current_piece = piece;
921 int block_index, block_length;
922 xbt_assert(remote_peer->bitfield, "bitfield not received");
923 xbt_assert(remote_peer->bitfield[piece] == '1', "WTF !!!");
924 block_index = get_first_block(peer, piece);
925 if (block_index != -1) {
926 block_length = PIECES_BLOCKS - block_index;
927 block_length = min(BLOCKS_REQUESTED, block_length);
928 send_request(peer, remote_peer->mailbox, piece, block_index, block_length);
934 * Indicates if a piece is currently being downloaded by the peer.
936 int in_current_pieces(peer_t peer, int piece)
940 xbt_dynar_foreach(peer->current_pieces, i, peer_piece) {
941 if (peer_piece == piece) {
951 /***********************************************************
953 * Low level message functions
955 ***********************************************************/
960 * Send a "interested" message to a peer
961 * @param peer peer data
962 * @param mailbox destination mailbox
964 void send_interested(peer_t peer, const char *mailbox)
967 task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
968 peer->id, task_message_size(MESSAGE_INTERESTED));
969 MSG_task_dsend(task, mailbox, task_message_free);
970 XBT_DEBUG("Sending INTERESTED to %s", mailbox);
975 * Send a "not interested" message to a peer
976 * @param peer peer data
977 * @param mailbox destination mailbox
979 void send_notinterested(peer_t peer, const char *mailbox)
982 task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox,
983 peer->id, task_message_size(MESSAGE_NOTINTERESTED));
984 MSG_task_dsend(task, mailbox, task_message_free);
985 XBT_DEBUG("Sending NOTINTERESTED to %s", mailbox);
990 * Send a handshake message to all the peers the peer has.
991 * @param peer peer data
993 void send_handshake_all(peer_t peer)
995 connection_t remote_peer;
996 xbt_dict_cursor_t cursor = NULL;
998 xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
1000 task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
1001 peer->id, task_message_size(MESSAGE_HANDSHAKE));
1002 MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
1003 XBT_DEBUG("Sending a HANDSHAKE to %s", remote_peer->mailbox);
1008 * Send a "handshake" message to an user
1009 * @param peer peer data
1010 * @param mailbox mailbox where to we send the message
1012 void send_handshake(peer_t peer, const char *mailbox)
1015 task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
1016 peer->id, task_message_size(MESSAGE_HANDSHAKE));
1017 MSG_task_dsend(task, mailbox, task_message_free);
1018 XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox);
1022 * Send a "choked" message to a peer.
1024 void send_choked(peer_t peer, const char *mailbox)
1026 XBT_DEBUG("Sending a CHOKE to %s", mailbox);
1028 task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox,
1029 peer->id, task_message_size(MESSAGE_CHOKE));
1030 MSG_task_dsend(task, mailbox, task_message_free);
1034 * Send a "unchoked" message to a peer
1036 void send_unchoked(peer_t peer, const char *mailbox)
1038 XBT_DEBUG("Sending a UNCHOKE to %s", mailbox);
1040 task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox,
1041 peer->id, task_message_size(MESSAGE_UNCHOKE));
1042 MSG_task_dsend(task, mailbox, task_message_free);
1046 * Send a "HAVE" message to all peers we are connected to
1048 void send_have(peer_t peer, int piece)
1050 XBT_DEBUG("Sending HAVE message to all my peers");
1051 connection_t remote_peer;
1052 xbt_dict_cursor_t cursor = NULL;
1054 xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
1056 task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox,
1058 task_message_size(MESSAGE_HAVE));
1059 MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
1064 * Send a bitfield message to all the peers the peer has.
1065 * @param peer peer data
1067 void send_bitfield(peer_t peer, const char *mailbox)
1069 XBT_DEBUG("Sending a BITFIELD to %s", mailbox);
1071 task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id,
1072 peer->bitfield, FILE_PIECES);
1073 //Async send and append to pending sends
1074 msg_comm_t comm = MSG_task_isend(task, mailbox);
1075 xbt_dynar_push(peer->pending_sends, &comm);
1079 * Send a "request" message to a pair, containing a request for a piece
1081 void send_request(peer_t peer, const char *mailbox, int piece,
1082 int block_index, int block_length)
1084 XBT_DEBUG("Sending a REQUEST to %s for piece %d (%d,%d)", mailbox, piece,
1085 block_index, block_length);
1087 task_message_request_new(peer->hostname, peer->mailbox, peer->id, piece,
1088 block_index, block_length);
1089 MSG_task_dsend(task, mailbox, task_message_free);
1093 * Send a "piece" message to a pair, containing a piece of the file
1095 void send_piece(peer_t peer, const char *mailbox, int piece, int stalled,
1096 int block_index, int block_length)
1098 XBT_DEBUG("Sending the PIECE %d (%d,%d) to %s", piece, block_index,
1099 block_length, mailbox);
1100 xbt_assert(piece >= 0, "Tried to send a piece that doesn't exist.");
1101 xbt_assert((peer->bitfield[piece] == '1'),
1102 "Tried to send a piece that we doesn't have.");
1104 task_message_piece_new(peer->hostname, peer->mailbox, peer->id, piece,
1105 stalled, block_index, block_length, BLOCK_SIZE);
1106 MSG_task_dsend(task, mailbox, task_message_free);