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

Commit d04fdb6

Browse files
author
Yu Wei Wu
committed
feat(config): Refactor configuration variables
Refactor configuration variables for future cli and file config feature. This also enable logger helper to record initializations and destroys.
1 parent 0668526 commit d04fdb6

File tree

5 files changed

+148
-48
lines changed

5 files changed

+148
-48
lines changed

accelerator/BUILD

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,21 @@ cc_library(
3939
"//utils:cache",
4040
"//utils:pow",
4141
"@com_github_uthash//:uthash",
42-
"@entangled//cclient/api",
43-
"@entangled//cclient/types",
4442
"@entangled//common/model:bundle",
4543
"@entangled//utils:time",
4644
],
4745
)
4846

4947
cc_library(
5048
name = "ta_config",
49+
srcs = ["config.c"],
5150
hdrs = ["config.h"],
5251
visibility = ["//visibility:public"],
52+
deps = [
53+
":ta_errors",
54+
"@entangled//cclient/api",
55+
"@entangled//cclient/types",
56+
],
5357
)
5458

5559
cc_library(

accelerator/common_core.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
#define ACCELERATOR_COMMON_CORE_H_
33

44
#include "accelerator/config.h"
5-
#include "accelerator/errors.h"
6-
#include "cclient/api/core/core_api.h"
7-
#include "cclient/api/extended/extended_api.h"
8-
#include "cclient/types/types.h"
95
#include "common/model/bundle.h"
106
#include "common/model/transfer.h"
117
#include "request/request.h"

accelerator/config.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "config.h"
2+
#include "utils/logger_helper.h"
3+
4+
#define CONFIG_LOGGER_ID "config"
5+
6+
static logger_id_t logger_id;
7+
8+
status_t ta_config_init(ta_config_t* const info, iota_config_t* const config,
9+
iota_client_service_t* const service) {
10+
status_t ret = SC_OK;
11+
if (info == NULL || config == NULL || service == NULL) {
12+
return SC_TA_NULL;
13+
}
14+
15+
logger_id = logger_helper_enable(CONFIG_LOGGER_ID, LOGGER_DEBUG, true);
16+
log_info(logger_id, "[%s:%d] enable logger %s.\n", __func__, __LINE__,
17+
CONFIG_LOGGER_ID);
18+
19+
log_info(logger_id, "Initializing TA information\n");
20+
info->version = TA_VERSION;
21+
info->host = TA_HOST;
22+
info->port = TA_PORT;
23+
info->thread_count = TA_THREAD_COUNT;
24+
25+
log_info(logger_id, "Initializing IRI configuration\n");
26+
config->depth = DEPTH;
27+
config->mwm = MWM;
28+
config->seed = SEED;
29+
30+
log_info(logger_id, "Initializing IRI connection\n");
31+
service->http.path = "/";
32+
service->http.content_type = "application/json";
33+
service->http.accept = "application/json";
34+
service->http.host = IRI_HOST;
35+
service->http.port = IRI_PORT;
36+
service->http.api_version = 1;
37+
service->serializer_type = SR_JSON;
38+
if (iota_client_core_init(service)) {
39+
log_critical(logger_id, "Initializing IRI connection failed!\n");
40+
ret = SC_TA_OOM;
41+
}
42+
iota_client_extended_init();
43+
44+
return ret;
45+
}
46+
47+
void ta_config_destroy(iota_client_service_t* const service) {
48+
log_info(logger_id, "Destroying IRI connection\n");
49+
iota_client_extended_destroy();
50+
iota_client_core_destroy(service);
51+
logger_helper_release(logger_id);
52+
}

accelerator/config.h

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,83 @@
11
#ifndef ACCELERATOR_CONFIG_H_
22
#define ACCELERATOR_CONFIG_H_
33

4+
#include "accelerator/errors.h"
5+
#include "cclient/api/core/core_api.h"
6+
#include "cclient/api/extended/extended_api.h"
7+
#include "cclient/types/types.h"
8+
49
#ifdef __cplusplus
510
extern "C" {
611
#endif
712

813
/**
914
* @file config.h
10-
* @brief Configuration of tangle-acclerator
15+
* @brief Configuration of tangle-accelerator
1116
*/
1217

13-
/** @name tangle-accelerator config */
14-
/** @{ */
1518
#define TA_VERSION "tangle-accelerator/0.3.0"
16-
#define TA_HOST "localhost" /**< Binding address of tangle-acclerator */
17-
#define TA_PORT "8000" /**< Binding port of tangle-acclerator */
18-
#define TA_THREAD_COUNT 10 /**< Thread count of tangle-acclerator instance */
19-
/** @} */
20-
21-
/** @name IRI connection config */
22-
/** @{ */
23-
#define IRI_HOST "localhost" /**< Address of IRI */
24-
#define IRI_PORT 14265 /**< Port of IRI */
25-
#define DEPTH 3 /**< Depth of API argument */
26-
#define MWM 14 /**< Maximum weight magnitude of API argument */
27-
/** Seed to generate address. This does not do any signature yet. */
19+
#define TA_HOST "localhost"
20+
#define TA_PORT "8000"
21+
#define TA_THREAD_COUNT 10
22+
#define IRI_HOST "localhost"
23+
#define IRI_PORT 14265
24+
#define DEPTH 3
25+
#define MWM 14
2826
#define SEED \
2927
"AMRWQP9BUMJALJHBXUCHOD9HFFD9LGTGEAWMJWWXSDVOF9PI9YGJAPBQLQUOMNYEQCZPGCTHGV" \
3028
"NNAPGHA"
31-
/** @} */
3229

3330
/** @name Redis connection config */
3431
/** @{ */
3532
#define REDIS_HOST "localhost" /**< Address of Redis server */
3633
#define REDIS_PORT 6379 /**< poer of Redis server */
3734
/** @} */
3835

36+
/** struct type of accelerator configuration */
37+
typedef struct ta_info_s {
38+
char* version; /**< Binding version of tangle-accelerator */
39+
char* host; /**< Binding address of tangle-accelerator */
40+
char* port; /**< Binding port of tangle-accelerator */
41+
uint8_t thread_count; /**< Thread count of tangle-accelerator instance */
42+
} ta_config_t;
43+
44+
/** struct type of iota configuration */
45+
typedef struct ta_config_s {
46+
uint8_t depth; /**< Depth of API argument */
47+
uint8_t mwm; /**< Minimum weight magnitude of API argument */
48+
/** Seed to generate address. This does not do any signature yet. */
49+
const char* seed;
50+
} iota_config_t;
51+
52+
/** struct type of accelerator core */
53+
typedef struct ta_core_s {
54+
ta_config_t info; /**< accelerator configiuration structure */
55+
iota_config_t config; /**< iota configuration structure */
56+
iota_client_service_t service; /**< iota connection structure */
57+
} ta_core_t;
58+
59+
/**
60+
* Initializes configurations with default values
61+
* Should be called first
62+
*
63+
* @param info[in] Tangle-accelerator configuration variables
64+
* @param config[in] iota configuration variables
65+
* @param service[in] IRI connection configuration variables
66+
*
67+
* @return
68+
* - SC_OK on success
69+
* - non-zero on error
70+
*/
71+
status_t ta_config_init(ta_config_t* const info, iota_config_t* const config,
72+
iota_client_service_t* const service);
73+
74+
/**
75+
* Free memory of configuration variables
76+
*
77+
* @param service[in] IRI connection configuration variables
78+
*/
79+
void ta_config_destroy(iota_client_service_t* const service);
80+
3981
#ifdef __cplusplus
4082
}
4183
#endif

accelerator/server.cc

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
#include "accelerator/config.h"
66
#include "accelerator/errors.h"
77
#include "cJSON.h"
8+
#include "utils/logger_helper.h"
9+
10+
#define MAIN_LOGGER_ID "main"
11+
12+
static ta_core_t ta_core;
13+
static logger_id_t logger_id;
814

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

1319
switch (method) {
@@ -48,16 +54,12 @@ int main(int, char const**) {
4854
served::multiplexer mux;
4955
mux.use_after(served::plugin::access_log);
5056

51-
iota_client_service_t service;
52-
service.http.path = "/";
53-
service.http.content_type = "application/json";
54-
service.http.accept = "application/json";
55-
service.http.host = IRI_HOST;
56-
service.http.port = IRI_PORT;
57-
service.http.api_version = 1;
58-
service.serializer_type = SR_JSON;
59-
iota_client_core_init(&service);
60-
iota_client_extended_init();
57+
if (logger_helper_init() != RC_OK) {
58+
return EXIT_FAILURE;
59+
}
60+
logger_id = logger_helper_enable(MAIN_LOGGER_ID, LOGGER_DEBUG, true);
61+
62+
ta_config_init(&ta_core.info, &ta_core.config, &ta_core.service);
6163

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

71-
ret = api_receive_mam_message(&service, req.params["bundle"].c_str(),
72-
&json_result);
73+
ret = api_receive_mam_message(
74+
&ta_core.service, req.params["bundle"].c_str(), &json_result);
7375
ret = set_response_content(ret, &json_result);
7476

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

96-
ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
97-
&json_result);
98+
ret = api_find_transactions_by_tag(
99+
&ta_core.service, req.params["tag"].c_str(), &json_result);
98100
ret = set_response_content(ret, &json_result);
99101
set_method_header(res, HTTP_METHOD_GET);
100102
res.set_status(ret);
@@ -117,8 +119,8 @@ int main(int, char const**) {
117119
status_t ret = SC_OK;
118120
char* json_result;
119121

120-
ret = api_get_transaction_object(&service, req.params["tx"].c_str(),
121-
&json_result);
122+
ret = api_get_transaction_object(
123+
&ta_core.service, req.params["tx"].c_str(), &json_result);
122124
ret = set_response_content(ret, &json_result);
123125
set_method_header(res, HTTP_METHOD_GET);
124126
res.set_status(ret);
@@ -142,7 +144,7 @@ int main(int, char const**) {
142144
char* json_result;
143145

144146
ret = api_find_transactions_obj_by_tag(
145-
&service, req.params["tag"].c_str(), &json_result);
147+
&ta_core.service, req.params["tag"].c_str(), &json_result);
146148
ret = set_response_content(ret, &json_result);
147149
set_method_header(res, HTTP_METHOD_GET);
148150
res.set_status(ret);
@@ -163,7 +165,7 @@ int main(int, char const**) {
163165
status_t ret = SC_OK;
164166
char* json_result;
165167

166-
ret = api_get_tips_pair(&service, &json_result);
168+
ret = api_get_tips_pair(&ta_core.service, &json_result);
167169
ret = set_response_content(ret, &json_result);
168170
set_method_header(res, HTTP_METHOD_GET);
169171
res.set_status(ret);
@@ -184,7 +186,7 @@ int main(int, char const**) {
184186
status_t ret = SC_OK;
185187
char* json_result;
186188

187-
ret = api_get_tips(&service, &json_result);
189+
ret = api_get_tips(&ta_core.service, &json_result);
188190
ret = set_response_content(ret, &json_result);
189191
set_method_header(res, HTTP_METHOD_GET);
190192
res.set_status(ret);
@@ -205,7 +207,7 @@ int main(int, char const**) {
205207
status_t ret = SC_OK;
206208
char* json_result;
207209

208-
ret = api_generate_address(&service, &json_result);
210+
ret = api_generate_address(&ta_core.service, &json_result);
209211
ret = set_response_content(ret, &json_result);
210212
set_method_header(res, HTTP_METHOD_GET);
211213
res.set_status(ret);
@@ -236,7 +238,8 @@ int main(int, char const**) {
236238
res.set_status(SC_HTTP_BAD_REQUEST);
237239
cJSON_Delete(json_obj);
238240
} else {
239-
ret = api_send_transfer(&service, req.body().c_str(), &json_result);
241+
ret = api_send_transfer(&ta_core.service, req.body().c_str(),
242+
&json_result);
240243
ret = set_response_content(ret, &json_result);
241244
res.set_status(ret);
242245
}
@@ -269,10 +272,13 @@ int main(int, char const**) {
269272
});
270273

271274
std::cout << "Starting..." << std::endl;
272-
served::net::server server(TA_HOST, TA_PORT, mux);
273-
server.run(TA_THREAD_COUNT);
275+
served::net::server server(ta_core.info.host, ta_core.info.port, mux);
276+
server.run(ta_core.info.thread_count);
274277

275-
iota_client_extended_destroy();
276-
iota_client_core_destroy(&service);
278+
ta_config_destroy(&ta_core.service);
279+
logger_helper_release(logger_id);
280+
if (logger_helper_destroy() != RC_OK) {
281+
return EXIT_FAILURE;
282+
}
277283
return 0;
278284
}

0 commit comments

Comments
 (0)