diff --git a/ChangeLog b/ChangeLog index af89b4035..ee687039f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * add function to get default settings * updating super seeding would include the torrent in state_update_alert * fix issue where num_seeds could be greater than num_peers in torrent_status * finished non-seed torrents can also be in super-seeding mode diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index 4e4aff1df..40c374c28 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -214,6 +214,11 @@ namespace return make_dict(ret); } + dict default_settings_wrapper() + { + return make_dict(default_settings()); + } + dict high_performance_seed_wrapper() { settings_pack ret; @@ -983,6 +988,7 @@ void bind_session() def("high_performance_seed", high_performance_seed_wrapper); def("min_memory_usage", min_memory_usage_wrapper); + def("default_settings", default_settings_wrapper); class_("stats_metric") .def_readonly("name", &stats_metric::name) diff --git a/bindings/python/test.py b/bindings/python/test.py index ab76e3c9a..51fa5260e 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -267,6 +267,11 @@ class test_session(unittest.TestCase): self.assertTrue('connection_speed' in seed_mode) self.assertTrue('file_pool_size' in seed_mode) + def test_default_settings(self): + + default = lt.default_settings() + print(default) + if __name__ == '__main__': print(lt.__version__) shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'url_seed_multi.torrent'), '.') diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index 93a835bda..47aeaa4e7 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -70,6 +70,9 @@ namespace libtorrent TORRENT_EXPORT int setting_by_name(std::string const& name); TORRENT_EXPORT char const* name_for_setting(int s); + // returns a settings_pack with every setting set to its default value + TORRENT_EXPORT settings_pack default_settings(); + #ifndef TORRENT_NO_DEPRECATE struct session_settings; settings_pack load_pack_from_struct(aux::session_settings const& current, session_settings const& s); diff --git a/src/session.cpp b/src/session.cpp index 125f4f310..b99e0c505 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -296,7 +296,6 @@ namespace libtorrent TORRENT_EXPORT session_settings min_memory_usage() { aux::session_settings def; - initialize_default_settings(def); settings_pack pack; min_memory_usage(pack); apply_pack(&pack, def, 0); @@ -308,7 +307,6 @@ namespace libtorrent TORRENT_EXPORT session_settings high_performance_seed() { aux::session_settings def; - initialize_default_settings(def); settings_pack pack; high_performance_seed(pack); apply_pack(&pack, def, 0); @@ -421,7 +419,6 @@ namespace libtorrent session_settings::session_settings(std::string const& user_agent_) { aux::session_settings def; - initialize_default_settings(def); def.set_str(settings_pack::user_agent, user_agent_); load_struct_from_settings(def, *this); } diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index fc5b5a3b4..75551e05e 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -56,6 +56,11 @@ namespace { if (i != c.end() && i->first == v.first) i->second = v.second; else c.insert(i, v); } + + // return the string, unless it's null, in which case the empty string is + // returned + char const* ensure_string(char const* str) + { return str == NULL ? "" : str; } } namespace libtorrent @@ -441,8 +446,7 @@ namespace libtorrent // loop over all settings that differ from default for (int i = 0; i < settings_pack::num_string_settings; ++i) { - char const* cmp = str_settings[i].default_value == 0 ? "" : str_settings[i].default_value; - if (cmp == s.m_strings[i]) continue; + if (ensure_string(str_settings[i].default_value) == s.m_strings[i]) continue; sett[str_settings[i].name] = s.m_strings[i]; } @@ -553,7 +557,7 @@ namespace libtorrent { for (int i = 0; i < settings_pack::num_string_settings; ++i) { - if (str_settings[i].default_value == 0) continue; + if (str_settings[i].default_value == NULL) continue; s.set_str(settings_pack::string_type_base + i, str_settings[i].default_value); TORRENT_ASSERT(s.get_str(settings_pack::string_type_base + i) == str_settings[i].default_value); } @@ -569,16 +573,28 @@ namespace libtorrent s.set_bool(settings_pack::bool_type_base + i, bool_settings[i].default_value); TORRENT_ASSERT(s.get_bool(settings_pack::bool_type_base + i) == bool_settings[i].default_value); } + } - // this seems questionable... -/* - // Some settings have dynamic defaults depending on the machine - // for instance, the disk cache size + settings_pack default_settings() + { + settings_pack ret; + // TODO: it would be nice to reserve() these vectors up front + for (int i = 0; i < settings_pack::num_string_settings; ++i) + { + if (str_settings[i].default_value == NULL) continue; + ret.set_str(settings_pack::string_type_base + i, str_settings[i].default_value); + } - // by default, set the cahe size to an 8:th of the total amount of physical RAM - boost::uint64_t phys_ram = total_physical_ram(); - if (phys_ram > 0) s.set_int(settings_pack::cache_size, phys_ram / 16 / 1024 / 8); -*/ + for (int i = 0; i < settings_pack::num_int_settings; ++i) + { + ret.set_int(settings_pack::int_type_base + i, int_settings[i].default_value); + } + + for (int i = 0; i < settings_pack::num_bool_settings; ++i) + { + ret.set_bool(settings_pack::bool_type_base + i, bool_settings[i].default_value); + } + return ret; } void apply_pack(settings_pack const* pack, aux::session_settings& sett diff --git a/test/test_bandwidth_limiter.cpp b/test/test_bandwidth_limiter.cpp index 097b16a7b..4d9763163 100644 --- a/test/test_bandwidth_limiter.cpp +++ b/test/test_bandwidth_limiter.cpp @@ -158,7 +158,6 @@ void run_test(connections_t& v , boost::bind(&peer_connection::start, _1)); libtorrent::aux::session_settings s; - initialize_default_settings(s); int tick_interval = s.get_int(settings_pack::tick_interval); for (int i = 0; i < int(sample_time * 1000 / tick_interval); ++i) diff --git a/test/test_settings_pack.cpp b/test/test_settings_pack.cpp index 5e465c6e9..79faf9c49 100644 --- a/test/test_settings_pack.cpp +++ b/test/test_settings_pack.cpp @@ -43,7 +43,6 @@ using namespace libtorrent::aux; TORRENT_TEST(default_settings) { aux::session_settings sett; - initialize_default_settings(sett); entry e; save_settings_to_dict(sett, e.dict()); @@ -57,10 +56,34 @@ TORRENT_TEST(default_settings) #endif } +TORRENT_TEST(default_settings2) +{ + aux::session_settings sett; + + settings_pack def = default_settings(); + + for (int i = 0; i < settings_pack::num_string_settings; ++i) + { + TEST_EQUAL(sett.get_str(settings_pack::string_type_base + i) + , def.get_str(settings_pack::string_type_base + i)); + } + + for (int i = 0; i < settings_pack::num_int_settings; ++i) + { + TEST_EQUAL(sett.get_int(settings_pack::int_type_base + i) + , def.get_int(settings_pack::int_type_base + i)); + } + + for (int i = 0; i < settings_pack::num_bool_settings; ++i) + { + TEST_EQUAL(sett.get_bool(settings_pack::bool_type_base + i) + , def.get_bool(settings_pack::bool_type_base + i)); + } +} + TORRENT_TEST(apply_pack) { aux::session_settings sett; - initialize_default_settings(sett); settings_pack sp; sp.set_int(settings_pack::max_out_request_queue, 1337);