Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c28fc37a19d460759a196824e21e0e16e1253bd
[simgrid.git] / examples / msg / app-bittorrent / peer.h
1 /* Copyright (c) 2012-2016. 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 <simgrid/msg.h>
10 #include <xbt/dict.h>
11 #include <xbt/dynar.h>
12 #include <xbt/RngStream.h>
13 #include "connection.h"
14 #include "bittorrent.h"
15
16 /** Peer data */
17 typedef struct s_peer {
18   int id;                       //peer id
19
20   unsigned int bitfield;        //list of pieces the peer has.
21   unsigned long long bitfield_blocks; //list of blocks the peer has.
22   short *pieces_count;          //number of peers that have each piece.
23
24   unsigned int current_pieces;   //current pieces the peer is downloading
25
26   xbt_dict_t peers;             //peers list
27   xbt_dict_t active_peers;      //active peers list
28   int round;                    //current round for the chocking algorithm.
29
30   char mailbox[MAILBOX_SIZE];   //peer mailbox.
31   char mailbox_tracker[MAILBOX_SIZE];   //pair mailbox while communicating with the tracker.
32   const char *hostname;         //peer hostname
33
34   msg_task_t task_received;     //current task being received
35   msg_comm_t comm_received;     //current comm
36
37   RngStream stream;             //RngStream for
38
39   double begin_receive_time;    //time when the receiving communication has begun, useful for calculating host speed.
40 } s_peer_t;
41 typedef s_peer_t *peer_t;
42
43 /** Peer main function */
44 int peer(int argc, char *argv[]);
45 void get_status(char **status, unsigned int bitfield);
46
47 int get_peers_data(peer_t peer);
48 void leech_loop(peer_t peer, double deadline);
49 void seed_loop(peer_t peer, double deadline);
50
51 peer_t peer_init(int id, int seed);
52 void peer_free(peer_t peer);
53
54 char* print_bitfield(unsigned int bitfield);
55 int has_finished(unsigned int bitfield);
56
57 void handle_message(peer_t peer, msg_task_t task);
58
59 void update_pieces_count_from_bitfield(peer_t peer, unsigned int bitfield);
60 void update_choked_peers(peer_t peer);
61
62 void update_interested_after_receive(peer_t peer);
63
64 void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length);
65 int piece_complete(peer_t peer, int index);
66 int get_first_block(peer_t peer, int piece);
67
68 int peer_has_not_piece(peer_t peer, unsigned int piece);
69 int peer_is_not_downloading_piece(peer_t peer, unsigned int piece);
70 int count_pieces(unsigned int bitfield);
71
72 int nb_interested_peers(peer_t peer);
73 int is_interested(peer_t peer, connection_t remote_peer);
74 int is_interested_and_free(peer_t peer, connection_t remote_peer);
75 int partially_downloaded_piece(peer_t peer, connection_t remote_peer);
76
77 void request_new_piece_to_peer(peer_t peer, connection_t remote_peer);
78 void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece);
79 void remove_current_piece(peer_t peer, connection_t remote_peer, int current_piece);
80
81 void update_active_peers_set(peer_t peer, connection_t remote_peer);
82 int select_piece_to_download(peer_t peer, connection_t remote_peer);
83
84 void send_handshake_all(peer_t peer);
85
86 void send_interested(peer_t peer, const char *mailbox);
87
88 void send_notinterested(peer_t peer, const char *mailbox);
89 void send_handshake(peer_t peer, const char *mailbox);
90 void send_bitfield(peer_t peer, const char *mailbox);
91 void send_choked(peer_t peer, const char *mailbox);
92 void send_unchoked(peer_t peer, const char *mailbox);
93 void send_have(peer_t peer, int piece);
94
95 void send_request(peer_t peer, const char *mailbox, int piece, int block_index, int block_length);
96 void send_piece(peer_t peer, const char *mailbox, int piece, int block_index, int block_length);
97
98 #endif                          /* BITTORRENT_PEER_H */