aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2017-02-26 21:00:38 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2017-03-05 16:59:25 +0000
commitf6211322e57a70f381856f8604b8fb9b19ec1f69 (patch)
tree6d8c661474b08161b262ec154b20826d6bf91233
parentdownload: async API (diff)
downloadmonero-f6211322e57a70f381856f8604b8fb9b19ec1f69.tar.xz
core: make update download cancellable
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp67
-rw-r--r--src/cryptonote_core/cryptonote_core.h5
2 files changed, 55 insertions, 17 deletions
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index cfe3b5441..e0484d063 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -75,7 +75,8 @@ namespace cryptonote
m_target_blockchain_height(0),
m_checkpoints_path(""),
m_last_dns_checkpoints_update(0),
- m_last_json_checkpoints_update(0)
+ m_last_json_checkpoints_update(0),
+ m_update_download(0)
{
set_cryptonote_protocol(pprotocol);
}
@@ -134,6 +135,15 @@ namespace cryptonote
void core::stop()
{
m_blockchain_storage.cancel();
+
+ tools::download_async_handle handle;
+ {
+ boost::lock_guard<boost::mutex> lock(m_update_mutex);
+ handle = m_update_download;
+ m_update_download = 0;
+ }
+ if (handle)
+ tools::download_cancel(handle);
}
//-----------------------------------------------------------------------------------
void core::init_options(boost::program_options::options_description& desc)
@@ -1133,32 +1143,55 @@ namespace cryptonote
boost::filesystem::path path(epee::string_tools::get_current_module_folder());
path /= filename;
+ boost::unique_lock<boost::mutex> lock(m_update_mutex);
+
+ if (m_update_download != 0)
+ {
+ MCDEBUG("updates", "Already downloading update");
+ return true;
+ }
+
crypto::hash file_hash;
if (!tools::sha256sum(path.string(), file_hash) || (hash != epee::string_tools::pod_to_hex(file_hash)))
{
MCDEBUG("updates", "We don't have that file already, downloading");
- if (!tools::download(path.string(), url))
- {
- MCERROR("updates", "Failed to download " << url);
- return false;
- }
- if (!tools::sha256sum(path.string(), file_hash))
- {
- MCERROR("updates", "Failed to hash " << path);
- return false;
- }
- if (hash != epee::string_tools::pod_to_hex(file_hash))
- {
- MCERROR("updates", "Download from " << url << " does not match the expected hash");
- return false;
- }
- MGINFO("New version downloaded to " << path);
+ m_last_update_length = 0;
+ m_update_download = tools::download_async(path.string(), url, [this, hash](const std::string &path, const std::string &uri, bool success) {
+ if (success)
+ {
+ crypto::hash file_hash;
+ if (!tools::sha256sum(path, file_hash))
+ {
+ MCERROR("updates", "Failed to hash " << path);
+ }
+ if (hash != epee::string_tools::pod_to_hex(file_hash))
+ {
+ MCERROR("updates", "Download from " << uri << " does not match the expected hash");
+ }
+ MGINFO("New version downloaded to " << path);
+ }
+ else
+ {
+ MCERROR("updates", "Failed to download " << uri);
+ }
+ boost::unique_lock<boost::mutex> lock(m_update_mutex);
+ m_update_download = 0;
+ }, [this](const std::string &path, const std::string &uri, size_t length, ssize_t content_length) {
+ if (length >= m_last_update_length + 1024 * 1024 * 10)
+ {
+ m_last_update_length = length;
+ MCDEBUG("updates", "Downloaded " << length << "/" << (content_length ? std::to_string(content_length) : "unknown"));
+ }
+ return true;
+ });
}
else
{
MCDEBUG("updates", "We already have " << path << " with expected hash");
}
+ lock.unlock();
+
if (check_updates_level == UPDATES_DOWNLOAD)
return true;
diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h
index 7323bef29..02d58691d 100644
--- a/src/cryptonote_core/cryptonote_core.h
+++ b/src/cryptonote_core/cryptonote_core.h
@@ -39,6 +39,7 @@
#include "p2p/net_node_common.h"
#include "cryptonote_protocol/cryptonote_protocol_handler_common.h"
#include "storages/portable_storage_template_helper.h"
+#include "common/download.h"
#include "tx_pool.h"
#include "blockchain.h"
#include "cryptonote_basic/miner.h"
@@ -844,6 +845,10 @@ namespace cryptonote
UPDATES_DOWNLOAD,
UPDATES_UPDATE,
} check_updates_level;
+
+ tools::download_async_handle m_update_download;
+ size_t m_last_update_length;
+ boost::mutex m_update_mutex;
};
}