delete_files bug fix
[libtorrent.git] / src / kademlia / refresh.cpp
blob1c597889329ac2551745dae85ff61ff42307f7eb
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/refresh.hpp>
36 #include <libtorrent/kademlia/routing_table.hpp>
37 #include <libtorrent/kademlia/rpc_manager.hpp>
38 #include <libtorrent/kademlia/logging.hpp>
39 #include <libtorrent/kademlia/msg.hpp>
41 #include <libtorrent/io.hpp>
43 #include <boost/bind.hpp>
45 using boost::bind;
47 namespace libtorrent { namespace dht
50 #ifdef TORRENT_DHT_VERBOSE_LOGGING
51 TORRENT_DEFINE_LOG(refresh)
52 #endif
54 refresh_observer::~refresh_observer()
56 if (m_algorithm) m_algorithm->failed(m_self, true);
59 void refresh_observer::reply(msg const& in)
61 if (!m_algorithm) return;
63 if (!in.nodes.empty())
65 for (msg::nodes_t::const_iterator i = in.nodes.begin()
66 , end(in.nodes.end()); i != end; ++i)
68 m_algorithm->traverse(i->id, i->addr);
71 m_algorithm->finished(m_self);
72 m_algorithm = 0;
75 void refresh_observer::timeout()
77 if (!m_algorithm) return;
78 m_algorithm->failed(m_self);
79 m_algorithm = 0;
82 ping_observer::~ping_observer()
84 if (m_algorithm) m_algorithm->ping_timeout(m_self, true);
87 void ping_observer::reply(msg const& m)
89 if (!m_algorithm) return;
91 m_algorithm->ping_reply(m_self);
92 m_algorithm = 0;
95 void ping_observer::timeout()
97 if (!m_algorithm) return;
98 m_algorithm->ping_timeout(m_self);
99 m_algorithm = 0;
102 void refresh::invoke(node_id const& nid, udp::endpoint addr)
104 TORRENT_ASSERT(m_rpc.allocation_size() >= sizeof(refresh_observer));
105 observer_ptr o(new (m_rpc.allocator().malloc()) refresh_observer(
106 this, nid, m_target));
107 #ifndef NDEBUG
108 o->m_in_constructor = false;
109 #endif
111 m_rpc.invoke(messages::find_node, addr, o);
114 void refresh::done()
116 m_leftover_nodes_iterator = (int)m_results.size() > m_max_results ?
117 m_results.begin() + m_max_results : m_results.end();
119 invoke_pings_or_finish();
122 void refresh::ping_reply(node_id nid)
124 m_active_pings--;
125 invoke_pings_or_finish();
128 void refresh::ping_timeout(node_id nid, bool prevent_request)
130 m_active_pings--;
131 invoke_pings_or_finish(prevent_request);
134 void refresh::invoke_pings_or_finish(bool prevent_request)
136 if (prevent_request)
138 --m_max_active_pings;
139 if (m_max_active_pings <= 0)
140 m_max_active_pings = 1;
142 else
144 while (m_active_pings < m_max_active_pings)
146 if (m_leftover_nodes_iterator == m_results.end()) break;
148 result const& node = *m_leftover_nodes_iterator;
150 // Skip initial nodes
151 if (node.flags & result::initial)
153 ++m_leftover_nodes_iterator;
154 continue;
159 TORRENT_ASSERT(m_rpc.allocation_size() >= sizeof(ping_observer));
160 observer_ptr o(new (m_rpc.allocator().malloc()) ping_observer(
161 this, node.id));
162 #ifndef NDEBUG
163 o->m_in_constructor = false;
164 #endif
165 m_rpc.invoke(messages::ping, node.addr, o);
166 ++m_active_pings;
167 ++m_leftover_nodes_iterator;
169 catch (std::exception& e) {}
173 if (m_active_pings == 0)
175 m_done_callback();
179 } } // namespace libtorrent::dht