diff --git a/SiaDrive.Api/DebugConsumer.cpp b/SiaDrive.Api/DebugConsumer.cpp index 4b75fc1..2d728df 100644 --- a/SiaDrive.Api/DebugConsumer.cpp +++ b/SiaDrive.Api/DebugConsumer.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" #include "DebugConsumer.h" +#include "EventSystem.h" using namespace Sia::Api; diff --git a/SiaDrive.Api/DebugConsumer.h b/SiaDrive.Api/DebugConsumer.h index 553fa0a..584e18f 100644 --- a/SiaDrive.Api/DebugConsumer.h +++ b/SiaDrive.Api/DebugConsumer.h @@ -1,10 +1,11 @@ #pragma once #include -#include NS_BEGIN(Sia) NS_BEGIN(Api) +class CEvent; + class AFX_EXT_CLASS CDebugConsumer { public: diff --git a/SiaDrive.Api/LoggingConsumer.cpp b/SiaDrive.Api/LoggingConsumer.cpp new file mode 100644 index 0000000..884b8b7 --- /dev/null +++ b/SiaDrive.Api/LoggingConsumer.cpp @@ -0,0 +1,20 @@ +#include "stdafx.h" +#include "LoggingConsumer.h" +#include "EventSystem.h" + +using namespace Sia::Api; + +CLoggingConsumer::CLoggingConsumer() +{ + CEventSystem::EventSystem.AddEventConsumer([=](const CEvent& event) {this->ProcessEvent(event); }); +} + + +CLoggingConsumer::~CLoggingConsumer() +{ +} + +void CLoggingConsumer::ProcessEvent(const CEvent& eventData) +{ + +} \ No newline at end of file diff --git a/SiaDrive.Api/LoggingConsumer.h b/SiaDrive.Api/LoggingConsumer.h new file mode 100644 index 0000000..dc7a92e --- /dev/null +++ b/SiaDrive.Api/LoggingConsumer.h @@ -0,0 +1,20 @@ +#pragma once +#include + +NS_BEGIN(Sia) +NS_BEGIN(Api) + +class CEvent; +class AFX_EXT_CLASS CLoggingConsumer +{ +public: + CLoggingConsumer(); + +public: + ~CLoggingConsumer(); + +private: + void ProcessEvent(const CEvent& eventData); +}; + +NS_END(2) \ No newline at end of file diff --git a/SiaDrive.Api/SiaDrive.Api.vcxproj b/SiaDrive.Api/SiaDrive.Api.vcxproj index 06e20e9..de4488e 100644 --- a/SiaDrive.Api/SiaDrive.Api.vcxproj +++ b/SiaDrive.Api/SiaDrive.Api.vcxproj @@ -206,6 +206,7 @@ + @@ -233,6 +234,7 @@ + diff --git a/SiaDrive.Api/SiaDrive.Api.vcxproj.filters b/SiaDrive.Api/SiaDrive.Api.vcxproj.filters index c5b1042..fed9c65 100644 --- a/SiaDrive.Api/SiaDrive.Api.vcxproj.filters +++ b/SiaDrive.Api/SiaDrive.Api.vcxproj.filters @@ -66,6 +66,9 @@ Source Files + + Source Files + @@ -112,6 +115,9 @@ Header Files + + Header Files + diff --git a/SiaDrive.Api/UploadManager.cpp b/SiaDrive.Api/UploadManager.cpp index 74470ee..13d5c35 100644 --- a/SiaDrive.Api/UploadManager.cpp +++ b/SiaDrive.Api/UploadManager.cpp @@ -336,62 +336,69 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath) // start again later std::lock_guard l(_uploadMutex); - // TODO Handle SQLite::Exception - SQLite::Statement query(_uploadDatabase, QUERY_UPLOADS_BY_SIA_PATH_AND_2_STATUS); - query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); - query.bind("@status1", static_cast(UploadStatus::Uploading)); - query.bind("@status2", static_cast(UploadStatus::Modified)); - // Check copying - if (query.executeStep()) + try { - UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(2))); - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(ExistingUploadFound(siaPath, filePath, uploadStatus))); - if (uploadStatus == UploadStatus::Uploading) + SQLite::Statement query(_uploadDatabase, QUERY_UPLOADS_BY_SIA_PATH_AND_2_STATUS); + query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); + query.bind("@status1", static_cast(UploadStatus::Uploading)); + query.bind("@status2", static_cast(UploadStatus::Modified)); + // Check copying + if (query.executeStep()) { - SET_STATUS(UploadStatus::Modified, UploadStatusModified, ModifyUploadStatusFailed) - } - } - else - { - // Strip drive specification (i.e. C:\) - // TODO If mount to folder is ever enabled, this will need to change - String siaDriveFileName = GenerateSha256(&filePath[3]) + L".siadrive"; - - String siaDriveFilePath; - siaDriveFilePath.resize(MAX_PATH + 1); - PathCombine(&siaDriveFilePath[0], rootPath.c_str(), siaDriveFileName.c_str()); - - String tempSourcePath; - tempSourcePath.resize(MAX_PATH + 1); - PathCombine(&tempSourcePath[0], rootPath.c_str(), (siaDriveFileName + L".temp").c_str()); - - // Add to db - try - { - SQLite::Statement insert(_uploadDatabase, INSERT_UPLOAD); - insert.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); - insert.bind("@file_path", CW2A(filePath.c_str()).m_psz); - insert.bind("@sd_file_path", CW2A(siaDriveFileName.c_str()).m_psz); - insert.bind("@status", static_cast(UploadStatus::Copying)); - if (insert.exec() != 1) + UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(2))); + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(ExistingUploadFound(siaPath, filePath, uploadStatus))); + if (uploadStatus == UploadStatus::Uploading) { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseInsertFailed(siaPath, filePath, CA2W(insert.getErrorMsg()).m_psz))); + SET_STATUS(UploadStatus::Modified, UploadStatusModified, ModifyUploadStatusFailed) + } + } + else + { + // Strip drive specification (i.e. C:\) + // TODO If mount to folder is ever enabled, this will need to change + String siaDriveFileName = GenerateSha256(&filePath[3]) + L".siadrive"; + + String siaDriveFilePath; + siaDriveFilePath.resize(MAX_PATH + 1); + PathCombine(&siaDriveFilePath[0], rootPath.c_str(), siaDriveFileName.c_str()); + + String tempSourcePath; + tempSourcePath.resize(MAX_PATH + 1); + PathCombine(&tempSourcePath[0], rootPath.c_str(), (siaDriveFileName + L".temp").c_str()); + + // Add to db + try + { + SQLite::Statement insert(_uploadDatabase, INSERT_UPLOAD); + insert.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); + insert.bind("@file_path", CW2A(filePath.c_str()).m_psz); + insert.bind("@sd_file_path", CW2A(siaDriveFileName.c_str()).m_psz); + insert.bind("@status", static_cast(UploadStatus::Copying)); + if (insert.exec() != 1) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseInsertFailed(siaPath, filePath, CA2W(insert.getErrorMsg()).m_psz))); + ret = UploadError::DatabaseError; + } + else + { + // Queue file upload operation + std::lock_guard l2(_fileQueueMutex); + _fileQueue.push_back([=]() { this->NewFileAction(siaPath, filePath, tempSourcePath, siaDriveFilePath); }); + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(NewFileAdded(siaPath, filePath, siaDriveFilePath))); + } + } + catch (SQLite::Exception e) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); ret = UploadError::DatabaseError; } - else - { - // Queue file upload operation - std::lock_guard l2(_fileQueueMutex); - _fileQueue.push_back([=]() { this->NewFileAction(siaPath, filePath, tempSourcePath, siaDriveFilePath); }); - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(NewFileAdded(siaPath, filePath, siaDriveFilePath))); - } - } - catch (SQLite::Exception e) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); - ret = UploadError::DatabaseError; } } + catch (SQLite::Exception e) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); + ret = UploadError::DatabaseError; + } } else { diff --git a/SiaDrive/EventSystemEvents.h b/SiaDrive/EventSystemEvents.h new file mode 100644 index 0000000..2304c21 --- /dev/null +++ b/SiaDrive/EventSystemEvents.h @@ -0,0 +1,9 @@ +#pragma once +#include + +NS_BEGIN(Sia) +NS_BEGIN(Api) + + + +NS_END(2) \ No newline at end of file diff --git a/SiaDrive/SiaDrive.vcxproj b/SiaDrive/SiaDrive.vcxproj index 8fbed51..7961662 100644 --- a/SiaDrive/SiaDrive.vcxproj +++ b/SiaDrive/SiaDrive.vcxproj @@ -193,6 +193,7 @@ + diff --git a/SiaDrive/SiaDrive.vcxproj.filters b/SiaDrive/SiaDrive.vcxproj.filters index 7c77a03..8d2daa7 100644 --- a/SiaDrive/SiaDrive.vcxproj.filters +++ b/SiaDrive/SiaDrive.vcxproj.filters @@ -36,6 +36,9 @@ Header Files + + Header Files + diff --git a/SiaDrive/SiaDriveApp.cpp b/SiaDrive/SiaDriveApp.cpp index f66fb5f..e681d97 100644 --- a/SiaDrive/SiaDriveApp.cpp +++ b/SiaDrive/SiaDriveApp.cpp @@ -20,6 +20,11 @@ END_MESSAGE_MAP() // CSiaDriveApp construction +#ifdef _DEBUG +const Sia::Api::CDebugConsumer CSiaDriveApp::_debugConsumer; +#endif +const Sia::Api::CLoggingConsumer CSiaDriveApp::_loggingConsumer; + CSiaDriveApp::CSiaDriveApp() { // support Restart Manager diff --git a/SiaDrive/SiaDriveApp.h b/SiaDrive/SiaDriveApp.h index 9f4a297..eb3d4e2 100644 --- a/SiaDrive/SiaDriveApp.h +++ b/SiaDrive/SiaDriveApp.h @@ -9,6 +9,8 @@ #endif #include "resource.h" // main symbols +#include "DebugConsumer.h" +#include "LoggingConsumer.h" // CSiaDriveApp: @@ -25,8 +27,13 @@ public: virtual BOOL InitInstance(); // Implementation - DECLARE_MESSAGE_MAP() + +private: +#ifdef _DEBUG + static const Sia::Api::CDebugConsumer _debugConsumer; +#endif + static const Sia::Api::CLoggingConsumer _loggingConsumer; }; extern CSiaDriveApp theApp; \ No newline at end of file diff --git a/UnitTests/UploadManagerTest.cpp b/UnitTests/UploadManagerTest.cpp index bee3cf0..854d42c 100644 --- a/UnitTests/UploadManagerTest.cpp +++ b/UnitTests/UploadManagerTest.cpp @@ -78,7 +78,7 @@ namespace UnitTests } }; - TEST_CLASS(UnitTests) + TEST_CLASS(UploadManagerTest) { private: const SiaHostConfig hostConfig = { L"127.0.0.1", 9988, TEST_SERVER_VERSION }; @@ -108,7 +108,7 @@ namespace UnitTests CEventSystem::EventSystem.Stop(); } - TEST_METHOD(AddOrUpdateNoExisting) + TEST_METHOD(AddNewFile) { siad->Start(SiadTestType::UploadFile); try @@ -154,7 +154,7 @@ namespace UnitTests } }; - std::unique_ptr UnitTests::siad; - CDebugConsumer UnitTests::_debugConsumer; - CEventAccumulator UnitTests::_eventAccumulator; + std::unique_ptr UploadManagerTest::siad; + CDebugConsumer UploadManagerTest::_debugConsumer; + CEventAccumulator UploadManagerTest::_eventAccumulator; } \ No newline at end of file