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

[DO NOT review/merge it - just proposal] Separated config reading code from the rest logic #157

Open
wants to merge 2 commits into
base: development/v.0.0.2
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ add_library(graft STATIC
${PROJECT_SOURCE_DIR}/modules/mongoose/mongoose.c
${PROJECT_SOURCE_DIR}/src/backtrace.cpp
${PROJECT_SOURCE_DIR}/src/server.cpp
${PROJECT_SOURCE_DIR}/src/supernode/server/server.cpp
${PROJECT_SOURCE_DIR}/src/supernode/server/config.cpp
${PROJECT_SOURCE_DIR}/src/supernode/server/config_loader.cpp
${PROJECT_SOURCE_DIR}/src/supernode/node.cpp
${PROJECT_SOURCE_DIR}/src/supernode/config.cpp
${PROJECT_SOURCE_DIR}/src/supernode/config_loader.cpp
${PROJECT_SOURCE_DIR}/src/supernode/route/router.cpp
${PROJECT_SOURCE_DIR}/src/supernode/route/route_set.cpp
${PROJECT_SOURCE_DIR}/src/supernode/route/handler3.cpp
${PROJECT_SOURCE_DIR}/src/log.cpp
${PROJECT_SOURCE_DIR}/src/supernode.cpp
${PROJECT_SOURCE_DIR}/src/sys_info.cpp
Expand Down
23 changes: 15 additions & 8 deletions include/common/utils.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#ifndef UTILS_H
#define UTILS_H

#pragma once

#include <string>
#include <random>
#include <set>

namespace graft { class ConnectionManager; }

namespace graft::supernode::utils {

namespace graft {
namespace utils {
std::string base64_decode(const std::string& encoded_data);
std::string base64_encode(const std::string& data);

std::string base64_decode(const std::string &encoded_data);
std::string base64_encode(const std::string &data);
std::string get_home_dir(void); // TODO: make it crossplatform
std::string trim_comments(std::string s);

template <typename T>
T random_number(T startRange, T endRange)
Expand All @@ -19,7 +24,9 @@ T random_number(T startRange, T endRange)
return dist(mt);
}

}
void check_routes(graft::ConnectionManager& cm);
void split_string_by_separator(const std::string& src, char sep, std::set<std::string>& dst);
void remove_duplicates_from_vector(std::vector<std::string>& vec);

}

#endif // UTILS_H
1 change: 1 addition & 0 deletions include/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Looper final : public TaskManager
{
public:
Looper(const ConfigOpts& copts);
Looper(const Config& cfg);
virtual ~Looper();

void serve();
Expand Down
4 changes: 4 additions & 0 deletions include/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace graft { class ConfigOpts; }
namespace graft::supernode::system_info { class Counter; }
namespace graft::supernode::server { class Config; }

namespace graft
{
Expand Down Expand Up @@ -50,10 +51,13 @@ class GlobalContextMapFriend : protected GlobalContextMap
};

using SysInfoCounter = supernode::system_info::Counter;
using supernode::server::Config;

class Context
{
public:
const Config& config(void) const;
void config(const Config& cfg);

const ConfigOpts& config_opts(void) const;
void config_opts(const ConfigOpts& copts);
Expand Down
4 changes: 3 additions & 1 deletion include/inout.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ struct mg_str; //from mongoose.h
* );
*/

namespace graft
namespace graft
{
using namespace supernode;

namespace serializer
{
class JsonParseError : public std::runtime_error
Expand Down
1 change: 1 addition & 0 deletions include/requestdefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static const std::string CONTEXT_SALE_DETAILS_RESULT(":sale_details_result");
static const double AUTHSAMPLE_FEE_PERCENTAGE = 0.5;

static const std::string CONTEXT_KEY_CONFIG_OPTS(":config-opts");
static const std::string CONTEXT_KEY_CONFIG(":config");
static const std::string CONTEXT_KEY_RUNTIME_SYS_INFO(":runtime-sys-info");

namespace graft {
Expand Down
12 changes: 4 additions & 8 deletions include/rta/fullsupernodelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>

namespace graft::utils { class ThreadPool; }

namespace graft {

namespace utils {
class ThreadPool;
}


class FullSupernodeList
{
public:
Expand All @@ -45,15 +41,15 @@ class FullSupernodeList
* \return - number of loaded supernode wallets
*/
size_t loadFromDir(const std::string &base_dir);

/*!
* \brief loadFromDirThreaded - loads list from directory.
* \param base_dir - directory where to search for wallets
* \param found_wallets - number of found wallets
* \return - number of loaded supernodes
*/
size_t loadFromDirThreaded(const std::string &base_dir, size_t &found_wallets);

/*!
* \brief remove - removes Supernode from list. closes it's wallet and frees memory
* \param address - supernode address
Expand Down Expand Up @@ -140,7 +136,7 @@ class FullSupernodeList
bool m_testnet;
DaemonRpcClient m_rpc_client;
mutable boost::shared_mutex m_access;
std::unique_ptr<utils::ThreadPool> m_tp;
std::unique_ptr<graft::utils::ThreadPool> m_tp;
std::atomic_size_t m_refresh_counter;
};

Expand Down
2 changes: 1 addition & 1 deletion include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GraftServer
//the order of members is important because of destruction order.
std::unique_ptr<graft::Looper> m_looper;
private:
void initLog(int log_level);
//void initLog(int log_level);
void initGlobalContext();
void initConnectionManagers();
void serve();
Expand Down
22 changes: 22 additions & 0 deletions include/supernode/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#pragma once

#include "supernode/server/config.h"

namespace graft::supernode {

struct Config : public server::Config
{
std::string data_dir; // base directory where supernode stake wallet and other supernodes wallets are located
std::string stake_wallet_name;
size_t stake_wallet_refresh_interval_ms;

std::string watchonly_wallets_path; // path to watch-only wallets (supernodes)
bool testnet; // testnet flag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not "bool testnet = false;" or ""bool testnet = {}" instead of "Config(void);" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I'd prefer use One way of initialization instead of two different ways.
And we can't always initialize all things inplace.



Config(void);
};

}

21 changes: 21 additions & 0 deletions include/supernode/config_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#pragma once

namespace graft::supernode {

class Config;

class ConfigLoader
{
public:
ConfigLoader(void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need (void)? Is C somewhere assumed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void is not related to C or C++. What the problem?

Copy link
Contributor

@jagerman jagerman Nov 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is most definitely related to C and is only even allowed in C++ because it needs to be compatible with C code. (But since this is a constructor it is especially pointless since C compatibility isn't even possible in the first place). See the C++ Core Guideline's strong recommendation against this.

~ConfigLoader(void);

ConfigLoader(const ConfigLoader&) = delete;
ConfigLoader& operator = (const ConfigLoader&) = delete;

bool load(int argc, const char** argv, Config& cfg);
};

}

34 changes: 34 additions & 0 deletions include/supernode/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "supernode/server/server.h"
#include "supernode/config.h"

namespace graft::supernode {

using server::Server;

class Node : public Server
{
public:
Node(void);
~Node(void);

bool run(const Config& cfg);

protected:
virtual void initMisc(server::Config& cfg) override;
//virtual bool initConfigOption(int argc, const char** argv, ConfigOpts& configOpts) override;
virtual void initRouters() override;

private:
void prepareDataDirAndSupernodes();
void startSupernodePeriodicTasks();
void setHttpRouters(ConnectionManager& httpcm);
void setCoapRouters(ConnectionManager& coapcm);

private:
Config m_cfg;
};

}

34 changes: 34 additions & 0 deletions include/supernode/route/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#pragma once

#include <functional>
#include <map>
#include <string>

namespace graft {
enum class Status: int;
class Context;
class InHttp;
class OutHttp;
}

namespace graft::supernode::route {

using Vars = std::multimap<std::string, std::string>;
using graft::Context;
using graft::Status;

template<typename In, typename Out>
using HandlerT = std::function<Status (const Vars&, const In&, Context&, Out&) >;

using Input = graft::InHttp;
using Output = graft::OutHttp;

using Handler = HandlerT<Input, Output>;

}





34 changes: 34 additions & 0 deletions include/supernode/route/handler3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#pragma once

#include "supernode/route/data.h"

#include <string>

namespace graft::supernode::route {

struct Handler3
{
public:
Handler pre_action;
Handler worker_action;
Handler post_action;
std::string name;

Handler3() = default;
~Handler3() = default;

Handler3(const Handler3&) = default;
Handler3(Handler3&&) = default;

Handler3& operator = (const Handler3&) = default;
Handler3& operator = (Handler3&&) = default;

Handler3(const Handler& pre_action, const Handler& action, const Handler& post_action, const std::string& name = std::string());
Handler3(Handler&& pre_action, Handler&& action, Handler&& post_action, std::string&& name = std::string());
Handler3(const Handler& worker_action);
Handler3(Handler&& worker_action);
};

}

22 changes: 22 additions & 0 deletions include/supernode/route/job_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#pragma once

#include "supernode/route/data.h"
#include "supernode/route/handler3.h"
#include "inout.h"

namespace graft::supernode::route {

struct JobParams
{
Input input;
Vars vars;
Handler3 h3;
};

}





42 changes: 42 additions & 0 deletions include/supernode/route/route_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#pragma once

#include "supernode/route/handler3.h"

#include <forward_list>
#include <string>

namespace graft::supernode::route {

struct Route
{
std::string endpoint;
int method;
Handler3 h3;

Route(const std::string& end_poirnt, int method, const Handler3& h3);
};

std::string method_to_str(int method);

class RouteSet
{
public:
explicit RouteSet(const std::string& prefix = std::string());
RouteSet(RouteSet&&);
~RouteSet();

void add(const std::string& endpoint, int method, const Handler3& ph3);
void add(const std::string& endpoint, int method, const Handler3&& ph3);

std::string dbg_dump(const std::string& prefix = std::string()) const;

std::forward_list<Route> routes(void);
const std::forward_list<Route> routes(void) const;

private:
std::forward_list<Route> m_routes;
std::string m_endpointPrefix;
};

}
Loading