1 /* Copyright (c) 2012-2020. 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. */
7 #ifndef BITTORRENT_PEER_H
8 #define BITTORRENT_PEER_H
9 #include "bittorrent.h"
10 #include "connection.h"
11 #include <simgrid/msg.h>
13 #include <xbt/dynar.h>
16 typedef struct s_peer {
19 unsigned int bitfield; // list of pieces the peer has.
20 unsigned long long bitfield_blocks; // list of blocks the peer has.
21 short* pieces_count; // number of peers that have each piece.
23 unsigned int current_pieces; // current pieces the peer is downloading
25 xbt_dict_t peers; // peers list
26 xbt_dict_t active_peers; // active peers list
27 int round; // current round for the chocking algorithm.
29 char mailbox[MAILBOX_SIZE]; // peer mailbox.
30 char mailbox_tracker[MAILBOX_SIZE]; // pair mailbox while communicating with the tracker.
31 const char* hostname; // peer hostname
33 msg_task_t task_received; // current task being received
34 msg_comm_t comm_received; // current comm
36 double begin_receive_time; // time when the receiving communication has begun, useful for calculating host speed.
38 typedef s_peer_t* peer_t;
40 /** Peer main function */
41 int peer(int argc, char* argv[]);
42 void get_status(char** status, unsigned int bitfield);
44 int get_peers_data(const s_peer_t* peer);
45 void leech_loop(peer_t peer, double deadline);
46 void seed_loop(peer_t peer, double deadline);
48 peer_t peer_init(int id, int seed);
49 void peer_free(peer_t peer);
51 int has_finished(unsigned int bitfield);
53 void handle_message(peer_t peer, msg_task_t task);
55 void update_pieces_count_from_bitfield(const s_peer_t* peer, unsigned int bitfield);
56 void update_choked_peers(peer_t peer);
58 void update_interested_after_receive(const s_peer_t* peer);
60 void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length);
61 int piece_complete(const s_peer_t* peer, int index);
62 int get_first_block(const s_peer_t* peer, int piece);
64 int peer_has_not_piece(const s_peer_t* peer, unsigned int piece);
65 int peer_is_not_downloading_piece(const s_peer_t* peer, unsigned int piece);
66 int count_pieces(unsigned int bitfield);
68 int nb_interested_peers(const s_peer_t* peer);
69 int is_interested(const s_peer_t* peer, const s_connection_t* remote_peer);
70 int is_interested_and_free(const s_peer_t* peer, const s_connection_t* remote_peer);
71 int partially_downloaded_piece(const s_peer_t* peer, const s_connection_t* remote_peer);
73 void request_new_piece_to_peer(peer_t peer, connection_t remote_peer);
74 void send_request_to_peer(const s_peer_t* peer, connection_t remote_peer, int piece);
75 void remove_current_piece(peer_t peer, connection_t remote_peer, unsigned int current_piece);
77 void update_active_peers_set(const s_peer_t* peer, connection_t remote_peer);
78 int select_piece_to_download(const s_peer_t* peer, const s_connection_t* remote_peer);
80 void send_handshake_all(const s_peer_t* peer);
82 void send_interested(const s_peer_t* peer, const char* mailbox);
84 void send_notinterested(const s_peer_t* peer, const char* mailbox);
85 void send_handshake(const s_peer_t* peer, const char* mailbox);
86 void send_bitfield(const s_peer_t* peer, const char* mailbox);
87 void send_choked(const s_peer_t* peer, const char* mailbox);
88 void send_unchoked(const s_peer_t* peer, const char* mailbox);
89 void send_have(const s_peer_t* peer, int piece);
91 void send_request(const s_peer_t* peer, const char* mailbox, int piece, int block_index, int block_length);
92 void send_piece(const s_peer_t* peer, const char* mailbox, int piece, int block_index, int block_length);
94 #endif /* BITTORRENT_PEER_H */