root/hydranode/hncore/bt/torrent.h

Revision 2913, 5.9 kB (checked in by madcat, 3 years ago)

Multi-tracker support.

Line 
1 /*
2  *  Copyright (C) 2005-2006 Alo Sarv <madcat_@users.sourceforge.net>
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
19 /**
20  * \file torrent.h       Interface for Torrent class
21  */
22
23 #ifndef __BT_TORRENT_H__
24 #define __BT_TORRENT_H__
25
26 #include <hnbase/ssocket.h>
27 #include <hnbase/hostinfo.h>
28 #include <hncore/bt/torrentinfo.h>
29 #include <hncore/bt/types.h>
30 #include <hncore/bt/client.h>
31 #include <boost/utility.hpp>
32
33 namespace Bt {
34
35 enum TorrentEvent {
36         EVT_DESTROY = 1
37 };
38
39 class Tracker;
40
41 /**
42  * Torrent class represents a single torrent, which is either being downloaded
43  * or seeded currently. It wraps together three components - TorrentFile,
44  * PartialTorrent (optional) and Clients (peers) set. It also is in charge of
45  * tracker connections (if the torrent has tracker).
46  *
47  * Client objects are owned by this torrent when they are attached to it. It
48  * also handles a number of signals from Client objects, namely connectionLost
49  * and handshakeReceived.
50  */
51 class Torrent : public Trackable {
52 public:
53         DECLARE_EVENT_TABLE(Torrent*, int);
54
55         /**
56          * Construct a Torrent from a TorrentFile and torrent info data.
57          *
58          * @param file         File this torrent represents
59          * @param info         Contains extra information about this torrent
60          */
61         Torrent(SharedFile *file, const TorrentInfo &info);
62
63         /**
64          * Destroys this torrent and all related clients.
65          */
66         ~Torrent();
67
68         /**
69          * @name Generic accessors
70          */
71         //!@{
72         Hash<SHA1Hash> getInfoHash()   const { return m_info.getInfoHash();  }
73         uint64_t       getChunkSize()  const { return m_info.getChunkSize(); }
74         uint32_t       getChunkCnt()   const { return m_info.getChunkCnt();  }
75         std::string    getName()       const { return m_info.getName();      }
76         uint64_t       getUploaded()   const { return m_uploaded;            }
77         uint64_t       getDownloaded() const { return m_downloaded;          }
78         PartData*      getPartData()   const { return m_partData;            }
79         //!@}
80
81         /**
82          * Add a client to this Torrent; this class takes ownership of the
83          * object.
84          *
85          * @param c      Client to be added.
86          *
87          * \note If the client doesn't belong to this torrent (based on the
88          *       info_hash value sent in handshake), it may be immediately
89          *       destroyed.
90          */
91         void addClient(Client *c);
92
93         /**
94          * Add a request to the request queue
95          *
96          * @param r         Request to be added
97          */
98         void addRequest(const Client::Request &r);
99
100         /**
101          * Remove a specific request from the request listing
102          *
103          * @param r        Request to be removed
104          */
105         void delRequest(const Client::Request &r);
106
107         /**
108          * Get a request; returns the oldest-added request, with the logic being
109          * that if a request hasn't been answered for a long time, it's most
110          * likely a slow source, and this method will allow re-allocating the
111          * request to a faster source.
112          *
113          * @param chunks  Chunks the client has
114          * @returns       A request to be sent
115          */
116         Client::Request getRequest(const std::vector<bool> &chunks);
117
118         /**
119          * Clear all requests with the specified index
120          *
121          * @param index         Index to be cleared
122          */
123         void clearRequests(uint32_t index);
124
125         /**
126          * Try to unchoke one of the interested clients and start uploading
127          *
128          * @param c          Optionally, client that called this
129          * @param dontChoke  Set to true if @c should continue uploading
130          * @returns true if uploading was started, false otherwise
131          *
132          * The purpose of the two parameters is that Client::sendChoke
133          * method calls this method, and passes those two pointers; if
134          * this method decides that the client should continue uploading,
135          * it will set dontChoke pointer to true.
136          */
137         bool tryUnchoke(Client *c = 0, bool *dontChoke = 0);
138
139         /**
140          * @returns Number of peers connected for this torrent
141          */
142         size_t getPeerCount() const { return m_clients.size(); }
143
144         //! Increase uploaded data counter (this session)
145         void addUploaded(uint32_t amount);
146
147         //! Increase downloaded data counter (this session)
148         void addDownloaded(uint32_t amount) { m_downloaded += amount; }
149
150         /**
151          * Returns availability bitfield for this torrent
152          */
153         std::string getBitfield() const { return m_bitField; }
154 private:
155         Torrent(const Torrent&);
156         Torrent& operator=(const Torrent&);
157
158         /**
159          * @name Event handlers
160          */
161         //!@{
162         void onPartDataEvent(PartData *file, int evt);
163         void onSharedFileEvent(SharedFile *file, int evt);
164         //!@}
165
166         /**
167          * @name Signal handlers
168          */
169         //!@{
170         void handshakeReceived(Client *c);
171         void connectionLost(Client *c);
172         void onChunkVerified(PartData *file, uint64_t csz, uint64_t chunk);
173         //!@}
174
175         /**
176          * Creates and attaches a peer to this torrent.
177          *
178          * @param addr       Peer ip/port info
179          */
180         void createClient(IPV4Address addr);
181
182         //! Initializes m_bitField member and connects onChunkVerified signal
183         void initBitfield();
184
185         SharedFile                  *m_file;      //!< Seeded file
186         PartData                    *m_partData;  //!< Downloaded file(optional)
187         TorrentInfo                  m_info;      //!< Info about this torrent
188
189         std::set<Tracker*>            m_trackers;
190
191         uint64_t    m_uploaded;    //!< How much we have downloaded this session
192         uint64_t    m_downloaded;  //!< How much we have uploaded this session
193
194         std::set<Client*> m_clients; //!< Peers attached to this torrent
195
196         //! Availability bitfield for this torrent
197         std::string m_bitField;
198
199         //! All outgoing requests
200         std::deque<boost::weak_ptr< ::Detail::LockedRange> > m_sharedReqs;
201 };
202
203 }
204
205 #endif
Note: See TracBrowser for help on using the browser.