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_DATA_DOWNLOAD_DATA_H
38 #define LIBTORRENT_DATA_DOWNLOAD_DATA_H
39 
40 #include lt_tr1_functional
41 
42 #include <torrent/common.h>
43 #include <torrent/bitfield.h>
44 #include <torrent/hash_string.h>
45 #include <torrent/utils/ranges.h>
46 
47 namespace torrent {
48 class ChunkListNode;
49 class ChunkSelector;
50 class Download;
51 class DownloadWrapper;
52 class FileList;
53 
54 class download_data {
55 public:
56   typedef ranges<uint32_t> priority_ranges;
57 
58   typedef void (function_void)(void);
59 
60   typedef std::function<function_void> slot_void;
61 
62   typedef void (function_chunk_list_node_p)(ChunkListNode *);
63   typedef std::function<function_chunk_list_node_p> slot_chunk_list_node_p;
download_data()64   download_data() : m_wanted_chunks(0) {}
65 
hash()66   const HashString&      hash() const                  { return m_hash; }
67 
is_partially_done()68   bool                   is_partially_done() const     { return m_wanted_chunks == 0; }
is_not_partially_done()69   bool                   is_not_partially_done() const { return m_wanted_chunks != 0; }
70 
completed_bitfield()71   const Bitfield*        completed_bitfield() const    { return &m_completed_bitfield; }
untouched_bitfield()72   const Bitfield*        untouched_bitfield() const    { return &m_untouched_bitfield; }
73 
high_priority()74   const priority_ranges* high_priority() const         { return &m_high_priority; }
normal_priority()75   const priority_ranges* normal_priority() const       { return &m_normal_priority; }
76 
wanted_chunks()77   uint32_t               wanted_chunks() const         { return m_wanted_chunks; }
78 
79   uint32_t               calc_wanted_chunks() const;
80   void                   verify_wanted_chunks(const char* where) const;
81 
slot_initial_hash()82   slot_void&             slot_initial_hash() const        { return m_slot_initial_hash; }
slot_download_done()83   slot_void&             slot_download_done() const       { return m_slot_download_done; }
slot_partially_done()84   slot_void&             slot_partially_done() const      { return m_slot_partially_done; }
slot_partially_restarted()85   slot_void&             slot_partially_restarted() const { return m_slot_partially_restarted; }
slot_chunk_done()86   slot_chunk_list_node_p& slot_chunk_done() const          {return m_slot_chunk_done;}
87 
88 protected:
89   friend class ChunkList;
90   friend class ChunkSelector;
91   friend class Download;
92   friend class DownloadWrapper;
93   friend class FileList;
94 
mutable_hash()95   HashString&            mutable_hash()                { return m_hash; }
96 
mutable_completed_bitfield()97   Bitfield*              mutable_completed_bitfield()  { return &m_completed_bitfield; }
mutable_untouched_bitfield()98   Bitfield*              mutable_untouched_bitfield()  { return &m_untouched_bitfield; }
99 
mutable_high_priority()100   priority_ranges*       mutable_high_priority()       { return &m_high_priority; }
mutable_normal_priority()101   priority_ranges*       mutable_normal_priority()     { return &m_normal_priority; }
102 
update_wanted_chunks()103   void                   update_wanted_chunks()        { m_wanted_chunks = calc_wanted_chunks(); }
set_wanted_chunks(uint32_t n)104   void                   set_wanted_chunks(uint32_t n) { m_wanted_chunks = n; }
105 
call_download_done()106   void                   call_download_done()          { if (m_slot_download_done) m_slot_download_done(); }
call_partially_done()107   void                   call_partially_done()         { if (m_slot_partially_done) m_slot_partially_done(); }
call_partially_restarted()108   void                   call_partially_restarted()    { if (m_slot_partially_restarted) m_slot_partially_restarted(); }
call_chunk_done(ChunkListNode * chunk_ptr)109   void                   call_chunk_done(ChunkListNode* chunk_ptr) {if(m_slot_chunk_done) m_slot_chunk_done(chunk_ptr);}
110 private:
111   HashString             m_hash;
112 
113   Bitfield               m_completed_bitfield;
114   Bitfield               m_untouched_bitfield;
115 
116   priority_ranges        m_high_priority;
117   priority_ranges        m_normal_priority;
118 
119   uint32_t               m_wanted_chunks;
120 
121   mutable slot_void      m_slot_initial_hash;
122   mutable slot_void      m_slot_download_done;
123   mutable slot_void      m_slot_partially_done;
124   mutable slot_void      m_slot_partially_restarted;
125   mutable slot_chunk_list_node_p m_slot_chunk_done;
126 };
127 
128 }
129 
130 #endif // LIBTORRENT_DATA_DOWNLOAD_DATA_H
131