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