From 43b301f0bc81ecd9624ee5f4ad1a03c4f74fb45b Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 4 Feb 2017 00:51:43 -0600 Subject: [PATCH] More changes --- SiaDrive.Api/SiaCommon.h | 14 +++--- SiaDrive.Api/SiaDrive.Api.vcxproj | 2 + SiaDrive.Api/SiaDrive.Api.vcxproj.filters | 6 +++ SiaDrive.Api/SiaDriveConfig.cpp | 51 +++++++++++++++++++++ SiaDrive.Api/SiaDriveConfig.h | 31 +++++++++++++ SiaDrive/SiaDrive.htm | 55 ++++++++++++----------- SiaDrive/SiaDriveDlg.cpp | 23 ++++++++-- SiaDrive/SiaDriveDlg.h | 3 ++ UnitTests/SiaApiTests.cpp | 2 +- UnitTests/SiaCurlTests.cpp | 2 +- UnitTests/SiaDriveConfigTests.cpp | 20 +++++++++ UnitTests/SiaWalletApiTests.cpp | 2 +- UnitTests/UnitTestConfig.h | 25 ++++++++--- UnitTests/UnitTests.vcxproj | 4 ++ UnitTests/UnitTests.vcxproj.filters | 3 ++ 15 files changed, 200 insertions(+), 43 deletions(-) create mode 100644 SiaDrive.Api/SiaDriveConfig.cpp create mode 100644 SiaDrive.Api/SiaDriveConfig.h create mode 100644 UnitTests/SiaDriveConfigTests.cpp diff --git a/SiaDrive.Api/SiaCommon.h b/SiaDrive.Api/SiaCommon.h index 3a79019..63ded21 100644 --- a/SiaDrive.Api/SiaCommon.h +++ b/SiaDrive.Api/SiaCommon.h @@ -6,11 +6,7 @@ using json = nlohmann::json; -#ifdef _UNICODE #define String std::wstring -#else -#define String std::wstring -#endif #define NS_BEGIN(n) namespace n { @@ -30,6 +26,12 @@ get_access:\ set_access:\ const type& Set##name(const type& value) { _##name = value; return _##name; } +#define JProperty(type, name, get_access, set_access, json_doc) \ +get_access:\ + type Get##name() const { return json_doc[#name].get();}\ +set_access:\ + type Set##name(const type& value) { json_doc[#name] = value; return value; } + struct SiaHostConfig { String HostName; @@ -37,4 +39,6 @@ struct SiaHostConfig String RequiredVersion; }; -#define API_SUCCESS(t, x) (x == t::Success) \ No newline at end of file +#define API_SUCCESS(t, x) (x == t::Success) + +#define DEFAULT_CONFIG_FILE_PATH L".\\Config\\SiaDriveConfig.json" \ No newline at end of file diff --git a/SiaDrive.Api/SiaDrive.Api.vcxproj b/SiaDrive.Api/SiaDrive.Api.vcxproj index 94bb23a..27652cc 100644 --- a/SiaDrive.Api/SiaDrive.Api.vcxproj +++ b/SiaDrive.Api/SiaDrive.Api.vcxproj @@ -194,6 +194,7 @@ + Create @@ -209,6 +210,7 @@ + diff --git a/SiaDrive.Api/SiaDrive.Api.vcxproj.filters b/SiaDrive.Api/SiaDrive.Api.vcxproj.filters index 13fc3cf..8aa72a1 100644 --- a/SiaDrive.Api/SiaDrive.Api.vcxproj.filters +++ b/SiaDrive.Api/SiaDrive.Api.vcxproj.filters @@ -33,6 +33,9 @@ Source Files + + Source Files + @@ -59,6 +62,9 @@ Header Files + + Header Files + diff --git a/SiaDrive.Api/SiaDriveConfig.cpp b/SiaDrive.Api/SiaDriveConfig.cpp new file mode 100644 index 0000000..a35d0a9 --- /dev/null +++ b/SiaDrive.Api/SiaDriveConfig.cpp @@ -0,0 +1,51 @@ +#include "stdafx.h" +#include "SiaDriveConfig.h" +#include +using namespace Sia::Api; + +CSiaDriveConfig::CSiaDriveConfig() : + CSiaDriveConfig(DEFAULT_CONFIG_FILE_PATH) +{ +} + +CSiaDriveConfig::CSiaDriveConfig(const String& filePath) : + _FilePath(filePath) +{ + Load(); +} + +CSiaDriveConfig::~CSiaDriveConfig() +{ + Save(); +} + +void CSiaDriveConfig::LoadDefaults() +{ + SetUI_Main_TabIndex(0); +} + +void CSiaDriveConfig::Load( ) +{ + CFile f; + if (f.Open(GetFilePath().c_str(), CFile::modeRead)) + { + std::string s; + s.resize(f.GetLength()); + + f.Read(&s[0], s.length()); + + f.Close(); + + _configDocument = json::parse(s.begin(), s.end()); + } + else + { + LoadDefaults(); + Save(); + } +} + +void CSiaDriveConfig::Save() const +{ + std::ofstream(CW2A(GetFilePath().c_str())) << std::setw(2) << _configDocument << std::endl; +} \ No newline at end of file diff --git a/SiaDrive.Api/SiaDriveConfig.h b/SiaDrive.Api/SiaDriveConfig.h new file mode 100644 index 0000000..55e5a47 --- /dev/null +++ b/SiaDrive.Api/SiaDriveConfig.h @@ -0,0 +1,31 @@ +#pragma once +#include + +NS_BEGIN(Sia) +NS_BEGIN(Api) + +class AFX_EXT_CLASS CSiaDriveConfig +{ +public: + CSiaDriveConfig(); + + CSiaDriveConfig(const String& filePath); + +public: + ~CSiaDriveConfig(); + + Property(String, FilePath, public, private) + JProperty(std::uint8_t, UI_Main_TabIndex, public, private, _configDocument) + +private: + json _configDocument; + +private: + void LoadDefaults(); + + void Load( ); + + void Save() const; +}; + +NS_END(2) \ No newline at end of file diff --git a/SiaDrive/SiaDrive.htm b/SiaDrive/SiaDrive.htm index d8cdc6c..c80062c 100644 --- a/SiaDrive/SiaDrive.htm +++ b/SiaDrive/SiaDrive.htm @@ -1,30 +1,33 @@ - -
- - - - - -
-
-
- - - - -
- - - -
-   -
-
- - + +
+ +
+
+ + + + + +
+
+
+ + + + +
+ + + +
+   +
+
+ + diff --git a/SiaDrive/SiaDriveDlg.cpp b/SiaDrive/SiaDriveDlg.cpp index b40588a..3015587 100644 --- a/SiaDrive/SiaDriveDlg.cpp +++ b/SiaDrive/SiaDriveDlg.cpp @@ -63,6 +63,7 @@ CSiaDriveDlg::CSiaDriveDlg(CWnd* pParent /*=NULL*/) void CSiaDriveDlg::DoDataExchange(CDataExchange* pDX) { CDHtmlDialog::DoDataExchange(pDX); + DDX_DHtml_ElementInnerText(pDX, _T("ServerVersion"), _serverVersion); } BEGIN_MESSAGE_MAP(CSiaDriveDlg, CDHtmlDialog) @@ -100,9 +101,7 @@ BOOL CSiaDriveDlg::OnInitDialog() // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon - - String version = _siaApi.GetServerVersion(); - + return TRUE; // return TRUE unless you set the focus to a control } @@ -166,3 +165,21 @@ HRESULT CSiaDriveDlg::OnButtonCancel(IHTMLElement* /*pElement*/) OnCancel(); return S_OK; } + +void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR) +{ + _serverVersion = _siaApi.GetServerVersion().c_str(); + if (_serverVersion) + { + if (_siaApi.GetWallet()->GetCreated()) + { + + } + else + { + + } + } + + UpdateData(FALSE); +} \ No newline at end of file diff --git a/SiaDrive/SiaDriveDlg.h b/SiaDrive/SiaDriveDlg.h index fdc8358..1690857 100644 --- a/SiaDrive/SiaDriveDlg.h +++ b/SiaDrive/SiaDriveDlg.h @@ -30,6 +30,8 @@ protected: // Generated message map functions virtual BOOL OnInitDialog(); + virtual void OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR szUrl); + afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); @@ -38,4 +40,5 @@ protected: private: CSiaApi _siaApi; + CString _serverVersion; }; diff --git a/UnitTests/SiaApiTests.cpp b/UnitTests/SiaApiTests.cpp index 6ba16f3..2e910b8 100644 --- a/UnitTests/SiaApiTests.cpp +++ b/UnitTests/SiaApiTests.cpp @@ -22,5 +22,5 @@ namespace UnitTests } }; - Daemon SiaApi::_daemon; + DEFINE_DAEMON(SiaApi); } \ No newline at end of file diff --git a/UnitTests/SiaCurlTests.cpp b/UnitTests/SiaCurlTests.cpp index b078f42..9a61339 100644 --- a/UnitTests/SiaCurlTests.cpp +++ b/UnitTests/SiaCurlTests.cpp @@ -73,5 +73,5 @@ namespace UnitTests } }; - Daemon SiaCurl::_daemon; + DEFINE_DAEMON(SiaCurl); } \ No newline at end of file diff --git a/UnitTests/SiaDriveConfigTests.cpp b/UnitTests/SiaDriveConfigTests.cpp new file mode 100644 index 0000000..5e7fe21 --- /dev/null +++ b/UnitTests/SiaDriveConfigTests.cpp @@ -0,0 +1,20 @@ +#include "stdafx.h" +#include "CppUnitTest.h" +#include "SiaDriveConfig.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace UnitTests +{ + TEST_CLASS(SiaDriveConfig) + { + public: + TEST_METHOD(DefaultFileCreation) + { + Sia::Api::CSiaDriveConfig cfg(L".\\test_cfg.json"); + Assert::AreEqual(static_cast(0), cfg.GetUI_Main_TabIndex()); + Assert::IsTrue(PathFileExists(L".\\test_cfg.json")); + Assert::AreEqual(L".\\test_cfg.json", cfg.GetFilePath().c_str()); + } + }; +} \ No newline at end of file diff --git a/UnitTests/SiaWalletApiTests.cpp b/UnitTests/SiaWalletApiTests.cpp index 9a668e2..49c44c4 100644 --- a/UnitTests/SiaWalletApiTests.cpp +++ b/UnitTests/SiaWalletApiTests.cpp @@ -43,5 +43,5 @@ namespace UnitTests } }; - Daemon SiaWalletApi::_daemon; + DEFINE_DAEMON(SiaWalletApi); } \ No newline at end of file diff --git a/UnitTests/UnitTestConfig.h b/UnitTests/UnitTestConfig.h index 1ce3816..410ecb6 100644 --- a/UnitTests/UnitTestConfig.h +++ b/UnitTests/UnitTestConfig.h @@ -1,15 +1,24 @@ #pragma once -#define TEST_SERVER_AND_PORT L"localhost", 19980 +#define TEST_SERVER_HOST L"localhost" +#define TEST_SERVER_PORT 11980 +#define TEST_SERVER_AND_PORT TEST_SERVER_HOST, TEST_SERVER_PORT #define TEST_SERVER_VERSION L"1.1.0" using namespace Sia::Api; class Daemon { +public: + ~Daemon() + { + Stop(); + } + private: PROCESS_INFORMATION pi; STARTUPINFO si; + private: void Cleanup() { @@ -26,17 +35,18 @@ private: CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } + + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); } public: void Start() { Cleanup(); - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); - ZeroMemory(&pi, sizeof(pi)); - String szCmdline = L"\"..\\..\\3rd-party\\Sia-v1.1.0-windows-amd64\\siad.exe\" -d .\\data --api-addr localhost:19980"; + String szCmdline = L"\"..\\..\\3rd-party\\Sia-v1.1.0-windows-amd64\\siad.exe\" -d .\\data --api-addr " + String(TEST_SERVER_HOST) + L":" + std::to_wstring(TEST_SERVER_PORT) + L" --no-bootstrap"; CreateProcess(nullptr, &szCmdline[0], nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi); } @@ -67,4 +77,7 @@ public: TEST_CLASS_CLEANUP(StopDaemon)\ {\ _daemon.Stop();\ - } \ No newline at end of file + } + + +#define DEFINE_DAEMON(class) Daemon class::_daemon \ No newline at end of file diff --git a/UnitTests/UnitTests.vcxproj b/UnitTests/UnitTests.vcxproj index 585566b..5f5b895 100644 --- a/UnitTests/UnitTests.vcxproj +++ b/UnitTests/UnitTests.vcxproj @@ -163,6 +163,10 @@
+ + + + diff --git a/UnitTests/UnitTests.vcxproj.filters b/UnitTests/UnitTests.vcxproj.filters index 6ea879a..360ed5a 100644 --- a/UnitTests/UnitTests.vcxproj.filters +++ b/UnitTests/UnitTests.vcxproj.filters @@ -38,5 +38,8 @@ Source Files + + Source Files + \ No newline at end of file