1
0

More background work/status

This commit is contained in:
Scott E. Graves
2017-02-17 14:56:22 -06:00
parent 91dd816c8c
commit 22207bfe17
11 changed files with 112 additions and 6 deletions

View File

@@ -7,7 +7,8 @@ using namespace Sia::Api;
CSiaApi::CSiaApi(const SiaHostConfig& hostConfig) :
_siaCurl(hostConfig),
_wallet(new CSiaWallet(_siaCurl)),
_renter(new CSiaRenter(_siaCurl))
_renter(new CSiaRenter(_siaCurl)),
_consensus(new CSiaConsensus(_siaCurl))
{
}
@@ -48,4 +49,9 @@ CSiaWalletPtr CSiaApi::GetWallet() const
CSiaRenterPtr CSiaApi::GetRenter() const
{
return _renter;
}
CSiaConsensusPtr CSiaApi::GetConsensus() const
{
return _consensus;
}

View File

@@ -125,6 +125,32 @@ public:
_SiaApiError GetFileTree(std::shared_ptr<_CSiaFileTree>& siaFileTree) const;
};
class AFX_EXT_CLASS _CSiaConsensus
{
friend CSiaApi;
private:
_CSiaConsensus(CSiaCurl& siaCurl);
public:
~_CSiaConsensus();
private:
CSiaCurl _siaCurl;
private:
HANDLE _stopEvent;
std::unique_ptr<std::thread> _refreshThread;
// Properties
Property(std::uint64_t, Height, public, private)
Property(bool, Synced, public, private)
Property(String, CurrentBlock, public, private)
private:
void StartRefreshThread();
void StopRefreshThread();
};
public:
CSiaApi(const SiaHostConfig& hostConfig);
@@ -135,6 +161,7 @@ private:
CSiaCurl _siaCurl;
std::shared_ptr<_CSiaWallet> _wallet;
std::shared_ptr<_CSiaRenter> _renter;
std::shared_ptr<_CSiaConsensus> _consensus;
public:
static String FormatToSiaPath(String path);
@@ -142,6 +169,7 @@ public:
public:
std::shared_ptr<_CSiaWallet> GetWallet() const;
std::shared_ptr<_CSiaRenter> GetRenter() const;
std::shared_ptr<_CSiaConsensus> GetConsensus() const;
String GetServerVersion() const;
};
@@ -149,8 +177,10 @@ typedef CSiaApi::_SiaApiError SiaApiError;
typedef CSiaApi::_SiaSeedLanguage SiaSeedLanguage;
typedef CSiaApi::_CSiaWallet CSiaWallet;
typedef CSiaApi::_CSiaRenter CSiaRenter;
typedef CSiaApi::_CSiaConsensus CSiaConsensus;
typedef std::shared_ptr<CSiaWallet> CSiaWalletPtr;
typedef std::shared_ptr<CSiaRenter> CSiaRenterPtr;
typedef std::shared_ptr<CSiaConsensus> CSiaConsensusPtr;
typedef CSiaApi::_CSiaFile CSiaFile;
typedef std::shared_ptr<CSiaFile> CSiaFilePtr;
typedef std::vector<CSiaFilePtr> CSiaFileCollection;

View File

@@ -0,0 +1,52 @@
#include "stdafx.h"
#include "SiaApi.h"
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))
{
StartRefreshThread();
}
CSiaApi::_CSiaConsensus::~_CSiaConsensus()
{
StopRefreshThread();
::CloseHandle(_stopEvent);
}
void CSiaApi::_CSiaConsensus::StartRefreshThread()
{
if (!_refreshThread)
{
_refreshThread.reset(new std::thread([this]() {
do
{
json result;
if (API_SUCCESS(SiaCurlError, _siaCurl.Get(L"/consensus", result)))
{
SetHeight(result["height"].get<std::uint64_t>());
SetSynced(result["synced"].get<bool>());
SetCurrentBlock(CA2W(result["currentblock"].get<std::string>().c_str()).m_psz);
}
else
{
SetHeight(0);
SetSynced(false);
SetCurrentBlock(L"");
}
} while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT);
}));
}
}
void CSiaApi::_CSiaConsensus::StopRefreshThread()
{
if (_refreshThread)
{
SetEvent(_stopEvent);
_refreshThread->join();
_refreshThread.reset(nullptr);
}
}

View File

@@ -205,6 +205,7 @@
</ClCompile>
<ClCompile Include="SiaApi.cpp" />
<ClCompile Include="SiaCommon.cpp" />
<ClCompile Include="SiaConsensus.cpp" />
<ClCompile Include="SiaCurl.cpp" />
<ClCompile Include="SiaDrive.Api.cpp" />
<ClCompile Include="SiaDriveConfig.cpp" />

View File

@@ -51,6 +51,9 @@
<ClCompile Include="SiaWallet.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SiaConsensus.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="SiaDrive.Api.def">

View File

@@ -38,3 +38,4 @@
#include <afxsock.h> // MFC socket extensions
#include <thread>