This commit is contained in:
Arvid Norberg 2008-03-24 02:19:47 +00:00
parent 1782fa04cc
commit 211ae62d33
5 changed files with 38 additions and 13 deletions

View File

@ -84,12 +84,11 @@ protected:
struct result
{
result(node_id const& id, udp::endpoint addr, unsigned char f = 0)
: id(id), addr(addr), flags(f)
{}
: id(id), addr(addr), flags(f) {}
node_id id;
udp::endpoint addr;
enum { queried = 1, initial = 2 };
enum { queried = 1, initial = 2, no_id = 4 };
unsigned char flags;
};
@ -150,7 +149,7 @@ traversal_algorithm::traversal_algorithm(
for (routing_table::router_iterator i = table.router_begin()
, end(table.router_end()); i != end; ++i)
{
add_entry(generate_id(), *i, result::initial);
add_entry(node_id(0), *i, result::initial);
}
}

View File

@ -111,12 +111,13 @@ void closest_nodes::invoke(node_id const& id, udp::endpoint addr)
void closest_nodes::done()
{
std::vector<node_entry> results;
int result_size = m_table.bucket_size();
if (result_size > (int)m_results.size()) result_size = (int)m_results.size();
int num_results = m_table.bucket_size();
for (std::vector<result>::iterator i = m_results.begin()
, end(m_results.begin() + result_size); i != end; ++i)
, end(m_results.end()); i != end && num_results >= 0; ++i)
{
if (i->flags & result::no_id) continue;
results.push_back(node_entry(i->id, i->addr));
--num_results;
}
m_done_callback(results);
}

View File

@ -173,6 +173,12 @@ void node_impl::refresh(node_id const& id
void node_impl::bootstrap(std::vector<udp::endpoint> const& nodes
, boost::function0<void> f)
{
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "bootrapping: " << nodes.size();
for (std::vector<udp::endpoint>::const_iterator i = nodes.begin()
, end(nodes.end()); i != end; ++i)
TORRENT_LOG(node) << " " << *i;
#endif
std::vector<node_entry> start;
start.reserve(nodes.size());
std::copy(nodes.begin(), nodes.end(), std::back_inserter(start));
@ -253,6 +259,11 @@ namespace
, int listen_port, sha1_hash const& ih
, boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f)
{
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "announce response [ ih: " << ih
<< " p: " << listen_port
<< " nodes: " << v.size() << " ]" ;
#endif
bool nodes = false;
// only store on the first k nodes
for (std::vector<node_entry>::const_iterator i = v.begin()
@ -270,6 +281,9 @@ namespace
void node_impl::add_router_node(udp::endpoint router)
{
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "adding router node: " << router;
#endif
m_table.add_router_node(router);
}
@ -287,6 +301,9 @@ void node_impl::add_node(udp::endpoint node)
void node_impl::announce(sha1_hash const& info_hash, int listen_port
, boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f)
{
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "announcing [ ih: " << info_hash << " p: " << listen_port << " ]" ;
#endif
// search for nodes with ids close to id, and then invoke the
// get_peers and then announce_peer rpc on them.
closest_nodes::initiate(info_hash, m_settings.search_branching

View File

@ -452,7 +452,8 @@ void routing_table::find_node(node_id const& target
routing_table::iterator routing_table::begin() const
{
return iterator(m_buckets.begin(), m_buckets.end());
// +1 to avoid ourself
return iterator(m_buckets.begin() + 1, m_buckets.end());
}
routing_table::iterator routing_table::end() const

View File

@ -51,7 +51,12 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
{
if (m_failed.find(addr) != m_failed.end()) return;
result const entry(id, addr, flags);
result entry(id, addr, flags);
if (entry.id.is_all_zeros())
{
entry.id = generate_id();
entry.flags |= result::no_id;
}
std::vector<result>::iterator i = std::lower_bound(
m_results.begin()
@ -83,6 +88,7 @@ boost::pool<>& traversal_algorithm::allocator() const
void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
{
TORRENT_ASSERT(!id.is_all_zeros());
add_entry(id, addr, 0);
}
@ -100,6 +106,7 @@ void traversal_algorithm::failed(node_id const& id, bool prevent_request)
{
m_invoke_count--;
TORRENT_ASSERT(!id.is_all_zeros());
std::vector<result>::iterator i = std::find_if(
m_results.begin()
, m_results.end()
@ -119,6 +126,10 @@ void traversal_algorithm::failed(node_id const& id, bool prevent_request)
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(traversal) << "failed: " << i->id << " " << i->addr;
#endif
// don't tell the routing table about
// node ids that we just generated ourself
if ((i->flags & result::no_id) == 0)
m_table.node_failed(id);
m_results.erase(i);
}
if (prevent_request)
@ -126,10 +137,6 @@ void traversal_algorithm::failed(node_id const& id, bool prevent_request)
--m_branch_factor;
if (m_branch_factor <= 0) m_branch_factor = 1;
}
else
{
m_table.node_failed(id);
}
add_requests();
if (m_invoke_count == 0) done();
}