Skip to content

Commit

Permalink
Feat: Changed init to receive the writable path containing the json c…
Browse files Browse the repository at this point in the history
…onfig file
  • Loading branch information
henriqueaklein committed Aug 9, 2024
1 parent 7769abc commit de80893
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmake/CommonBuildParameters.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 5 additions & 8 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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"
)
)
32 changes: 32 additions & 0 deletions example/SDKIdleExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @file SDKIdleExample.cpp
* @brief
* @date 2024-07-31
* @author Henrique A. Klein ([email protected])
*/

#include <math.h>
#include <fstream>
#include <memory>
#include <iostream>
#include <cstdlib>
#include <cstdint>

#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/asio.hpp>
#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;
}
92 changes: 89 additions & 3 deletions src/GeniusSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,99 @@
#include "GeniusSDK.h"
#include "account/GeniusNode.hpp"
#include <memory>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
#include <boost/outcome.hpp>
#include <boost/exception/all.hpp>
#include <string>
#include <cstring>

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<DevConfig_st, JsonError> 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<sgns::GeniusNode> 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<sgns::GeniusNode>( 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<sgns::GeniusNode>( 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 )
Expand Down
4 changes: 2 additions & 2 deletions src/GeniusSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit de80893

Please sign in to comment.