Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 3021

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: trackerclient.cpp Example File (network/torrent/trackerclient.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">trackerclient.cpp Example File<br /><span class="small-subtitle">network/torrent/trackerclient.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &quot;bencodeparser.h&quot;
 #include &quot;connectionmanager.h&quot;
 #include &quot;torrentclient.h&quot;
 #include &quot;torrentserver.h&quot;
 #include &quot;trackerclient.h&quot;

 #include &lt;QtCore&gt;

 TrackerClient::TrackerClient(TorrentClient *downloader, QObject *parent)
     : QObject(parent), torrentDownloader(downloader)
 {
     length = 0;
     requestInterval = 5 * 60;
     requestIntervalTimer = -1;
     firstTrackerRequest = true;
     lastTrackerRequest = false;
     firstSeeding = true;

     connect(&amp;http, SIGNAL(done(bool)), this, SLOT(httpRequestDone(bool)));
 }

 void TrackerClient::start(const MetaInfo &amp;info)
 {
     metaInfo = info;
     QTimer::singleShot(0, this, SLOT(fetchPeerList()));

     if (metaInfo.fileForm() == MetaInfo::SingleFileForm) {
         length = metaInfo.singleFile().length;
     } else {
         QList&lt;MetaInfoMultiFile&gt; files = metaInfo.multiFiles();
         for (int i = 0; i &lt; files.size(); ++i)
             length += files.at(i).length;
     }
 }

 void TrackerClient::startSeeding()
 {
     firstSeeding = true;
     fetchPeerList();
 }

 void TrackerClient::stop()
 {
     lastTrackerRequest = true;
     http.abort();
     fetchPeerList();
 }

 void TrackerClient::timerEvent(QTimerEvent *event)
 {
     if (event-&gt;timerId() == requestIntervalTimer) {
         if (http.state() == QHttp::Unconnected)
             fetchPeerList();
     } else {
         QObject::timerEvent(event);
     }
 }

 void TrackerClient::fetchPeerList()
 {
     <span class="comment">// Prepare connection details</span>
     QString fullUrl = metaInfo.announceUrl();
     QUrl url(fullUrl);
     QString passkey = &quot;?&quot;;
     if (fullUrl.contains(&quot;?passkey&quot;)) {
         passkey = metaInfo.announceUrl().mid(fullUrl.indexOf(&quot;?passkey&quot;), -1);
         passkey += '&amp;';
     }

     <span class="comment">// Percent encode the hash</span>
     QByteArray infoHash = torrentDownloader-&gt;infoHash();
     QString encodedSum;
     for (int i = 0; i &lt; infoHash.size(); ++i) {
         encodedSum += '%';
         encodedSum += QString::number(infoHash[i], 16).right(2).rightJustified(2, '0');
     }

     bool seeding = (torrentDownloader-&gt;state() == TorrentClient::Seeding);
     QByteArray query;
     query += url.path().toLatin1();
     query += passkey;
     query += &quot;info_hash=&quot; + encodedSum;
     query += &quot;&amp;peer_id=&quot; + ConnectionManager::instance()-&gt;clientId();
     query += &quot;&amp;port=&quot; + QByteArray::number(TorrentServer::instance()-&gt;serverPort());
     query += &quot;&amp;compact=1&quot;;
     query += &quot;&amp;uploaded=&quot; + QByteArray::number(torrentDownloader-&gt;uploadedBytes());

     if (!firstSeeding) {
         query += &quot;&amp;downloaded=0&quot;;
         query += &quot;&amp;left=0&quot;;
     } else {
         query += &quot;&amp;downloaded=&quot; + QByteArray::number(
             torrentDownloader-&gt;downloadedBytes());
         int left = qMax&lt;int&gt;(0, metaInfo.totalSize() - torrentDownloader-&gt;downloadedBytes());
         query += &quot;&amp;left=&quot; + QByteArray::number(seeding ? 0 : left);
     }

     if (seeding &amp;&amp; firstSeeding) {
         query += &quot;&amp;event=completed&quot;;
         firstSeeding = false;
     } else if (firstTrackerRequest) {
         firstTrackerRequest = false;
         query += &quot;&amp;event=started&quot;;
     } else if(lastTrackerRequest) {
         query += &quot;&amp;event=stopped&quot;;
     }

     if (!trackerId.isEmpty())
         query += &quot;&amp;trackerid=&quot; + trackerId;

     http.setHost(url.host(), url.port() == -1 ? 80 : url.port());
     if (!url.userName().isEmpty())
         http.setUser(url.userName(), url.password());
     http.get(query);
 }

 void TrackerClient::httpRequestDone(bool error)
 {
     if (lastTrackerRequest) {
         emit stopped();
         return;
     }

     if (error) {
         emit connectionError(http.error());
         return;
     }

     QByteArray response = http.readAll();
     http.abort();

     BencodeParser parser;
     if (!parser.parse(response)) {
         qWarning(&quot;Error parsing bencode response from tracker: %s&quot;,
                  qPrintable(parser.errorString()));
         http.abort();
         return;
     }

     QMap&lt;QByteArray, QVariant&gt; dict = parser.dictionary();

     if (dict.contains(&quot;failure reason&quot;)) {
         <span class="comment">// no other items are present</span>
         emit failure(QString::fromUtf8(dict.value(&quot;failure reason&quot;).toByteArray()));
         return;
     }

     if (dict.contains(&quot;warning message&quot;)) {
         <span class="comment">// continue processing</span>
         emit warning(QString::fromUtf8(dict.value(&quot;warning message&quot;).toByteArray()));
     }

     if (dict.contains(&quot;tracker id&quot;)) {
         <span class="comment">// store it</span>
         trackerId = dict.value(&quot;tracker id&quot;).toByteArray();
     }

     if (dict.contains(&quot;interval&quot;)) {
         <span class="comment">// Mandatory item</span>
         if (requestIntervalTimer != -1)
             killTimer(requestIntervalTimer);
         requestIntervalTimer = startTimer(dict.value(&quot;interval&quot;).toInt() * 1000);
     }

     if (dict.contains(&quot;peers&quot;)) {
         <span class="comment">// store it</span>
         peers.clear();
         QVariant peerEntry = dict.value(&quot;peers&quot;);
         if (peerEntry.type() == QVariant::List) {
             QList&lt;QVariant&gt; peerTmp = peerEntry.toList();
             for (int i = 0; i &lt; peerTmp.size(); ++i) {
                 TorrentPeer tmp;
                 QMap&lt;QByteArray, QVariant&gt; peer = qVariantValue&lt;QMap&lt;QByteArray, QVariant&gt; &gt;(peerTmp.at(i));
                 tmp.id = QString::fromUtf8(peer.value(&quot;peer id&quot;).toByteArray());
                 tmp.address.setAddress(QString::fromUtf8(peer.value(&quot;ip&quot;).toByteArray()));
                 tmp.port = peer.value(&quot;port&quot;).toInt();
                 peers &lt;&lt; tmp;
             }
         } else {
             QByteArray peerTmp = peerEntry.toByteArray();
             for (int i = 0; i &lt; peerTmp.size(); i += 6) {
                 TorrentPeer tmp;
                 uchar *data = (uchar *)peerTmp.constData() + i;
                 tmp.port = (int(data[4]) &lt;&lt; 8) + data[5];
                 uint ipAddress = 0;
                 ipAddress += uint(data[0]) &lt;&lt; 24;
                 ipAddress += uint(data[1]) &lt;&lt; 16;
                 ipAddress += uint(data[2]) &lt;&lt; 8;
                 ipAddress += uint(data[3]);
                 tmp.address.setAddress(ipAddress);
                 peers &lt;&lt; tmp;
             }
         }
         emit peerListUpdated(peers);
     }
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>