summaryrefslogtreecommitdiffstats
path: root/libktorrent/kademlia/dhttrackerbackend.cpp
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 02:37:40 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 02:37:40 +0000
commit9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 (patch)
treed088b5210e77d9fa91d954d8550e00e372b47378 /libktorrent/kademlia/dhttrackerbackend.cpp
downloadktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.tar.gz
ktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.zip
Updated to final KDE3 ktorrent release (2.2.6)
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktorrent@1077377 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'libktorrent/kademlia/dhttrackerbackend.cpp')
-rw-r--r--libktorrent/kademlia/dhttrackerbackend.cpp154
1 files changed, 154 insertions, 0 deletions
diff --git a/libktorrent/kademlia/dhttrackerbackend.cpp b/libktorrent/kademlia/dhttrackerbackend.cpp
new file mode 100644
index 0000000..c90e6f7
--- /dev/null
+++ b/libktorrent/kademlia/dhttrackerbackend.cpp
@@ -0,0 +1,154 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include <kurl.h>
+#include <qhostaddress.h>
+#include <util/log.h>
+#include <util/functions.h>
+#include <torrent/globals.h>
+#include <torrent/server.h>
+#include <torrent/peermanager.h>
+#include <interfaces/torrentinterface.h>
+#include "dhttrackerbackend.h"
+#include "dht.h"
+#include "announcetask.h"
+
+using namespace bt;
+
+namespace dht
+{
+
+ DHTTrackerBackend::DHTTrackerBackend(DHTBase & dh_table,kt::TorrentInterface* tor)
+ : dh_table(dh_table),curr_task(0),tor(tor)
+ {
+ connect(&timer,SIGNAL(timeout()),this,SLOT(onTimeout()));
+ connect(&dh_table,SIGNAL(started()),this,SLOT(manualUpdate()));
+ connect(&dh_table,SIGNAL(stopped()),this,SLOT(dhtStopped()));
+ started = false;
+ }
+
+
+ DHTTrackerBackend::~DHTTrackerBackend()
+ {
+ if (curr_task)
+ curr_task->kill();
+ }
+
+ void DHTTrackerBackend::start()
+ {
+ started = true;
+ if (dh_table.isRunning())
+ doRequest();
+ }
+
+ void DHTTrackerBackend::dhtStopped()
+ {
+ stop(0);
+ curr_task = 0;
+ }
+
+ void DHTTrackerBackend::stop(bt::WaitJob*)
+ {
+ started = false;
+ if (curr_task)
+ {
+ curr_task->kill();
+ timer.stop();
+ }
+ }
+
+ void DHTTrackerBackend::manualUpdate()
+ {
+ if (dh_table.isRunning() && started)
+ doRequest();
+ }
+
+
+ bool DHTTrackerBackend::doRequest()
+ {
+ if (!dh_table.isRunning())
+ return false;
+
+ if (curr_task)
+ return true;
+
+ const SHA1Hash & info_hash = tor->getInfoHash();
+ Uint16 port = bt::Globals::instance().getServer().getPortInUse();
+ curr_task = dh_table.announce(info_hash,port);
+ if (curr_task)
+ {
+ for (Uint32 i = 0;i < tor->getNumDHTNodes();i++)
+ {
+ const kt::DHTNode & n = tor->getDHTNode(i);
+ curr_task->addDHTNode(n.ip,n.port);
+ }
+ connect(curr_task,SIGNAL(dataReady( Task* )),this,SLOT(onDataReady( Task* )));
+ connect(curr_task,SIGNAL(finished( Task* )),this,SLOT(onFinished( Task* )));
+
+ return true;
+ }
+
+ return false;
+ }
+
+ void DHTTrackerBackend::onFinished(Task* t)
+ {
+ if (curr_task == t)
+ {
+ onDataReady(curr_task);
+ curr_task = 0;
+ // do another announce in 5 minutes or so
+ timer.start(5 * 60 * 1000,true);
+ }
+ }
+
+ void DHTTrackerBackend::onDataReady(Task* t)
+ {
+ if (curr_task == t)
+ {
+ Uint32 cnt = 0;
+ DBItem item;
+ while (curr_task->takeItem(item))
+ {
+ Uint16 port = bt::ReadUint16(item.getData(),4);
+ QString ip = QHostAddress(ReadUint32(item.getData(),0)).toString();
+
+ addPeer(ip,port);
+ cnt++;
+ }
+
+ if (cnt)
+ {
+ Out(SYS_DHT|LOG_NOTICE) <<
+ QString("DHT: Got %1 potential peers for torrent %2")
+ .arg(cnt).arg(tor->getStats().torrent_name) << endl;
+ peersReady(this);
+ }
+ }
+ }
+
+ void DHTTrackerBackend::onTimeout()
+ {
+ if (dh_table.isRunning() && started)
+ doRequest();
+ }
+
+}
+
+#include "dhttrackerbackend.moc"