Upload manager changes and events
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "stdafx.h"
|
||||
#include "DebugConsumer.h"
|
||||
#include "EventSystem.h"
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
|
@@ -1,10 +1,11 @@
|
||||
#pragma once
|
||||
#include <SiaCommon.h>
|
||||
#include <EventSystem.h>
|
||||
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
class CEvent;
|
||||
|
||||
class AFX_EXT_CLASS CDebugConsumer
|
||||
{
|
||||
public:
|
||||
|
20
SiaDrive.Api/LoggingConsumer.cpp
Normal file
20
SiaDrive.Api/LoggingConsumer.cpp
Normal file
@@ -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)
|
||||
{
|
||||
|
||||
}
|
20
SiaDrive.Api/LoggingConsumer.h
Normal file
20
SiaDrive.Api/LoggingConsumer.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <SiaCommon.h>
|
||||
|
||||
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)
|
@@ -206,6 +206,7 @@
|
||||
</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="EventSystem.cpp" />
|
||||
<ClCompile Include="LoggingConsumer.cpp" />
|
||||
<ClCompile Include="SiaApi.cpp" />
|
||||
<ClCompile Include="SiaCommon.cpp" />
|
||||
<ClCompile Include="SiaConsensus.cpp" />
|
||||
@@ -233,6 +234,7 @@
|
||||
<ClInclude Include="DebugConsumer.h" />
|
||||
<ClInclude Include="EventSystem.h" />
|
||||
<ClInclude Include="json.hpp" />
|
||||
<ClInclude Include="LoggingConsumer.h" />
|
||||
<ClInclude Include="Resource.h" />
|
||||
<ClInclude Include="SiaApi.h" />
|
||||
<ClInclude Include="SiaCommon.h" />
|
||||
|
@@ -66,6 +66,9 @@
|
||||
<ClCompile Include="DebugConsumer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="LoggingConsumer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="SiaDrive.Api.def">
|
||||
@@ -112,6 +115,9 @@
|
||||
<ClInclude Include="DebugConsumer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="LoggingConsumer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="SiaDrive.Api.rc">
|
||||
|
@@ -336,62 +336,69 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath)
|
||||
// start again later
|
||||
std::lock_guard<std::mutex> 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<unsigned>(UploadStatus::Uploading));
|
||||
query.bind("@status2", static_cast<unsigned>(UploadStatus::Modified));
|
||||
// Check copying
|
||||
if (query.executeStep())
|
||||
try
|
||||
{
|
||||
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(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<unsigned>(UploadStatus::Uploading));
|
||||
query.bind("@status2", static_cast<unsigned>(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<unsigned>(UploadStatus::Copying));
|
||||
if (insert.exec() != 1)
|
||||
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(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<unsigned>(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<std::mutex> 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<std::mutex> 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
|
||||
{
|
||||
|
9
SiaDrive/EventSystemEvents.h
Normal file
9
SiaDrive/EventSystemEvents.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <SiaCommon.h>
|
||||
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
|
||||
|
||||
NS_END(2)
|
@@ -193,6 +193,7 @@
|
||||
<Text Include="ReadMe.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="EventSystemEvents.h" />
|
||||
<ClInclude Include="ntray.h" />
|
||||
<ClInclude Include="Resource.h" />
|
||||
<ClInclude Include="SiaDriveApp.h" />
|
||||
|
@@ -36,6 +36,9 @@
|
||||
<ClInclude Include="ntray.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="EventSystemEvents.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SiaDriveDlg.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
|
||||
|
@@ -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;
|
@@ -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<CMockSiad> UnitTests::siad;
|
||||
CDebugConsumer UnitTests::_debugConsumer;
|
||||
CEventAccumulator UnitTests::_eventAccumulator;
|
||||
std::unique_ptr<CMockSiad> UploadManagerTest::siad;
|
||||
CDebugConsumer UploadManagerTest::_debugConsumer;
|
||||
CEventAccumulator UploadManagerTest::_eventAccumulator;
|
||||
}
|
Reference in New Issue
Block a user