1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #ifndef TORRENTCLIENT_H
|
---|
42 | #define TORRENTCLIENT_H
|
---|
43 |
|
---|
44 | #include <QBitArray>
|
---|
45 | #include <QHostAddress>
|
---|
46 | #include <QList>
|
---|
47 |
|
---|
48 | class MetaInfo;
|
---|
49 | class PeerWireClient;
|
---|
50 | class TorrentClientPrivate;
|
---|
51 | class TorrentPeer;
|
---|
52 | class TorrentPiece;
|
---|
53 | QT_BEGIN_NAMESPACE
|
---|
54 | class QTimerEvent;
|
---|
55 | QT_END_NAMESPACE
|
---|
56 |
|
---|
57 | class TorrentPeer {
|
---|
58 | public:
|
---|
59 | QHostAddress address;
|
---|
60 | quint16 port;
|
---|
61 | QString id;
|
---|
62 | bool interesting;
|
---|
63 | bool seed;
|
---|
64 | uint lastVisited;
|
---|
65 | uint connectStart;
|
---|
66 | uint connectTime;
|
---|
67 | QBitArray pieces;
|
---|
68 | int numCompletedPieces;
|
---|
69 |
|
---|
70 | inline bool operator==(const TorrentPeer &other)
|
---|
71 | {
|
---|
72 | return port == other.port
|
---|
73 | && address == other.address
|
---|
74 | && id == other.id;
|
---|
75 | }
|
---|
76 | };
|
---|
77 |
|
---|
78 | class TorrentClient : public QObject
|
---|
79 | {
|
---|
80 | Q_OBJECT
|
---|
81 |
|
---|
82 | public:
|
---|
83 | enum State {
|
---|
84 | Idle,
|
---|
85 | Paused,
|
---|
86 | Stopping,
|
---|
87 | Preparing,
|
---|
88 | Searching,
|
---|
89 | Connecting,
|
---|
90 | WarmingUp,
|
---|
91 | Downloading,
|
---|
92 | Endgame,
|
---|
93 | Seeding
|
---|
94 | };
|
---|
95 | enum Error {
|
---|
96 | UnknownError,
|
---|
97 | TorrentParseError,
|
---|
98 | InvalidTrackerError,
|
---|
99 | FileError,
|
---|
100 | ServerError
|
---|
101 | };
|
---|
102 |
|
---|
103 | TorrentClient(QObject *parent = 0);
|
---|
104 | ~TorrentClient();
|
---|
105 |
|
---|
106 | bool setTorrent(const QString &fileName);
|
---|
107 | bool setTorrent(const QByteArray &torrentData);
|
---|
108 | MetaInfo metaInfo() const;
|
---|
109 |
|
---|
110 | void setMaxConnections(int connections);
|
---|
111 | int maxConnections() const;
|
---|
112 |
|
---|
113 | void setDestinationFolder(const QString &directory);
|
---|
114 | QString destinationFolder() const;
|
---|
115 |
|
---|
116 | void setDumpedState(const QByteArray &dumpedState);
|
---|
117 | QByteArray dumpedState() const;
|
---|
118 |
|
---|
119 | // Progress and stats for download feedback.
|
---|
120 | qint64 progress() const;
|
---|
121 | void setDownloadedBytes(qint64 bytes);
|
---|
122 | qint64 downloadedBytes() const;
|
---|
123 | void setUploadedBytes(qint64 bytes);
|
---|
124 | qint64 uploadedBytes() const;
|
---|
125 | int connectedPeerCount() const;
|
---|
126 | int seedCount() const;
|
---|
127 |
|
---|
128 | // Accessors for the tracker
|
---|
129 | QByteArray peerId() const;
|
---|
130 | QByteArray infoHash() const;
|
---|
131 | quint16 serverPort() const;
|
---|
132 |
|
---|
133 | // State and error.
|
---|
134 | State state() const;
|
---|
135 | QString stateString() const;
|
---|
136 | Error error() const;
|
---|
137 | QString errorString() const;
|
---|
138 |
|
---|
139 | signals:
|
---|
140 | void stateChanged(TorrentClient::State state);
|
---|
141 | void error(TorrentClient::Error error);
|
---|
142 |
|
---|
143 | void downloadCompleted();
|
---|
144 | void peerInfoUpdated();
|
---|
145 |
|
---|
146 | void dataSent(int uploadedBytes);
|
---|
147 | void dataReceived(int downloadedBytes);
|
---|
148 | void progressUpdated(int percentProgress);
|
---|
149 | void downloadRateUpdated(int bytesPerSecond);
|
---|
150 | void uploadRateUpdated(int bytesPerSecond);
|
---|
151 |
|
---|
152 | void stopped();
|
---|
153 |
|
---|
154 | public slots:
|
---|
155 | void start();
|
---|
156 | void stop();
|
---|
157 | void setPaused(bool paused);
|
---|
158 | void setupIncomingConnection(PeerWireClient *client);
|
---|
159 |
|
---|
160 | protected slots:
|
---|
161 | void timerEvent(QTimerEvent *event);
|
---|
162 |
|
---|
163 | private slots:
|
---|
164 | // File management
|
---|
165 | void sendToPeer(int readId, int pieceIndex, int begin, const QByteArray &data);
|
---|
166 | void fullVerificationDone();
|
---|
167 | void pieceVerified(int pieceIndex, bool ok);
|
---|
168 | void handleFileError();
|
---|
169 |
|
---|
170 | // Connection handling
|
---|
171 | void connectToPeers();
|
---|
172 | QList<TorrentPeer *> weighedFreePeers() const;
|
---|
173 | void setupOutgoingConnection();
|
---|
174 | void initializeConnection(PeerWireClient *client);
|
---|
175 | void removeClient();
|
---|
176 | void peerPiecesAvailable(const QBitArray &pieces);
|
---|
177 | void peerRequestsBlock(int pieceIndex, int begin, int length);
|
---|
178 | void blockReceived(int pieceIndex, int begin, const QByteArray &data);
|
---|
179 | void peerWireBytesWritten(qint64 bytes);
|
---|
180 | void peerWireBytesReceived(qint64 bytes);
|
---|
181 | int blocksLeftForPiece(const TorrentPiece *piece) const;
|
---|
182 |
|
---|
183 | // Scheduling
|
---|
184 | void scheduleUploads();
|
---|
185 | void scheduleDownloads();
|
---|
186 | void schedulePieceForClient(PeerWireClient *client);
|
---|
187 | void requestMore(PeerWireClient *client);
|
---|
188 | int requestBlocks(PeerWireClient *client, TorrentPiece *piece, int maxBlocks);
|
---|
189 | void peerChoked();
|
---|
190 | void peerUnchoked();
|
---|
191 |
|
---|
192 | // Tracker handling
|
---|
193 | void addToPeerList(const QList<TorrentPeer> &peerList);
|
---|
194 | void trackerStopped();
|
---|
195 |
|
---|
196 | // Progress
|
---|
197 | void updateProgress(int progress = -1);
|
---|
198 |
|
---|
199 | private:
|
---|
200 | TorrentClientPrivate *d;
|
---|
201 | friend class TorrentClientPrivate;
|
---|
202 | };
|
---|
203 |
|
---|
204 | #endif
|
---|