Merge pull request #22 from aldenml/session-dht-api

Added dht_get_peers and dht_announce at session level
This commit is contained in:
Arvid Norberg 2015-07-12 12:35:50 -04:00
commit 3036bce311
5 changed files with 44 additions and 10 deletions

View File

@ -57,6 +57,7 @@ namespace
{ {
std::string ip = extract<std::string>(n[0]); std::string ip = extract<std::string>(n[0]);
int port = extract<int>(n[1]); int port = extract<int>(n[1]);
allow_threading_guard guard;
s.add_dht_node(std::make_pair(ip, port)); s.add_dht_node(std::make_pair(ip, port));
} }
@ -65,7 +66,8 @@ namespace
allow_threading_guard guard; allow_threading_guard guard;
return s.add_dht_router(std::make_pair(router_, port_)); return s.add_dht_router(std::make_pair(router_, port_));
} }
#endif
#endif // TORRENT_DISABLE_DHT
void add_extension(lt::session& s, object const& e) void add_extension(lt::session& s, object const& e)
{ {
@ -499,6 +501,15 @@ namespace
void bind_session() void bind_session()
{ {
#ifndef TORRENT_DISABLE_DHT
void (lt::session::*dht_get_immutable_item)(sha1_hash const&) = &lt::session::dht_get_item;
void (lt::session::*dht_get_mutable_item)(boost::array<char, 32>, std::string) = &lt::session::dht_get_item;
sha1_hash (lt::session::*dht_put_immutable_item)(entry data) = &lt::session::dht_put_item;
void (lt::session::*dht_put_mutable_item)(boost::array<char, 32> key
, boost::function<void(entry&, boost::array<char,64>&, boost::uint64_t&, std::string const&)> cb
, std::string salt) = &lt::session::dht_put_item; // TODO: resolve the callback type for python
#endif // TORRENT_DISABLE_DHT
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
void (lt::session::*start_dht0)() = &lt::session::start_dht; void (lt::session::*start_dht0)() = &lt::session::start_dht;
@ -659,7 +670,7 @@ void bind_session()
.def("is_listening", allow_threads(&lt::session::is_listening)) .def("is_listening", allow_threads(&lt::session::is_listening))
.def("listen_port", allow_threads(&lt::session::listen_port)) .def("listen_port", allow_threads(&lt::session::listen_port))
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
.def("add_dht_node", add_dht_node) .def("add_dht_node", &add_dht_node)
.def( .def(
"add_dht_router", &add_dht_router "add_dht_router", &add_dht_router
, (arg("router"), "port") , (arg("router"), "port")
@ -667,7 +678,13 @@ void bind_session()
.def("is_dht_running", allow_threads(&lt::session::is_dht_running)) .def("is_dht_running", allow_threads(&lt::session::is_dht_running))
.def("set_dht_settings", allow_threads(&lt::session::set_dht_settings)) .def("set_dht_settings", allow_threads(&lt::session::set_dht_settings))
.def("get_dht_settings", allow_threads(&lt::session::get_dht_settings)) .def("get_dht_settings", allow_threads(&lt::session::get_dht_settings))
#endif .def("dht_get_immutable_item", allow_threads(dht_get_immutable_item))
.def("dht_get_mutable_item", allow_threads(dht_get_mutable_item))
.def("dht_put_immutable_item", allow_threads(dht_put_immutable_item))
.def("dht_put_mutable_item", allow_threads(dht_put_mutable_item))
.def("dht_get_peers", allow_threads(&lt::session::dht_get_peers))
.def("dht_announce", allow_threads(&lt::session::dht_announce))
#endif // TORRENT_DISABLE_DHT
.def("add_torrent", &add_torrent) .def("add_torrent", &add_torrent)
.def("async_add_torrent", &async_add_torrent) .def("async_add_torrent", &async_add_torrent)
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS

View File

@ -307,7 +307,7 @@ namespace libtorrent
void dht_get_mutable_item(boost::array<char, 32> key void dht_get_mutable_item(boost::array<char, 32> key
, std::string salt = std::string()); , std::string salt = std::string());
void dht_put_item(entry data, sha1_hash target); void dht_put_immutable_item(entry data, sha1_hash target);
void dht_put_mutable_item(boost::array<char, 32> key void dht_put_mutable_item(boost::array<char, 32> key
, boost::function<void(entry&, boost::array<char,64>& , boost::function<void(entry&, boost::array<char,64>&

View File

@ -422,9 +422,12 @@ namespace libtorrent
// calling the callback in between is convenient. // calling the callback in between is convenient.
void dht_put_item(boost::array<char, 32> key void dht_put_item(boost::array<char, 32> key
, boost::function<void(entry&, boost::array<char,64>& , boost::function<void(entry&, boost::array<char,64>&
, boost::uint64_t&, std::string const&)> cb , boost::uint64_t&, std::string const&)> cb
, std::string salt = std::string()); , std::string salt = std::string());
void dht_get_peers(sha1_hash const& info_hash);
void dht_announce(sha1_hash const& info_hash, int port = 0, int flags = 0);
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
// deprecated in 0.15 // deprecated in 0.15
// use save_state and load_state instead // use save_state and load_state instead

View File

@ -367,14 +367,14 @@ namespace libtorrent
sha1_hash ret = hasher(&buf[0], buf.size()).final(); sha1_hash ret = hasher(&buf[0], buf.size()).final();
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
TORRENT_ASYNC_CALL2(dht_put_item, data, ret); TORRENT_ASYNC_CALL2(dht_put_immutable_item, data, ret);
#endif #endif
return ret; return ret;
} }
void session_handle::dht_put_item(boost::array<char, 32> key void session_handle::dht_put_item(boost::array<char, 32> key
, boost::function<void(entry&, boost::array<char,64>& , boost::function<void(entry&, boost::array<char,64>&
, boost::uint64_t&, std::string const&)> cb , boost::uint64_t&, std::string const&)> cb
, std::string salt) , std::string salt)
{ {
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
@ -382,6 +382,20 @@ namespace libtorrent
#endif #endif
} }
void session_handle::dht_get_peers(sha1_hash const& info_hash)
{
#ifndef TORRENT_DISABLE_DHT
TORRENT_ASYNC_CALL1(dht_get_peers, info_hash);
#endif
}
void session_handle::dht_announce(sha1_hash const& info_hash, int port, int flags)
{
#ifndef TORRENT_DISABLE_DHT
TORRENT_ASYNC_CALL3(dht_announce, info_hash, port, flags);
#endif
}
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
entry session_handle::dht_state() const entry session_handle::dht_state() const
{ {

View File

@ -5496,7 +5496,7 @@ retry:
namespace { namespace {
void on_dht_put(alert_manager& alerts, sha1_hash target) void on_dht_put_immutable_item(alert_manager& alerts, sha1_hash target)
{ {
if (alerts.should_post<dht_put_alert>()) if (alerts.should_post<dht_put_alert>())
alerts.emplace_alert<dht_put_alert>(target); alerts.emplace_alert<dht_put_alert>(target);
@ -5526,10 +5526,10 @@ retry:
} // anonymous namespace } // anonymous namespace
void session_impl::dht_put_item(entry data, sha1_hash target) void session_impl::dht_put_immutable_item(entry data, sha1_hash target)
{ {
if (!m_dht) return; if (!m_dht) return;
m_dht->put_item(data, boost::bind(&on_dht_put, boost::ref(m_alerts) m_dht->put_item(data, boost::bind(&on_dht_put_immutable_item, boost::ref(m_alerts)
, target)); , target));
} }