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 #ifndef TRAVERSAL_ALGORITHM_050324_HPP
34 #define TRAVERSAL_ALGORITHM_050324_HPP
39 #include <libtorrent/kademlia/node_id.hpp>
40 #include <libtorrent/kademlia/routing_table.hpp>
41 #include <libtorrent/kademlia/logging.hpp>
43 #include <boost/noncopyable.hpp>
44 #include <boost/intrusive_ptr.hpp>
45 #include <boost/bind.hpp>
46 #include <boost/pool/pool.hpp>
48 namespace libtorrent
{ namespace dht
50 #ifdef TORRENT_DHT_VERBOSE_LOGGING
51 TORRENT_DECLARE_LOG(traversal
);
56 // this class may not be instantiated as a stack object
57 class traversal_algorithm
: boost::noncopyable
60 void traverse(node_id
const& id
, udp::endpoint addr
);
61 void finished(node_id
const& id
);
62 void failed(node_id
const& id
, bool prevent_request
= false);
63 virtual ~traversal_algorithm() {}
64 boost::pool
<>& allocator() const;
72 , routing_table
& table
79 void add_entry(node_id
const& id
, udp::endpoint addr
, unsigned char flags
);
81 virtual void done() = 0;
82 virtual void invoke(node_id
const& id
, udp::endpoint addr
) = 0;
86 result(node_id
const& id
, udp::endpoint addr
, unsigned char f
= 0)
87 : id(id
), addr(addr
), flags(f
) {}
91 enum { queried
= 1, initial
= 2, no_id
= 4 };
95 std::vector
<result
>::iterator
last_iterator();
97 friend void intrusive_ptr_add_ref(traversal_algorithm
* p
)
102 friend void intrusive_ptr_release(traversal_algorithm
* p
)
104 if (--p
->m_ref_count
== 0)
113 std::vector
<result
> m_results
;
114 std::set
<udp::endpoint
> m_failed
;
115 routing_table
& m_table
;
121 traversal_algorithm::traversal_algorithm(
125 , routing_table
& table
127 , InIt start
// <- nodes to initiate traversal with
132 , m_branch_factor(branch_factor
)
133 , m_max_results(max_results
)
140 for (InIt i
= start
; i
!= end
; ++i
)
142 add_entry(i
->id
, i
->addr
, result::initial
);
145 // in case the routing table is empty, use the
146 // router nodes in the table
149 for (routing_table::router_iterator i
= table
.router_begin()
150 , end(table
.router_end()); i
!= end
; ++i
)
152 add_entry(node_id(0), *i
, result::initial
);
158 } } // namespace libtorrent::dht
160 #endif // TRAVERSAL_ALGORITHM_050324_HPP