1 /* Copyright (c) 2012-2023. 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 "app-bittorrent.h"
11 #include <simgrid/comm.h>
12 #include <simgrid/mailbox.h>
14 #include <xbt/dynar.h>
16 /** Contains the connection data of a peer. */
17 typedef struct s_connection {
20 unsigned int bitfield; // Fields
24 unsigned int am_interested : 1; // Indicates if we are interested in something the peer has
25 unsigned int interested : 1; // Indicates if the peer is interested in one of our pieces
26 unsigned int choked_upload : 1; // Indicates if the peer is choked for the current peer
27 unsigned int choked_download : 1; // Indicates if the peer has choked the current peer
30 typedef s_connection_t* connection_t;
31 typedef const s_connection_t* const_connection_t;
33 connection_t connection_new(int id);
34 void connection_add_speed_value(connection_t connection, double speed);
35 int connection_has_piece(const_connection_t connection, unsigned int piece);
38 typedef struct s_peer {
41 sg_mailbox_t mailbox; // peer mailbox.
43 xbt_dict_t connected_peers; // peers list
44 xbt_dict_t active_peers; // active peers list
46 unsigned int bitfield; // list of pieces the peer has.
47 unsigned long long bitfield_blocks; // list of blocks the peer has.
48 short* pieces_count; // number of peers that have each piece.
49 unsigned int current_pieces; // current pieces the peer is downloading
50 double begin_receive_time; // time when the receiving communication has begun, useful for calculating host speed.
51 int round; // current round for the chocking algorithm.
53 sg_comm_t comm_received; // current comm
54 message_t message; // current message being received
56 typedef s_peer_t* peer_t;
57 typedef const s_peer_t* const_peer_t;
59 /** Peer main function */
60 void peer_run(int argc, char* argv[]);
62 int get_peers_from_tracker(const_peer_t peer);
63 void send_handshake_to_all_peers(const_peer_t peer);
64 void send_message(const_peer_t peer, sg_mailbox_t mailbox, e_message_type type, uint64_t size);
65 void send_bitfield(const_peer_t peer, sg_mailbox_t mailbox);
66 void send_piece(const_peer_t peer, sg_mailbox_t mailbox, int piece, int block_index, int block_length);
67 void send_have_to_all_peers(const_peer_t peer, int piece);
68 void send_request_to_peer(const_peer_t peer, connection_t remote_peer, int piece);
70 void get_status(char* status, unsigned int bitfield);
71 int has_finished(unsigned int bitfield);
72 int is_interested(const_peer_t peer, const_connection_t remote_peer);
73 int is_interested_and_free(const_peer_t peer, const_connection_t remote_peer);
74 void update_pieces_count_from_bitfield(const_peer_t peer, unsigned int bitfield);
76 unsigned int count_pieces(unsigned int bitfield);
77 int nb_interested_peers(const_peer_t peer);
79 void leech(peer_t peer);
80 void seed(peer_t peer);
82 void handle_message(peer_t peer, message_t task);
84 void update_choked_peers(peer_t peer);
86 void update_interested_after_receive(const_peer_t peer);
88 void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length);
89 int piece_complete(const_peer_t peer, int index);
90 int get_first_missing_block_from(const_peer_t peer, int piece);
92 int peer_has_not_piece(const_peer_t peer, unsigned int piece);
93 int peer_is_not_downloading_piece(const_peer_t peer, unsigned int piece);
95 int partially_downloaded_piece(const_peer_t peer, const_connection_t remote_peer);
97 void request_new_piece_to_peer(peer_t peer, connection_t remote_peer);
98 void remove_current_piece(peer_t peer, connection_t remote_peer, unsigned int current_piece);
100 void update_active_peers_set(const_peer_t peer, connection_t remote_peer);
101 int select_piece_to_download(const_peer_t peer, const_connection_t remote_peer);
103 #endif /* BITTORRENT_PEER_H */