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>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers");
15 //TODO: Let users change this
17 * File transfered data
19 * File size: 10 pieces * 5 blocks/piece * 16384 bytes/block = 819200 bytes
21 static int FILE_SIZE = 10 * 5 * 16384;
22 static int FILE_PIECES = 10;
24 static int PIECES_BLOCKS = 5;
25 static int BLOCK_SIZE = 16384;
26 static int BLOCKS_REQUESTED = 2;
31 int peer(int argc, char *argv[])
35 xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
38 peer_init(&peer, atoi(argv[1]), 1);
40 peer_init(&peer, atoi(argv[1]), 0);
43 double deadline = atof(argv[2]);
44 xbt_assert(deadline > 0, "Wrong deadline supplied");
45 XBT_INFO("Hi, I'm joining the network with id %d", peer.id);
46 //Getting peer data from the tracker.
47 if (get_peers_data(&peer)) {
48 XBT_DEBUG("Got %d peers from the tracker", xbt_dict_length(peer.peers));
49 XBT_DEBUG("Here is my current status: %s", peer.bitfield);
50 peer.begin_receive_time = MSG_get_clock();
51 if (has_finished(peer.bitfield)) {
52 peer.pieces = FILE_PIECES;
53 send_handshake_all(&peer);
54 seed_loop(&peer, deadline);
56 leech_loop(&peer, deadline);
57 seed_loop(&peer, deadline);
60 XBT_INFO("Couldn't contact the tracker.");
63 XBT_INFO("Here is my current status: %s", peer.bitfield);
64 if (peer.comm_received) {
65 MSG_comm_destroy(peer.comm_received);
73 * Peer main loop when it is leeching.
74 * @param peer peer data
75 * @param deadline time at which the peer has to leave
77 void leech_loop(peer_t peer, double deadline)
79 double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL;
80 XBT_DEBUG("Start downloading.");
82 * Send a "handshake" message to all the peers it got
83 * (since it couldn't have gotten more than 50 peers)
85 send_handshake_all(peer);
86 //Wait for at least one "bitfield" message.
87 wait_for_pieces(peer, deadline);
88 XBT_DEBUG("Starting main leech loop");
89 while (MSG_get_clock() < deadline && peer->pieces < FILE_PIECES) {
90 if (peer->comm_received == NULL) {
91 peer->task_received = NULL;
92 peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
94 if (MSG_comm_test(peer->comm_received)) {
95 msg_error_t status = MSG_comm_get_status(peer->comm_received);
96 MSG_comm_destroy(peer->comm_received);
97 peer->comm_received = NULL;
98 if (status == MSG_OK) {
99 handle_message(peer, peer->task_received);
102 if (peer->current_piece != -1) {
103 send_interested_to_peers(peer);
105 //If the current interested pieces is < MAX
106 if (peer->pieces_requested < MAX_PIECES) {
107 update_current_piece(peer);
110 //We don't execute the choke algorithm if we don't already have a piece
111 if (MSG_get_clock() >= next_choked_update && peer->pieces > 0) {
112 update_choked_peers(peer);
113 next_choked_update += UPDATE_CHOKED_INTERVAL;
115 MSG_process_sleep(1);
122 * Peer main loop when it is seeding
123 * @param peer peer data
124 * @param deadline time when the peer will leave
126 void seed_loop(peer_t peer, double deadline)
128 double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL;
129 XBT_DEBUG("Start seeding.");
130 //start the main seed loop
131 while (MSG_get_clock() < deadline) {
132 if (peer->comm_received == NULL) {
133 peer->task_received = NULL;
134 peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
136 if (MSG_comm_test(peer->comm_received)) {
137 msg_error_t status = MSG_comm_get_status(peer->comm_received);
138 MSG_comm_destroy(peer->comm_received);
139 peer->comm_received = NULL;
140 if (status == MSG_OK) {
141 handle_message(peer, peer->task_received);
144 if (MSG_get_clock() >= next_choked_update) {
145 update_choked_peers(peer);
146 //TODO: Change the choked peer algorithm when seeding.
147 next_choked_update += UPDATE_CHOKED_INTERVAL;
149 MSG_process_sleep(1);
156 * Retrieves the peer list from the tracker
157 * @param peer current peer data
159 int get_peers_data(peer_t peer)
161 int success = 0, send_success = 0;
162 double timeout = MSG_get_clock() + GET_PEERS_TIMEOUT;
163 //Build the task to send to the tracker
164 tracker_task_data_t data =
165 tracker_task_data_new(MSG_host_get_name(MSG_host_self()),
166 peer->mailbox_tracker, peer->id, 0, 0, FILE_SIZE);
167 //Build the task to send.
168 msg_task_t task_send = MSG_task_create(NULL, 0, TRACKER_COMM_SIZE, data);
169 msg_task_t task_received = NULL;
170 msg_comm_t comm_received;
171 while (!send_success && MSG_get_clock() < timeout) {
172 XBT_DEBUG("Sending a peer request to the tracker.");
174 MSG_task_send_with_timeout(task_send, TRACKER_MAILBOX,
176 if (status == MSG_OK) {
180 while (!success && MSG_get_clock() < timeout) {
181 comm_received = MSG_task_irecv(&task_received, peer->mailbox_tracker);
182 msg_error_t status = MSG_comm_wait(comm_received, GET_PEERS_TIMEOUT);
183 if (status == MSG_OK) {
184 tracker_task_data_t data = MSG_task_get_data(task_received);
187 //Add the peers the tracker gave us to our peer list.
188 xbt_dynar_foreach(data->peers, i, peer_id) {
189 if (peer_id != peer->id)
190 xbt_dict_set_ext(peer->peers, (char *) &peer_id, sizeof(int),
191 connection_new(peer_id), NULL);
194 //free the communication and the task
195 MSG_comm_destroy(comm_received);
196 tracker_task_data_free(data);
197 MSG_task_destroy(task_received);
198 comm_received = NULL;
206 * Initialize the peer data.
207 * @param peer peer data
208 * @param id id of the peer to take in the network
209 * @param seed indicates if the peer is a seed.
211 void peer_init(peer_t peer, int id, int seed)
214 sprintf(peer->mailbox, "%d", id);
215 sprintf(peer->mailbox_tracker, "tracker_%d", id);
216 peer->peers = xbt_dict_new();
217 peer->active_peers = xbt_dict_new();
218 peer->hostname = MSG_host_get_name(MSG_host_self());
220 peer->bitfield = xbt_new(char, FILE_PIECES + 1);
221 peer->bitfield_blocks = xbt_new(char, (FILE_PIECES) * (PIECES_BLOCKS) + 1);
223 memset(peer->bitfield, '1', sizeof(char) * (FILE_PIECES + 1));
224 memset(peer->bitfield_blocks, '1',
225 sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
227 memset(peer->bitfield, '0', sizeof(char) * (FILE_PIECES + 1));
228 memset(peer->bitfield_blocks, '0',
229 sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
232 peer->bitfield[FILE_PIECES] = '\0';
235 peer->pieces_count = xbt_new0(short, FILE_PIECES);
236 peer->pieces_requested = 0;
238 peer->current_pieces = xbt_dynar_new(sizeof(int), NULL);
239 peer->current_piece = -1;
241 peer->stream = RngStream_CreateStream("");
242 peer->comm_received = NULL;
248 * Destroys a poor peer object.
250 void peer_free(peer_t peer)
253 connection_t connection;
254 xbt_dict_cursor_t cursor;
255 xbt_dict_foreach(peer->peers, cursor, key, connection) {
256 connection_free(connection);
258 xbt_dict_free(&peer->peers);
259 xbt_dict_free(&peer->active_peers);
260 xbt_dynar_free(&peer->current_pieces);
261 xbt_free(peer->pieces_count);
262 xbt_free(peer->bitfield);
263 xbt_free(peer->bitfield_blocks);
265 RngStream_DeleteStream(&peer->stream);
269 * Returns if a peer has finished downloading the file
270 * @param bitfield peer bitfield
272 int has_finished(char *bitfield)
274 return ((memchr(bitfield, '0', sizeof(char) * FILE_PIECES) == NULL) ? 1 : 0);
278 * Handle a received message sent by another peer
279 * @param peer Peer data
280 * @param task task received.
282 void handle_message(peer_t peer, msg_task_t task)
284 message_t message = MSG_task_get_data(task);
285 connection_t remote_peer;
287 xbt_dict_get_or_null_ext(peer->peers, (char *) &message->peer_id,
289 switch (message->type) {
291 case MESSAGE_HANDSHAKE:
292 XBT_DEBUG("Received a HANDSHAKE from %s (%s)", message->mailbox,
293 message->issuer_host_name);
294 //Check if the peer is in our connection list.
296 xbt_dict_set_ext(peer->peers, (char *) &message->peer_id, sizeof(int),
297 connection_new(message->peer_id), NULL);
298 send_handshake(peer, message->mailbox);
300 //Send our bitfield to the peer
301 send_bitfield(peer, message->mailbox);
303 case MESSAGE_BITFIELD:
304 XBT_DEBUG("Recieved a BITFIELD message from %s (%s)", message->mailbox,
305 message->issuer_host_name);
306 //Update the pieces list
307 update_pieces_count_from_bitfield(peer, message->bitfield);
309 remote_peer->bitfield = xbt_strdup(message->bitfield);
310 //Update the current piece
311 if (peer->current_piece == -1 && peer->pieces < FILE_PIECES) {
312 update_current_piece(peer);
315 case MESSAGE_INTERESTED:
316 XBT_DEBUG("Recieved an INTERESTED message from %s (%s)", message->mailbox,
317 message->issuer_host_name);
318 xbt_assert((remote_peer != NULL),
319 "A non-in-our-list peer has sent us a message. WTH ?");
320 //Update the interested state of the peer.
321 remote_peer->interested = 1;
323 case MESSAGE_NOTINTERESTED:
324 XBT_DEBUG("Received a NOTINTERESTED message from %s (%s)", message->mailbox,
325 message->issuer_host_name);
326 xbt_assert((remote_peer != NULL),
327 "A non-in-our-list peer has sent us a message. WTH ?");
328 remote_peer->interested = 0;
330 case MESSAGE_UNCHOKE:
331 xbt_assert((remote_peer != NULL),
332 "A non-in-our-list peer has sent us a message. WTH ?");
333 XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox,
334 message->issuer_host_name);
335 remote_peer->choked_download = 0;
336 xbt_dict_set_ext(peer->active_peers, (char *) &message->peer_id,
337 sizeof(int), remote_peer, NULL);
338 //Send requests to the peer, since it has unchoked us
339 send_requests_to_peer(peer, remote_peer);
342 xbt_assert((remote_peer != NULL),
343 "A non-in-our-list peer has sent us a message. WTH ?");
344 XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox,
345 message->issuer_host_name);
346 remote_peer->choked_download = 1;
349 xbt_dict_remove_ext(peer->active_peers, (char *) &message->peer_id,
357 XBT_DEBUG("Received a HAVE message from %s (%s) of piece %d",
358 message->mailbox, message->issuer_host_name, message->index);
359 xbt_assert((message->index >= 0
360 && message->index < FILE_PIECES),
361 "Wrong HAVE message received");
362 if (remote_peer->bitfield == NULL)
364 remote_peer->bitfield[message->index] = '1';
365 peer->pieces_count[message->index]++;
366 //If the piece is in our pieces, we tell the peer that we are interested.
367 if (!remote_peer->am_interested && in_current_pieces(peer, message->index)) {
368 remote_peer->am_interested = 1;
369 send_interested(peer, remote_peer->mailbox);
372 case MESSAGE_REQUEST:
373 xbt_assert((message->index >= 0
374 && message->index < FILE_PIECES), "Wrong request received");
375 if (!remote_peer->choked_upload) {
376 XBT_DEBUG("Received a REQUEST from %s (%s) for %d (%d,%d)",
377 message->mailbox, message->issuer_host_name, message->peer_id,
378 message->block_index,
379 message->block_index + message->block_length);
380 if (peer->bitfield[message->index] == '1') {
381 send_piece(peer, message->mailbox, message->index, 0,
382 message->block_index, message->block_length);
385 XBT_DEBUG("Received a REQUEST from %s (%s) for %d but he is choked.",
386 message->mailbox, message->issuer_host_name, message->peer_id);
390 xbt_assert((message->index >= 0
391 && message->index < FILE_PIECES), "Wrong piece received");
392 //TODO: Execute à computation.
393 if (message->stalled) {
394 XBT_DEBUG("The received piece %d from %s (%s) is STALLED", message->index,
395 message->mailbox, message->issuer_host_name);
397 XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index,
398 message->block_index,
399 message->block_index + message->block_length, message->mailbox,
400 message->issuer_host_name);
401 if (peer->bitfield[message->index] == '0') {
402 update_bitfield_blocks(peer, message->index, message->block_index,
403 message->block_length);
404 if (piece_complete(peer, message->index)) {
405 peer->pieces_requested--;
406 //Removing the piece from our piece list
408 int piece_index = -1, piece;
409 xbt_dynar_foreach(peer->current_pieces, i, piece) {
410 if (piece == message->index) {
415 xbt_assert(piece_index != -1, "Received an incorrect piece");
416 xbt_dynar_remove_at(peer->current_pieces, piece_index, NULL);
417 //Setting the fact that we have the piece
418 peer->bitfield[message->index] = '1';
420 XBT_DEBUG("My status is now %s", peer->bitfield);
421 //Sending the information to all the peers we are connected to
422 send_have(peer, message->index);
423 //sending UNINTERSTED to peers that doesn't have what we want.
424 update_interested_after_receive(peer);
427 XBT_DEBUG("However, we already have it");
432 //Update the peer speed.
434 connection_add_speed_value(remote_peer,
435 1.0 / (MSG_get_clock() -
436 peer->begin_receive_time));
438 peer->begin_receive_time = MSG_get_clock();
440 task_message_free(task);
444 * Wait for the node to receive interesting bitfield messages (ie: non empty)
446 * @param deadline peer deadline
447 * @param peer peer data
449 void wait_for_pieces(peer_t peer, double deadline)
452 while (MSG_get_clock() < deadline && !finished) {
453 if (peer->comm_received == NULL) {
454 peer->task_received = NULL;
455 peer->comm_received = MSG_task_irecv(&peer->task_received, peer->mailbox);
457 msg_error_t status = MSG_comm_wait(peer->comm_received, TIMEOUT_MESSAGE);
458 //free the comm already, we don't need it anymore
459 MSG_comm_destroy(peer->comm_received);
460 peer->comm_received = NULL;
461 if (status == MSG_OK) {
462 MSG_task_get_data(peer->task_received);
463 handle_message(peer, peer->task_received);
464 if (peer->current_piece != -1) {
472 * Updates the list of who has a piece from a bitfield
473 * @param peer peer we want to update the list
474 * @param bitfield bitfield
476 void update_pieces_count_from_bitfield(peer_t peer, char *bitfield)
479 for (i = 0; i < FILE_PIECES; i++) {
480 if (bitfield[i] == '1') {
481 peer->pieces_count[i]++;
487 * Update the piece the peer is currently interested in.
488 * There is two cases (as described in "Bittorrent Architecture Protocol", Ryan Toole :
489 * If the peer has less than 3 pieces, he chooses a piece at random.
490 * If the peer has more than pieces, he downloads the pieces that are the less
493 void update_current_piece(peer_t peer)
495 if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces)) {
498 if (1 || peer->pieces < 3) {
501 peer->current_piece =
502 RngStream_RandInt(peer->stream, 0, FILE_PIECES - 1);;
505 (peer->bitfield[peer->current_piece] == '0'
506 && !in_current_pieces(peer, peer->current_piece)));
508 //Trivial min algorithm.
511 for (i = 0; i < FILE_PIECES; i++) {
512 if (peer->bitfield[i] == '0') {
513 min = peer->pieces_count[i];
518 xbt_assert((min > -1), "Couldn't find a minimum");
519 for (i = 1; i < FILE_PIECES; i++) {
520 if (peer->pieces_count[i] < min && peer->bitfield[i] == '0') {
521 min = peer->pieces_count[i];
525 peer->current_piece = min_id;
527 xbt_dynar_push_as(peer->current_pieces, int, peer->current_piece);
528 XBT_DEBUG("New interested piece: %d", peer->current_piece);
529 xbt_assert((peer->current_piece >= 0 && peer->current_piece < FILE_PIECES),
530 "Peer want to retrieve a piece that doesn't exist.");
534 * Update the list of current choked and unchoked peers, using the
536 * @param peer the current peer
538 void update_choked_peers(peer_t peer)
540 //update the current round
541 peer->round = (peer->round + 1) % 3;
543 connection_t peer_choosed = NULL;
544 //remove a peer from the list
545 xbt_dict_cursor_t cursor = NULL;
546 xbt_dict_cursor_first(peer->active_peers, &cursor);
547 if (xbt_dict_length(peer->active_peers) > 0) {
548 key = xbt_dict_cursor_get_key(cursor);
549 connection_t peer_choked = xbt_dict_cursor_get_data(cursor);
551 send_choked(peer, peer_choked->mailbox);
552 peer_choked->choked_upload = 1;
554 xbt_dict_remove_ext(peer->active_peers, key, sizeof(int));
556 xbt_dict_cursor_free(&cursor);
559 * If we are currently seeding, we unchoke the peer which has
560 * been unchoke the least time.
562 if (peer->pieces == FILE_PIECES) {
563 connection_t connection;
564 double unchoke_time = MSG_get_clock() + 1;
566 xbt_dict_foreach(peer->peers, cursor, key, connection) {
567 if (connection->last_unchoke < unchoke_time && connection->interested) {
568 unchoke_time = connection->last_unchoke;
569 peer_choosed = connection;
573 //Random optimistic unchoking
574 if (peer->round == 0) {
577 //We choose a random peer to unchoke.
579 RngStream_RandInt(peer->stream, 0,
580 xbt_dict_length(peer->peers) - 1);
582 connection_t connection;
583 xbt_dict_foreach(peer->peers, cursor, key, connection) {
584 if (i == id_chosen) {
585 peer_choosed = connection;
590 xbt_dict_cursor_free(&cursor);
591 if (peer_choosed->interested == 0) {
595 } while (peer_choosed == NULL && j < MAXIMUM_PAIRS);
597 //Use the "fastest download" policy.
598 connection_t connection;
599 double fastest_speed = 0.0;
600 xbt_dict_foreach(peer->peers, cursor, key, connection) {
601 if (connection->peer_speed > fastest_speed && connection->choked_upload
602 && connection->interested) {
603 peer_choosed = connection;
604 fastest_speed = connection->peer_speed;
610 if (peer_choosed != NULL) {
611 peer_choosed->choked_upload = 0;
612 xbt_dict_set_ext(peer->active_peers, (char *) &peer_choosed->id,
613 sizeof(int), peer_choosed, NULL);
614 peer_choosed->last_unchoke = MSG_get_clock();
615 send_unchoked(peer, peer_choosed->mailbox);
621 * Updates our "interested" state about peers: send "not interested" to peers
622 * that don't have any more pieces we want.
623 * @param peer our peer data
625 void update_interested_after_receive(peer_t peer)
628 xbt_dict_cursor_t cursor;
629 connection_t connection;
631 int interested, piece;
632 xbt_dict_foreach(peer->peers, cursor, key, connection) {
634 if (connection->am_interested) {
635 //Check if the peer still has a piece we want.
636 xbt_dynar_foreach(peer->current_pieces, cpt, piece) {
637 xbt_assert((piece >= 0), "Wrong piece.");
638 if (connection->bitfield && connection->bitfield[piece] == '1') {
644 connection->am_interested = 0;
645 send_notinterested(peer, connection->mailbox);
651 void update_bitfield_blocks(peer_t peer, int index, int block_index,
655 xbt_assert((index >= 0 && index <= FILE_PIECES), "Wrong piece.");
656 xbt_assert((block_index >= 0
657 && block_index <= PIECES_BLOCKS), "Wrong block : %d.",
659 for (i = block_index; i < (block_index + block_length); i++) {
660 peer->bitfield_blocks[index * PIECES_BLOCKS + i] = '1';
665 * Returns if a peer has completed the download of a piece
667 int piece_complete(peer_t peer, int index)
670 for (i = 0; i < PIECES_BLOCKS; i++) {
671 if (peer->bitfield_blocks[index * PIECES_BLOCKS + i] == '0') {
679 * Returns the first block that a peer doesn't have in a piece
681 int get_first_block(peer_t peer, int piece)
684 for (i = 0; i < PIECES_BLOCKS; i++) {
685 if (peer->bitfield_blocks[piece * PIECES_BLOCKS + i] == '0') {
693 * Send request messages to a peer that have unchoked us
695 * @param remote_peer peer data to the peer we want to send the request
697 void send_requests_to_peer(peer_t peer, connection_t remote_peer)
700 int piece, block_index, block_length;
701 xbt_dynar_foreach(peer->current_pieces, i, piece) {
702 if (remote_peer->bitfield && remote_peer->bitfield[piece] == '1') {
703 block_index = get_first_block(peer, piece);
704 if (block_index != -1) {
705 block_length = PIECES_BLOCKS - block_index;
706 block_length = min(BLOCKS_REQUESTED, block_length);
707 send_request(peer, remote_peer->mailbox, piece, block_index,
716 * Find the peers that have the current interested piece and send them
717 * the "interested" message
719 void send_interested_to_peers(peer_t peer)
722 xbt_dict_cursor_t cursor = NULL;
723 connection_t connection;
724 xbt_assert((peer->current_piece != -1),
725 "Tried to send a interested message wheras the current_piece is -1");
726 xbt_dict_foreach(peer->peers, cursor, key, connection) {
727 if (connection->bitfield
728 && connection->bitfield[peer->current_piece] == '1') {
729 connection->am_interested = 1;
731 task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
732 peer->id, task_message_size(MESSAGE_INTERESTED));
733 MSG_task_dsend(task, connection->mailbox, task_message_free);
734 XBT_DEBUG("Send INTERESTED to %s", connection->mailbox);
737 peer->current_piece = -1;
738 peer->pieces_requested++;
742 * Send a "interested" message to a peer
743 * @param peer peer data
744 * @param mailbox destination mailbox
746 void send_interested(peer_t peer, const char *mailbox)
749 task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox,
750 peer->id, task_message_size(MESSAGE_INTERESTED));
751 MSG_task_dsend(task, mailbox, task_message_free);
752 XBT_DEBUG("Sending INTERESTED to %s", mailbox);
757 * Send a "not interested" message to a peer
758 * @param peer peer data
759 * @param mailbox destination mailbox
761 void send_notinterested(peer_t peer, const char *mailbox)
764 task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox,
765 peer->id, task_message_size(MESSAGE_NOTINTERESTED));
766 MSG_task_dsend(task, mailbox, task_message_free);
767 XBT_DEBUG("Sending NOTINTERESTED to %s", mailbox);
772 * Send a handshake message to all the peers the peer has.
773 * @param peer peer data
775 void send_handshake_all(peer_t peer)
777 connection_t remote_peer;
778 xbt_dict_cursor_t cursor = NULL;
780 xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
782 task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
783 peer->id, task_message_size(MESSAGE_HANDSHAKE));
784 MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
785 XBT_DEBUG("Sending a HANDSHAKE to %s", remote_peer->mailbox);
790 * Send a "handshake" message to an user
791 * @param peer peer data
792 * @param mailbox mailbox where to we send the message
794 void send_handshake(peer_t peer, const char *mailbox)
797 task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,
798 peer->id, task_message_size(MESSAGE_HANDSHAKE));
799 MSG_task_dsend(task, mailbox, task_message_free);
800 XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox);
804 * Send a "choked" message to a peer.
806 void send_choked(peer_t peer, const char *mailbox)
808 XBT_DEBUG("Sending a CHOKE to %s", mailbox);
810 task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox,
811 peer->id, task_message_size(MESSAGE_CHOKE));
812 MSG_task_dsend(task, mailbox, task_message_free);
816 * Send a "unchoked" message to a peer
818 void send_unchoked(peer_t peer, const char *mailbox)
820 XBT_DEBUG("Sending a UNCHOKE to %s", mailbox);
822 task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox,
823 peer->id, task_message_size(MESSAGE_UNCHOKE));
824 MSG_task_dsend(task, mailbox, task_message_free);
828 * Send a "HAVE" message to all peers we are connected to
830 void send_have(peer_t peer, int piece)
832 XBT_DEBUG("Sending HAVE message to all my peers");
833 connection_t remote_peer;
834 xbt_dict_cursor_t cursor = NULL;
836 xbt_dict_foreach(peer->peers, cursor, key, remote_peer) {
838 task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox,
839 peer->id, piece, task_message_size(MESSAGE_HAVE));
840 MSG_task_dsend(task, remote_peer->mailbox, task_message_free);
845 * Send a bitfield message to all the peers the peer has.
846 * @param peer peer data
848 void send_bitfield(peer_t peer, const char *mailbox)
850 XBT_DEBUG("Sending a BITFIELD to %s", mailbox);
852 task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id,
853 peer->bitfield, FILE_PIECES);
854 MSG_task_dsend(task, mailbox, task_message_free);
858 * Send a "request" message to a pair, containing a request for a piece
860 void send_request(peer_t peer, const char *mailbox, int piece, int block_index,
863 XBT_DEBUG("Sending a REQUEST to %s for piece %d (%d,%d)", mailbox, piece,
864 block_index, block_length);
866 task_message_request_new(peer->hostname, peer->mailbox, peer->id, piece,
867 block_index, block_length);
868 MSG_task_dsend(task, mailbox, task_message_free);
872 * Send a "piece" message to a pair, containing a piece of the file
874 void send_piece(peer_t peer, const char *mailbox, int piece, int stalled,
875 int block_index, int block_length)
877 XBT_DEBUG("Sending the PIECE %d (%d,%d) to %s", piece, block_index,
878 block_length, mailbox);
879 xbt_assert(piece >= 0, "Tried to send a piece that doesn't exist.");
880 xbt_assert((peer->bitfield[piece] == '1'),
881 "Tried to send a piece that we doesn't have.");
883 task_message_piece_new(peer->hostname, peer->mailbox, peer->id, piece,
884 stalled, block_index, block_length, BLOCK_SIZE);
885 MSG_task_dsend(task, mailbox, task_message_free);
888 int in_current_pieces(peer_t peer, int piece)
891 int is_in = 0, peer_piece;
892 xbt_dynar_foreach(peer->current_pieces, i, peer_piece) {
893 if (peer_piece == piece) {