00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef ROUTING_TABLE_HPP
00034 #define ROUTING_TABLE_HPP
00035
00036 #include <vector>
00037 #include <deque>
00038 #include <boost/cstdint.hpp>
00039 #include <boost/date_time/posix_time/posix_time.hpp>
00040
00041 #include <boost/iterator/iterator_facade.hpp>
00042 #include <boost/iterator/iterator_categories.hpp>
00043 #include <boost/utility.hpp>
00044 #include <boost/tuple/tuple.hpp>
00045 #include <boost/array.hpp>
00046 #include <set>
00047
00048 #include <libtorrent/kademlia/logging.hpp>
00049
00050 #include <libtorrent/kademlia/node_id.hpp>
00051 #include <libtorrent/kademlia/node_entry.hpp>
00052 #include <libtorrent/session_settings.hpp>
00053
00054 namespace pt = boost::posix_time;
00055
00056 namespace libtorrent { namespace dht
00057 {
00058
00059 using asio::ip::udp;
00060
00061
00062
00063 typedef std::deque<node_entry> bucket_t;
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 class routing_table;
00078
00079 namespace aux
00080 {
00081
00082
00083 class routing_table_iterator
00084 : public boost::iterator_facade<
00085 routing_table_iterator
00086 , node_entry const
00087 , boost::forward_traversal_tag
00088 >
00089 {
00090 public:
00091 routing_table_iterator()
00092 {
00093 }
00094
00095 private:
00096 friend class libtorrent::dht::routing_table;
00097 friend class boost::iterator_core_access;
00098
00099 typedef boost::array<std::pair<bucket_t, bucket_t>, 160>::const_iterator
00100 bucket_iterator_t;
00101
00102 routing_table_iterator(
00103 bucket_iterator_t begin
00104 , bucket_iterator_t end)
00105 : m_bucket_iterator(begin)
00106 , m_bucket_end(end)
00107 , m_iterator(begin != end ? begin->first.begin() : bucket_t::iterator())
00108 {
00109 if (m_bucket_iterator == m_bucket_end) return;
00110 while (m_iterator == m_bucket_iterator->first.end())
00111 {
00112 if (++m_bucket_iterator == m_bucket_end)
00113 break;
00114 m_iterator = m_bucket_iterator->first.begin();
00115 }
00116 }
00117
00118 bool equal(routing_table_iterator const& other) const
00119 {
00120 return m_bucket_iterator == other.m_bucket_iterator
00121 && (m_bucket_iterator == m_bucket_end
00122 || m_iterator == other.m_iterator);
00123 }
00124
00125 void increment()
00126 {
00127 assert(m_bucket_iterator != m_bucket_end);
00128 ++m_iterator;
00129 while (m_iterator == m_bucket_iterator->first.end())
00130 {
00131 if (++m_bucket_iterator == m_bucket_end)
00132 break;
00133 m_iterator = m_bucket_iterator->first.begin();
00134 }
00135 }
00136
00137 node_entry const& dereference() const
00138 {
00139 assert(m_bucket_iterator != m_bucket_end);
00140 return *m_iterator;
00141 }
00142
00143 bucket_iterator_t m_bucket_iterator;
00144 bucket_iterator_t m_bucket_end;
00145 bucket_t::const_iterator m_iterator;
00146 };
00147
00148 }
00149
00150 class routing_table
00151 {
00152 public:
00153 typedef aux::routing_table_iterator iterator;
00154 typedef iterator const_iterator;
00155
00156 routing_table(node_id const& id, int bucket_size
00157 , dht_settings const& settings);
00158
00159 void node_failed(node_id const& id);
00160
00161
00162
00163 void add_router_node(udp::endpoint router);
00164
00165
00166 typedef std::set<udp::endpoint>::const_iterator router_iterator;
00167 router_iterator router_begin() const { return m_router_nodes.begin(); }
00168 router_iterator router_end() const { return m_router_nodes.end(); }
00169
00170
00171
00172
00173
00174 bool node_seen(node_id const& id, udp::endpoint addr);
00175
00176
00177
00178
00179
00180 boost::posix_time::ptime next_refresh(int bucket);
00181
00182
00183
00184 void find_node(node_id const& id, std::vector<node_entry>& l
00185 , bool include_self, int count = 0);
00186
00187
00188
00189
00190 bool need_node(node_id const& id);
00191
00192
00193
00194 void touch_bucket(int bucket);
00195
00196 int bucket_size(int bucket)
00197 {
00198 assert(bucket >= 0 && bucket < 160);
00199 return (int)m_buckets[bucket].first.size();
00200 }
00201 int bucket_size() const { return m_bucket_size; }
00202
00203 iterator begin() const;
00204 iterator end() const;
00205
00206 boost::tuple<int, int> size() const;
00207
00208
00209
00210 bool need_bootstrap() const;
00211
00212 void replacement_cache(bucket_t& nodes) const;
00213
00214
00215
00216 void print_state(std::ostream& os) const;
00217
00218 private:
00219
00220
00221 int m_bucket_size;
00222
00223 dht_settings const& m_settings;
00224
00225
00226 typedef boost::array<std::pair<bucket_t, bucket_t>, 160> table_t;
00227 table_t m_buckets;
00228
00229 typedef boost::array<boost::posix_time::ptime, 160> table_activity_t;
00230 table_activity_t m_bucket_activity;
00231 node_id m_id;
00232
00233
00234
00235
00236
00237 std::set<udp::endpoint> m_router_nodes;
00238
00239
00240 int m_lowest_active_bucket;
00241 };
00242
00243 } }
00244
00245 #endif // ROUTING_TABLE_HPP
00246