2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2006, Arvid Norberg & Daniel Wallin
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <libtorrent/kademlia/refresh.hpp>
|
|
|
|
#include <libtorrent/kademlia/routing_table.hpp>
|
|
|
|
#include <libtorrent/kademlia/rpc_manager.hpp>
|
|
|
|
#include <libtorrent/kademlia/logging.hpp>
|
2007-05-23 10:45:12 +02:00
|
|
|
#include <libtorrent/kademlia/msg.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <libtorrent/io.hpp>
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
using boost::bind;
|
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
|
|
|
using asio::ip::udp;
|
|
|
|
|
2006-08-02 00:23:05 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2006-08-01 17:27:08 +02:00
|
|
|
TORRENT_DEFINE_LOG(refresh)
|
2006-08-02 00:23:05 +02:00
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
refresh_observer::~refresh_observer()
|
|
|
|
{
|
|
|
|
if (m_algorithm) m_algorithm->failed(m_self, true);
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void refresh_observer::reply(msg const& in)
|
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (!m_algorithm) return;
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
if (!in.nodes.empty())
|
|
|
|
{
|
|
|
|
for (msg::nodes_t::const_iterator i = in.nodes.begin()
|
|
|
|
, end(in.nodes.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
m_algorithm->traverse(i->id, i->addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_algorithm->finished(m_self);
|
2007-01-29 08:39:33 +01:00
|
|
|
m_algorithm = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void refresh_observer::timeout()
|
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (!m_algorithm) return;
|
2006-08-01 17:27:08 +02:00
|
|
|
m_algorithm->failed(m_self);
|
2007-01-29 08:39:33 +01:00
|
|
|
m_algorithm = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
ping_observer::~ping_observer()
|
|
|
|
{
|
|
|
|
if (m_algorithm) m_algorithm->ping_timeout(m_self, true);
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void ping_observer::reply(msg const& m)
|
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (!m_algorithm) return;
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
m_algorithm->ping_reply(m_self);
|
2007-01-29 08:39:33 +01:00
|
|
|
m_algorithm = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ping_observer::timeout()
|
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (!m_algorithm) return;
|
2006-08-01 17:27:08 +02:00
|
|
|
m_algorithm->ping_timeout(m_self);
|
2007-01-29 08:39:33 +01:00
|
|
|
m_algorithm = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2006-08-02 00:23:05 +02:00
|
|
|
void refresh::invoke(node_id const& nid, udp::endpoint addr)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2007-05-23 10:45:12 +02:00
|
|
|
observer_ptr o(new (m_rpc.allocator().malloc()) refresh_observer(
|
|
|
|
this, nid, m_target));
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-05-23 10:45:12 +02:00
|
|
|
m_rpc.invoke(messages::find_node, addr, o);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void refresh::done()
|
|
|
|
{
|
|
|
|
m_leftover_nodes_iterator = (int)m_results.size() > m_max_results ?
|
|
|
|
m_results.begin() + m_max_results : m_results.end();
|
|
|
|
|
|
|
|
invoke_pings_or_finish();
|
|
|
|
}
|
|
|
|
|
2006-08-02 00:23:05 +02:00
|
|
|
void refresh::ping_reply(node_id nid)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
m_active_pings--;
|
|
|
|
invoke_pings_or_finish();
|
|
|
|
}
|
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
void refresh::ping_timeout(node_id nid, bool prevent_request)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
m_active_pings--;
|
2007-01-29 08:39:33 +01:00
|
|
|
invoke_pings_or_finish(prevent_request);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
void refresh::invoke_pings_or_finish(bool prevent_request)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (prevent_request)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
--m_max_active_pings;
|
|
|
|
if (m_max_active_pings <= 0)
|
|
|
|
m_max_active_pings = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (m_active_pings < m_max_active_pings)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (m_leftover_nodes_iterator == m_results.end()) break;
|
|
|
|
|
|
|
|
result const& node = *m_leftover_nodes_iterator;
|
|
|
|
|
|
|
|
// Skip initial nodes
|
|
|
|
if (node.flags & result::initial)
|
|
|
|
{
|
|
|
|
++m_leftover_nodes_iterator;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2007-05-23 10:45:12 +02:00
|
|
|
observer_ptr o(new (m_rpc.allocator().malloc()) ping_observer(
|
|
|
|
this, node.id));
|
|
|
|
m_rpc.invoke(messages::ping, node.addr, o);
|
2007-01-29 08:39:33 +01:00
|
|
|
++m_active_pings;
|
|
|
|
++m_leftover_nodes_iterator;
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_active_pings == 0)
|
|
|
|
{
|
|
|
|
m_done_callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|