1 // libTorrent - BitTorrent library
2 // Copyright (C) 2005-2011, Jari Sundell
3 //
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.
8 //
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.
13 //
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
17 //
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
22 // including the two.
23 //
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.
31 //
32 // Contact:  Jari Sundell <jaris@ifi.uio.no>
33 //
34 //           Skomakerveien 33
35 //           3185 Skoppum, NORWAY
36 
37 #ifndef LIBTORRENT_DOWNLOAD_H
38 #define LIBTORRENT_DOWNLOAD_H
39 
40 #include <list>
41 #include <vector>
42 #include <string>
43 
44 #include <torrent/common.h>
45 #include <torrent/peer/peer.h>
46 
47 namespace torrent {
48 
49 class ConnectionList;
50 class DownloadInfo;
51 class DownloadMain;
52 class download_data;
53 class TrackerController;
54 
55 // Download is safe to copy and destory as it is just a pointer to an
56 // internal class.
57 
58 class LIBTORRENT_EXPORT Download {
59 public:
60   static const uint32_t numwanted_diabled = ~uint32_t();
61 
62   // Start and open flags can be stored in the same integer, same for
63   // stop and close flags.
64   static const int open_enable_fallocate = (1 << 0);
65 
66   static const int start_no_create       = (1 << 1);
67   static const int start_keep_baseline   = (1 << 2);
68   static const int start_skip_tracker    = (1 << 3);
69 
70   static const int stop_skip_tracker     = (1 << 0);
71 
m_ptr(d)72   Download(DownloadWrapper* d = NULL) : m_ptr(d) {}
73 
74   const DownloadInfo*  info() const;
75   const download_data* data() const;
76 
77   // Not active atm. Opens and prepares/closes the files.
78   void                open(int flags = 0);
79   void                close(int flags = 0);
80 
81   // When 'tryQuick' is true, it will only check if the chunks can be
82   // mmaped and stops if one is encountered. If it doesn't find any
83   // mappable chunks it will return true to indicate that it is
84   // finished and a hash done signal has been queued.
85   //
86   // Chunk ranges that have valid resume data won't be checked.
87   bool                hash_check(bool tryQuick);
88   void                hash_stop();
89 
90   // Start/stop the download. The torrent must be open.
91   void                start(int flags = 0);
92   void                stop(int flags = 0);
93 
94   // Does not check if the download has been removed.
is_valid()95   bool                is_valid() const { return m_ptr; }
96 
97   bool                is_hash_checked() const;
98   bool                is_hash_checking() const;
99 
100   void                set_pex_enabled(bool enabled);
101 
102   Object*             bencode();
103   const Object*       bencode() const;
104 
105   TrackerController*  tracker_controller() const;
106   TrackerList*        tracker_list() const;
107 
108   FileList*           file_list() const;
109   PeerList*           peer_list();
110   const PeerList*     peer_list() const;
111   const TransferList* transfer_list() const;
112 
113   ConnectionList*       connection_list();
114   const ConnectionList* connection_list() const;
115 
116   // Bytes completed.
117   uint64_t            bytes_done() const;
118 
119   uint32_t            chunks_hashed() const;
120 
121   const uint8_t*      chunks_seen() const;
122 
123   // Set the number of finished chunks for closed torrents.
124   void                set_chunks_done(uint32_t chunks_done, uint32_t chunks_wanted);
125 
126   // Use the below to set the resume data and what chunk ranges need
127   // to be hash checked. If they arn't called then by default it will
128   // use an cleared bitfield and check the whole range.
129   //
130   // These must be called when is_open, !is_checked and !is_checking.
131   void                set_bitfield(bool allSet);
132   void                set_bitfield(uint8_t* first, uint8_t* last);
133 
134   static const int update_range_recheck = (1 << 0);
135   static const int update_range_clear   = (1 << 1);
136 
137   void                update_range(int flags, uint32_t first, uint32_t last);
138 
139   // Temporary hack for syncing chunks to disk before hash resume is
140   // saved.
141   void                sync_chunks();
142 
143   uint32_t            peers_complete() const;
144   uint32_t            peers_accounted() const;
145 
146   uint32_t            peers_currently_unchoked() const;
147   uint32_t            peers_currently_interested() const;
148 
149   uint32_t            size_pex() const;
150   uint32_t            max_size_pex() const;
151 
152   bool                accepting_new_peers() const;
153 
154   uint32_t            uploads_max() const;
155   void                set_uploads_max(uint32_t v);
156 
157   uint32_t            uploads_min() const;
158   void                set_uploads_min(uint32_t v);
159 
160   uint32_t            downloads_max() const;
161   void                set_downloads_max(uint32_t v);
162 
163   uint32_t            downloads_min() const;
164   void                set_downloads_min(uint32_t v);
165 
166   void                set_upload_throttle(Throttle* t);
167   void                set_download_throttle(Throttle* t);
168 
169   // Some temporary functions that are routed to
170   // TrackerManager... Clean this up.
171   void                send_completed();
172 
173   void                manual_request(bool force);
174   void                manual_cancel();
175 
176   typedef enum {
177     CONNECTION_LEECH,
178     CONNECTION_SEED,
179     CONNECTION_INITIAL_SEED,
180     CONNECTION_METADATA,
181   } ConnectionType;
182 
183   ConnectionType      connection_type() const;
184   void                set_connection_type(ConnectionType t);
185 
186   typedef enum {
187   } HeuristicType;
188 
189   HeuristicType       upload_choke_heuristic() const;
190   void                set_upload_choke_heuristic(HeuristicType t);
191 
192   HeuristicType       download_choke_heuristic() const;
193   void                set_download_choke_heuristic(HeuristicType t);
194 
195   // Call this when you want the modifications of the download priorities
196   // in the entries to take effect. It is slightly expensive as it rechecks
197   // all the peer bitfields to see if we are still interested.
198   void                update_priorities();
199 
200   void                add_peer(const sockaddr* addr, int port);
201 
ptr()202   DownloadWrapper*    ptr() { return m_ptr; }
203   DownloadMain*       main();
204 
205 private:
206   DownloadWrapper*    m_ptr;
207 };
208 
209 }
210 
211 #endif
212