From b1411fcdf79d56a2648d7017dbf2815d18e1456c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 30 Aug 2006 00:09:58 +0000 Subject: [PATCH] the DHT can now change listen port runtime, and the same listen interface will be used for the DHT as is used for the torrent listen port. The DHT buckets are refreshed slightly more evenly distributed over time. --- examples/client_test.cpp | 4 ++-- include/libtorrent/kademlia/dht_tracker.hpp | 4 +++- include/libtorrent/kademlia/routing_table.hpp | 3 ++- src/kademlia/dht_tracker.cpp | 12 ++++++++++-- src/kademlia/routing_table.cpp | 6 ++++-- src/session.cpp | 17 ++++++++++++++--- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index be98ae6ee..86cee60ba 100755 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -899,10 +899,10 @@ int main(int ac, char* av[]) for (int i = 0; i < info.num_files(); ++i) { if (file_progress[i] == 1.f) - out << progress_bar(file_progress[i], 10, "32") << " " + out << progress_bar(file_progress[i], 20, "32") << " " << info.file_at(i).path.leaf() << "\n"; else - out << progress_bar(file_progress[i], 10, "33") << " " + out << progress_bar(file_progress[i], 20, "33") << " " << info.file_at(i).path.leaf() << "\n"; } diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 52d25a392..c5c1abfdc 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -62,10 +62,12 @@ namespace libtorrent { namespace dht struct dht_tracker { dht_tracker(asio::io_service& d, dht_settings const& settings - , entry const& bootstrap); + , asio::ip::address listen_interface, entry const& bootstrap); void add_node(udp::endpoint node); void add_node(std::pair const& node); + + void rebind(asio::ip::address interface, int listen_port); entry state() const; diff --git a/include/libtorrent/kademlia/routing_table.hpp b/include/libtorrent/kademlia/routing_table.hpp index b2489cae9..96449bda1 100644 --- a/include/libtorrent/kademlia/routing_table.hpp +++ b/include/libtorrent/kademlia/routing_table.hpp @@ -216,7 +216,8 @@ private: typedef boost::array, 160> table_t; table_t m_buckets; // timestamps of the last activity in each bucket - boost::array m_bucket_activity; + typedef boost::array table_activity_t; + table_activity_t m_bucket_activity; node_id m_id; // our own node id // this is the lowest bucket index with nodes in it diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 54b5455d0..ecf4491db 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -115,9 +115,9 @@ namespace libtorrent { namespace dht // class that puts the networking and the kademlia node in a single // unit and connecting them together. dht_tracker::dht_tracker(asio::io_service& d, dht_settings const& settings - , entry const& bootstrap) + , asio::ip::address listen_interface, entry const& bootstrap) : m_demuxer(d) - , m_socket(m_demuxer, udp::endpoint(address(), settings.service_port)) + , m_socket(m_demuxer, udp::endpoint(listen_interface, settings.service_port)) , m_dht(bind(&dht_tracker::send_packet, this, _1), settings , read_id(bootstrap)) , m_buffer(0) @@ -215,6 +215,14 @@ namespace libtorrent { namespace dht assert(false); }; + void dht_tracker::rebind(asio::ip::address listen_interface, int listen_port) + { + m_socket.close(); + m_socket.open(asio::ip::udp::v4()); + m_socket.bind(udp::endpoint(listen_interface, listen_port)); + assert(m_settings.service_port == listen_port); + } + void dht_tracker::tick(asio::error const& e) try { diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index 1deb3e32b..2d85849d3 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -66,8 +66,10 @@ routing_table::routing_table(node_id const& id, int bucket_size , m_id(id) , m_lowest_active_bucket(160) { - std::fill(m_bucket_activity.begin(), m_bucket_activity.end() - , second_clock::universal_time() - hours(1)); + // distribute the refresh times for the buckets in an + // attempt do even out the network load + for (int i = 0; i < 160; ++i) + m_bucket_activity[i] = second_clock::universal_time() - seconds(15*60 - i*5); } boost::tuple routing_table::size() const diff --git a/src/session.cpp b/src/session.cpp index 5aa94d3b5..9eed09ff6 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -1424,6 +1424,16 @@ namespace libtorrent if (m_impl.m_listen_socket) m_impl.m_listen_socket.reset(); + +#ifndef TORRENT_DISABLE_DHT + if (m_impl.m_listen_interface.address() != new_interface.address() + && m_impl.m_dht) + { + // the listen interface changed, rebind the dht listen socket as well + m_impl.m_dht->rebind(new_interface.address() + , m_impl.m_dht_settings.service_port); + } +#endif m_impl.m_incoming_connection = false; m_impl.m_listen_interface = new_interface; @@ -1466,7 +1476,8 @@ namespace libtorrent void session::start_dht(entry const& startup_state) { m_impl.m_dht.reset(new dht::dht_tracker(m_impl.m_selector - , m_impl.m_dht_settings, startup_state)); + , m_impl.m_dht_settings, m_impl.m_listen_interface.address() + , startup_state)); } void session::stop_dht() @@ -1479,8 +1490,8 @@ namespace libtorrent if (settings.service_port != m_impl.m_dht_settings.service_port && m_impl.m_dht) { - assert(false); // not implemented yet - // TODO: change dht service port! + m_impl.m_dht->rebind(m_impl.m_listen_interface.address() + , settings.service_port); } m_impl.m_dht_settings = settings; }