Add main page for Doxygen generated documentation.
[tairent.git] / src / main / torrentclient.h
blob5e29d1d7e8ec75f76385feb9d1412d3172657224
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _main_torrentclient_h
18 #define _main_torrentclient_h
20 #include <map>
22 #include <tairon/core/string.h>
24 using Tairon::Core::String;
26 namespace Tairon
29 namespace Net
32 class Timer;
38 namespace Tairent
41 namespace Core
44 class BEncode;
48 namespace Main
51 struct TorrentStruct;
53 class Connection;
54 class Storage;
55 class TrackerClient;
57 /** Struct with informations about one peer that this client can connect to.
59 struct PeerStruct
61 /** Peer's id.
63 String id;
65 /** IP address of the peer.
67 String ip;
69 /** Port on which the peer is listening.
71 uint16_t port;
74 /** \brief TorrentClient manages running of one torrent.
76 class TorrentClient
78 public:
79 /** Creates a TorrentClient object.
81 * \param t Structure that contains informations about this torrent.
82 * \param ih 20 byte SHA1 hash of the info field of metainfo.
83 * \param s Storage created with fast-resume data.
85 TorrentClient(TorrentStruct *t, const String &ih, Storage *s = 0);
87 /** Destroys the object.
89 ~TorrentClient();
91 /** Informs this client about new possible peers.
93 void addPeers(const std::list<PeerStruct> &peers);
95 /** Called when a connecting to a peer failed. Erases connection from
96 * the list pending connections and deletes that connection.
98 void connectingFailed(Connection *c);
100 /** Called when a connection has been closed.
102 void connectionClosed(Connection *c);
104 /** Returns number of bytes downloaded so far.
106 uint64_t getDownloadedSize() {
107 return downloaded;
110 /** Returns 20 byte SHA1 hash of the info field of metainfo.
112 const String &getInfoHash();
114 /** Returns metainfo associated with this client.
116 const Tairent::Core::BEncode &getMetaInfo();
118 /** Returns number of bytes that are still left to download.
120 uint64_t getRemainingSize();
122 /** Returns client's Storage object.
124 Storage *getStorage();
126 /** Returns number of bytes uploaded so far.
128 uint64_t getUploadedSize() {
129 return uploaded;
132 /** Called when we get a peer bitfield.
134 void gotBitField(Connection *c);
136 /** Called when we get a choke.
138 void gotChoke(Connection *c);
140 /** Called when we get a message that a peer has something new.
142 * \param index Index of the piece the peer has.
143 * \param c Connection with the peer.
145 void gotHave(uint32_t index, Connection *c);
147 /** Called when the peer changes its interested state.
149 void gotInterestedChange(Connection *c);
151 /** Called when we get a piece.
153 * \param index Index of the piece.
154 * \param start Start of the chunk within the piece.
155 * \param c Connection that got this message.
157 void gotPiece(uint32_t index, uint32_t start, Connection *c);
159 /** Called when we get an unchoke.
161 void gotUnchoke(Connection *c);
163 /** Called when a new peer is completely connected.
165 void newConnection(Connection *c);
167 /** Called when a complete piece is downloaded.
169 * \param index Index of the piece.
170 * \param start Start of the chunk within the piece.
171 * \param c Connection that got this message.
173 void pieceDownloaded(uint32_t index, uint32_t start, Connection *c);
175 /** Called when a piece has been sent to the peer.
177 * \param index Index of the piece.
178 * \param length Length of the piece.
179 * \param c Connection with the peer.
181 void pieceSent(uint32_t index, uint32_t length, Connection *c);
183 /** Stops this client.
185 void stop();
187 private:
188 /** This is connected to Storage::invalidDataSignal and closes the connection.
190 void invalidDataReceived(Connection *c);
192 /** Chokes/unchokes peers.
194 void rechoke();
196 /** Requests more pieces from the peer.
198 void requestMore(Connection *c);
200 /** Informs this client that a piece has been downloaded and its hash
201 * is correct.
203 void pieceDownloadedAndChecked(uint32_t index);
205 private:
206 /** Mapping peer id => connection object. These connections are fully
207 * established.
209 std::map<String, Connection *> connections;
211 /** Number of bytes downloaded so far.
213 uint64_t downloaded;
215 /** 20 byte SHA1 hash of the info field of metainfo.
217 const String &infoHash;
219 /** Maximum number of connections this client will handle.
221 unsigned int maxConnections;
223 /** Maximum number of uploading slots.
225 int maxUploads;
227 /** Minimum number of uploading slots.
229 int minUploads;
231 /** Mapping peer id => connection object. These connections are not
232 * fully established.
234 std::map<String, Connection *> pendingConnections;
236 /** Storage for this torrent.
238 Storage *storage;
240 /** Timer used for rechoking.
242 Tairon::Net::Timer *timer;
244 /** Informations about this torrent.
246 TorrentStruct *torrent;
248 /** Tracker used for this torrent.
250 TrackerClient *trackerClient;
252 /** Number of bytes uploaded so far.
254 uint64_t uploaded;
257 }; // namespace Main
259 }; // namespace Tairent
261 #endif
263 // vim: ai sw=4 ts=4 noet fdm=marker