Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graftlets configuration #252

Open
wants to merge 3 commits into
base: feature/graftlets-periodic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ add_library(graft STATIC
${PROJECT_SOURCE_DIR}/src/lib/graft/common/utils.cpp
${PROJECT_SOURCE_DIR}/src/lib/graft/backtrace.cpp
${PROJECT_SOURCE_DIR}/src/lib/graft/blacklist.cpp
${PROJECT_SOURCE_DIR}/src/lib/graft/ConfigIni.cpp
${PROJECT_SOURCE_DIR}/src/lib/graft/connection.cpp
${PROJECT_SOURCE_DIR}/src/lib/graft/context.cpp
${PROJECT_SOURCE_DIR}/src/lib/graft/inout.cpp
Expand Down Expand Up @@ -382,6 +383,7 @@ if (OPT_BUILD_TESTS)
)

add_executable(supernode_test
${PROJECT_SOURCE_DIR}/test/configini_test.cpp
${PROJECT_SOURCE_DIR}/test/expiring_set_test.cpp
${PROJECT_SOURCE_DIR}/test/upstream_test.cpp
${PROJECT_SOURCE_DIR}/test/blacklist_test.cpp
Expand Down
8 changes: 7 additions & 1 deletion graftlets/TestGraftlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __GRAFTLET__
#include "lib/graft/GraftletRegistry.h"
#include "lib/graft/IGraftlet.h"
#include "lib/graft/ConfigIni.h"

#include<cassert>

Expand Down Expand Up @@ -75,8 +76,13 @@ class TestGraftlet: public IGraftlet
return (stop)? graft::Status::Stop : graft::Status::Ok;
}

virtual void initOnce(const graft::CommonOpts& opts) override
virtual void initOnce(const graft::CommonOpts& opts, graft::Context& ctx) override
{
if(!opts.config_filename.empty())
{
graft::ConfigIniSubtree config = graft::ConfigIniSubtree::create(opts.config_filename);
ctx.global["graftlets.dirs"] = config.get<std::string>("graftlets.dirs");
}
// REGISTER_ACTION(TestGraftlet, testUndefined);
REGISTER_ACTION(TestGraftlet, testInt1);
REGISTER_ACTION(TestGraftlet, testInt2);
Expand Down
2 changes: 1 addition & 1 deletion graftlets/TestGraftlet1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TestGraftlet1: public IGraftlet
return graft::Status::Ok;
}

virtual void initOnce(const graft::CommonOpts& opts) override
virtual void initOnce(const graft::CommonOpts& opts, graft::Context& ctx) override
{
// REGISTER_ACTION(TestGraftlet1, testUndefined);
REGISTER_ACTION(TestGraftlet1, testInt1);
Expand Down
2 changes: 1 addition & 1 deletion graftlets/WalletAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WalletAddress: public IGraftlet
return true;
}

virtual void initOnce(const graft::CommonOpts& opts) override
virtual void initOnce(const graft::CommonOpts& opts, graft::Context& ctx) override
{
makeGetWalletAddressResponse(opts);

Expand Down
223 changes: 223 additions & 0 deletions include/lib/graft/ConfigIni.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// Copyright (c) 2018, The Graft Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <string>
#include <optional>
#include <memory>
#include <type_traits>

namespace graft
{

class ConfigIniSubtree;
class ConfigIniSubtreeRef;

namespace hidden
{

class VConfigIniSubtree
{
public:
virtual ~VConfigIniSubtree() = default;
//get_... that throw exit_error
virtual bool get_bool(const std::string& path) const = 0;
virtual long long get_long_long(const std::string& path) const = 0;
virtual unsigned long long get_u_long_long(const std::string& path) const = 0;
virtual long double get_long_double(const std::string& path) const = 0;
virtual std::string get_string(const std::string& path) const = 0;

virtual std::unique_ptr<VConfigIniSubtree> clone() const = 0;
};

class VIter
{
protected:
VIter() = default;
public:
virtual ~VIter() = default;
VIter(const VIter&) = delete;
VIter& operator=(const VIter&) = delete;

virtual bool operator == (const VIter& it) const = 0;
virtual VIter& operator++() = 0; //prefix increment
// virtual VIter& operator++(int) = 0; //postfix increment
virtual ConfigIniSubtreeRef operator*() const = 0;
};

} //namespace hidden

class ConfigIniSubtreeRef final
{
std::unique_ptr<hidden::VConfigIniSubtree> v;
std::string n;
public:
~ConfigIniSubtreeRef() = default;
ConfigIniSubtreeRef(const ConfigIniSubtreeRef& sr) : v(sr.v->clone()), n(sr.n) { }
ConfigIniSubtreeRef& operator = (const ConfigIniSubtreeRef& sr) { v = sr.v->clone(); n = sr.n; return *this; }
ConfigIniSubtreeRef(ConfigIniSubtreeRef&& sr) = default;
ConfigIniSubtreeRef& operator = (ConfigIniSubtreeRef&& sr) = default;

ConfigIniSubtreeRef(std::unique_ptr<hidden::VConfigIniSubtree>&& v, std::string&& n)
: v(std::move(v))
, n(std::move(n)) { }
const std::string& name() const { return n; }
ConfigIniSubtree value() const;
};

//Interface to get values from config.ini
class ConfigIniSubtree
{
private:

protected:
std::unique_ptr<hidden::VConfigIniSubtree> v;
ConfigIniSubtree(std::unique_ptr<hidden::VConfigIniSubtree>&& v) : v(std::move(v)) { }
friend class ConfigIniSubtreeRef;
public:
~ConfigIniSubtree() = default;
ConfigIniSubtree(const ConfigIniSubtree& s) : v( s.v->clone() ) { }
ConfigIniSubtree& operator =(const ConfigIniSubtree& s) { v = s.v->clone(); return *this; }
ConfigIniSubtree(ConfigIniSubtree&& s) = default;
ConfigIniSubtree& operator =(ConfigIniSubtree&& s) = default;

static ConfigIniSubtree create(const std::string& config_filename);

class iterator
{
std::unique_ptr<hidden::VIter> vi;
friend ConfigIniSubtree;
public:
~iterator() = default;
iterator(const iterator& it);
iterator& operator =(const iterator& it);
iterator(iterator&& it) = default;
iterator& operator =(iterator&& it) = default;

iterator(std::unique_ptr<hidden::VIter>&& vi) : vi(std::move(vi)) { }

bool operator ==(const iterator& it) const
{
vi->operator ==(*it.vi);
}
bool operator !=(const iterator& it) const
{
return ! operator ==(it);
}

iterator& operator ++() //prefix increment
{
vi->operator ++();
return *this;
}
// iterator& operator++(int); //postfix increment
ConfigIniSubtreeRef operator *() const
{
return vi->operator *();
}
std::unique_ptr<ConfigIniSubtreeRef> operator->() const
{
return std::make_unique<ConfigIniSubtreeRef>( operator *() );
}
};

iterator begin();
iterator end();

//throw exit_error
ConfigIniSubtree get_child(const std::string& path) const;

std::optional<ConfigIniSubtree> get_child_optional(const std::string& path) const
{
try
{
ConfigIniSubtree res = get_child(path);
return std::optional<ConfigIniSubtree>(std::move(res));
}
catch(std::exception&){ return std::optional<ConfigIniSubtree>(); }
}

//get functions that throw exit_error
template<typename T>
typename std::enable_if<std::is_same<T, bool>::value, T>::type
get(const std::string& path) const
{
return v->get_bool(path);
}

template<typename T>
typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value && !std::is_same<T, bool>::value, T>::type
get(const std::string& path) const
{
return static_cast<T>( v->get_long_long(path) );
}

template<typename T>
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value && !std::is_same<T, bool>::value, T>::type
get(const std::string& path) const
{
return static_cast<T>( v->get_u_long_long(path) );
}

template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
get(const std::string& path) const
{
return static_cast<T>( v->get_long_double(path) );
}

template<typename T>
typename std::enable_if<std::is_same<T, std::string>::value, T>::type
get(const std::string& path) const
{
return v->get_string(path);
}

//get functions with default
template<typename T>
T get(const std::string& path, const T& default_value) const
{
try{ return get<T>(path); }
catch(std::exception&){ return default_value; }
}

//get functions with optional
template<typename T>
std::optional<T> get_optional(const std::string& path) const
{
try
{
T res = get<T>(path);
return std::optional<T>(std::move(res));
}
catch(std::exception&){ return std::optional<T>(); }
}
};

} //namespace graft
5 changes: 3 additions & 2 deletions include/lib/graft/GraftletLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class GraftletLoader
using Version = int;
using GraftletExceptionList = std::vector< std::pair< DllName, std::vector< std::pair<Version, Version> >>>;

GraftletLoader(const graft::CommonOpts& opts) : m_opts(opts) { }
GraftletLoader(const graft::CommonOpts& opts, graft::GlobalContextMap& gcm) : m_opts(opts), m_ctx(gcm) { }

static Version getFwVersion() { return m_fwVersion; }
static void setFwVersion(Version fwVersion) { m_fwVersion = fwVersion; }
Expand Down Expand Up @@ -224,7 +224,7 @@ class GraftletLoader
GraftletRegistry* gr = it1->second;
std::shared_ptr<BaseT> concreteGraftlet = gr->resolveGraftlet<BaseT>();
if(!concreteGraftlet.get()) throw std::runtime_error("Cannot resolve dll name:" + dllName + " type:" + typeid(BaseT).name());
concreteGraftlet->init(m_opts);
concreteGraftlet->init(m_opts, m_ctx);
ClsName name = concreteGraftlet->getClsName();
std::any any(concreteGraftlet);

Expand Down Expand Up @@ -295,6 +295,7 @@ class GraftletLoader
static ExceptionMap m_exceptionMap;

const graft::CommonOpts& m_opts;
graft::Context m_ctx;

//we can use functions in a dll until we release object of boost::dll::shared_library
//dll name -> (lib, version, path)
Expand Down
6 changes: 3 additions & 3 deletions include/lib/graft/IGraftlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class IGraftlet
IGraftlet(const IGraftlet&) = delete;
IGraftlet& operator = (const IGraftlet&) = delete;

void init(const graft::CommonOpts& opts)
void init(const graft::CommonOpts& opts, graft::Context& ctx)
{
if(m_inited) return;
m_inited = true;
initOnce(opts);
initOnce(opts, ctx);
}

const ClsName& getClsName() const { return m_clsName; }
Expand Down Expand Up @@ -183,7 +183,7 @@ class IGraftlet

protected:
IGraftlet(const ClsName& name = ClsName() ) : m_clsName(name) { }
virtual void initOnce(const graft::CommonOpts& opts) = 0;
virtual void initOnce(const graft::CommonOpts& opts, graft::Context& ctx) = 0;
private:
using TypeIndex2any = std::map<std::type_index, std::tuple<std::any, EndpointPath, Methods> >;
using Map = std::map<FuncName, TypeIndex2any>;
Expand Down
2 changes: 1 addition & 1 deletion include/lib/graft/serveropts.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct CommonOpts
{
// testnet flag
bool testnet;
std::string config_filename;
// data directory - base directory where supernode stake wallet and other supernodes wallets are located
std::string data_dir;
std::string wallet_public_address;
Expand All @@ -25,7 +26,6 @@ struct IPFilterOpts

struct ConfigOpts
{
std::string config_filename;
std::string http_address;
std::string coap_address;
double http_connection_timeout;
Expand Down
Loading