From 2dd5de4373b5303ad29ec7bfc2bddbfe28fd7c80 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 9 Aug 2016 10:17:40 -0400 Subject: [PATCH] handle default subscription in separate thread --- AddressBook.cpp | 13 ++++++------- AddressBook.h | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index e996b5f0..b3f0f1ce 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -260,8 +260,6 @@ namespace client m_Storage = nullptr; } m_DefaultSubscription = nullptr; - for (auto& it: m_Subscriptions) - delete it; m_Subscriptions.clear (); } @@ -403,7 +401,7 @@ namespace client { getline(f, s); if (!s.length()) continue; // skip empty line - m_Subscriptions.push_back (new AddressBookSubscription (*this, s)); + m_Subscriptions.push_back (std::make_shared (*this, s)); } LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded"); } @@ -462,7 +460,7 @@ namespace client int nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT; if (success) { - if (m_DefaultSubscription) m_DefaultSubscription.reset (nullptr); + if (m_DefaultSubscription) m_DefaultSubscription = nullptr; if (m_IsLoaded) nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT; else @@ -516,16 +514,17 @@ namespace client // download it from http://i2p-projekt.i2p/hosts.txt LogPrint (eLogInfo, "Addressbook: trying to download it from default subscription."); if (!m_DefaultSubscription) - m_DefaultSubscription.reset (new AddressBookSubscription (*this, DEFAULT_SUBSCRIPTION_ADDRESS)); + m_DefaultSubscription = std::make_shared(*this, DEFAULT_SUBSCRIPTION_ADDRESS); m_IsDownloading = true; - m_DefaultSubscription->CheckUpdates (); + std::thread load_hosts(std::bind (&AddressBookSubscription::CheckUpdates, m_DefaultSubscription)); + load_hosts.detach(); // TODO: use join } else if (!m_Subscriptions.empty ()) { // pick random subscription auto ind = rand () % m_Subscriptions.size(); m_IsDownloading = true; - std::thread load_hosts(&AddressBookSubscription::CheckUpdates, m_Subscriptions[ind]); + std::thread load_hosts(std::bind (&AddressBookSubscription::CheckUpdates, m_Subscriptions[ind])); load_hosts.detach(); // TODO: use join } } diff --git a/AddressBook.h b/AddressBook.h index fd852907..7f559cd0 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -102,8 +102,8 @@ namespace client std::map m_Lookups; // nonce -> address AddressBookStorage * m_Storage; volatile bool m_IsLoaded, m_IsDownloading; - std::vector m_Subscriptions; - std::unique_ptr m_DefaultSubscription; // in case if we don't know any addresses yet + std::vector > m_Subscriptions; + std::shared_ptr m_DefaultSubscription; // in case if we don't know any addresses yet boost::asio::deadline_timer * m_SubscriptionsUpdateTimer; };