author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef TORRENTCLIENT_H |
|
43 |
#define TORRENTCLIENT_H |
|
44 |
||
45 |
#include <QBitArray> |
|
46 |
#include <QHostAddress> |
|
47 |
#include <QList> |
|
48 |
||
49 |
class MetaInfo; |
|
50 |
class PeerWireClient; |
|
51 |
class TorrentClientPrivate; |
|
52 |
class TorrentPeer; |
|
53 |
class TorrentPiece; |
|
54 |
QT_BEGIN_NAMESPACE |
|
55 |
class QTimerEvent; |
|
56 |
QT_END_NAMESPACE |
|
57 |
||
58 |
class TorrentPeer { |
|
59 |
public: |
|
60 |
QHostAddress address; |
|
61 |
quint16 port; |
|
62 |
QString id; |
|
63 |
bool interesting; |
|
64 |
bool seed; |
|
65 |
uint lastVisited; |
|
66 |
uint connectStart; |
|
67 |
uint connectTime; |
|
68 |
QBitArray pieces; |
|
69 |
int numCompletedPieces; |
|
70 |
||
71 |
inline bool operator==(const TorrentPeer &other) |
|
72 |
{ |
|
73 |
return port == other.port |
|
74 |
&& address == other.address |
|
75 |
&& id == other.id; |
|
76 |
} |
|
77 |
}; |
|
78 |
||
79 |
class TorrentClient : public QObject |
|
80 |
{ |
|
81 |
Q_OBJECT |
|
82 |
||
83 |
public: |
|
84 |
enum State { |
|
85 |
Idle, |
|
86 |
Paused, |
|
87 |
Stopping, |
|
88 |
Preparing, |
|
89 |
Searching, |
|
90 |
Connecting, |
|
91 |
WarmingUp, |
|
92 |
Downloading, |
|
93 |
Endgame, |
|
94 |
Seeding |
|
95 |
}; |
|
96 |
enum Error { |
|
97 |
UnknownError, |
|
98 |
TorrentParseError, |
|
99 |
InvalidTrackerError, |
|
100 |
FileError, |
|
101 |
ServerError |
|
102 |
}; |
|
103 |
||
104 |
TorrentClient(QObject *parent = 0); |
|
105 |
~TorrentClient(); |
|
106 |
||
107 |
bool setTorrent(const QString &fileName); |
|
108 |
bool setTorrent(const QByteArray &torrentData); |
|
109 |
MetaInfo metaInfo() const; |
|
110 |
||
111 |
void setMaxConnections(int connections); |
|
112 |
int maxConnections() const; |
|
113 |
||
114 |
void setDestinationFolder(const QString &directory); |
|
115 |
QString destinationFolder() const; |
|
116 |
||
117 |
void setDumpedState(const QByteArray &dumpedState); |
|
118 |
QByteArray dumpedState() const; |
|
119 |
||
120 |
// Progress and stats for download feedback. |
|
121 |
qint64 progress() const; |
|
122 |
void setDownloadedBytes(qint64 bytes); |
|
123 |
qint64 downloadedBytes() const; |
|
124 |
void setUploadedBytes(qint64 bytes); |
|
125 |
qint64 uploadedBytes() const; |
|
126 |
int connectedPeerCount() const; |
|
127 |
int seedCount() const; |
|
128 |
||
129 |
// Accessors for the tracker |
|
130 |
QByteArray peerId() const; |
|
131 |
QByteArray infoHash() const; |
|
132 |
quint16 serverPort() const; |
|
133 |
||
134 |
// State and error. |
|
135 |
State state() const; |
|
136 |
QString stateString() const; |
|
137 |
Error error() const; |
|
138 |
QString errorString() const; |
|
139 |
||
140 |
signals: |
|
141 |
void stateChanged(TorrentClient::State state); |
|
142 |
void error(TorrentClient::Error error); |
|
143 |
||
144 |
void downloadCompleted(); |
|
145 |
void peerInfoUpdated(); |
|
146 |
||
147 |
void dataSent(int uploadedBytes); |
|
148 |
void dataReceived(int downloadedBytes); |
|
149 |
void progressUpdated(int percentProgress); |
|
150 |
void downloadRateUpdated(int bytesPerSecond); |
|
151 |
void uploadRateUpdated(int bytesPerSecond); |
|
152 |
||
153 |
void stopped(); |
|
154 |
||
155 |
public slots: |
|
156 |
void start(); |
|
157 |
void stop(); |
|
158 |
void setPaused(bool paused); |
|
159 |
void setupIncomingConnection(PeerWireClient *client); |
|
160 |
||
161 |
protected slots: |
|
162 |
void timerEvent(QTimerEvent *event); |
|
163 |
||
164 |
private slots: |
|
165 |
// File management |
|
166 |
void sendToPeer(int readId, int pieceIndex, int begin, const QByteArray &data); |
|
167 |
void fullVerificationDone(); |
|
168 |
void pieceVerified(int pieceIndex, bool ok); |
|
169 |
void handleFileError(); |
|
170 |
||
171 |
// Connection handling |
|
172 |
void connectToPeers(); |
|
173 |
QList<TorrentPeer *> weighedFreePeers() const; |
|
174 |
void setupOutgoingConnection(); |
|
175 |
void initializeConnection(PeerWireClient *client); |
|
176 |
void removeClient(); |
|
177 |
void peerPiecesAvailable(const QBitArray &pieces); |
|
178 |
void peerRequestsBlock(int pieceIndex, int begin, int length); |
|
179 |
void blockReceived(int pieceIndex, int begin, const QByteArray &data); |
|
180 |
void peerWireBytesWritten(qint64 bytes); |
|
181 |
void peerWireBytesReceived(qint64 bytes); |
|
182 |
int blocksLeftForPiece(const TorrentPiece *piece) const; |
|
183 |
||
184 |
// Scheduling |
|
185 |
void scheduleUploads(); |
|
186 |
void scheduleDownloads(); |
|
187 |
void schedulePieceForClient(PeerWireClient *client); |
|
188 |
void requestMore(PeerWireClient *client); |
|
189 |
int requestBlocks(PeerWireClient *client, TorrentPiece *piece, int maxBlocks); |
|
190 |
void peerChoked(); |
|
191 |
void peerUnchoked(); |
|
192 |
||
193 |
// Tracker handling |
|
194 |
void addToPeerList(const QList<TorrentPeer> &peerList); |
|
195 |
void trackerStopped(); |
|
196 |
||
197 |
// Progress |
|
198 |
void updateProgress(int progress = -1); |
|
199 |
||
200 |
private: |
|
201 |
TorrentClientPrivate *d; |
|
202 |
friend class TorrentClientPrivate; |
|
203 |
}; |
|
204 |
||
205 |
#endif |