1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* [email protected] *
* *
* 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"
|