1
0

Upload manager changes and events

This commit is contained in:
Scott E. Graves
2017-02-25 21:35:59 -06:00
parent ced91ecaa9
commit 0d16d224db
13 changed files with 138 additions and 56 deletions

View File

@@ -1,5 +1,6 @@
#include "stdafx.h" #include "stdafx.h"
#include "DebugConsumer.h" #include "DebugConsumer.h"
#include "EventSystem.h"
using namespace Sia::Api; using namespace Sia::Api;

View File

@@ -1,10 +1,11 @@
#pragma once #pragma once
#include <SiaCommon.h> #include <SiaCommon.h>
#include <EventSystem.h>
NS_BEGIN(Sia) NS_BEGIN(Sia)
NS_BEGIN(Api) NS_BEGIN(Api)
class CEvent;
class AFX_EXT_CLASS CDebugConsumer class AFX_EXT_CLASS CDebugConsumer
{ {
public: public:

View 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)
{
}

View 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)

View File

@@ -206,6 +206,7 @@
</PrecompiledHeader> </PrecompiledHeader>
</ClCompile> </ClCompile>
<ClCompile Include="EventSystem.cpp" /> <ClCompile Include="EventSystem.cpp" />
<ClCompile Include="LoggingConsumer.cpp" />
<ClCompile Include="SiaApi.cpp" /> <ClCompile Include="SiaApi.cpp" />
<ClCompile Include="SiaCommon.cpp" /> <ClCompile Include="SiaCommon.cpp" />
<ClCompile Include="SiaConsensus.cpp" /> <ClCompile Include="SiaConsensus.cpp" />
@@ -233,6 +234,7 @@
<ClInclude Include="DebugConsumer.h" /> <ClInclude Include="DebugConsumer.h" />
<ClInclude Include="EventSystem.h" /> <ClInclude Include="EventSystem.h" />
<ClInclude Include="json.hpp" /> <ClInclude Include="json.hpp" />
<ClInclude Include="LoggingConsumer.h" />
<ClInclude Include="Resource.h" /> <ClInclude Include="Resource.h" />
<ClInclude Include="SiaApi.h" /> <ClInclude Include="SiaApi.h" />
<ClInclude Include="SiaCommon.h" /> <ClInclude Include="SiaCommon.h" />

View File

@@ -66,6 +66,9 @@
<ClCompile Include="DebugConsumer.cpp"> <ClCompile Include="DebugConsumer.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="LoggingConsumer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="SiaDrive.Api.def"> <None Include="SiaDrive.Api.def">
@@ -112,6 +115,9 @@
<ClInclude Include="DebugConsumer.h"> <ClInclude Include="DebugConsumer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="LoggingConsumer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="SiaDrive.Api.rc"> <ResourceCompile Include="SiaDrive.Api.rc">

View File

@@ -336,62 +336,69 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath)
// start again later // start again later
std::lock_guard<std::mutex> l(_uploadMutex); std::lock_guard<std::mutex> l(_uploadMutex);
// TODO Handle SQLite::Exception try
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())
{ {
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(2))); SQLite::Statement query(_uploadDatabase, QUERY_UPLOADS_BY_SIA_PATH_AND_2_STATUS);
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(ExistingUploadFound(siaPath, filePath, uploadStatus))); query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
if (uploadStatus == UploadStatus::Uploading) 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) UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(2)));
} CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(ExistingUploadFound(siaPath, filePath, uploadStatus)));
} if (uploadStatus == UploadStatus::Uploading)
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))); 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; 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 else
{ {

View File

@@ -0,0 +1,9 @@
#pragma once
#include <SiaCommon.h>
NS_BEGIN(Sia)
NS_BEGIN(Api)
NS_END(2)

View File

@@ -193,6 +193,7 @@
<Text Include="ReadMe.txt" /> <Text Include="ReadMe.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="EventSystemEvents.h" />
<ClInclude Include="ntray.h" /> <ClInclude Include="ntray.h" />
<ClInclude Include="Resource.h" /> <ClInclude Include="Resource.h" />
<ClInclude Include="SiaDriveApp.h" /> <ClInclude Include="SiaDriveApp.h" />

View File

@@ -36,6 +36,9 @@
<ClInclude Include="ntray.h"> <ClInclude Include="ntray.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="EventSystemEvents.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="SiaDriveDlg.cpp"> <ClCompile Include="SiaDriveDlg.cpp">

View File

@@ -20,6 +20,11 @@ END_MESSAGE_MAP()
// CSiaDriveApp construction // CSiaDriveApp construction
#ifdef _DEBUG
const Sia::Api::CDebugConsumer CSiaDriveApp::_debugConsumer;
#endif
const Sia::Api::CLoggingConsumer CSiaDriveApp::_loggingConsumer;
CSiaDriveApp::CSiaDriveApp() CSiaDriveApp::CSiaDriveApp()
{ {
// support Restart Manager // support Restart Manager

View File

@@ -9,6 +9,8 @@
#endif #endif
#include "resource.h" // main symbols #include "resource.h" // main symbols
#include "DebugConsumer.h"
#include "LoggingConsumer.h"
// CSiaDriveApp: // CSiaDriveApp:
@@ -25,8 +27,13 @@ public:
virtual BOOL InitInstance(); virtual BOOL InitInstance();
// Implementation // Implementation
DECLARE_MESSAGE_MAP() DECLARE_MESSAGE_MAP()
private:
#ifdef _DEBUG
static const Sia::Api::CDebugConsumer _debugConsumer;
#endif
static const Sia::Api::CLoggingConsumer _loggingConsumer;
}; };
extern CSiaDriveApp theApp; extern CSiaDriveApp theApp;

View File

@@ -78,7 +78,7 @@ namespace UnitTests
} }
}; };
TEST_CLASS(UnitTests) TEST_CLASS(UploadManagerTest)
{ {
private: private:
const SiaHostConfig hostConfig = { L"127.0.0.1", 9988, TEST_SERVER_VERSION }; const SiaHostConfig hostConfig = { L"127.0.0.1", 9988, TEST_SERVER_VERSION };
@@ -108,7 +108,7 @@ namespace UnitTests
CEventSystem::EventSystem.Stop(); CEventSystem::EventSystem.Stop();
} }
TEST_METHOD(AddOrUpdateNoExisting) TEST_METHOD(AddNewFile)
{ {
siad->Start(SiadTestType::UploadFile); siad->Start(SiadTestType::UploadFile);
try try
@@ -154,7 +154,7 @@ namespace UnitTests
} }
}; };
std::unique_ptr<CMockSiad> UnitTests::siad; std::unique_ptr<CMockSiad> UploadManagerTest::siad;
CDebugConsumer UnitTests::_debugConsumer; CDebugConsumer UploadManagerTest::_debugConsumer;
CEventAccumulator UnitTests::_eventAccumulator; CEventAccumulator UploadManagerTest::_eventAccumulator;
} }