add flags to session::save_state to filter what is saved

This commit is contained in:
Arvid Norberg
2010-03-06 07:49:40 +00:00
parent e3a9578e2d
commit 013cef68e8
5 changed files with 104 additions and 32 deletions

View File

@@ -117,8 +117,22 @@ The ``session`` class has the following synopsis::
| add_default_plugins | add_default_plugins
, int alert_mask = alert::error_notification); , 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 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( torrent_handle add_torrent(
add_torrent_params const& params); add_torrent_params const& params);
@@ -278,7 +292,7 @@ load_state() save_state()
:: ::
void load_state(lazy_entry const& e); 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 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 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 ``load_state`` expects a ``lazy_entry`` which can be built from a bencoded buffer with
``lazy_bdecode``. ``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() pause() resume() is_paused()
---------------------------- ----------------------------

View File

@@ -291,7 +291,7 @@ namespace libtorrent
void announce_lsd(sha1_hash const& ih); 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 load_state(lazy_entry const& e);
void set_peer_proxy(proxy_settings const& s) void set_peer_proxy(proxy_settings const& s)

View File

@@ -176,7 +176,20 @@ namespace libtorrent
~session(); ~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); void load_state(lazy_entry const& e);
// returns a list of all torrents in this session // returns a list of all torrents in this session

View File

@@ -313,10 +313,10 @@ namespace libtorrent
m_impl->abort(); 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); 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) void session::load_state(lazy_entry const& e)
@@ -382,7 +382,7 @@ namespace libtorrent
{ {
entry ret; entry ret;
mutex::scoped_lock l(m_impl->m_mutex); mutex::scoped_lock l(m_impl->m_mutex);
m_impl->save_state(ret, l); m_impl->save_state(ret, 0xffffffff, l);
return ret; return ret;
} }
#endif #endif

View File

@@ -647,17 +647,26 @@ namespace aux {
m_thread.reset(new thread(boost::bind(&session_impl::main_thread, this))); 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 if (flags & session::save_settings)
, sizeof(session_settings_map)/sizeof(session_settings_map[0])); {
save_struct(e["settings"], &m_settings, session_settings_map
, sizeof(session_settings_map)/sizeof(session_settings_map[0]));
}
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
save_struct(e["dht"], &m_dht_settings, dht_settings_map if (flags & session::save_dht_settings)
, sizeof(dht_settings_map)/sizeof(dht_settings_map[0])); {
save_struct(e["dht proxy"], &m_dht_proxy, proxy_settings_map save_struct(e["dht"], &m_dht_settings, dht_settings_map
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); , 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; condition cond;
entry& state = e["dht state"]; entry& state = e["dht state"];
@@ -669,30 +678,48 @@ namespace aux {
#endif #endif
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
save_struct(e["i2p"], &i2p_proxy(), proxy_settings_map if (flags & session::save_i2p_proxy)
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); {
save_struct(e["i2p"], &i2p_proxy(), proxy_settings_map
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0]));
}
#endif #endif
#ifndef TORRENT_DISABLE_ENCRYPTION #ifndef TORRENT_DISABLE_ENCRYPTION
save_struct(e["encryption"], &m_pe_settings, pe_settings_map if (flags & session::save_encryption_settings)
, sizeof(pe_settings_map)/sizeof(pe_settings_map[0])); {
save_struct(e["encryption"], &m_pe_settings, pe_settings_map
, sizeof(pe_settings_map)/sizeof(pe_settings_map[0]));
}
#endif #endif
save_struct(e["peer proxy"], &m_peer_proxy, proxy_settings_map if (flags & session::save_peer_proxy)
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); {
save_struct(e["web proxy"], &m_web_seed_proxy, proxy_settings_map save_struct(e["peer proxy"], &m_peer_proxy, proxy_settings_map
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])); , 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_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 #ifndef TORRENT_DISABLE_GEO_IP
entry::dictionary_type& as_map = e["AS map"].dict(); if (flags & session::save_as_map)
char buf[10];
for (std::map<int, int>::const_iterator i = m_as_peak.begin()
, end(m_as_peak.end()); i != end; ++i)
{ {
if (i->second == 0) continue; entry::dictionary_type& as_map = e["AS map"].dict();
sprintf(buf, "%05d", i->first); char buf[10];
as_map[buf] = i->second; for (std::map<int, int>::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 #endif