author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 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); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
passkey += '&'; |
0 | 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 |
} |