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/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>
47 namespace libtorrent
{ namespace dht
50 #ifdef TORRENT_DHT_VERBOSE_LOGGING
51 TORRENT_DEFINE_LOG(refresh
)
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
);
75 void refresh_observer::timeout()
77 if (!m_algorithm
) return;
78 m_algorithm
->failed(m_self
);
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
);
95 void ping_observer::timeout()
97 if (!m_algorithm
) return;
98 m_algorithm
->ping_timeout(m_self
);
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
));
108 o
->m_in_constructor
= false;
111 m_rpc
.invoke(messages::find_node
, addr
, o
);
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
)
125 invoke_pings_or_finish();
128 void refresh::ping_timeout(node_id nid
, bool prevent_request
)
131 invoke_pings_or_finish(prevent_request
);
134 void refresh::invoke_pings_or_finish(bool prevent_request
)
138 --m_max_active_pings
;
139 if (m_max_active_pings
<= 0)
140 m_max_active_pings
= 1;
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
;
159 TORRENT_ASSERT(m_rpc
.allocation_size() >= sizeof(ping_observer
));
160 observer_ptr
o(new (m_rpc
.allocator().malloc()) ping_observer(
163 o
->m_in_constructor
= false;
165 m_rpc
.invoke(messages::ping
, node
.addr
, o
);
167 ++m_leftover_nodes_iterator
;
169 catch (std::exception
& e
) {}
173 if (m_active_pings
== 0)
179 } } // namespace libtorrent::dht