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
52
#ifdef TORRENT_DHT_VERBOSE_LOGGING
53
TORRENT_DEFINE_LOG(refresh)
56
refresh_observer::~refresh_observer()
58
if (m_algorithm) m_algorithm->failed(m_self, true);
61
void refresh_observer::reply(msg const& in)
63
if (!m_algorithm) return;
65
if (!in.nodes.empty())
67
for (msg::nodes_t::const_iterator i = in.nodes.begin()
68
, end(in.nodes.end()); i != end; ++i)
70
m_algorithm->traverse(i->id, i->addr);
73
m_algorithm->finished(m_self);
77
void refresh_observer::timeout()
79
if (!m_algorithm) return;
80
m_algorithm->failed(m_self);
84
ping_observer::~ping_observer()
86
if (m_algorithm) m_algorithm->ping_timeout(m_self, true);
89
void ping_observer::reply(msg const& m)
91
if (!m_algorithm) return;
93
m_algorithm->ping_reply(m_self);
97
void ping_observer::timeout()
99
if (!m_algorithm) return;
100
m_algorithm->ping_timeout(m_self);
104
void refresh::invoke(node_id const& nid, udp::endpoint addr)
106
observer_ptr o(new (m_rpc.allocator().malloc()) refresh_observer(
107
this, nid, m_target));
109
o->m_in_constructor = false;
112
m_rpc.invoke(messages::find_node, addr, o);
117
m_leftover_nodes_iterator = (int)m_results.size() > m_max_results ?
118
m_results.begin() + m_max_results : m_results.end();
120
invoke_pings_or_finish();
123
void refresh::ping_reply(node_id nid)
126
invoke_pings_or_finish();
129
void refresh::ping_timeout(node_id nid, bool prevent_request)
132
invoke_pings_or_finish(prevent_request);
135
void refresh::invoke_pings_or_finish(bool prevent_request)
139
--m_max_active_pings;
140
if (m_max_active_pings <= 0)
141
m_max_active_pings = 1;
145
while (m_active_pings < m_max_active_pings)
147
if (m_leftover_nodes_iterator == m_results.end()) break;
149
result const& node = *m_leftover_nodes_iterator;
151
// Skip initial nodes
152
if (node.flags & result::initial)
154
++m_leftover_nodes_iterator;
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