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

Commit 6aecb05

Browse files
committed
feat(conf): Implement CLI configuration interface
1 parent 894f4d7 commit 6aecb05

File tree

7 files changed

+203
-17
lines changed

7 files changed

+203
-17
lines changed

accelerator/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ cc_library(
5454
hdrs = ["config.h"],
5555
visibility = ["//visibility:public"],
5656
deps = [
57+
":message",
5758
":ta_errors",
5859
"//utils:cache",
5960
"//utils:pow",

accelerator/config.c

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,74 @@
55

66
static logger_id_t logger_id;
77

8-
status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle,
9-
iota_client_service_t* const service) {
8+
struct option* cli_build_options() {
9+
struct option* long_options =
10+
(struct option*)malloc(cli_cmd_num * sizeof(struct option));
11+
for (int i = 0; i < cli_cmd_num; ++i) {
12+
long_options[i].name = ta_cli_arguments_g[i].name;
13+
long_options[i].has_arg = ta_cli_arguments_g[i].has_arg;
14+
long_options[i].flag = NULL;
15+
long_options[i].val = ta_cli_arguments_g[i].val;
16+
}
17+
return long_options;
18+
}
19+
20+
status_t cli_config_set(ta_config_t* const info, iota_config_t* const tangle,
21+
ta_cache_t* const cache,
22+
iota_client_service_t* const service, int key,
23+
char* const value) {
24+
if (value == NULL || info == NULL || tangle == NULL || cache == NULL ||
25+
service == NULL) {
26+
return SC_CONF_NULL;
27+
}
28+
switch (key) {
29+
// TA configuration
30+
case TA_HOST_CLI:
31+
info->host = value;
32+
break;
33+
case TA_PORT_CLI:
34+
info->port = value;
35+
break;
36+
case TA_THREAD_COUNT_CLI:
37+
info->thread_count = atoi(value);
38+
break;
39+
40+
// IRI configuration
41+
case IRI_HOST_CLI:
42+
service->http.host = value;
43+
break;
44+
case IRI_PORT_CLI:
45+
service->http.port = atoi(value);
46+
break;
47+
48+
// Cache configuration
49+
case REDIS_HOST_CLI:
50+
cache->host = value;
51+
break;
52+
case REDIS_PORT_CLI:
53+
cache->port = atoi(value);
54+
break;
55+
56+
// Tangle configuration
57+
case MILESTONE_DEPTH_CLI:
58+
tangle->milestone_depth = atoi(value);
59+
break;
60+
case MWM_CLI:
61+
tangle->mwm = atoi(value);
62+
break;
63+
case SEED_CLI:
64+
tangle->seed = value;
65+
break;
66+
}
67+
return SC_OK;
68+
}
69+
70+
status_t ta_config_default_init(ta_config_t* const info,
71+
iota_config_t* const tangle,
72+
ta_cache_t* const cache,
73+
iota_client_service_t* const service) {
1074
status_t ret = SC_OK;
11-
if (info == NULL || tangle == NULL || service == NULL) {
75+
if (info == NULL || tangle == NULL || cache == NULL || service == NULL) {
1276
return SC_TA_NULL;
1377
}
1478

@@ -22,6 +86,10 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle,
2286
info->port = TA_PORT;
2387
info->thread_count = TA_THREAD_COUNT;
2488

89+
log_info(logger_id, "Initializing Redis information\n");
90+
cache->host = REDIS_HOST;
91+
cache->port = REDIS_PORT;
92+
2593
log_info(logger_id, "Initializing IRI configuration\n");
2694
tangle->milestone_depth = MILESTONE_DEPTH;
2795
tangle->mss_depth = MSS_DEPTH;
@@ -36,6 +104,49 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle,
36104
service->http.port = IRI_PORT;
37105
service->http.api_version = 1;
38106
service->serializer_type = SR_JSON;
107+
return ret;
108+
}
109+
110+
status_t ta_config_cli_init(ta_core_t* const conf, int argc, char** argv) {
111+
int key = 0;
112+
status_t ret = SC_OK;
113+
struct option* long_options = cli_build_options();
114+
115+
while ((key = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) {
116+
switch (key) {
117+
case ':':
118+
ret = SC_CONF_MISSING_ARGUMENT;
119+
break;
120+
case '?':
121+
ret = SC_CONF_UNKNOWN_OPTION;
122+
break;
123+
case 'h':
124+
ta_usage();
125+
exit(EXIT_SUCCESS);
126+
case 'v':
127+
printf("%s\n", TA_VERSION);
128+
exit(EXIT_SUCCESS);
129+
default:
130+
ret = cli_config_set(&conf->info, &conf->tangle, &conf->cache,
131+
&conf->service, key, optarg);
132+
break;
133+
}
134+
if (ret != SC_OK) {
135+
break;
136+
}
137+
}
138+
139+
free(long_options);
140+
return ret;
141+
}
142+
143+
status_t ta_config_set(ta_cache_t* const cache,
144+
iota_client_service_t* const service) {
145+
status_t ret = SC_OK;
146+
if (cache == NULL || service == NULL) {
147+
return SC_TA_NULL;
148+
}
149+
39150
if (iota_client_core_init(service)) {
40151
log_critical(logger_id, "Initializing IRI connection failed!\n");
41152
ret = SC_TA_OOM;
@@ -46,8 +157,7 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle,
46157
pow_init();
47158

48159
log_info(logger_id, "Initializing cache connection\n");
49-
cache_init(REDIS_HOST, REDIS_PORT);
50-
160+
cache_init(cache->host, cache->port);
51161
return ret;
52162
}
53163

accelerator/config.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#ifndef ACCELERATOR_CONFIG_H_
22
#define ACCELERATOR_CONFIG_H_
33

4+
#include <getopt.h>
5+
46
#include "accelerator/errors.h"
7+
#include "accelerator/message.h"
58
#include "cclient/api/core/core_api.h"
69
#include "cclient/api/extended/extended_api.h"
710
#include "cclient/types/types.h"
@@ -53,9 +56,16 @@ typedef struct ta_config_s {
5356
const char* seed;
5457
} iota_config_t;
5558

59+
/** struct type of accelerator cache */
60+
typedef struct ta_cache_s {
61+
char* host; /**< Binding address of redis server */
62+
uint16_t port; /**< Binding port of redis server */
63+
} ta_cache_t;
64+
5665
/** struct type of accelerator core */
5766
typedef struct ta_core_s {
5867
ta_config_t info; /**< accelerator configiuration structure */
68+
ta_cache_t cache; /**< redis configiuration structure */
5969
iota_config_t tangle; /**< iota configuration structure */
6070
iota_client_service_t service; /**< iota connection structure */
6171
} ta_core_t;
@@ -66,14 +76,44 @@ typedef struct ta_core_s {
6676
*
6777
* @param info[in] Tangle-accelerator configuration variables
6878
* @param tangle[in] iota configuration variables
79+
* @param cache[in] redis configuration variables
80+
* @param service[in] IRI connection configuration variables
81+
*
82+
* @return
83+
* - SC_OK on success
84+
* - non-zero on error
85+
*/
86+
status_t ta_config_default_init(ta_config_t* const info,
87+
iota_config_t* const tangle,
88+
ta_cache_t* const cache,
89+
iota_client_service_t* const service);
90+
91+
/**
92+
* Initializes configurations with CLI values
93+
* Should be called third
94+
*
95+
* @param ta_conf[in] All configuration variables
96+
* @param argc[in] Number of argumentof CLI
97+
* @param argv[in] Argument of CLI
98+
*
99+
* @return
100+
* - SC_OK on success
101+
* - non-zero on error
102+
*/
103+
status_t ta_config_cli_init(ta_core_t* const ta_conf, int argc, char** argv);
104+
105+
/**
106+
* Start services after configurations are set
107+
*
108+
* @param cache[in] Redis server configuration variables
69109
* @param service[in] IRI connection configuration variables
70110
*
71111
* @return
72112
* - SC_OK on success
73113
* - non-zero on error
74114
*/
75-
status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle,
76-
iota_client_service_t* const service);
115+
status_t ta_config_set(ta_cache_t* const cache,
116+
iota_client_service_t* const service);
77117

78118
/**
79119
* Free memory of configuration variables

accelerator/errors.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern "C" {
4141
#define SC_MODULE_CACHE (0x04 << SC_MODULE_SHIFT)
4242
#define SC_MODULE_MAM (0x05 << SC_MODULE_SHIFT)
4343
#define SC_MODULE_RES (0x06 << SC_MODULE_SHIFT)
44+
#define SC_MODULE_CONF (0x07 << SC_MODULE_SHIFT)
4445
/** @} */
4546

4647
/** @name serverity code */
@@ -112,6 +113,14 @@ typedef enum {
112113
/**< Fail to create response object */
113114
SC_RES_NULL = 0x02 | SC_MODULE_RES | SC_SEVERITY_FATAL,
114115
/**< NULL object in response */
116+
117+
// configuration module
118+
SC_CONF_NULL = 0x02 | SC_MODULE_CONF | SC_SEVERITY_FATAL,
119+
/**< NULL object in response */
120+
SC_CONF_MISSING_ARGUMENT = 0x04 | SC_MODULE_CONF | SC_SEVERITY_FATAL,
121+
/**< No argument in CLI */
122+
SC_CONF_UNKNOWN_OPTION = 0x05 | SC_MODULE_CONF | SC_SEVERITY_FATAL,
123+
/**< undefined option in CLI */
115124
} status_t;
116125

117126
typedef enum {

accelerator/message.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ extern "C" {
66
#endif
77

88
typedef enum ta_cli_arg_value_e {
9-
HELP_CLI,
10-
119
/** TA */
12-
TA_HOST_CLI,
10+
TA_HOST_CLI = 127,
1311
TA_PORT_CLI,
1412
TA_THREAD_COUNT_CLI,
15-
TA_VERSION_CLI,
1613

1714
/** IRI */
1815
IRI_HOST_CLI,
@@ -35,12 +32,24 @@ typedef enum ta_cli_arg_requirement_e {
3532
} ta_cli_arg_requirement_t;
3633

3734
static struct ta_cli_argument_s {
38-
char* name;
35+
char const* name;
3936
int val;
40-
char* desc;
37+
char const* desc;
4138
ta_cli_arg_requirement_t has_arg;
4239
} ta_cli_arguments_g[] = {
43-
{"ta-help", HELP_CLI, "Show tangle-accelerator usage.", NO_ARG}};
40+
{"help", 'h', "Show tangle-accelerator usage", NO_ARG},
41+
{"version", 'v', "tangle-accelerator version", NO_ARG},
42+
{"ta_host", TA_HOST_CLI, "TA listening host", REQUIRED_ARG},
43+
{"ta_port", TA_PORT_CLI, "TA listening port", REQUIRED_ARG},
44+
{"ta_thread", TA_THREAD_COUNT_CLI, "TA executing thread", OPTIONAL_ARG},
45+
{"iri_host", IRI_HOST_CLI, "IRI listening host", REQUIRED_ARG},
46+
{"iri_port", IRI_PORT_CLI, "IRI listening port", REQUIRED_ARG},
47+
{"redis_host", REDIS_HOST_CLI, "Redis server listening host", REQUIRED_ARG},
48+
{"redis_port", REDIS_PORT_CLI, "Redis server listening port", REQUIRED_ARG},
49+
{"milestone_depth", MILESTONE_DEPTH_CLI, "IRI milestone depth",
50+
OPTIONAL_ARG},
51+
{"mwm", MWM_CLI, "minimum weight magnitude", OPTIONAL_ARG},
52+
{"seed", SEED_CLI, "IOTA seed", OPTIONAL_ARG}};
4453

4554
static const int cli_cmd_num =
4655
sizeof(ta_cli_arguments_g) / sizeof(struct ta_cli_argument_s);

accelerator/server.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ status_t set_response_content(status_t ret, char** json_result) {
5050
return http_ret;
5151
}
5252

53-
int main(int, char const**) {
53+
int main(int argc, char* argv[]) {
5454
served::multiplexer mux;
5555
mux.use_after(served::plugin::access_log);
5656

@@ -59,7 +59,22 @@ int main(int, char const**) {
5959
}
6060
logger_id = logger_helper_enable(MAIN_LOGGER_ID, LOGGER_DEBUG, true);
6161

62-
ta_config_init(&ta_core.info, &ta_core.tangle, &ta_core.service);
62+
// Initialize configurations with default value
63+
if (ta_config_default_init(&ta_core.info, &ta_core.tangle, &ta_core.cache,
64+
&ta_core.service) != SC_OK) {
65+
return EXIT_FAILURE;
66+
}
67+
68+
// Initialize configurations with CLI value
69+
if (ta_config_cli_init(&ta_core, argc, argv) != SC_OK) {
70+
return EXIT_FAILURE;
71+
}
72+
73+
if (ta_config_set(&ta_core.cache, &ta_core.service) != SC_OK) {
74+
log_critical(logger_id, "[%s:%d] Configure failed %s.\n", __func__,
75+
__LINE__, MAIN_LOGGER_ID);
76+
return EXIT_FAILURE;
77+
}
6378

6479
mux.handle("/mam/{bundle:[A-Z9]{81}}")
6580
.method(served::method::OPTIONS,

tests/driver.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ void test_receive_mam_message(void) {
182182
int main(void) {
183183
UNITY_BEGIN();
184184

185-
ta_config_init(&ta_core.info, &ta_core.tangle, &ta_core.service);
185+
ta_config_default_init(&ta_core.info, &ta_core.tangle, &ta_core.cache,
186+
&ta_core.service);
187+
ta_config_set(&ta_core.cache, &ta_core.service);
186188

187189
printf("Total samples for each API test: %d\n", TEST_COUNT);
188190
RUN_TEST(test_generate_address);

0 commit comments

Comments
 (0)