1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
42
#include "dht/dht_router.h"
43
#include "torrent/connection_manager.h"
44
#include "torrent/download_info.h"
45
#include "torrent/dht_manager.h"
46
#include "torrent/exceptions.h"
47
#include "torrent/tracker_list.h"
49
#include "tracker_dht.h"
56
const char* TrackerDht::states[] = { "Idle", "Searching", "Announcing" };
58
TrackerDht::TrackerDht(TrackerList* parent, const std::string& url) :
62
if (!manager->dht_manager()->is_valid())
63
throw internal_error("Trying to add DHT tracker with no DHT manager.");
66
TrackerDht::~TrackerDht() {
68
manager->dht_manager()->router()->cancel_announce(NULL, this);
72
TrackerDht::is_busy() const {
73
return m_state != state_idle;
77
TrackerDht::is_usable() const {
78
return m_enabled && manager->dht_manager()->is_active();
82
TrackerDht::send_state(int state) {
84
throw internal_error("TrackerDht::send_state(...) does not have a valid m_parent.");
87
manager->dht_manager()->router()->cancel_announce(m_parent->info(), this);
90
throw internal_error("TrackerDht::send_state cancel_announce did not cancel announce.");
93
if (state == DownloadInfo::STOPPED)
96
m_state = state_searching;
98
if (!manager->dht_manager()->is_active())
99
return receive_failed("DHT server not active.");
101
manager->dht_manager()->router()->announce(m_parent->info(), this);
103
set_normal_interval(20 * 60);
108
TrackerDht::close() {
110
manager->dht_manager()->router()->cancel_announce(m_parent->info(), this);
114
TrackerDht::type() const {
119
TrackerDht::receive_peers(raw_list peers) {
121
throw internal_error("TrackerDht::receive_peers called while not busy.");
123
m_peers.parse_address_bencode(peers);
127
TrackerDht::receive_success() {
129
throw internal_error("TrackerDht::receive_success called while not busy.");
131
m_state = state_idle;
132
m_parent->receive_success(this, &m_peers);
137
TrackerDht::receive_failed(const char* msg) {
139
throw internal_error("TrackerDht::receive_failed called while not busy.");
141
m_state = state_idle;
142
m_parent->receive_failed(this, msg);
147
TrackerDht::receive_progress(int replied, int contacted) {
149
throw internal_error("TrackerDht::receive_status called while not busy.");
152
m_contacted = contacted;
156
TrackerDht::get_status(char* buffer, int length) {
158
throw internal_error("TrackerDht::get_status called while not busy.");
160
snprintf(buffer, length, "[%s: %d/%d nodes replied]", states[m_state], m_replied, m_contacted);