1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
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: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 | #include "bencodeparser.h"
|
---|
42 | #include "connectionmanager.h"
|
---|
43 | #include "torrentclient.h"
|
---|
44 | #include "torrentserver.h"
|
---|
45 | #include "trackerclient.h"
|
---|
46 |
|
---|
47 | #include <QtCore>
|
---|
48 |
|
---|
49 | TrackerClient::TrackerClient(TorrentClient *downloader, QObject *parent)
|
---|
50 | : QObject(parent), torrentDownloader(downloader)
|
---|
51 | {
|
---|
52 | length = 0;
|
---|
53 | requestInterval = 5 * 60;
|
---|
54 | requestIntervalTimer = -1;
|
---|
55 | firstTrackerRequest = true;
|
---|
56 | lastTrackerRequest = false;
|
---|
57 | firstSeeding = true;
|
---|
58 |
|
---|
59 | connect(&http, SIGNAL(done(bool)), this, SLOT(httpRequestDone(bool)));
|
---|
60 | }
|
---|
61 |
|
---|
62 | void TrackerClient::start(const MetaInfo &info)
|
---|
63 | {
|
---|
64 | metaInfo = info;
|
---|
65 | QTimer::singleShot(0, this, SLOT(fetchPeerList()));
|
---|
66 |
|
---|
67 | if (metaInfo.fileForm() == MetaInfo::SingleFileForm) {
|
---|
68 | length = metaInfo.singleFile().length;
|
---|
69 | } else {
|
---|
70 | QList<MetaInfoMultiFile> files = metaInfo.multiFiles();
|
---|
71 | for (int i = 0; i < files.size(); ++i)
|
---|
72 | length += files.at(i).length;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | void TrackerClient::startSeeding()
|
---|
77 | {
|
---|
78 | firstSeeding = true;
|
---|
79 | fetchPeerList();
|
---|
80 | }
|
---|
81 |
|
---|
82 | void TrackerClient::stop()
|
---|
83 | {
|
---|
84 | lastTrackerRequest = true;
|
---|
85 | http.abort();
|
---|
86 | fetchPeerList();
|
---|
87 | }
|
---|
88 |
|
---|
89 | void TrackerClient::timerEvent(QTimerEvent *event)
|
---|
90 | {
|
---|
91 | if (event->timerId() == requestIntervalTimer) {
|
---|
92 | if (http.state() == QHttp::Unconnected)
|
---|
93 | fetchPeerList();
|
---|
94 | } else {
|
---|
95 | QObject::timerEvent(event);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | void TrackerClient::fetchPeerList()
|
---|
100 | {
|
---|
101 | // Prepare connection details
|
---|
102 | QString fullUrl = metaInfo.announceUrl();
|
---|
103 | QUrl url(fullUrl);
|
---|
104 | QString passkey = "?";
|
---|
105 | if (fullUrl.contains("?passkey")) {
|
---|
106 | passkey = metaInfo.announceUrl().mid(fullUrl.indexOf("?passkey"), -1);
|
---|
107 | passkey += '&';
|
---|
108 | }
|
---|
109 |
|
---|
110 | // Percent encode the hash
|
---|
111 | QByteArray infoHash = torrentDownloader->infoHash();
|
---|
112 | QString encodedSum;
|
---|
113 | for (int i = 0; i < infoHash.size(); ++i) {
|
---|
114 | encodedSum += '%';
|
---|
115 | encodedSum += QString::number(infoHash[i], 16).right(2).rightJustified(2, '0');
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool seeding = (torrentDownloader->state() == TorrentClient::Seeding);
|
---|
119 | QByteArray query;
|
---|
120 | query += url.path().toLatin1();
|
---|
121 | query += passkey;
|
---|
122 | query += "info_hash=" + encodedSum;
|
---|
123 | query += "&peer_id=" + ConnectionManager::instance()->clientId();
|
---|
124 | query += "&port=" + QByteArray::number(TorrentServer::instance()->serverPort());
|
---|
125 | query += "&compact=1";
|
---|
126 | query += "&uploaded=" + QByteArray::number(torrentDownloader->uploadedBytes());
|
---|
127 |
|
---|
128 | if (!firstSeeding) {
|
---|
129 | query += "&downloaded=0";
|
---|
130 | query += "&left=0";
|
---|
131 | } else {
|
---|
132 | query += "&downloaded=" + QByteArray::number(
|
---|
133 | torrentDownloader->downloadedBytes());
|
---|
134 | int left = qMax<int>(0, metaInfo.totalSize() - torrentDownloader->downloadedBytes());
|
---|
135 | query += "&left=" + QByteArray::number(seeding ? 0 : left);
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (seeding && firstSeeding) {
|
---|
139 | query += "&event=completed";
|
---|
140 | firstSeeding = false;
|
---|
141 | } else if (firstTrackerRequest) {
|
---|
142 | firstTrackerRequest = false;
|
---|
143 | query += "&event=started";
|
---|
144 | } else if(lastTrackerRequest) {
|
---|
145 | query += "&event=stopped";
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (!trackerId.isEmpty())
|
---|
149 | query += "&trackerid=" + trackerId;
|
---|
150 |
|
---|
151 | http.setHost(url.host(), url.port() == -1 ? 80 : url.port());
|
---|
152 | if (!url.userName().isEmpty())
|
---|
153 | http.setUser(url.userName(), url.password());
|
---|
154 | http.get(query);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void TrackerClient::httpRequestDone(bool error)
|
---|
158 | {
|
---|
159 | if (lastTrackerRequest) {
|
---|
160 | emit stopped();
|
---|
161 | return;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (error) {
|
---|
165 | emit connectionError(http.error());
|
---|
166 | return;
|
---|
167 | }
|
---|
168 |
|
---|
169 | QByteArray response = http.readAll();
|
---|
170 | http.abort();
|
---|
171 |
|
---|
172 | BencodeParser parser;
|
---|
173 | if (!parser.parse(response)) {
|
---|
174 | qWarning("Error parsing bencode response from tracker: %s",
|
---|
175 | qPrintable(parser.errorString()));
|
---|
176 | http.abort();
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | QMap<QByteArray, QVariant> dict = parser.dictionary();
|
---|
181 |
|
---|
182 | if (dict.contains("failure reason")) {
|
---|
183 | // no other items are present
|
---|
184 | emit failure(QString::fromUtf8(dict.value("failure reason").toByteArray()));
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (dict.contains("warning message")) {
|
---|
189 | // continue processing
|
---|
190 | emit warning(QString::fromUtf8(dict.value("warning message").toByteArray()));
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (dict.contains("tracker id")) {
|
---|
194 | // store it
|
---|
195 | trackerId = dict.value("tracker id").toByteArray();
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (dict.contains("interval")) {
|
---|
199 | // Mandatory item
|
---|
200 | if (requestIntervalTimer != -1)
|
---|
201 | killTimer(requestIntervalTimer);
|
---|
202 | requestIntervalTimer = startTimer(dict.value("interval").toInt() * 1000);
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (dict.contains("peers")) {
|
---|
206 | // store it
|
---|
207 | peers.clear();
|
---|
208 | QVariant peerEntry = dict.value("peers");
|
---|
209 | if (peerEntry.type() == QVariant::List) {
|
---|
210 | QList<QVariant> peerTmp = peerEntry.toList();
|
---|
211 | for (int i = 0; i < peerTmp.size(); ++i) {
|
---|
212 | TorrentPeer tmp;
|
---|
213 | QMap<QByteArray, QVariant> peer = qVariantValue<QMap<QByteArray, QVariant> >(peerTmp.at(i));
|
---|
214 | tmp.id = QString::fromUtf8(peer.value("peer id").toByteArray());
|
---|
215 | tmp.address.setAddress(QString::fromUtf8(peer.value("ip").toByteArray()));
|
---|
216 | tmp.port = peer.value("port").toInt();
|
---|
217 | peers << tmp;
|
---|
218 | }
|
---|
219 | } else {
|
---|
220 | QByteArray peerTmp = peerEntry.toByteArray();
|
---|
221 | for (int i = 0; i < peerTmp.size(); i += 6) {
|
---|
222 | TorrentPeer tmp;
|
---|
223 | uchar *data = (uchar *)peerTmp.constData() + i;
|
---|
224 | tmp.port = (int(data[4]) << 8) + data[5];
|
---|
225 | uint ipAddress = 0;
|
---|
226 | ipAddress += uint(data[0]) << 24;
|
---|
227 | ipAddress += uint(data[1]) << 16;
|
---|
228 | ipAddress += uint(data[2]) << 8;
|
---|
229 | ipAddress += uint(data[3]);
|
---|
230 | tmp.address.setAddress(ipAddress);
|
---|
231 | peers << tmp;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | emit peerListUpdated(peers);
|
---|
235 | }
|
---|
236 | }
|
---|