diff --git a/ChangeLog b/ChangeLog index 44c34a24e..68c4b23e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * use a python python dictionary for settings instead of session_settings object (in python bindings) * optimized metadata transfer (magnet link) startup time (shaved off about 1 second) * optimized swarm startup time (shaved off about 1 second) * support DHT name lookup diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index 0b9760a5d..2dc4809e4 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -4,6 +4,7 @@ #include #include +#include // for bencode_map_entry #include #include #include @@ -58,6 +59,75 @@ namespace s.add_extension(invoke_extension_factory(e)); } + void session_set_settings(session& ses, dict const& sett_dict) + { + bencode_map_entry* map; + int len; + boost::tie(map, len) = aux::settings_map(); + + session_settings sett; + for (int i = 0; i < len; ++i) + { + if (!sett_dict.has_key(map[i].name)) continue; + + void* dest = ((char*)&sett) + map[i].offset; + char const* name = map[i].name; + switch (map[i].type) + { + case std_string: + *((std::string*)dest) = extract(sett_dict[name]); + break; + case character: + *((char*)dest) = extract(sett_dict[name]); + break; + case boolean: + *((bool*)dest) = extract(sett_dict[name]); + break; + case integer: + *((int*)dest) = extract(sett_dict[name]); + break; + case floating_point: + *((float*)dest) = extract(sett_dict[name]); + break; + } + } + + ses.set_settings(sett); + } + + dict session_get_settings(session const& ses) + { + session_settings sett = ses.settings(); + dict sett_dict; + bencode_map_entry* map; + int len; + boost::tie(map, len) = aux::settings_map(); + for (int i = 0; i < len; ++i) + { + void const* dest = ((char const*)&sett) + map[i].offset; + char const* name = map[i].name; + switch (map[i].type) + { + case std_string: + sett_dict[name] = *((std::string const*)dest); + break; + case character: + sett_dict[name] = *((char const*)dest); + break; + case boolean: + sett_dict[name] = *((bool const*)dest); + break; + case integer: + sett_dict[name] = *((int const*)dest); + break; + case floating_point: + sett_dict[name] = *((float const*)dest); + break; + } + } + return sett_dict; + } + #ifndef BOOST_NO_EXCEPTIONS #ifndef TORRENT_NO_DEPRECATE torrent_handle add_torrent_depr(session& s, torrent_info const& ti @@ -342,8 +412,14 @@ void bind_session() .def("set_max_connections", allow_threads(&session::set_max_connections)) .def("set_max_half_open_connections", allow_threads(&session::set_max_half_open_connections)) .def("num_connections", allow_threads(&session::num_connections)) - .def("set_settings", allow_threads(&session::set_settings)) - .def("settings", allow_threads(&session::settings)) +#ifndef TORRENT_NO_DEPRECATE + .def("set_settings", &session::set_settings) + .def("settings", &session::settings) + .def("get_settings", &session_get_settings) +#else + .def("settings", &session_get_settings) +#endif + .def("set_settings", &session_set_settings) #ifndef TORRENT_DISABLE_ENCRYPTION .def("set_pe_settings", allow_threads(&session::set_pe_settings)) .def("get_pe_settings", allow_threads(&session::get_pe_settings)) diff --git a/docs/manual.rst b/docs/manual.rst index fb19b5143..fadeafc29 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -160,6 +160,7 @@ The ``session`` class has the following synopsis:: std::vector get_torrents() const; void set_settings(session_settings const& settings); + session_settings settings() const; void set_pe_settings(pe_settings const& settings); void set_upload_rate_limit(int bytes_per_second); diff --git a/docs/python_binding.rst b/docs/python_binding.rst index 1aa92803e..a29c5ba30 100644 --- a/docs/python_binding.rst +++ b/docs/python_binding.rst @@ -98,6 +98,14 @@ a list of entries. ``create_torrent::add_node()`` takes two arguments, one string and one integer, instead of a pair. The string is the address and the integer is the port. +``session::set_settings()`` not only accepts a ``session_settings`` object, but also +a dictionary with keys matching the names of the members of the ``session_settings`` struct. +When calling ``set_settings``, the dictionary does not need to have every settings set, +keys that are not present, are set to their default value. + +For backwards compatibility, ``session::settings()`` still returns a ``session_settings`` +struct. To get a python dictionary of the settings, call ``session::get_settings``. + .. _`main library reference`: manual.html For an example python program, see ``client.py`` in the ``bindings/python`` diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 9c55a960d..81b20ceef 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -112,6 +112,8 @@ namespace libtorrent struct dht_tracker; } + struct bencode_map_entry; + namespace aux { struct session_impl; @@ -127,6 +129,8 @@ namespace libtorrent initialize_timer(); }; + std::pair settings_map(); + // this is the link between the main thread and the // thread started to run the main downloader loop struct session_impl: boost::noncopyable, initialize_timer diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index ecb0ba913..9dbee25e6 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -328,7 +328,7 @@ namespace libtorrent void remove_torrent(const torrent_handle& h, int options = none); void set_settings(session_settings const& s); - session_settings settings(); + session_settings settings() const; void set_proxy(proxy_settings const& s); proxy_settings proxy() const; diff --git a/src/session.cpp b/src/session.cpp index 9c8ea3083..e5a30a851 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -691,7 +691,7 @@ namespace libtorrent TORRENT_ASYNC_CALL1(set_settings, s); } - session_settings session::settings() + session_settings session::settings() const { TORRENT_SYNC_CALL_RET(session_settings, settings); return r; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 41ec44ba9..fb79faaef 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -435,6 +435,10 @@ namespace aux { #endif }; + std::pair settings_map() + { + std::make_pair(session_settings_map, lenof(session_settings_map)); + } #undef lenof #ifdef TORRENT_STATS