Refactoring
This commit is contained in:
43
SiaDrive.Api/AutoThread.cpp
Normal file
43
SiaDrive.Api/AutoThread.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "stdafx.h"
|
||||
#include "AutoThread.h"
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CAutoThread::CAutoThread(const CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl.GetHostConfig()),
|
||||
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr))
|
||||
{
|
||||
}
|
||||
|
||||
CAutoThread::~CAutoThread()
|
||||
{
|
||||
StopAutoThread();
|
||||
::CloseHandle(_stopEvent);
|
||||
}
|
||||
|
||||
void CAutoThread::StartAutoThread()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_startStopMutex);
|
||||
if (!_thread)
|
||||
{
|
||||
_thread.reset(new std::thread([this]() {
|
||||
do
|
||||
{
|
||||
AutoThreadCallback(_siaCurl);
|
||||
} while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void CAutoThread::StopAutoThread()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_startStopMutex);
|
||||
if (_thread)
|
||||
{
|
||||
::SetEvent(_stopEvent);
|
||||
_thread->join();
|
||||
_thread.reset(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
28
SiaDrive.Api/AutoThread.h
Normal file
28
SiaDrive.Api/AutoThread.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <SiaCurl.h>
|
||||
#include <mutex>
|
||||
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
class AFX_EXT_CLASS CAutoThread
|
||||
{
|
||||
public:
|
||||
CAutoThread(const CSiaCurl& siaCurl);
|
||||
|
||||
public:
|
||||
virtual ~CAutoThread();
|
||||
|
||||
private:
|
||||
CSiaCurl _siaCurl;
|
||||
HANDLE _stopEvent;
|
||||
std::unique_ptr<std::thread> _thread;
|
||||
std::mutex _startStopMutex;
|
||||
|
||||
protected:
|
||||
void StartAutoThread();
|
||||
void StopAutoThread();
|
||||
virtual void AutoThreadCallback(const CSiaCurl& siaCurl) = 0;
|
||||
};
|
||||
|
||||
NS_END(2)
|
@@ -1,10 +1,36 @@
|
||||
#pragma once
|
||||
#include "SiaCommon.h"
|
||||
#include "SiaCurl.h"
|
||||
#include "AutoThread.h"
|
||||
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
|
||||
class AFX_EXT_CLASS CSiaBase
|
||||
{
|
||||
public:
|
||||
explicit CSiaBase(const CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual ~CSiaBase() = 0
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
const CSiaCurl& _siaCurl;
|
||||
|
||||
protected:
|
||||
inline const CSiaCurl& GetSiaCurl() const
|
||||
{
|
||||
return _siaCurl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class AFX_EXT_CLASS CSiaApi
|
||||
{
|
||||
public:
|
||||
@@ -27,19 +53,17 @@ public:
|
||||
};
|
||||
|
||||
class _CSiaFileTree;
|
||||
class AFX_EXT_CLASS _CSiaFile
|
||||
class AFX_EXT_CLASS _CSiaFile :
|
||||
public virtual CSiaBase
|
||||
{
|
||||
friend CSiaApi;
|
||||
friend _CSiaFileTree;
|
||||
|
||||
private:
|
||||
_CSiaFile(CSiaCurl& siaCurl, const json& fileJson);
|
||||
explicit _CSiaFile(const CSiaCurl& siaCurl, const json& fileJson);
|
||||
|
||||
public:
|
||||
~_CSiaFile();
|
||||
|
||||
private:
|
||||
CSiaCurl& _siaCurl;
|
||||
virtual ~_CSiaFile();
|
||||
|
||||
// Properties
|
||||
Property(String, SiaPath, public, private)
|
||||
@@ -51,17 +75,15 @@ public:
|
||||
Property(std::uint32_t, Expiration, public, private)
|
||||
};
|
||||
|
||||
class AFX_EXT_CLASS _CSiaFileTree
|
||||
class AFX_EXT_CLASS _CSiaFileTree :
|
||||
public virtual CSiaBase
|
||||
{
|
||||
friend CSiaApi;
|
||||
public:
|
||||
_CSiaFileTree(CSiaCurl& siaCurl);
|
||||
explicit _CSiaFileTree(const CSiaCurl& siaCurl);
|
||||
|
||||
public:
|
||||
~_CSiaFileTree();
|
||||
|
||||
private:
|
||||
CSiaCurl& _siaCurl;
|
||||
virtual ~_CSiaFileTree();
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<_CSiaFile>> _fileList;
|
||||
@@ -78,17 +100,15 @@ public:
|
||||
bool FileExists(const String& siaPath) const;
|
||||
};
|
||||
|
||||
class AFX_EXT_CLASS _CSiaWallet
|
||||
class AFX_EXT_CLASS _CSiaWallet :
|
||||
public virtual CSiaBase
|
||||
{
|
||||
friend CSiaApi;
|
||||
private:
|
||||
_CSiaWallet(CSiaCurl& siaCurl);
|
||||
explicit _CSiaWallet(const CSiaCurl& siaCurl);
|
||||
|
||||
public:
|
||||
~_CSiaWallet();
|
||||
|
||||
private:
|
||||
CSiaCurl& _siaCurl;
|
||||
virtual ~_CSiaWallet();
|
||||
|
||||
// Properties
|
||||
Property(bool, Created, public, private)
|
||||
@@ -105,17 +125,20 @@ public:
|
||||
_SiaApiError GetAddress(String& address) const;
|
||||
};
|
||||
|
||||
class AFX_EXT_CLASS _CSiaRenter
|
||||
class AFX_EXT_CLASS _CSiaRenter :
|
||||
public virtual CSiaBase,
|
||||
public virtual CAutoThread
|
||||
{
|
||||
friend CSiaApi;
|
||||
|
||||
private:
|
||||
_CSiaRenter(CSiaCurl& siaCurl);
|
||||
explicit _CSiaRenter(const CSiaCurl& siaCurl);
|
||||
|
||||
public:
|
||||
~_CSiaRenter();
|
||||
virtual ~_CSiaRenter();
|
||||
|
||||
private:
|
||||
CSiaCurl& _siaCurl;
|
||||
protected:
|
||||
virtual void AutoThreadCallback(const CSiaCurl& siaCurl) override;
|
||||
|
||||
public:
|
||||
_SiaApiError FileExists(const String& siaPath, bool& exists) const;
|
||||
@@ -125,34 +148,29 @@ public:
|
||||
_SiaApiError GetFileTree(std::shared_ptr<_CSiaFileTree>& siaFileTree) const;
|
||||
};
|
||||
|
||||
class AFX_EXT_CLASS _CSiaConsensus
|
||||
class AFX_EXT_CLASS _CSiaConsensus :
|
||||
public virtual CSiaBase,
|
||||
public virtual CAutoThread
|
||||
{
|
||||
friend CSiaApi;
|
||||
|
||||
private:
|
||||
_CSiaConsensus(CSiaCurl& siaCurl);
|
||||
explicit _CSiaConsensus(const CSiaCurl& siaCurl);
|
||||
|
||||
public:
|
||||
~_CSiaConsensus();
|
||||
|
||||
private:
|
||||
CSiaCurl _siaCurl;
|
||||
|
||||
private:
|
||||
HANDLE _stopEvent;
|
||||
std::unique_ptr<std::thread> _refreshThread;
|
||||
virtual ~_CSiaConsensus();
|
||||
|
||||
// Properties
|
||||
Property(std::uint64_t, Height, public, private)
|
||||
Property(bool, Synced, public, private)
|
||||
Property(String, CurrentBlock, public, private)
|
||||
|
||||
private:
|
||||
void StartRefreshThread();
|
||||
void StopRefreshThread();
|
||||
protected:
|
||||
virtual void AutoThreadCallback(const CSiaCurl& siaCurl) override;
|
||||
};
|
||||
|
||||
public:
|
||||
CSiaApi(const SiaHostConfig& hostConfig);
|
||||
explicit CSiaApi(const SiaHostConfig& hostConfig);
|
||||
|
||||
public:
|
||||
~CSiaApi();
|
||||
|
@@ -112,4 +112,16 @@ inline Hastings CalculateAverageUploadPrice(const std::vector<IHost>& hosts)
|
||||
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetUploadPrice(); });
|
||||
}
|
||||
|
||||
static String& ReplaceStringInPlace(String& subject, const String& search, const String& replace)
|
||||
{
|
||||
size_t pos = 0;
|
||||
while ((pos = subject.find(search, pos)) != std::string::npos)
|
||||
{
|
||||
subject.replace(pos, search.length(), replace);
|
||||
pos += replace.length();
|
||||
}
|
||||
|
||||
return subject;
|
||||
}
|
||||
|
||||
NS_END(2)
|
@@ -3,28 +3,24 @@
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaApi::_CSiaConsensus::_CSiaConsensus(CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl.GetHostConfig()), // IMPORTANT: Need a copy of CSiaCurl for use in thread
|
||||
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr))
|
||||
CSiaApi::_CSiaConsensus::_CSiaConsensus(const CSiaCurl& siaCurl) :
|
||||
CSiaBase(siaCurl),
|
||||
CAutoThread(siaCurl),
|
||||
_Height(0),
|
||||
_Synced(false)
|
||||
{
|
||||
StartRefreshThread();
|
||||
StartAutoThread();
|
||||
}
|
||||
|
||||
CSiaApi::_CSiaConsensus::~_CSiaConsensus()
|
||||
{
|
||||
StopRefreshThread();
|
||||
::CloseHandle(_stopEvent);
|
||||
StopAutoThread();
|
||||
}
|
||||
|
||||
void CSiaApi::_CSiaConsensus::StartRefreshThread()
|
||||
{
|
||||
if (!_refreshThread)
|
||||
{
|
||||
_refreshThread.reset(new std::thread([this]() {
|
||||
do
|
||||
void CSiaApi::_CSiaConsensus::AutoThreadCallback(const CSiaCurl& siaCurl)
|
||||
{
|
||||
json result;
|
||||
if (ApiSuccess(_siaCurl.Get(L"/consensus", result)))
|
||||
if (ApiSuccess(siaCurl.Get(L"/consensus", result)))
|
||||
{
|
||||
SetHeight(result["height"].get<std::uint64_t>());
|
||||
SetSynced(result["synced"].get<bool>());
|
||||
@@ -36,17 +32,4 @@ void CSiaApi::_CSiaConsensus::StartRefreshThread()
|
||||
SetSynced(false);
|
||||
SetCurrentBlock(L"");
|
||||
}
|
||||
} while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void CSiaApi::_CSiaConsensus::StopRefreshThread()
|
||||
{
|
||||
if (_refreshThread)
|
||||
{
|
||||
SetEvent(_stopEvent);
|
||||
_refreshThread->join();
|
||||
_refreshThread.reset(nullptr);
|
||||
}
|
||||
}
|
@@ -189,6 +189,7 @@
|
||||
<Text Include="ReadMe.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="AutoThread.cpp" />
|
||||
<ClCompile Include="dllmain.cpp">
|
||||
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
@@ -225,6 +226,7 @@
|
||||
<None Include="SiaDrive.Api.def" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="AutoThread.h" />
|
||||
<ClInclude Include="json.hpp" />
|
||||
<ClInclude Include="Resource.h" />
|
||||
<ClInclude Include="SiaApi.h" />
|
||||
|
@@ -54,6 +54,9 @@
|
||||
<ClCompile Include="SiaConsensus.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="AutoThread.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="SiaDrive.Api.def">
|
||||
@@ -88,6 +91,9 @@
|
||||
<ClInclude Include="SiaDriveConfig.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="AutoThread.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="SiaDrive.Api.rc">
|
||||
|
@@ -3,8 +3,8 @@
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaApi::_CSiaFile::_CSiaFile(CSiaCurl& siaCurl, const json& fileJson) :
|
||||
_siaCurl(siaCurl),
|
||||
CSiaApi::_CSiaFile::_CSiaFile(const CSiaCurl& siaCurl, const json& fileJson) :
|
||||
CSiaBase(siaCurl),
|
||||
_SiaPath(CA2W(fileJson["siapath"].get<std::string>().c_str())),
|
||||
_FileSize(fileJson["filesize"].get<std::uint64_t>()),
|
||||
_Available(fileJson["available"].get<bool>()),
|
||||
|
@@ -4,20 +4,8 @@
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
static String& ReplaceStringInPlace(String& subject, const String& search, const String& replace)
|
||||
{
|
||||
size_t pos = 0;
|
||||
while ((pos = subject.find(search, pos)) != std::string::npos)
|
||||
{
|
||||
subject.replace(pos, search.length(), replace);
|
||||
pos += replace.length();
|
||||
}
|
||||
|
||||
return subject;
|
||||
}
|
||||
|
||||
CSiaApi::_CSiaFileTree::_CSiaFileTree(CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl)
|
||||
CSiaApi::_CSiaFileTree::_CSiaFileTree(const CSiaCurl& siaCurl) :
|
||||
CSiaBase(siaCurl)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -32,7 +20,7 @@ void CSiaApi::_CSiaFileTree::BuildTree(const json& result)
|
||||
_fileList.clear();
|
||||
for (const auto& file : result["files"])
|
||||
{
|
||||
_fileList.push_back(CSiaFilePtr(new CSiaFile(_siaCurl, file)));
|
||||
_fileList.push_back(CSiaFilePtr(new CSiaFile(GetSiaCurl(), file)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,14 +3,25 @@
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaApi::_CSiaRenter::_CSiaRenter(CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl)
|
||||
CSiaApi::_CSiaRenter::_CSiaRenter(const CSiaCurl& siaCurl) :
|
||||
CSiaBase(siaCurl),
|
||||
CAutoThread(siaCurl)
|
||||
{
|
||||
StartAutoThread();
|
||||
}
|
||||
|
||||
CSiaApi::_CSiaRenter::~_CSiaRenter()
|
||||
{
|
||||
StopAutoThread();
|
||||
}
|
||||
|
||||
void CSiaApi::_CSiaRenter::AutoThreadCallback(const CSiaCurl& siaCurl)
|
||||
{
|
||||
json result;
|
||||
if (ApiSuccess(siaCurl.Get(L"/renter", result)))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SiaApiError CSiaApi::_CSiaRenter::FileExists(const String& siaPath, bool& exists) const
|
||||
@@ -42,9 +53,9 @@ SiaApiError CSiaApi::_CSiaRenter::QueueUploadFile(const String& siaPath, const S
|
||||
SiaApiError CSiaApi::_CSiaRenter::GetFileTree(CSiaFileTreePtr& siaFileTree) const
|
||||
{
|
||||
SiaApiError ret = SiaApiError::RequestError;
|
||||
siaFileTree.reset(new CSiaFileTree(_siaCurl));
|
||||
siaFileTree.reset(new CSiaFileTree(GetSiaCurl()));
|
||||
json result;
|
||||
if (ApiSuccess(_siaCurl.Get(L"/renter/files", result)))
|
||||
if (ApiSuccess(GetSiaCurl().Get(L"/renter/files", result)))
|
||||
{
|
||||
siaFileTree->BuildTree(result);
|
||||
ret = SiaApiError::Success;
|
||||
|
@@ -21,8 +21,8 @@ static String SeedLangToString(const SiaSeedLanguage& lang)
|
||||
}
|
||||
}
|
||||
|
||||
CSiaApi::_CSiaWallet::_CSiaWallet(CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl),
|
||||
CSiaApi::_CSiaWallet::_CSiaWallet(const CSiaCurl& siaCurl) :
|
||||
CSiaBase(siaCurl),
|
||||
_Created(false),
|
||||
_Locked(false)
|
||||
{
|
||||
@@ -37,7 +37,7 @@ CSiaApi::_CSiaWallet::~_CSiaWallet()
|
||||
bool CSiaApi::_CSiaWallet::Refresh()
|
||||
{
|
||||
json result;
|
||||
SiaCurlError error = _siaCurl.Get(L"/wallet", result);
|
||||
SiaCurlError error = GetSiaCurl().Get(L"/wallet", result);
|
||||
if (ApiSuccess(error))
|
||||
{
|
||||
SetCreated(result["encrypted"].get<bool>());
|
||||
@@ -59,7 +59,7 @@ SiaApiError CSiaApi::_CSiaWallet::Create(const SiaSeedLanguage& seedLanguage, St
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/init", { {L"dictionary", SeedLangToString(seedLanguage)} }, result);
|
||||
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/init", { {L"dictionary", SeedLangToString(seedLanguage)} }, result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
error = SiaApiError::Success;
|
||||
@@ -87,7 +87,7 @@ SiaApiError CSiaApi::_CSiaWallet::Lock()
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/lock", {}, result);
|
||||
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/lock", {}, result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
Refresh();
|
||||
@@ -106,7 +106,7 @@ SiaApiError CSiaApi::_CSiaWallet::Unlock(const String& password)
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/unlock", { {L"encryptionpassword", password} }, result);
|
||||
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/unlock", { {L"encryptionpassword", password} }, result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
Refresh();
|
||||
@@ -134,7 +134,7 @@ SiaApiError CSiaApi::_CSiaWallet::GetConfirmedBalance(SiaCurrency& balance) cons
|
||||
balance = 0;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Get(L"/wallet", result);
|
||||
SiaCurlError cerror = GetSiaCurl().Get(L"/wallet", result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
balance = HastingsStringToSiaCurrency(String(CA2W(result["confirmedsiacoinbalance"].get<std::string>().c_str())));
|
||||
@@ -150,7 +150,7 @@ SiaApiError CSiaApi::_CSiaWallet::GetUnonfirmedBalance(SiaCurrency& balance) con
|
||||
balance = 0;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Get(L"/wallet", result);
|
||||
SiaCurlError cerror = GetSiaCurl().Get(L"/wallet", result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
balance = HastingsStringToSiaCurrency(String(CA2W(result["unconfirmedincomingsiacoins"].get<std::string>().c_str())));
|
||||
@@ -166,7 +166,7 @@ SiaApiError CSiaApi::_CSiaWallet::GetAddress(String& address) const
|
||||
address = L"";
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Get(L"/wallet/address", result);
|
||||
SiaCurlError cerror = GetSiaCurl().Get(L"/wallet/address", result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
address = CA2W(result["address"].get<std::string>().c_str());
|
||||
|
Reference in New Issue
Block a user