Logo AND Algorithmique Numérique Distribuée

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