~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/policy.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2003, Arvid Norberg
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
10
    * Redistributions of source code must retain the above copyright
 
11
      notice, this list of conditions and the following disclaimer.
 
12
    * Redistributions in binary form must reproduce the above copyright
 
13
      notice, this list of conditions and the following disclaimer in
 
14
      the documentation and/or other materials provided with the distribution.
 
15
    * Neither the name of the author nor the names of its
 
16
      contributors may be used to endorse or promote products derived
 
17
      from this software without specific prior written permission.
 
18
 
 
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
29
POSSIBILITY OF SUCH DAMAGE.
 
30
 
 
31
*/
 
32
 
 
33
#ifndef TORRENT_POLICY_HPP_INCLUDED
 
34
#define TORRENT_POLICY_HPP_INCLUDED
 
35
 
 
36
#include <algorithm>
 
37
#include <vector>
 
38
 
 
39
#ifdef _MSC_VER
 
40
#pragma warning(push, 1)
 
41
#endif
 
42
 
 
43
#ifdef _MSC_VER
 
44
#pragma warning(pop)
 
45
#endif
 
46
 
 
47
#include "libtorrent/peer.hpp"
 
48
#include "libtorrent/piece_picker.hpp"
 
49
#include "libtorrent/socket.hpp"
 
50
#include "libtorrent/size_type.hpp"
 
51
#include "libtorrent/invariant_check.hpp"
 
52
#include "libtorrent/config.hpp"
 
53
#include "libtorrent/time.hpp"
 
54
 
 
55
namespace libtorrent
 
56
{
 
57
 
 
58
        class torrent;
 
59
        class peer_connection;
 
60
 
 
61
        enum
 
62
        {
 
63
                // the limits of the download queue size
 
64
                min_request_queue = 2,
 
65
 
 
66
                // the amount of free upload allowed before
 
67
                // the peer is choked
 
68
                free_upload_amount = 4 * 16 * 1024
 
69
        };
 
70
 
 
71
        void request_a_block(torrent& t, peer_connection& c);
 
72
 
 
73
        class TORRENT_EXPORT policy
 
74
        {
 
75
        public:
 
76
 
 
77
                policy(torrent* t);
 
78
 
 
79
                // this is called every 10 seconds to allow
 
80
                // for peer choking management
 
81
                void pulse();
 
82
 
 
83
                struct peer;
 
84
                // this is called once for every peer we get from
 
85
                // the tracker, pex, lsd or dht.
 
86
                policy::peer* peer_from_tracker(const tcp::endpoint& remote, const peer_id& pid
 
87
                        , int source, char flags);
 
88
                void update_peer_port(int port, policy::peer* p, int src);
 
89
 
 
90
                // called when an incoming connection is accepted
 
91
                void new_connection(peer_connection& c);
 
92
 
 
93
                // the given connection was just closed
 
94
                void connection_closed(const peer_connection& c) throw();
 
95
 
 
96
                // the peer has got at least one interesting piece
 
97
                void peer_is_interesting(peer_connection& c);
 
98
 
 
99
                void piece_finished(int index, bool successfully_verified);
 
100
 
 
101
                // the peer choked us
 
102
                void choked(peer_connection& c);
 
103
 
 
104
                int count_choked() const;
 
105
 
 
106
                // the peer unchoked us
 
107
                void unchoked(peer_connection& c);
 
108
 
 
109
                // the peer is interested in our pieces
 
110
                void interested(peer_connection& c);
 
111
 
 
112
                // the peer is not interested in our pieces
 
113
                void not_interested(peer_connection& c);
 
114
 
 
115
                void ip_filter_updated();
 
116
 
 
117
#ifndef NDEBUG
 
118
                bool has_connection(const peer_connection* p);
 
119
 
 
120
                void check_invariant() const;
 
121
#endif
 
122
 
 
123
                struct peer
 
124
                {
 
125
                        enum connection_type { not_connectable, connectable };
 
126
 
 
127
                        peer(tcp::endpoint const& ip, connection_type t, int src);
 
128
 
 
129
                        size_type total_download() const;
 
130
                        size_type total_upload() const;
 
131
 
 
132
                        // the ip/port pair this peer is or was connected on
 
133
                        // if it was a remote (incoming) connection, type is
 
134
                        // set thereafter. If it was a peer we got from the
 
135
                        // tracker, type is set to local_connection.
 
136
                        tcp::endpoint ip;
 
137
                        connection_type type;
 
138
 
 
139
#ifndef TORRENT_DISABLE_ENCRYPTION
 
140
                        // Hints encryption support of peer. Only effective for
 
141
                        // and when the outgoing encryption policy allows both
 
142
                        // encrypted and non encrypted connections
 
143
                        // (pe_settings::out_enc_policy == enabled). The initial
 
144
                        // state of this flag determines the initial connection
 
145
                        // attempt type (true = encrypted, false = standard).
 
146
                        // This will be toggled everytime either an encrypted or
 
147
                        // non-encrypted handshake fails.
 
148
                        bool pe_support;
 
149
#endif
 
150
                        // the number of failed connection attempts this peer has
 
151
                        int failcount;
 
152
 
 
153
                        // the number of times this peer has been
 
154
                        // part of a piece that failed the hash check
 
155
                        int hashfails;
 
156
 
 
157
                        // this is true if the peer is a seed
 
158
                        bool seed;
 
159
 
 
160
                        int fast_reconnects;
 
161
 
 
162
                        // true if this peer currently is unchoked
 
163
                        // because of an optimistic unchoke.
 
164
                        // when the optimistic unchoke is moved to
 
165
                        // another peer, this peer will be choked
 
166
                        // if this is true
 
167
                        bool optimistically_unchoked;
 
168
 
 
169
                        // the time when this peer was optimistically unchoked
 
170
                        // the last time.
 
171
                        libtorrent::ptime last_optimistically_unchoked;
 
172
 
 
173
                        // the time when the peer connected to us
 
174
                        // or disconnected if it isn't connected right now
 
175
                        libtorrent::ptime connected;
 
176
 
 
177
                        // for every valid piece we receive where this
 
178
                        // peer was one of the participants, we increase
 
179
                        // this value. For every invalid piece we receive
 
180
                        // where this peer was a participant, we decrease
 
181
                        // this value. If it sinks below a threshold, its
 
182
                        // considered a bad peer and will be banned.
 
183
                        int trust_points;
 
184
 
 
185
                        // if this is true, the peer has previously participated
 
186
                        // in a piece that failed the piece hash check. This will
 
187
                        // put the peer on parole and only request entire pieces.
 
188
                        // if a piece pass that was partially requested from this
 
189
                        // peer it will leave parole mode and continue download
 
190
                        // pieces as normal peers.
 
191
                        bool on_parole;
 
192
 
 
193
                        // this is the accumulated amount of
 
194
                        // uploaded and downloaded data to this
 
195
                        // peer. It only accounts for what was
 
196
                        // shared during the last connection to
 
197
                        // this peer. i.e. These are only updated
 
198
                        // when the connection is closed. For the
 
199
                        // total amount of upload and download
 
200
                        // we'll have to add thes figures with the
 
201
                        // statistics from the peer_connection.
 
202
                        size_type prev_amount_upload;
 
203
                        size_type prev_amount_download;
 
204
 
 
205
                        // is set to true if this peer has been banned
 
206
                        bool banned;
 
207
 
 
208
                        // a bitmap combining the peer_source flags
 
209
                        // from peer_info.
 
210
                        int source;
 
211
 
 
212
                        // if the peer is connected now, this
 
213
                        // will refer to a valid peer_connection
 
214
                        peer_connection* connection;
 
215
                };
 
216
 
 
217
                int num_peers() const { return m_peers.size(); }
 
218
 
 
219
                typedef std::multimap<address, peer>::iterator iterator;
 
220
                typedef std::multimap<address, peer>::const_iterator const_iterator;
 
221
                iterator begin_peer() { return m_peers.begin(); }
 
222
                iterator end_peer() { return m_peers.end(); }
 
223
                const_iterator begin_peer() const { return m_peers.begin(); }
 
224
                const_iterator end_peer() const { return m_peers.end(); }
 
225
 
 
226
                bool connect_one_peer();
 
227
                bool disconnect_one_peer();
 
228
 
 
229
        private:
 
230
/*
 
231
                bool unchoke_one_peer();
 
232
                void choke_one_peer();
 
233
                iterator find_choke_candidate();
 
234
                iterator find_unchoke_candidate();
 
235
 
 
236
                // the seed prefix means that the
 
237
                // function is used while seeding.
 
238
                bool seed_unchoke_one_peer();
 
239
                void seed_choke_one_peer();
 
240
                iterator find_seed_choke_candidate();
 
241
                iterator find_seed_unchoke_candidate();
 
242
*/
 
243
                iterator find_disconnect_candidate();
 
244
                iterator find_connect_candidate();
 
245
 
 
246
                std::multimap<address, peer> m_peers;
 
247
 
 
248
                torrent* m_torrent;
 
249
 
 
250
                // free download we have got that hasn't
 
251
                // been distributed yet.
 
252
                size_type m_available_free_upload;
 
253
 
 
254
                // if there is a connection limit,
 
255
                // we disconnect one peer every minute in hope of
 
256
                // establishing a connection with a better peer
 
257
//              ptime m_last_optimistic_disconnect;
 
258
        };
 
259
 
 
260
}
 
261
 
 
262
#endif // TORRENT_POLICY_HPP_INCLUDED
 
263