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_INFO_H
38 #define LIBTORRENT_DOWNLOAD_INFO_H
39 
40 #include <list>
41 #include <string>
42 #include <inttypes.h>
43 #include lt_tr1_functional
44 
45 #include <torrent/rate.h>
46 #include <torrent/hash_string.h>
47 
48 namespace torrent {
49 
50 class FileList;
51 class DownloadMain;
52 
53 // This will become a Download 'handle' of kinds.
54 
55 class LIBTORRENT_EXPORT DownloadInfo {
56 public:
57   typedef std::function<uint64_t ()>                              slot_stat_type;
58 
59   typedef std::list<std::function<void ()> >                      signal_void_type;
60   typedef std::list<std::function<void (const std::string&)> >    signal_string_type;
61 
62   enum State {
63     NONE,
64     COMPLETED,
65     STARTED,
66     STOPPED
67   };
68 
69   static const int flag_open                = (1 << 0);
70   static const int flag_active              = (1 << 1);
71   static const int flag_compact             = (1 << 2);
72   static const int flag_accepting_new_peers = (1 << 3);
73   static const int flag_accepting_seeders   = (1 << 4); // Only used during leeching.
74   static const int flag_private             = (1 << 5);
75   static const int flag_meta_download       = (1 << 6);
76   static const int flag_pex_enabled         = (1 << 7);
77   static const int flag_pex_active          = (1 << 8);
78 
79   static const int public_flags = flag_accepting_seeders;
80 
81   static const uint32_t unlimited = ~uint32_t();
82 
83   DownloadInfo();
84 
name()85   const std::string&  name() const                                 { return m_name; }
set_name(const std::string & s)86   void                set_name(const std::string& s)               { m_name = s; }
87 
hash()88   const HashString&   hash() const                                 { return m_hash; }
mutable_hash()89   HashString&         mutable_hash()                               { return m_hash; }
90 
hash_obfuscated()91   const HashString&   hash_obfuscated() const                      { return m_hashObfuscated; }
mutable_hash_obfuscated()92   HashString&         mutable_hash_obfuscated()                    { return m_hashObfuscated; }
93 
local_id()94   const HashString&   local_id() const                             { return m_localId; }
mutable_local_id()95   HashString&         mutable_local_id()                           { return m_localId; }
96 
is_open()97   bool                is_open() const                              { return m_flags & flag_open; }
is_active()98   bool                is_active() const                            { return m_flags & flag_active; }
is_compact()99   bool                is_compact() const                           { return m_flags & flag_compact; }
is_accepting_new_peers()100   bool                is_accepting_new_peers() const               { return m_flags & flag_accepting_new_peers; }
is_accepting_seeders()101   bool                is_accepting_seeders() const                 { return m_flags & flag_accepting_seeders; }
is_private()102   bool                is_private() const                           { return m_flags & flag_private; }
is_meta_download()103   bool                is_meta_download() const                     { return m_flags & flag_meta_download; }
is_pex_enabled()104   bool                is_pex_enabled() const                       { return m_flags & flag_pex_enabled; }
is_pex_active()105   bool                is_pex_active() const                        { return m_flags & flag_pex_active; }
106 
flags()107   int                 flags() const                                { return m_flags; }
108 
set_flags(int flags)109   void                set_flags(int flags)                         { m_flags |= flags; }
unset_flags(int flags)110   void                unset_flags(int flags)                       { m_flags &= ~flags; }
change_flags(int flags,bool state)111   void                change_flags(int flags, bool state)          { if (state) set_flags(flags); else unset_flags(flags); }
112 
public_set_flags(int flags)113   void                public_set_flags(int flags) const                { m_flags |= (flags & public_flags); }
public_unset_flags(int flags)114   void                public_unset_flags(int flags) const              { m_flags &= ~(flags & public_flags); }
public_change_flags(int flags,bool state)115   void                public_change_flags(int flags, bool state) const { if (state) public_set_flags(flags); else public_unset_flags(flags); }
116 
set_private()117   void                set_private()                                { set_flags(flag_private); unset_flags(flag_pex_enabled); }
set_pex_enabled()118   void                set_pex_enabled()                            { if (!is_private()) set_flags(flag_pex_enabled); }
119 
up_rate()120   const Rate*         up_rate() const                              { return &m_upRate; }
down_rate()121   const Rate*         down_rate() const                            { return &m_downRate; }
skip_rate()122   const Rate*         skip_rate() const                            { return &m_skipRate; }
123 
mutable_up_rate()124   Rate*               mutable_up_rate() const                      { return &m_upRate; }
mutable_down_rate()125   Rate*               mutable_down_rate() const                    { return &m_downRate; }
mutable_skip_rate()126   Rate*               mutable_skip_rate() const                    { return &m_skipRate; }
127 
uploaded_baseline()128   uint64_t            uploaded_baseline() const                    { return m_uploadedBaseline; }
uploaded_adjusted()129   uint64_t            uploaded_adjusted() const                    { return std::max<int64_t>(m_upRate.total() - uploaded_baseline(), 0); }
set_uploaded_baseline(uint64_t b)130   void                set_uploaded_baseline(uint64_t b)            { m_uploadedBaseline = b; }
131 
completed_baseline()132   uint64_t            completed_baseline() const                   { return m_completedBaseline; }
completed_adjusted()133   uint64_t            completed_adjusted() const                   { return std::max<int64_t>(m_slotStatCompleted() - completed_baseline(), 0); }
set_completed_baseline(uint64_t b)134   void                set_completed_baseline(uint64_t b)           { m_completedBaseline = b; }
135 
metadata_size()136   size_t              metadata_size() const                        { return m_metadataSize; }
set_metadata_size(size_t size)137   void                set_metadata_size(size_t size)               { m_metadataSize = size; }
138 
size_pex()139   uint32_t            size_pex() const                             { return m_sizePex; }
set_size_pex(uint32_t b)140   void                set_size_pex(uint32_t b)                     { m_sizePex = b; }
141 
max_size_pex()142   uint32_t            max_size_pex() const                         { return m_maxSizePex; }
set_max_size_pex(uint32_t b)143   void                set_max_size_pex(uint32_t b)                 { m_maxSizePex = b; }
144 
max_size_pex_list()145   uint32_t            max_size_pex_list() const                    { return 200; }
146 
147   // Unix epoche, 0 == unknown.
creation_date()148   uint32_t            creation_date() const                        { return m_creationDate; }
load_date()149   uint32_t            load_date() const                            { return m_loadDate; }
150 
http_timeout()151   uint32_t            http_timeout() const                         { return 60; }
udp_timeout()152   uint32_t            udp_timeout() const                          { return 30; }
udp_tries()153   uint32_t            udp_tries() const                            { return 2; }
154 
upload_unchoked()155   uint32_t            upload_unchoked() const                      { return m_upload_unchoked; }
download_unchoked()156   uint32_t            download_unchoked() const                    { return m_download_unchoked; }
157 
158   // The list of addresses is guaranteed to be sorted and unique.
signal_tracker_success()159   signal_void_type&   signal_tracker_success() const               { return m_signalTrackerSuccess; }
signal_tracker_failed()160   signal_string_type& signal_tracker_failed() const                { return m_signalTrackerFailed; }
161 
162   //
163   // Libtorrent internal:
164   //
165 
set_creation_date(uint32_t d)166   void                set_creation_date(uint32_t d)                { m_creationDate = d; }
167 
set_upload_unchoked(uint32_t num)168   void                set_upload_unchoked(uint32_t num)            { m_upload_unchoked = num; }
set_download_unchoked(uint32_t num)169   void                set_download_unchoked(uint32_t num)          { m_download_unchoked = num; }
170 
slot_left()171   slot_stat_type&     slot_left()                                  { return m_slotStatLeft; }
slot_completed()172   slot_stat_type&     slot_completed()                             { return m_slotStatCompleted; }
173 
174 private:
175   std::string         m_name;
176   HashString          m_hash;
177   HashString          m_hashObfuscated;
178   HashString          m_localId;
179 
180   mutable int         m_flags;
181 
182   mutable Rate        m_upRate;
183   mutable Rate        m_downRate;
184   mutable Rate        m_skipRate;
185 
186   uint64_t            m_uploadedBaseline;
187   uint64_t            m_completedBaseline;
188   uint32_t            m_sizePex;
189   uint32_t            m_maxSizePex;
190   size_t              m_metadataSize;
191 
192   uint32_t            m_creationDate;
193   uint32_t            m_loadDate;
194 
195   uint32_t            m_upload_unchoked;
196   uint32_t            m_download_unchoked;
197 
198   slot_stat_type      m_slotStatLeft;
199   slot_stat_type      m_slotStatCompleted;
200 
201   mutable signal_void_type    m_signalTrackerSuccess;
202   mutable signal_string_type  m_signalTrackerFailed;
203 };
204 
205 }
206 
207 #endif
208