3
Copyright (c) 2006, Arvid Norberg & Daniel Wallin
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#include "libtorrent/pch.hpp"
35
#include <libtorrent/kademlia/closest_nodes.hpp>
36
#include <libtorrent/kademlia/routing_table.hpp>
37
#include <libtorrent/kademlia/rpc_manager.hpp>
38
#include "libtorrent/assert.hpp"
40
namespace libtorrent { namespace dht
45
closest_nodes_observer::~closest_nodes_observer()
47
if (m_algorithm) m_algorithm->failed(m_self, true);
50
void closest_nodes_observer::reply(msg const& in)
54
TORRENT_ASSERT(false);
58
if (!in.nodes.empty())
60
for (msg::nodes_t::const_iterator i = in.nodes.begin()
61
, end(in.nodes.end()); i != end; ++i)
63
m_algorithm->traverse(i->id, i->addr);
66
m_algorithm->finished(m_self);
70
void closest_nodes_observer::timeout()
72
if (!m_algorithm) return;
73
m_algorithm->failed(m_self);
78
closest_nodes::closest_nodes(
82
, routing_table& table
84
, done_callback const& callback
86
: traversal_algorithm(
95
, m_done_callback(callback)
97
boost::intrusive_ptr<closest_nodes> self(this);
101
void closest_nodes::invoke(node_id const& id, udp::endpoint addr)
103
observer_ptr o(new (m_rpc.allocator().malloc()) closest_nodes_observer(this, id, m_target));
105
o->m_in_constructor = false;
107
m_rpc.invoke(messages::find_node, addr, o);
110
void closest_nodes::done()
112
std::vector<node_entry> results;
113
int num_results = m_max_results;
114
for (std::vector<result>::iterator i = m_results.begin()
115
, end(m_results.end()); i != end && num_results > 0; ++i)
117
if (i->flags & result::no_id) continue;
118
if ((i->flags & result::queried) == 0) continue;
119
results.push_back(node_entry(i->id, i->addr));
122
m_done_callback(results);
125
void closest_nodes::initiate(
129
, routing_table& table
131
, done_callback const& callback
134
new closest_nodes(target, branch_factor, max_results, table, rpc, callback);
137
} } // namespace libtorrent::dht