~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to src/kademlia/closest_nodes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2006, Arvid Norberg & Daniel Wallin
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
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.
 
18
 
 
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.
 
30
 
 
31
*/
 
32
 
 
33
#include "libtorrent/pch.hpp"
 
34
 
 
35
#include <libtorrent/kademlia/closest_nodes.hpp>
 
36
#include <libtorrent/kademlia/routing_table.hpp>
 
37
#include <libtorrent/kademlia/rpc_manager.hpp>
 
38
#include "libtorrent/assert.hpp"
 
39
 
 
40
namespace libtorrent { namespace dht
 
41
{
 
42
 
 
43
using asio::ip::udp;
 
44
 
 
45
closest_nodes_observer::~closest_nodes_observer()
 
46
{
 
47
        if (m_algorithm) m_algorithm->failed(m_self, true);
 
48
}
 
49
 
 
50
void closest_nodes_observer::reply(msg const& in)
 
51
{
 
52
        if (!m_algorithm)
 
53
        {
 
54
                TORRENT_ASSERT(false);
 
55
                return;
 
56
        }
 
57
 
 
58
        if (!in.nodes.empty())
 
59
        {
 
60
                for (msg::nodes_t::const_iterator i = in.nodes.begin()
 
61
                        , end(in.nodes.end()); i != end; ++i)
 
62
                {
 
63
                        m_algorithm->traverse(i->id, i->addr);
 
64
                }
 
65
        }
 
66
        m_algorithm->finished(m_self);
 
67
        m_algorithm = 0;
 
68
}
 
69
 
 
70
void closest_nodes_observer::timeout()
 
71
{
 
72
        if (!m_algorithm) return;
 
73
        m_algorithm->failed(m_self);
 
74
        m_algorithm = 0;
 
75
}
 
76
 
 
77
 
 
78
closest_nodes::closest_nodes(
 
79
        node_id target
 
80
        , int branch_factor
 
81
        , int max_results
 
82
        , routing_table& table
 
83
        , rpc_manager& rpc
 
84
        , done_callback const& callback
 
85
)
 
86
        : traversal_algorithm(
 
87
                target
 
88
                , branch_factor
 
89
                , max_results
 
90
                , table
 
91
                , rpc
 
92
                , table.begin()
 
93
                , table.end()
 
94
        )
 
95
        , m_done_callback(callback)
 
96
{
 
97
        boost::intrusive_ptr<closest_nodes> self(this);
 
98
        add_requests();
 
99
}
 
100
 
 
101
void closest_nodes::invoke(node_id const& id, udp::endpoint addr)
 
102
{
 
103
        observer_ptr o(new (m_rpc.allocator().malloc()) closest_nodes_observer(this, id, m_target));
 
104
#ifndef NDEBUG
 
105
        o->m_in_constructor = false;
 
106
#endif
 
107
        m_rpc.invoke(messages::find_node, addr, o);
 
108
}
 
109
 
 
110
void closest_nodes::done()
 
111
{
 
112
        std::vector<node_entry> results;
 
113
        int num_results = m_max_results;
 
114
        for (std::vector<result>::iterator i = m_results.begin()
 
115
                , end(m_results.end()); i != end && num_results > 0; ++i)
 
116
        {
 
117
                if (i->flags & result::no_id) continue;
 
118
                if ((i->flags & result::queried) == 0) continue;
 
119
                results.push_back(node_entry(i->id, i->addr));
 
120
                --num_results;
 
121
        }
 
122
        m_done_callback(results);
 
123
}
 
124
 
 
125
void closest_nodes::initiate(
 
126
        node_id target
 
127
        , int branch_factor
 
128
        , int max_results
 
129
        , routing_table& table
 
130
        , rpc_manager& rpc
 
131
        , done_callback const& callback
 
132
)
 
133
{
 
134
        new closest_nodes(target, branch_factor, max_results, table, rpc, callback);
 
135
}
 
136
 
 
137
} } // namespace libtorrent::dht
 
138