From 169143dd745487945423ce351f8359762d46c42b Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 25 Feb 2017 16:45:27 -0600 Subject: [PATCH] Upload manager changes --- SiaDrive.Api/UploadManager.cpp | 40 ++++++++++++++++------------ SiaDrive.Api/UploadManager.h | 46 ++++++++++++++++++++++++++++----- UnitTests/MockSiad.cpp | 4 +-- UnitTests/UploadManagerTest.cpp | 2 +- 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/SiaDrive.Api/UploadManager.cpp b/SiaDrive.Api/UploadManager.cpp index fa1740d..1b1cb58 100644 --- a/SiaDrive.Api/UploadManager.cpp +++ b/SiaDrive.Api/UploadManager.cpp @@ -120,7 +120,7 @@ void CUploadManager::FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig } } -void CUploadManager::FileUploadAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath) +void CUploadManager::NewFileAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath) { CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(CreatingTemporarySiaDriveFile(siaPath, filePath, tempSourcePath))); if (RetryableAction(::CopyFile(filePath.c_str(), tempSourcePath.c_str(), FALSE), DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS)) @@ -138,7 +138,7 @@ void CUploadManager::FileUploadAction(const String& siaPath, const String& fileP { std::lock_guard l(_uploadMutex); SET_STATUS(UploadStatus::Queued, UploadAddedToQueue, ModifyUploadStatusFailed) - if (statusUpdated && !RetryDeleteFileIfExists(siaDriveFilePath)) + if (statusUpdated && !RetryDeleteFileIfExists(tempSourcePath)) { CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); } @@ -156,7 +156,7 @@ void CUploadManager::FileUploadAction(const String& siaPath, const String& fileP CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath))); } - // Requeue + // Requeued } } else @@ -170,7 +170,7 @@ void CUploadManager::FileUploadAction(const String& siaPath, const String& fileP CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); } - // Requeue + // Requeued } } @@ -194,11 +194,9 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig fileTree->BuildTree(result); if (query.executeStep()) { - std::string tempSiaPath = query.getColumn(1); - String siaPath = CA2W(tempSiaPath.c_str()).m_psz; - std::string tempFilePath = query.getColumn(2); - String filePath = CA2W(tempSiaPath.c_str()).m_psz; - UploadStatus uploadStatus = static_cast(query.getColumn(2).getUInt()); + String siaPath = CA2W(query.getColumn(1)).m_psz; + String filePath = CA2W(query.getColumn(2)).m_psz; + UploadStatus uploadStatus = static_cast(query.getColumn(3).getUInt()); auto fileList = fileTree->GetFileList(); auto it = std::find_if(fileList.begin(), fileList.end(), [&](const CSiaFilePtr& ptr) @@ -212,8 +210,17 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig } else if (uploadStatus == UploadStatus::Modified) { - // TODO Delete from Sia first - SET_STATUS(UploadStatus::Queued, ModifiedUploadQueued, ModifyUploadStatusFailed) + json response; + SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete/") + siaPath, {}, response); + if (ApiSuccess(cerror)) + { + // TODO validate response + SET_STATUS(UploadStatus::Queued, ModifiedUploadQueued, ModifyUploadStatusFailed) + } + else + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FailedToDeleteFromSia(siaPath, filePath, cerror))); + } } else if ((*it)->GetUploadProgress() >= 100) { @@ -243,6 +250,7 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig { try { + std::lock_guard l(_uploadMutex); SQLite::Statement query(_uploadDatabase, QUERY_UPLOADS_BY_STATUS); query.bind("@status", static_cast(UploadStatus::Queued)); @@ -251,13 +259,13 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig if (query.executeStep()) { String siaPath = CA2W(query.getColumn(1)).m_psz; - String tempFilePath; + String filePath = CA2W(query.getColumn(2)).m_psz; json response; - SiaCurlError cerror = siaCurl.Post(String(L"/renter/upload/") + siaPath, { {L"source", tempFilePath} }, response); + SiaCurlError cerror = siaCurl.Post(String(L"/renter/upload/") + siaPath, { {L"source", filePath} }, response); if (ApiSuccess(cerror)) { - // TODO Update status in DB + SET_STATUS(UploadStatus::Uploading, UploadComplete, ModifyUploadStatusFailed) } } } @@ -368,8 +376,8 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath) { // Queue file upload operation std::lock_guard l2(_fileQueueMutex); - _fileQueue.push_back([=]() { this->FileUploadAction(siaPath, filePath, tempSourcePath, siaDriveFilePath); }); - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(NewUploadAdded(siaPath, filePath, siaDriveFilePath))); + _fileQueue.push_back([=]() { this->NewFileAction(siaPath, filePath, tempSourcePath, siaDriveFilePath); }); + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(NewFileAdded(siaPath, filePath, siaDriveFilePath))); } } catch (SQLite::Exception e) diff --git a/SiaDrive.Api/UploadManager.h b/SiaDrive.Api/UploadManager.h index bf7f814..578065d 100644 --- a/SiaDrive.Api/UploadManager.h +++ b/SiaDrive.Api/UploadManager.h @@ -54,7 +54,7 @@ private: std::deque> _fileQueue; private: - void FileUploadAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath); + void NewFileAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath); protected: virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) override; @@ -237,6 +237,40 @@ public: } }; +class FailedToDeleteFromSia : + public CEvent +{ +public: + FailedToDeleteFromSia(const String& siaPath, const String& filePath, const SiaCurlError& curlError) : + _siaPath(siaPath), + _filePath(filePath), + _curlError(curlError) + { + + } + +public: + virtual ~FailedToDeleteFromSia() + { + } + +private: + const String _siaPath; + const String _filePath; + const SiaCurlError _curlError = SiaCurlError::Success; + +public: + virtual String GetSingleLineMessage() const override + { + return L"FailedToDeleteFromSia|SP|" + _siaPath + L"|FP|" + _filePath; + } + + virtual std::shared_ptr Clone() const override + { + return std::shared_ptr(new FailedToDeleteFromSia(_siaPath, _filePath, _curlError)); + } +}; + class ModifyUploadStatusFailed : public CEvent { @@ -515,11 +549,11 @@ public: } }; -class NewUploadAdded : +class NewFileAdded : public CEvent { public: - NewUploadAdded(const String& siaPath, const String& filePath, const String& siaDriveFilePath) : + NewFileAdded(const String& siaPath, const String& filePath, const String& siaDriveFilePath) : _siaPath(siaPath), _filePath(filePath), _siaDriveFilePath(siaDriveFilePath) @@ -528,7 +562,7 @@ public: } public: - virtual ~NewUploadAdded() + virtual ~NewFileAdded() { } @@ -540,12 +574,12 @@ private: public: virtual String GetSingleLineMessage() const override { - return L"NewUploadAdded|SP|" + _siaPath + L"|FP|" + _filePath + L"|SDP|" + _siaDriveFilePath; + return L"NewFileAdded|SP|" + _siaPath + L"|FP|" + _filePath + L"|SDP|" + _siaDriveFilePath; } virtual std::shared_ptr Clone() const override { - return std::shared_ptr(new NewUploadAdded(_siaPath, _filePath, _siaDriveFilePath)); + return std::shared_ptr(new NewFileAdded(_siaPath, _filePath, _siaDriveFilePath)); } }; diff --git a/UnitTests/MockSiad.cpp b/UnitTests/MockSiad.cpp index 22f4c65..365388a 100644 --- a/UnitTests/MockSiad.cpp +++ b/UnitTests/MockSiad.cpp @@ -12,7 +12,7 @@ private: const std::string SERVER_VERSION_SUCCESS_CONTENT = "{\"version\": \"1.1.0\"}"; public: - CMockClient(SOCKET socket, std::function removedCallback) : + CMockClient(SOCKET socket, std::function removedCallback) : _socket(socket), _stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr)) { @@ -29,7 +29,7 @@ public: private: SOCKET _socket; HANDLE _stopEvent; - std::function NotifyClientRemoved; + std::function NotifyClientRemoved; std::unique_ptr _clientThread; private: diff --git a/UnitTests/UploadManagerTest.cpp b/UnitTests/UploadManagerTest.cpp index 919324b..df9c747 100644 --- a/UnitTests/UploadManagerTest.cpp +++ b/UnitTests/UploadManagerTest.cpp @@ -124,7 +124,7 @@ namespace UnitTests CUploadManager uploadManager(siaCurl, &driveConfig); uploadManager.AddOrUpdate(L"/test1/test.rtf", L"./TestCacheFolder/test1/test.rtf"); - Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); + Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); Assert::IsTrue(_eventAccumulator.WaitForEvent(5000));