From 013cef68e8e05166eaadc9b9cff3394e3b4b8ef5 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 6 Mar 2010 07:49:40 +0000 Subject: [PATCH] add flags to session::save_state to filter what is saved --- docs/manual.rst | 36 ++++++++++- include/libtorrent/aux_/session_impl.hpp | 2 +- include/libtorrent/session.hpp | 15 ++++- src/session.cpp | 6 +- src/session_impl.cpp | 77 ++++++++++++++++-------- 5 files changed, 104 insertions(+), 32 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 83169158a..c055c9a94 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -117,8 +117,22 @@ The ``session`` class has the following synopsis:: | add_default_plugins , int alert_mask = alert::error_notification); + enum save_state_flags_t + { + save_settings = 0x001, + save_dht_settings = 0x002, + save_dht_proxy = 0x004, + save_dht_state = 0x008, + save_i2p_proxy = 0x010, + save_encryption_settings = 0x020, + save_peer_proxy = 0x040, + save_web_proxy = 0x080, + save_tracker_proxy = 0x100, + save_as_map = 0x200, + }; + void load_state(lazy_entry const& e); - void save_state(entry& e) const; + void save_state(entry& e, boost::uint32_t flags) const; torrent_handle add_torrent( add_torrent_params const& params); @@ -278,7 +292,7 @@ load_state() save_state() :: void load_state(lazy_entry const& e); - void save_state(entry& e) const; + void save_state(entry& e, boost::uint32_t flags) const; loads and saves all session settings, including dht_settings, encryption settings and proxy settings. ``save_state`` writes all keys to the ``entry`` that's passed in, which needs to @@ -287,6 +301,24 @@ either not be initialized, or initialized as a dictionary. ``load_state`` expects a ``lazy_entry`` which can be built from a bencoded buffer with ``lazy_bdecode``. +The ``flags`` arguments passed in to ``save_state`` can be used to filter which parts +of the session state to save. By default, all state is saved (except for the individual +torrents). These are the possible flags. A flag that's set, means those settings are saved:: + + enum save_state_flags_t + { + save_settings = 0x001, + save_dht_settings = 0x002, + save_dht_proxy = 0x004, + save_dht_state = 0x008, + save_i2p_proxy = 0x010, + save_encryption_settings = 0x020, + save_peer_proxy = 0x040, + save_web_proxy = 0x080, + save_tracker_proxy = 0x100, + save_as_map = 0x200, + }; + pause() resume() is_paused() ---------------------------- diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index bc39c8b6f..8e6d74fd5 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -291,7 +291,7 @@ namespace libtorrent void announce_lsd(sha1_hash const& ih); - void save_state(entry& e, mutex::scoped_lock& l) const; + void save_state(entry& e, boost::uint32_t flags, mutex::scoped_lock& l) const; void load_state(lazy_entry const& e); void set_peer_proxy(proxy_settings const& s) diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 1cf9b696a..19e1b2dca 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -176,7 +176,20 @@ namespace libtorrent ~session(); - void save_state(entry& e) const; + enum save_state_flags_t + { + save_settings = 0x001, + save_dht_settings = 0x002, + save_dht_proxy = 0x004, + save_dht_state = 0x008, + save_i2p_proxy = 0x010, + save_encryption_settings = 0x020, + save_peer_proxy = 0x040, + save_web_proxy = 0x080, + save_tracker_proxy = 0x100, + save_as_map = 0x200, + }; + void save_state(entry& e, boost::uint32_t flags = 0xffffffff) const; void load_state(lazy_entry const& e); // returns a list of all torrents in this session diff --git a/src/session.cpp b/src/session.cpp index 75a8f26b3..299e5aa6e 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -313,10 +313,10 @@ namespace libtorrent m_impl->abort(); } - void session::save_state(entry& e) const + void session::save_state(entry& e, boost::uint32_t flags) const { mutex::scoped_lock l(m_impl->m_mutex); - m_impl->save_state(e, l); + m_impl->save_state(e, flags, l); } void session::load_state(lazy_entry const& e) @@ -382,7 +382,7 @@ namespace libtorrent { entry ret; mutex::scoped_lock l(m_impl->m_mutex); - m_impl->save_state(ret, l); + m_impl->save_state(ret, 0xffffffff, l); return ret; } #endif diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 5b53651b6..38b59bd1d 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -647,17 +647,26 @@ namespace aux { m_thread.reset(new thread(boost::bind(&session_impl::main_thread, this))); } - void session_impl::save_state(entry& e, mutex::scoped_lock& l) const + void session_impl::save_state(entry& e, boost::uint32_t flags, mutex::scoped_lock& l) const { - save_struct(e["settings"], &m_settings, session_settings_map - , sizeof(session_settings_map)/sizeof(session_settings_map[0])); + if (flags & session::save_settings) + { + save_struct(e["settings"], &m_settings, session_settings_map + , sizeof(session_settings_map)/sizeof(session_settings_map[0])); + } #ifndef TORRENT_DISABLE_DHT - save_struct(e["dht"], &m_dht_settings, dht_settings_map - , sizeof(dht_settings_map)/sizeof(dht_settings_map[0])); - save_struct(e["dht proxy"], &m_dht_proxy, proxy_settings_map - , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + if (flags & session::save_dht_settings) + { + save_struct(e["dht"], &m_dht_settings, dht_settings_map + , sizeof(dht_settings_map)/sizeof(dht_settings_map[0])); + } + if (flags & session::save_dht_proxy) + { + save_struct(e["dht proxy"], &m_dht_proxy, proxy_settings_map + , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + } - if (m_dht) + if (m_dht && (flags & session::save_dht_state)) { condition cond; entry& state = e["dht state"]; @@ -669,30 +678,48 @@ namespace aux { #endif #if TORRENT_USE_I2P - save_struct(e["i2p"], &i2p_proxy(), proxy_settings_map - , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + if (flags & session::save_i2p_proxy) + { + save_struct(e["i2p"], &i2p_proxy(), proxy_settings_map + , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + } #endif #ifndef TORRENT_DISABLE_ENCRYPTION - save_struct(e["encryption"], &m_pe_settings, pe_settings_map - , sizeof(pe_settings_map)/sizeof(pe_settings_map[0])); + if (flags & session::save_encryption_settings) + { + save_struct(e["encryption"], &m_pe_settings, pe_settings_map + , sizeof(pe_settings_map)/sizeof(pe_settings_map[0])); + } #endif - save_struct(e["peer proxy"], &m_peer_proxy, proxy_settings_map - , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); - save_struct(e["web proxy"], &m_web_seed_proxy, proxy_settings_map - , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); - save_struct(e["tracker proxy"], &m_tracker_proxy, proxy_settings_map - , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + if (flags & session::save_peer_proxy) + { + save_struct(e["peer proxy"], &m_peer_proxy, proxy_settings_map + , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + } + if (flags & session::save_web_proxy) + { + save_struct(e["web proxy"], &m_web_seed_proxy, proxy_settings_map + , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + } + if (flags & session::save_tracker_proxy) + { + save_struct(e["tracker proxy"], &m_tracker_proxy, proxy_settings_map + , sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); + } #ifndef TORRENT_DISABLE_GEO_IP - entry::dictionary_type& as_map = e["AS map"].dict(); - char buf[10]; - for (std::map::const_iterator i = m_as_peak.begin() - , end(m_as_peak.end()); i != end; ++i) + if (flags & session::save_as_map) { - if (i->second == 0) continue; - sprintf(buf, "%05d", i->first); - as_map[buf] = i->second; + entry::dictionary_type& as_map = e["AS map"].dict(); + char buf[10]; + for (std::map::const_iterator i = m_as_peak.begin() + , end(m_as_peak.end()); i != end; ++i) + { + if (i->second == 0) continue; + sprintf(buf, "%05d", i->first); + as_map[buf] = i->second; + } } #endif