1 /***************************************************************************
3 * Copyright (C) 2006 David Brodsky *
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. *
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. *
15 ***************************************************************************/
17 #ifndef _main_torrentclient_h
18 #define _main_torrentclient_h
22 #include <tairon/core/string.h>
24 using Tairon::Core::String
;
57 /** Struct with informations about one peer that this client can connect to.
65 /** IP address of the peer.
69 /** Port on which the peer is listening.
74 /** \brief TorrentClient manages running of one torrent.
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.
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() {
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() {
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.
188 /** This is connected to Storage::invalidDataSignal and closes the connection.
190 void invalidDataReceived(Connection
*c
);
192 /** Chokes/unchokes peers.
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
203 void pieceDownloadedAndChecked(uint32_t index
);
206 /** Mapping peer id => connection object. These connections are fully
209 std::map
<String
, Connection
*> connections
;
211 /** Number of bytes downloaded so far.
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.
227 /** Minimum number of uploading slots.
231 /** Mapping peer id => connection object. These connections are not
234 std::map
<String
, Connection
*> pendingConnections
;
236 /** Storage for this torrent.
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.
259 }; // namespace Tairent
263 // vim: ai sw=4 ts=4 noet fdm=marker