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