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
43 closest_nodes_observer::~closest_nodes_observer()
45 if (m_algorithm
) m_algorithm
->failed(m_self
, true);
48 void closest_nodes_observer::reply(msg
const& in
)
52 TORRENT_ASSERT(false);
56 if (!in
.nodes
.empty())
58 for (msg::nodes_t::const_iterator i
= in
.nodes
.begin()
59 , end(in
.nodes
.end()); i
!= end
; ++i
)
61 m_algorithm
->traverse(i
->id
, i
->addr
);
64 m_algorithm
->finished(m_self
);
68 void closest_nodes_observer::timeout()
70 if (!m_algorithm
) return;
71 m_algorithm
->failed(m_self
);
76 closest_nodes::closest_nodes(
80 , routing_table
& table
82 , done_callback
const& callback
84 : traversal_algorithm(
93 , m_done_callback(callback
)
95 boost::intrusive_ptr
<closest_nodes
> self(this);
99 void closest_nodes::invoke(node_id
const& id
, udp::endpoint addr
)
101 TORRENT_ASSERT(m_rpc
.allocation_size() >= sizeof(closest_nodes_observer
));
102 observer_ptr
o(new (m_rpc
.allocator().malloc()) closest_nodes_observer(this, id
, m_target
));
104 o
->m_in_constructor
= false;
106 m_rpc
.invoke(messages::find_node
, addr
, o
);
109 void closest_nodes::done()
111 std::vector
<node_entry
> results
;
112 int num_results
= m_max_results
;
113 for (std::vector
<result
>::iterator i
= m_results
.begin()
114 , end(m_results
.end()); i
!= end
&& num_results
> 0; ++i
)
116 if (i
->flags
& result::no_id
) continue;
117 if ((i
->flags
& result::queried
) == 0) continue;
118 results
.push_back(node_entry(i
->id
, i
->addr
));
121 m_done_callback(results
);
124 void closest_nodes::initiate(
128 , routing_table
& table
130 , done_callback
const& callback
133 new closest_nodes(target
, branch_factor
, max_results
, table
, rpc
, callback
);
136 } } // namespace libtorrent::dht