1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
#ifndef LIBTORRENT_DOWNLOAD_MAIN_H
38
#define LIBTORRENT_DOWNLOAD_MAIN_H
41
#include <rak/functional.h>
42
#include <sigc++/signal.h>
46
#include "delegator.h"
48
#include "data/chunk_handle.h"
49
#include "download/available_list.h"
50
#include "net/data_buffer.h"
51
#include "torrent/download_info.h"
52
#include "torrent/data/file_list.h"
53
#include "torrent/peer/peer_list.h"
59
class ChunkStatistics;
63
class DownloadWrapper;
64
class HandshakeManager;
72
typedef std::deque<std::pair<rak::timer, uint32_t> > have_queue_type;
73
typedef std::vector<SocketAddressCompact> pex_list;
84
ChokeManager* upload_choke_manager() { return m_uploadChokeManager; }
85
ChokeManager* download_choke_manager() { return m_downloadChokeManager; }
86
TrackerManager* tracker_manager() const { return m_trackerManager; }
88
DownloadInfo* info() { return m_info; }
90
// Only retrieve writable chunks when the download is active.
91
ChunkList* chunk_list() { return m_chunkList; }
92
ChunkSelector* chunk_selector() { return m_chunkSelector; }
93
ChunkStatistics* chunk_statistics() { return m_chunkStatistics; }
95
Delegator* delegator() { return &m_delegator; }
97
have_queue_type* have_queue() { return &m_haveQueue; }
99
InitialSeeding* initial_seeding() { return m_initialSeeding; }
100
bool start_initial_seeding();
101
void initial_seeding_done(PeerConnectionBase* pcb);
103
ConnectionList* connection_list() { return m_connectionList; }
104
FileList* file_list() { return &m_fileList; }
105
PeerList* peer_list() { return &m_peerList; }
107
std::pair<ThrottleList*, ThrottleList*> throttles(const sockaddr* sa);
109
ThrottleList* upload_throttle() { return m_uploadThrottle; }
110
void set_upload_throttle(ThrottleList* t) { m_uploadThrottle = t; }
112
ThrottleList* download_throttle() { return m_downloadThrottle; }
113
void set_download_throttle(ThrottleList* t) { m_downloadThrottle = t; }
115
DataBuffer get_ut_pex(bool initial) { return (initial ? m_ut_pex_initial : m_ut_pex_delta).clone(); }
117
bool want_pex_msg() { return m_info->is_pex_active() && m_peerList.available_list()->want_more(); };
119
void set_metadata_size(size_t s);
121
// Carefull with these.
122
void setup_delegator();
123
void setup_tracker();
125
typedef rak::const_mem_fun1<HandshakeManager, uint32_t, DownloadMain*> SlotCountHandshakes;
126
typedef rak::mem_fun1<DownloadWrapper, void, ChunkHandle> SlotHashCheckAdd;
128
typedef rak::mem_fun2<HandshakeManager, void, const rak::socket_address&, DownloadMain*> slot_start_handshake_type;
129
typedef rak::mem_fun1<HandshakeManager, void, DownloadMain*> slot_stop_handshakes_type;
131
void slot_start_handshake(slot_start_handshake_type s) { m_slotStartHandshake = s; }
132
void slot_stop_handshakes(slot_stop_handshakes_type s) { m_slotStopHandshakes = s; }
133
void slot_count_handshakes(SlotCountHandshakes s) { m_slotCountHandshakes = s; }
134
void slot_hash_check_add(SlotHashCheckAdd s) { m_slotHashCheckAdd = s; }
136
void add_peer(const rak::socket_address& sa);
138
void receive_connect_peers();
139
void receive_chunk_done(unsigned int index);
140
void receive_corrupt_chunk(PeerInfo* peerInfo);
142
void receive_tracker_success();
143
void receive_tracker_request();
145
void receive_do_peer_exchange();
147
void do_peer_exchange();
149
void update_endgame();
151
rak::priority_item& delay_disconnect_peers() { return m_delayDisconnectPeers; }
152
rak::priority_item& delay_download_done() { return m_delayDownloadDone; }
155
// Disable copy ctor and assignment.
156
DownloadMain(const DownloadMain&);
157
void operator = (const DownloadMain&);
162
DownloadInfo* m_info;
164
TrackerManager* m_trackerManager;
165
ChokeManager* m_uploadChokeManager;
166
ChokeManager* m_downloadChokeManager;
168
ChunkList* m_chunkList;
169
ChunkSelector* m_chunkSelector;
170
ChunkStatistics* m_chunkStatistics;
172
Delegator m_delegator;
173
have_queue_type m_haveQueue;
174
InitialSeeding* m_initialSeeding;
176
ConnectionList* m_connectionList;
180
uint32_t m_lastConnectedSize;
182
DataBuffer m_ut_pex_delta;
183
DataBuffer m_ut_pex_initial;
184
pex_list m_ut_pex_list;
186
ThrottleList* m_uploadThrottle;
187
ThrottleList* m_downloadThrottle;
189
slot_start_handshake_type m_slotStartHandshake;
190
slot_stop_handshakes_type m_slotStopHandshakes;
192
SlotCountHandshakes m_slotCountHandshakes;
193
SlotHashCheckAdd m_slotHashCheckAdd;
195
rak::priority_item m_delayDownloadDone;
196
rak::priority_item m_delayDisconnectPeers;
197
rak::priority_item m_taskTrackerRequest;