From de8089302fe915f3849a2ea9e287a077ef722eb1 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Fri, 9 Aug 2024 08:44:12 -0300 Subject: [PATCH] Feat: Changed init to receive the writable path containing the json config file --- cmake/CommonBuildParameters.cmake | 4 +- example/CMakeLists.txt | 13 ++--- example/SDKIdleExample.cpp | 32 +++++++++++ src/GeniusSDK.cpp | 92 ++++++++++++++++++++++++++++++- src/GeniusSDK.h | 4 +- 5 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 example/SDKIdleExample.cpp diff --git a/cmake/CommonBuildParameters.cmake b/cmake/CommonBuildParameters.cmake index c134875..6348362 100644 --- a/cmake/CommonBuildParameters.cmake +++ b/cmake/CommonBuildParameters.cmake @@ -9,6 +9,8 @@ set(BOOST_VERSION_3U "${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_${BOOST_PATC set(BOOST_VERSION_2U "${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}") set(SHARED_LIB_BUILD OFF CACHE BOOL "Shared library option for GeniusSDK") +find_package(Vulkan REQUIRED) + if(SHARED_LIB_BUILD) set(LIB_TYPE SHARED) @@ -269,7 +271,7 @@ include_directories(${c-ares_INCLUDE_DIR}) # -------------------------------------------------------- # Set config of ipfs-lite-cpp -set(ipfs-lite-cpp_DIR "${THIRDPARTY_BUILD_DIR}/ipfs-lite-cpp/cmake/ipfs-lite-cpp") +set(ipfs-lite-cpp_DIR "${THIRDPARTY_BUILD_DIR}/ipfs-lite-cpp/lib/cmake/ipfs-lite-cpp") set(ipfs-lite-cpp_INCLUDE_DIR "${THIRDPARTY_BUILD_DIR}/ipfs-lite-cpp/include") set(ipfs-lite-cpp_LIB_DIR "${THIRDPARTY_BUILD_DIR}/ipfs-lite-cpp/lib") set(CBOR_INCLUDE_DIR "${THIRDPARTY_BUILD_DIR}/ipfs-lite-cpp/include/deps/tinycbor/src") diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index aad7e52..fac676a 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,10 +1,7 @@ -include_directories( - ../include +add_executable(SDKIdleExample + SDKIdleExample.cpp ) +target_link_libraries(SDKIdleExample PRIVATE "-Wl,--whole-archive" + GeniusSDK "-Wl,--no-whole-archive" -file(GLOB ALL_SRCS - "../include/*.h" - "../include/*.hpp" - "../src/*.cpp" - "../src/*.c" - ) +) diff --git a/example/SDKIdleExample.cpp b/example/SDKIdleExample.cpp new file mode 100644 index 0000000..f9c4ce2 --- /dev/null +++ b/example/SDKIdleExample.cpp @@ -0,0 +1,32 @@ +/** + * @file SDKIdleExample.cpp + * @brief + * @date 2024-07-31 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + + #include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "local_secure_storage/impl/json/JSONSecureStorage.hpp" +#include "GeniusSDK.h" + + +int main( int argc, char *argv[] ) +{ + const char* no_path = "/home/henrique/SGNSNode/"; + GeniusSDKInit(no_path); + + + while ( true ) + { + } + return 0; +} \ No newline at end of file diff --git a/src/GeniusSDK.cpp b/src/GeniusSDK.cpp index b8a3a11..e257b4b 100644 --- a/src/GeniusSDK.cpp +++ b/src/GeniusSDK.cpp @@ -8,13 +8,99 @@ #include "GeniusSDK.h" #include "account/GeniusNode.hpp" #include +#include +#include +#include +#include +#include +#include +#include +#include + +class JsonError : public boost::exception +{ +public: + explicit JsonError( std::string msg ) : message( std::move( msg ) ) + { + } + + const char *what() const noexcept + { + return message.c_str(); + } + +private: + std::string message; +}; + +static outcome::result ReadDevConfigFromJSON( const std::string &base_path ) +{ + std::ifstream file( base_path + "dev_config.json" ); + if ( !file.is_open() ) + { + return outcome::failure( JsonError( "Configuration file \"dev_config.json\" not found on " + base_path ) ); + } + DevConfig_st config_from_file = {}; + std::stringstream buffer; + buffer << file.rdbuf(); + std::string jsonStr = buffer.str(); + + rapidjson::Document document; + rapidjson::ParseResult parseResult = document.Parse( jsonStr.c_str() ); + if ( !parseResult ) + { + return outcome::failure( JsonError( "Parse error " ) ); + } + + if ( !document.HasMember( "Address" ) || !document["Address"].IsString() ) + { + return outcome::failure( JsonError( "Missing or invalid 'Address'" ) ); + } + if ( !document.HasMember( "Cut" ) || !document["Cut"].IsDouble() ) + { + return outcome::failure( JsonError( "Missing or invalid 'Cut'" ) ); + } + if ( !document.HasMember( "TokenValue" ) || !document["TokenValue"].IsDouble() ) + { + return outcome::failure( JsonError( "Missing or invalid 'TokenValue'" ) ); + } + if ( !document.HasMember( "TokenID" ) || !document["TokenID"].IsInt() ) + { + return outcome::failure( JsonError( "Missing or invalid 'TokenID'" ) ); + } + + strncpy( config_from_file.Addr, document["Address"].GetString(), document["Address"].GetStringLength() ); + config_from_file.Cut = document["Cut"].GetDouble(); + config_from_file.TokenValueInGNUS = document["TokenValue"].GetDouble(); + config_from_file.TokenID = document["TokenID"].GetInt(); + strncpy( config_from_file.BaseWritePath, base_path.data(), base_path.size() ); + + return outcome::success( config_from_file ); +} std::shared_ptr GeniusNodeInstance; -DevConfig_st DEV_CONFIG{ "0xdeadbeef", 0.7, 1.0, 0, "writable/path/" }; -GNUS_VISIBILITY_DEFAULT GNUS_EXPORT void GeniusSDKInit() +GNUS_VISIBILITY_DEFAULT GNUS_EXPORT const char *GeniusSDKInit( const char *base_path ) { - GeniusNodeInstance = std::make_shared( DEV_CONFIG ); + std::string path = ""; + if ( base_path ) + { + path.assign( base_path ); + } + auto load_config_ret = ReadDevConfigFromJSON( path ); + static std::string ret_val = "Initialized on "; + if ( load_config_ret ) + { + GeniusNodeInstance = std::make_shared( load_config_ret.value() ); + ret_val.append( load_config_ret.value().BaseWritePath ); + } + else + { + ret_val.assign( load_config_ret.error().what() ); + std::cout << load_config_ret.error().what() << std::endl; + } + + return ret_val.c_str(); } GNUS_VISIBILITY_DEFAULT GNUS_EXPORT void GeniusSDKProcess( const ImagePath_t path, const PayAmount_t amount ) diff --git a/src/GeniusSDK.h b/src/GeniusSDK.h index d33d9d3..a378964 100644 --- a/src/GeniusSDK.h +++ b/src/GeniusSDK.h @@ -31,7 +31,7 @@ extern "C" } #endif -GNUS_VISIBILITY_DEFAULT GNUS_EXPORT void GeniusSDKInit(); -GNUS_VISIBILITY_DEFAULT GNUS_EXPORT void GeniusSDKProcess( const ImagePath_t path, const PayAmount_t amount ); +GNUS_VISIBILITY_DEFAULT GNUS_EXPORT const char *GeniusSDKInit( const char *base_path ); +GNUS_VISIBILITY_DEFAULT GNUS_EXPORT void GeniusSDKProcess( const ImagePath_t path, const PayAmount_t amount ); #endif //GENIUSSDK_H