Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanups
[simgrid.git] / examples / c / app-bittorrent / bittorrent-peer.h
1 /* Copyright (c) 2012-2020. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef BITTORRENT_PEER_H
8 #define BITTORRENT_PEER_H
9 #include "app-bittorrent.h"
10
11 #include <simgrid/comm.h>
12 #include <simgrid/mailbox.h>
13 #include <xbt/dict.h>
14 #include <xbt/dynar.h>
15
16 /**  Contains the connection data of a peer. */
17 typedef struct s_connection {
18   int id; // Peer id
19   sg_mailbox_t mailbox;
20   unsigned int bitfield; // Fields
21   double peer_speed;
22   double last_unchoke;
23   int current_piece;
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
28 } s_connection_t;
29
30 typedef s_connection_t* connection_t;
31 typedef const s_connection_t* const_connection_t;
32
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);
36
37 /** Peer data */
38 typedef struct s_peer {
39   int id; // peer id
40   double deadline;
41   sg_mailbox_t mailbox; // peer mailbox.
42
43   xbt_dict_t connected_peers; // peers list
44   xbt_dict_t active_peers;    // active peers list
45
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.
52
53   sg_comm_t comm_received; // current comm
54   message_t message;       // current message being received
55 } s_peer_t;
56 typedef s_peer_t* peer_t;
57 typedef const s_peer_t* const_peer_t;
58
59 /** Peer main function */
60 void peer(int argc, char* argv[]);
61
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);
69
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);
75
76 int count_pieces(unsigned int bitfield);
77 int nb_interested_peers(const_peer_t peer);
78
79 void leech(peer_t peer);
80 void seed(peer_t peer);
81
82 void handle_message(peer_t peer, message_t task);
83
84 void update_choked_peers(peer_t peer);
85
86 void update_interested_after_receive(const_peer_t peer);
87
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);
91
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);
94
95 int partially_downloaded_piece(const_peer_t peer, const_connection_t remote_peer);
96
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);
99
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);
102
103 #endif /* BITTORRENT_PEER_H */