delete_files bug fix
[libtorrent.git] / src / kademlia / find_data.cpp
blob8da689d6855075f967d5caf5a9d5e08b1bc30a8f
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/find_data.hpp>
36 #include <libtorrent/kademlia/routing_table.hpp>
37 #include <libtorrent/kademlia/rpc_manager.hpp>
38 #include <libtorrent/io.hpp>
39 #include <libtorrent/socket.hpp>
41 namespace libtorrent { namespace dht
44 find_data_observer::~find_data_observer()
46 if (m_algorithm) m_algorithm->failed(m_self);
49 void find_data_observer::reply(msg const& m)
51 if (!m_algorithm)
53 TORRENT_ASSERT(false);
54 return;
57 if (!m.peers.empty())
59 m_algorithm->got_data(&m);
61 else
63 for (msg::nodes_t::const_iterator i = m.nodes.begin()
64 , end(m.nodes.end()); i != end; ++i)
66 m_algorithm->traverse(i->id, i->addr);
69 m_algorithm->finished(m_self);
70 m_algorithm = 0;
73 void find_data_observer::timeout()
75 if (!m_algorithm) return;
76 m_algorithm->failed(m_self);
77 m_algorithm = 0;
81 find_data::find_data(
82 node_id target
83 , int branch_factor
84 , int max_results
85 , routing_table& table
86 , rpc_manager& rpc
87 , done_callback const& callback
89 : traversal_algorithm(
90 target
91 , branch_factor
92 , max_results
93 , table
94 , rpc
95 , table.begin()
96 , table.end()
98 , m_done_callback(callback)
99 , m_done(false)
101 boost::intrusive_ptr<find_data> self(this);
102 add_requests();
105 void find_data::invoke(node_id const& id, udp::endpoint addr)
107 if (m_done)
109 m_invoke_count = -1;
110 return;
113 TORRENT_ASSERT(m_rpc.allocation_size() >= sizeof(find_data_observer));
114 observer_ptr o(new (m_rpc.allocator().malloc()) find_data_observer(this, id, m_target));
115 #ifndef NDEBUG
116 o->m_in_constructor = false;
117 #endif
118 m_rpc.invoke(messages::get_peers, addr, o);
121 void find_data::got_data(msg const* m)
123 m_done = true;
124 m_done_callback(m);
127 void find_data::done()
129 if (m_invoke_count != 0) return;
130 if (!m_done) m_done_callback(0);
133 void find_data::initiate(
134 node_id target
135 , int branch_factor
136 , int max_results
137 , routing_table& table
138 , rpc_manager& rpc
139 , done_callback const& callback
142 std::cerr << "find_data::initiate, key: " << target << "\n";
143 new find_data(target, branch_factor, max_results, table, rpc, callback);
146 } } // namespace libtorrent::dht