file_progress fix
[libtorrent.git] / src / kademlia / closest_nodes.cpp
blob859170f7f14b98ae75e31b9978ffd3d2c83575c3
1 /*
3 Copyright (c) 2006, Arvid Norberg & Daniel Wallin
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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)
50 if (!m_algorithm)
52 TORRENT_ASSERT(false);
53 return;
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);
65 m_algorithm = 0;
68 void closest_nodes_observer::timeout()
70 if (!m_algorithm) return;
71 m_algorithm->failed(m_self);
72 m_algorithm = 0;
76 closest_nodes::closest_nodes(
77 node_id target
78 , int branch_factor
79 , int max_results
80 , routing_table& table
81 , rpc_manager& rpc
82 , done_callback const& callback
84 : traversal_algorithm(
85 target
86 , branch_factor
87 , max_results
88 , table
89 , rpc
90 , table.begin()
91 , table.end()
93 , m_done_callback(callback)
95 boost::intrusive_ptr<closest_nodes> self(this);
96 add_requests();
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));
103 #ifndef NDEBUG
104 o->m_in_constructor = false;
105 #endif
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));
119 --num_results;
121 m_done_callback(results);
124 void closest_nodes::initiate(
125 node_id target
126 , int branch_factor
127 , int max_results
128 , routing_table& table
129 , rpc_manager& rpc
130 , done_callback const& callback
133 new closest_nodes(target, branch_factor, max_results, table, rpc, callback);
136 } } // namespace libtorrent::dht