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/find_data.hpp>
|
|
|
|
#include <libtorrent/kademlia/routing_table.hpp>
|
|
|
|
#include <libtorrent/kademlia/rpc_manager.hpp>
|
2008-09-20 19:42:25 +02:00
|
|
|
#include <libtorrent/kademlia/node.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <libtorrent/io.hpp>
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <libtorrent/socket.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
find_data_observer::~find_data_observer()
|
|
|
|
{
|
|
|
|
if (m_algorithm) m_algorithm->failed(m_self);
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void find_data_observer::reply(msg const& m)
|
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
if (!m_algorithm)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2007-01-29 08:39:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-23 21:04:12 +01:00
|
|
|
if (!m.write_token.empty())
|
|
|
|
m_algorithm->got_write_token(m.id, m.write_token);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
if (!m.peers.empty())
|
|
|
|
m_algorithm->got_data(&m);
|
2008-12-23 21:04:12 +01:00
|
|
|
|
|
|
|
if (!m.nodes.empty())
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
for (msg::nodes_t::const_iterator i = m.nodes.begin()
|
|
|
|
, end(m.nodes.end()); i != end; ++i)
|
|
|
|
{
|
2008-11-10 03:08:42 +01:00
|
|
|
m_algorithm->traverse(i->id, udp::endpoint(i->addr, i->port));
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 find_data_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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
find_data::find_data(
|
2008-09-20 19:42:25 +02:00
|
|
|
node_impl& node
|
|
|
|
, node_id target
|
2008-12-23 21:04:12 +01:00
|
|
|
, data_callback const& dcallback
|
|
|
|
, nodes_callback const& ncallback)
|
2008-09-20 19:42:25 +02:00
|
|
|
: traversal_algorithm(node, target, node.m_table.begin(), node.m_table.end())
|
2008-12-23 21:04:12 +01:00
|
|
|
, m_data_callback(dcallback)
|
|
|
|
, m_nodes_callback(ncallback)
|
|
|
|
, m_target(target)
|
2006-08-01 17:27:08 +02:00
|
|
|
, m_done(false)
|
|
|
|
{
|
|
|
|
boost::intrusive_ptr<find_data> self(this);
|
|
|
|
add_requests();
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void find_data::invoke(node_id const& id, udp::endpoint addr)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
if (m_done)
|
|
|
|
{
|
|
|
|
m_invoke_count = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
TORRENT_ASSERT(m_node.m_rpc.allocation_size() >= sizeof(find_data_observer));
|
2009-05-14 00:18:41 +02:00
|
|
|
void* ptr = m_node.m_rpc.allocator().malloc();
|
|
|
|
if (ptr == 0)
|
|
|
|
{
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_node.m_rpc.allocator().set_next_size(10);
|
|
|
|
observer_ptr o(new (ptr) find_data_observer(this, id));
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-02-09 22:04:24 +01:00
|
|
|
o->m_in_constructor = false;
|
|
|
|
#endif
|
2008-09-20 19:42:25 +02:00
|
|
|
m_node.m_rpc.invoke(messages::get_peers, addr, o);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void find_data::got_data(msg const* m)
|
|
|
|
{
|
2008-12-23 21:04:12 +01:00
|
|
|
m_data_callback(m->peers);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void find_data::done()
|
|
|
|
{
|
|
|
|
if (m_invoke_count != 0) return;
|
2008-12-23 21:04:12 +01:00
|
|
|
|
|
|
|
std::vector<std::pair<node_entry, std::string> > results;
|
|
|
|
int num_results = m_node.m_table.bucket_size();
|
|
|
|
for (std::vector<result>::iterator i = m_results.begin()
|
|
|
|
, end(m_results.end()); i != end && num_results > 0; ++i)
|
|
|
|
{
|
|
|
|
if (i->flags & result::no_id) continue;
|
|
|
|
if ((i->flags & result::queried) == 0) continue;
|
|
|
|
std::map<node_id, std::string>::iterator j = m_write_tokens.find(i->id);
|
|
|
|
if (j == m_write_tokens.end()) continue;
|
|
|
|
results.push_back(std::make_pair(node_entry(i->id, i->addr), j->second));
|
|
|
|
--num_results;
|
|
|
|
}
|
|
|
|
m_nodes_callback(results);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|