Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(config): Refactor configuration variables
Browse files Browse the repository at this point in the history
Refactor configuration variables for future cli and file config feature.
This also enable logger helper to record initializations and destroys.
  • Loading branch information
Yu Wei Wu committed Mar 21, 2019
1 parent 0668526 commit d04fdb6
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 48 deletions.
8 changes: 6 additions & 2 deletions accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ cc_library(
"//utils:cache",
"//utils:pow",
"@com_github_uthash//:uthash",
"@entangled//cclient/api",
"@entangled//cclient/types",
"@entangled//common/model:bundle",
"@entangled//utils:time",
],
)

cc_library(
name = "ta_config",
srcs = ["config.c"],
hdrs = ["config.h"],
visibility = ["//visibility:public"],
deps = [
":ta_errors",
"@entangled//cclient/api",
"@entangled//cclient/types",
],
)

cc_library(
Expand Down
4 changes: 0 additions & 4 deletions accelerator/common_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#define ACCELERATOR_COMMON_CORE_H_

#include "accelerator/config.h"
#include "accelerator/errors.h"
#include "cclient/api/core/core_api.h"
#include "cclient/api/extended/extended_api.h"
#include "cclient/types/types.h"
#include "common/model/bundle.h"
#include "common/model/transfer.h"
#include "request/request.h"
Expand Down
52 changes: 52 additions & 0 deletions accelerator/config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "config.h"
#include "utils/logger_helper.h"

#define CONFIG_LOGGER_ID "config"

static logger_id_t logger_id;

status_t ta_config_init(ta_config_t* const info, iota_config_t* const config,
iota_client_service_t* const service) {
status_t ret = SC_OK;
if (info == NULL || config == NULL || service == NULL) {
return SC_TA_NULL;
}

logger_id = logger_helper_enable(CONFIG_LOGGER_ID, LOGGER_DEBUG, true);
log_info(logger_id, "[%s:%d] enable logger %s.\n", __func__, __LINE__,
CONFIG_LOGGER_ID);

log_info(logger_id, "Initializing TA information\n");
info->version = TA_VERSION;
info->host = TA_HOST;
info->port = TA_PORT;
info->thread_count = TA_THREAD_COUNT;

log_info(logger_id, "Initializing IRI configuration\n");
config->depth = DEPTH;
config->mwm = MWM;
config->seed = SEED;

log_info(logger_id, "Initializing IRI connection\n");
service->http.path = "/";
service->http.content_type = "application/json";
service->http.accept = "application/json";
service->http.host = IRI_HOST;
service->http.port = IRI_PORT;
service->http.api_version = 1;
service->serializer_type = SR_JSON;
if (iota_client_core_init(service)) {
log_critical(logger_id, "Initializing IRI connection failed!\n");
ret = SC_TA_OOM;
}
iota_client_extended_init();

return ret;
}

void ta_config_destroy(iota_client_service_t* const service) {
log_info(logger_id, "Destroying IRI connection\n");
iota_client_extended_destroy();
iota_client_core_destroy(service);
logger_helper_release(logger_id);
}
74 changes: 58 additions & 16 deletions accelerator/config.h
Original file line number Diff line number Diff line change
@@ -1,41 +1,83 @@
#ifndef ACCELERATOR_CONFIG_H_
#define ACCELERATOR_CONFIG_H_

#include "accelerator/errors.h"
#include "cclient/api/core/core_api.h"
#include "cclient/api/extended/extended_api.h"
#include "cclient/types/types.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @file config.h
* @brief Configuration of tangle-acclerator
* @brief Configuration of tangle-accelerator
*/

/** @name tangle-accelerator config */
/** @{ */
#define TA_VERSION "tangle-accelerator/0.3.0"
#define TA_HOST "localhost" /**< Binding address of tangle-acclerator */
#define TA_PORT "8000" /**< Binding port of tangle-acclerator */
#define TA_THREAD_COUNT 10 /**< Thread count of tangle-acclerator instance */
/** @} */

/** @name IRI connection config */
/** @{ */
#define IRI_HOST "localhost" /**< Address of IRI */
#define IRI_PORT 14265 /**< Port of IRI */
#define DEPTH 3 /**< Depth of API argument */
#define MWM 14 /**< Maximum weight magnitude of API argument */
/** Seed to generate address. This does not do any signature yet. */
#define TA_HOST "localhost"
#define TA_PORT "8000"
#define TA_THREAD_COUNT 10
#define IRI_HOST "localhost"
#define IRI_PORT 14265
#define DEPTH 3
#define MWM 14
#define SEED \
"AMRWQP9BUMJALJHBXUCHOD9HFFD9LGTGEAWMJWWXSDVOF9PI9YGJAPBQLQUOMNYEQCZPGCTHGV" \
"NNAPGHA"
/** @} */

/** @name Redis connection config */
/** @{ */
#define REDIS_HOST "localhost" /**< Address of Redis server */
#define REDIS_PORT 6379 /**< poer of Redis server */
/** @} */

/** struct type of accelerator configuration */
typedef struct ta_info_s {
char* version; /**< Binding version of tangle-accelerator */
char* host; /**< Binding address of tangle-accelerator */
char* port; /**< Binding port of tangle-accelerator */
uint8_t thread_count; /**< Thread count of tangle-accelerator instance */
} ta_config_t;

/** struct type of iota configuration */
typedef struct ta_config_s {
uint8_t depth; /**< Depth of API argument */
uint8_t mwm; /**< Minimum weight magnitude of API argument */
/** Seed to generate address. This does not do any signature yet. */
const char* seed;
} iota_config_t;

/** struct type of accelerator core */
typedef struct ta_core_s {
ta_config_t info; /**< accelerator configiuration structure */
iota_config_t config; /**< iota configuration structure */
iota_client_service_t service; /**< iota connection structure */
} ta_core_t;

/**
* Initializes configurations with default values
* Should be called first
*
* @param info[in] Tangle-accelerator configuration variables
* @param config[in] iota configuration variables
* @param service[in] IRI connection configuration variables
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t ta_config_init(ta_config_t* const info, iota_config_t* const config,
iota_client_service_t* const service);

/**
* Free memory of configuration variables
*
* @param service[in] IRI connection configuration variables
*/
void ta_config_destroy(iota_client_service_t* const service);

#ifdef __cplusplus
}
#endif
Expand Down
58 changes: 32 additions & 26 deletions accelerator/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
#include "accelerator/config.h"
#include "accelerator/errors.h"
#include "cJSON.h"
#include "utils/logger_helper.h"

#define MAIN_LOGGER_ID "main"

static ta_core_t ta_core;
static logger_id_t logger_id;

void set_method_header(served::response& res, http_method_t method) {
res.set_header("Server", TA_VERSION);
res.set_header("Server", ta_core.info.version);
res.set_header("Access-Control-Allow-Origin", "*");

switch (method) {
Expand Down Expand Up @@ -48,16 +54,12 @@ int main(int, char const**) {
served::multiplexer mux;
mux.use_after(served::plugin::access_log);

iota_client_service_t service;
service.http.path = "/";
service.http.content_type = "application/json";
service.http.accept = "application/json";
service.http.host = IRI_HOST;
service.http.port = IRI_PORT;
service.http.api_version = 1;
service.serializer_type = SR_JSON;
iota_client_core_init(&service);
iota_client_extended_init();
if (logger_helper_init() != RC_OK) {
return EXIT_FAILURE;
}
logger_id = logger_helper_enable(MAIN_LOGGER_ID, LOGGER_DEBUG, true);

ta_config_init(&ta_core.info, &ta_core.config, &ta_core.service);

mux.handle("/mam/{bundle:[A-Z9]{81}}")
.method(served::method::OPTIONS,
Expand All @@ -68,8 +70,8 @@ int main(int, char const**) {
status_t ret = SC_OK;
char* json_result = NULL;

ret = api_receive_mam_message(&service, req.params["bundle"].c_str(),
&json_result);
ret = api_receive_mam_message(
&ta_core.service, req.params["bundle"].c_str(), &json_result);
ret = set_response_content(ret, &json_result);

set_method_header(res, HTTP_METHOD_GET);
Expand All @@ -93,8 +95,8 @@ int main(int, char const**) {
status_t ret = SC_OK;
char* json_result;

ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
&json_result);
ret = api_find_transactions_by_tag(
&ta_core.service, req.params["tag"].c_str(), &json_result);
ret = set_response_content(ret, &json_result);
set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
Expand All @@ -117,8 +119,8 @@ int main(int, char const**) {
status_t ret = SC_OK;
char* json_result;

ret = api_get_transaction_object(&service, req.params["tx"].c_str(),
&json_result);
ret = api_get_transaction_object(
&ta_core.service, req.params["tx"].c_str(), &json_result);
ret = set_response_content(ret, &json_result);
set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
Expand All @@ -142,7 +144,7 @@ int main(int, char const**) {
char* json_result;

ret = api_find_transactions_obj_by_tag(
&service, req.params["tag"].c_str(), &json_result);
&ta_core.service, req.params["tag"].c_str(), &json_result);
ret = set_response_content(ret, &json_result);
set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
Expand All @@ -163,7 +165,7 @@ int main(int, char const**) {
status_t ret = SC_OK;
char* json_result;

ret = api_get_tips_pair(&service, &json_result);
ret = api_get_tips_pair(&ta_core.service, &json_result);
ret = set_response_content(ret, &json_result);
set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
Expand All @@ -184,7 +186,7 @@ int main(int, char const**) {
status_t ret = SC_OK;
char* json_result;

ret = api_get_tips(&service, &json_result);
ret = api_get_tips(&ta_core.service, &json_result);
ret = set_response_content(ret, &json_result);
set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
Expand All @@ -205,7 +207,7 @@ int main(int, char const**) {
status_t ret = SC_OK;
char* json_result;

ret = api_generate_address(&service, &json_result);
ret = api_generate_address(&ta_core.service, &json_result);
ret = set_response_content(ret, &json_result);
set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
Expand Down Expand Up @@ -236,7 +238,8 @@ int main(int, char const**) {
res.set_status(SC_HTTP_BAD_REQUEST);
cJSON_Delete(json_obj);
} else {
ret = api_send_transfer(&service, req.body().c_str(), &json_result);
ret = api_send_transfer(&ta_core.service, req.body().c_str(),
&json_result);
ret = set_response_content(ret, &json_result);
res.set_status(ret);
}
Expand Down Expand Up @@ -269,10 +272,13 @@ int main(int, char const**) {
});

std::cout << "Starting..." << std::endl;
served::net::server server(TA_HOST, TA_PORT, mux);
server.run(TA_THREAD_COUNT);
served::net::server server(ta_core.info.host, ta_core.info.port, mux);
server.run(ta_core.info.thread_count);

iota_client_extended_destroy();
iota_client_core_destroy(&service);
ta_config_destroy(&ta_core.service);
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
return EXIT_FAILURE;
}
return 0;
}

0 comments on commit d04fdb6

Please sign in to comment.