1
0

Refactoring

This commit is contained in:
Scott E. Graves
2017-02-18 13:53:21 -06:00
parent 531472fdb8
commit e17e3d4934
5 changed files with 75 additions and 64 deletions

View File

@@ -7,7 +7,8 @@ CSiaApi::_CSiaConsensus::_CSiaConsensus(const CSiaCurl& siaCurl) :
CSiaBase(siaCurl),
CAutoThread(siaCurl),
_Height(0),
_Synced(false)
_Synced(false),
_CurrentBlock(L"")
{
StartAutoThread();
}

View File

@@ -47,7 +47,11 @@ using namespace Sia::Api;
}*/
CSiaApi::_CSiaRenter::_CSiaRenter(const CSiaCurl& siaCurl) :
CSiaBase(siaCurl),
CAutoThread(siaCurl)
CAutoThread(siaCurl),
_Funds(0),
_Hosts(0),
_Unspent(0),
_TotalUsedBytes(0)
{
StartAutoThread();
}

View File

@@ -198,9 +198,7 @@
<h3>Receive Address</h3>
<label id="ID_WalletReceiveAddress"></label>
<div id="ID_ActiveTab"></div>
</div>
<div class="fill">
<div class="fill" style="padding: 0; margin: 0;">
<h3>Mounting</h3>
<p>Choose an available drive letter and click 'Mount'</p>
<table>
@@ -212,6 +210,8 @@
</tr>
</table>
</div>
</div>
<div class="footer">
<table class="fill">

View File

@@ -90,8 +90,8 @@ END_DHTML_EVENT_MAP()
CSiaDriveDlg::CSiaDriveDlg(CWnd* pParent /*=NULL*/)
: CDHtmlDialog(IDD_SIADRIVE_DIALOG, IDR_HTML_SIADRIVE_DIALOG, pParent),
_uiThreadId(GetCurrentThreadId()),
_siaApi({L"localhost", 9980, L"1.1.0"})
_uiThreadId(GetCurrentThreadId())
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
@@ -308,8 +308,8 @@ HRESULT CSiaDriveDlg::OnButtonConfirmSeed(IHTMLElement* /*pElement*/)
{
_seedCreation = false;
_walletCreatedSeed = L"";
UpdateData(FALSE);
this->Navigate(this->m_strCurrentUrl);
ReloadDisplay();
return S_OK;
}
@@ -319,12 +319,12 @@ HRESULT CSiaDriveDlg::OnButtonUnlockWallet(IHTMLElement* /*pElement*/)
SetBodyEnabled(false);
String pwd = GetWalletUnlockPassword();
std::thread th([this, pwd]() {
if (ApiSuccess(_siaApi.GetWallet()->Unlock(pwd)))
if (ApiSuccess(_siaApi->GetWallet()->Unlock(pwd)))
{
QueueUiAction([this]() {
SetWalletUnlockPassword(L"");
SetBodyEnabled(true);
this->Navigate(this->m_strCurrentUrl);
ReloadDisplay();
});
}
else
@@ -403,6 +403,7 @@ void CSiaDriveDlg::SetWalletTotalBalance(const String& balance)
void CSiaDriveDlg::SetWalletReceiveAddress(const String& address)
{
_receiveAddress = address;
CallClientScript(L"setWalletReceiveAddress", address, nullptr);
}
@@ -458,17 +459,14 @@ void CSiaDriveDlg::SetRenterTotalUsed(const std::uint64_t& bytes)
CallClientScript(L"setRenterTotalUsedGb", std::to_wstring(total), nullptr);
}
void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR)
void CSiaDriveDlg::ClearDisplay()
{
KillTimer(IDT_UPDATE);
KillTimer(IDT_UI_ACTION_QUEUE);
SetServerVersion(L"x.x.x.");
SetServerVersion(L"...");
SetClientVersion(L"1.0.0");
SetWalletConfirmedBalance(L"");
SetWalletUnconfirmedBalance(L"");
SetWalletTotalBalance(L"");
SetWalletConfirmedBalance(L"...");
SetWalletUnconfirmedBalance(L"...");
SetWalletTotalBalance(L"...");
SetWalletReceiveAddress(L"");
SetRenterAllocatedFunds(0);
@@ -478,6 +476,19 @@ void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR)
SetRenterTotalUsed(0);
SetConsensusHeight(0);
}
void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR)
{
KillTimer(IDT_UPDATE);
KillTimer(IDT_UI_ACTION_QUEUE);
// Create new API to clear all cached data used by threaded implementations
_siaApi.reset(new CSiaApi({ L"localhost", 9980, L"1.1.0" }));
ClearDisplay();
CallClientScript(L"setAvailableDrives", json(GetAvailableDrives()), nullptr);
SetTimer(IDT_UPDATE, 2000, nullptr);
SetTimer(IDT_UI_ACTION_QUEUE, 100, nullptr);
@@ -540,16 +551,16 @@ void CSiaDriveDlg::ProcessUiActionQueue()
bool CSiaDriveDlg::UpdateSiaInfo()
{
const String serverVersion = _siaApi.GetServerVersion();
const String serverVersion = _siaApi->GetServerVersion();
if (serverVersion.length())
{
if (_siaApi.GetWallet()->Refresh())
if (_siaApi->GetWallet()->Refresh())
{
SiaCurrency confirmed;
if (ApiSuccess(_siaApi.GetWallet()->GetConfirmedBalance(confirmed)))
if (ApiSuccess(_siaApi->GetWallet()->GetConfirmedBalance(confirmed)))
{
SiaCurrency unconfirmed;
if (ApiSuccess(_siaApi.GetWallet()->GetUnonfirmedBalance(unconfirmed)))
if (ApiSuccess(_siaApi->GetWallet()->GetUnonfirmedBalance(unconfirmed)))
{
SiaCurrency total = confirmed + unconfirmed;
@@ -557,44 +568,39 @@ bool CSiaDriveDlg::UpdateSiaInfo()
SetWalletConfirmedBalance(SiaCurrencyToString(confirmed));
SetWalletUnconfirmedBalance(SiaCurrencyToString(unconfirmed));
SetWalletTotalBalance(SiaCurrencyToString(total));
if (GetWalletReceiveAddress().empty())
if (_receiveAddress.empty())
{
String address;
_siaApi.GetWallet()->GetAddress(address);
SetWalletReceiveAddress(address);
String receiveAddress;
_siaApi->GetWallet()->GetAddress(receiveAddress);
SetWalletReceiveAddress(receiveAddress);
}
SiaCurrency allocatedFunds = _siaApi.GetRenter()->GetFunds();
SiaCurrency unspentFunds = _siaApi.GetRenter()->GetUnspent();
SiaCurrency allocatedFunds = _siaApi->GetRenter()->GetFunds();
SiaCurrency unspentFunds = _siaApi->GetRenter()->GetUnspent();
SetRenterAllocatedFunds(allocatedFunds);
SetRenterAvailableFunds(unspentFunds);
SetRenterUsedFunds(allocatedFunds - unspentFunds);
SetRenterHosts(_siaApi.GetRenter()->GetHosts());
SetRenterTotalUsed(_siaApi.GetRenter()->GetTotalUsedBytes());
SetRenterHosts(_siaApi->GetRenter()->GetHosts());
SetRenterTotalUsed(_siaApi->GetRenter()->GetTotalUsedBytes());
SetConsensusHeight(_siaApi.GetConsensus()->GetHeight());
SetConsensusHeight(_siaApi->GetConsensus()->GetHeight());
return true;
}
}
}
}
SetServerVersion(L"x.x.x");
SetWalletConfirmedBalance(L"");
SetWalletUnconfirmedBalance(L"");
SetWalletTotalBalance(L"");
SetWalletReceiveAddress(L"");
SetRenterAllocatedFunds(0);
SetRenterAvailableFunds(0);
SetRenterUsedFunds(0);
SetRenterHosts(0);
SetRenterTotalUsed(0);
SetConsensusHeight(0);
ClearDisplay();
return false;
}
void CSiaDriveDlg::ReloadDisplay()
{
KillTimer(IDT_UPDATE);
ClearDisplay();
this->Navigate(this->m_strCurrentUrl);
}
bool CSiaDriveDlg::UpdateUi(const bool& refresh)
{
bool ret = UpdateSiaInfo();
@@ -602,15 +608,13 @@ bool CSiaDriveDlg::UpdateUi(const bool& refresh)
{
if (!_connected && !_seedCreation)
{
KillTimer(IDT_UPDATE);
this->Navigate(this->m_strCurrentUrl);
ReloadDisplay();
}
}
else if (refresh && !_seedCreation)
{
_connected = false;
KillTimer(IDT_UPDATE);
this->Navigate(this->m_strCurrentUrl);
ReloadDisplay();
}
return ret;
@@ -665,16 +669,15 @@ void CSiaDriveDlg::DisplayRenterTab()
void CSiaDriveDlg::ConfigureWallet()
{
if (_siaApi.GetWallet()->GetCreated())
if (_siaApi->GetWallet()->GetCreated())
{
RemoveCreateWalletItems();
if (_siaApi.GetWallet()->GetLocked())
if (_siaApi->GetWallet()->GetLocked())
{
DisplayUnlockWallet();
}
else
{
CallClientScript(L"setAvailableDrives", json(GetAvailableDrives()), nullptr);
SetMainWindow(L"ID_TabWindow");
switch (_siaConfig.GetUI_Main_TabIndex())
@@ -782,7 +785,7 @@ HRESULT CSiaDriveDlg::OnButtonCreateWallet(IHTMLElement* pElement)
KillTimer(IDT_UPDATE);
String seed;
if (ApiSuccess(_siaApi.GetWallet()->Create(SiaSeedLanguage::English, seed)))
if (ApiSuccess(_siaApi->GetWallet()->Create(SiaSeedLanguage::English, seed)))
{
DisplaySeedCreated(seed);
}

View File

@@ -53,6 +53,7 @@ private:
void DisplayRenterTab();
void DisplaySeedCreated(const String& seed);
bool UpdateSiaInfo();
void ReloadDisplay();
void DisplayUnlockWallet();
void ConfigureWallet();
void RemoveCreateWalletItems();
@@ -85,12 +86,14 @@ private:
void SetRenterTotalUsed(const std::uint64_t& bytes);
void QueueUiAction(std::function<void()> action);
void ProcessUiActionQueue();
void ClearDisplay();
private:
const DWORD _uiThreadId;
CSiaDriveConfig _siaConfig;
CSiaApi _siaApi;
std::unique_ptr<CSiaApi> _siaApi;
CString _walletCreatedSeed;
String _receiveAddress;
std::mutex _uiActionQueueMutex;
std::deque<std::function<void()>> _uiActionQueue;
bool _connected = false;