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

Commit

Permalink
feat: Runtime commmand line interface
Browse files Browse the repository at this point in the history
Closes #357, closes #589.

- Moved logger initialize/release declaration to `common/logger.h` to have
better control of logger functions (we can know what logger could be
enabled by inspecting `logger.h`).

- Wrapped the logger initialize/release functions so we can
enable/disable verbose mode at ease in runtime-cli utility.

- Implement runtime command line utility

The runtime command line utility is a separated from the main thread.
The commands are maintained by several states defined in
`runtime_cli.c`. We also have a static global pointer indicating which
state we are at now.

Take the states in `runtime_cli.c` for example, we are at `start_state`
in the beginning. Everytime we need to `handle_commands`, we will
extract one more string (separated by spaces), and find if the command
names in current state matches the string.

Upon matching, we will then determine if current command needs more
subcommand. If so, the command type would be `CMD_STATE` and
`ta_state_ptr` would be assigned to `next_state`.

On the other hand, if current command reaches the end and is expecting
zero or more parameters, the type would be `CMD_LAST`, and will execute
the specified function `fn`.

If no command names match, the program will simply print help messages
according to the current state `ta_state_ptr` points to.

Runtime-cli can be enabled by adding `--runtime_cli` argument.
  • Loading branch information
afcidk committed Jul 17, 2020
1 parent 8ef4605 commit c5f3581
Show file tree
Hide file tree
Showing 42 changed files with 783 additions and 347 deletions.
15 changes: 15 additions & 0 deletions accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cc_binary(
}),
deps = [
":ta_config",
":ta_cli",
"//connectivity/http",
"@org_iota_common//utils/handles:signal",
] + select({
Expand All @@ -30,6 +31,19 @@ cc_binary(
}),
)

cc_library(
name = "ta_cli",
srcs = ["runtime_cli.c"],
hdrs = ["runtime_cli.h"],
visibility = ["//visibility:public"],
deps = [
":ta_config",
"//common:ta_logger",
"//utils/cache",
"@org_iota_common//utils:macros",
],
)

cc_library(
name = "ta_config",
srcs = ["config.c"],
Expand All @@ -39,6 +53,7 @@ cc_library(
":cli_info",
"//accelerator/core:pow",
"//common",
"//utils:timer",
":build_option",
"//utils/cache",
"//utils:cpuinfo",
Expand Down
4 changes: 3 additions & 1 deletion accelerator/cli_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ extern "C" {

/**
* @file accelerator/cli_info.h
* @brief Message and options for tangled-accelerator configures
* @brief Message and options for tangle-accelerator configuration
*/

typedef enum ta_cli_arg_value_e {
/** tangle-accelerator */
TA_HOST_CLI = 127,
TA_PORT_CLI,
RUNTIME_CLI,

/** IOTA full node */
NODE_HOST_CLI,
Expand Down Expand Up @@ -97,6 +98,7 @@ static struct ta_cli_argument_s {
{"done_list", required_argument, NULL, DONE_LIST, "Set the value of `done_list_name`"},
{"cache_capacity", required_argument, NULL, CACHE_CAPACITY, "Set the maximum capacity of caching server"},
{"quiet", no_argument, NULL, QUIET, "Disable logger"},
{"runtime_cli", no_argument, NULL, RUNTIME_CLI, "Enable runtime command line"},
{NULL, 0, NULL, 0, NULL}};

static const int cli_cmd_num = ARRAY_SIZE(ta_cli_arguments_g);
Expand Down
97 changes: 80 additions & 17 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,22 @@ static char* read_ca_pem(char* ca_pem_path) {
}

status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
if (value == NULL && (key != CACHE && key != PROXY_API && key != QUIET && key != NO_GTTA)) {
ta_log_error("%s\n", "SC_NULL");
return SC_NULL;
if (value == NULL) {
switch (key) {
/* No argument */
case CACHE:
case PROXY_API:
case QUIET:
case NO_GTTA:
case RUNTIME_CLI:
break;
/* Need argument but no value found */
default:
ta_log_error("%s\n", "SC_NULL");
return SC_NULL;
}
}

ta_config_t* const ta_conf = &core->ta_conf;
iota_config_t* const iota_conf = &core->iota_conf;
ta_cache_t* const cache = &core->cache;
Expand Down Expand Up @@ -250,22 +262,25 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
case SEED_CLI:
iota_conf->seed = value;
break;
case BUFFER_LIST:
cache->buffer_list_name = value;
break;
case DONE_LIST:
cache->done_list_name = value;
break;

// Quiet mode configuration
// Command line options configuration
case QUIET:
quiet_mode = true;
ta_conf->cli_options |= CLI_QUIET_MODE;
break;
case PROXY_API:
ta_conf->proxy_passthrough = true;
ta_conf->cli_options |= CLI_PROXY_PASSTHROUGH;
break;
case NO_GTTA:
ta_conf->gtta = false;
ta_conf->cli_options |= CLI_GTTA;
break;
case BUFFER_LIST:
cache->buffer_list_name = value;
break;
case DONE_LIST:
cache->done_list_name = value;
case RUNTIME_CLI:
ta_conf->cli_options |= CLI_RUNTIME_CLI;
break;

// File configuration
Expand Down Expand Up @@ -318,9 +333,7 @@ status_t ta_core_default_init(ta_core_t* const core) {
ta_conf->iota_port_list[i] = NODE_PORT;
}
ta_conf->http_tpool_size = DEFAULT_HTTP_TPOOL_SIZE;
ta_conf->proxy_passthrough = false;
ta_conf->health_track_period = HEALTH_TRACK_PERIOD;
ta_conf->gtta = true;
#ifdef MQTT_ENABLE
ta_conf->mqtt_host = MQTT_HOST;
ta_conf->mqtt_topic_root = TOPIC_ROOT;
Expand Down Expand Up @@ -354,8 +367,13 @@ status_t ta_core_default_init(ta_core_t* const core) {
ta_log_info("Initializing DB connection\n");
db_service->host = strdup(DB_HOST);
#endif
// Turn off quiet mode default
quiet_mode = false;
// Command line options set default to 0
ta_conf->cli_options &= ~CLI_QUIET_MODE;
ta_conf->cli_options &= ~CLI_RUNTIME_CLI;
ta_conf->cli_options &= ~CLI_PROXY_PASSTHROUGH;

// Command line options set default to 1
ta_conf->cli_options |= CLI_GTTA;

return ret;
}
Expand Down Expand Up @@ -544,7 +562,52 @@ void ta_core_destroy(ta_core_t* const core) {
logger_destroy_client_core();
logger_destroy_client_extended();
logger_destroy_json_serializer();
br_logger_release();
}

bool is_option_enabled(const ta_config_t* const ta_config, uint32_t option) {
return (bool)(ta_config->cli_options & option);
}

void ta_logger_switch(bool quiet, bool init, ta_config_t* ta_conf) {
if (quiet != is_option_enabled(ta_conf, CLI_QUIET_MODE) || init) {
if (quiet == false) {
#ifdef MQTT_ENABLE
mqtt_utils_logger_init();
mqtt_common_logger_init();
mqtt_callback_logger_init();
mqtt_pub_logger_init();
mqtt_sub_logger_init();
#else
http_logger_init();
#endif
conn_logger_init();
apis_logger_init();
cc_logger_init();
serializer_logger_init();
pow_logger_init();
timer_logger_init();
br_logger_init();
ta_conf->cli_options &= ~CLI_QUIET_MODE;
} else {
#ifdef MQTT_ENABLE
mqtt_utils_logger_release();
mqtt_common_logger_release();
mqtt_callback_logger_release();
mqtt_pub_logger_release();
mqtt_sub_logger_release();
#else
http_logger_release();
#endif
conn_logger_release();
apis_logger_release();
cc_logger_release();
serializer_logger_release();
pow_logger_release();
timer_logger_release();
br_logger_release();
ta_conf->cli_options |= CLI_QUIET_MODE;
}
}
}

#define DOMAIN_SOCKET "/tmp/tangle-accelerator-socket"
Expand Down
32 changes: 30 additions & 2 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ typedef struct ta_config_s {
char* mqtt_topic_root; /**< The topic root of MQTT topic */
#endif
uint8_t http_tpool_size; /**< Thread count of tangle-accelerator instance */
bool proxy_passthrough; /**< Pass proxy api directly without processing */
bool gtta; /**< The option to turn on or off GTTA. The default value is true which enabling GTTA */
uint32_t cli_options; /**< Command line options */
} ta_config_t;

/** Command line options */
enum {
CLI_PROXY_PASSTHROUGH = 1 << 0, /**< Pass proxy api directly without processing */
CLI_GTTA = 1 << 1, /**< The option to turn on or off GTTA. The default value is true which enabling GTTA */
CLI_RUNTIME_CLI = 1 << 2, /**< The option to turn on or off runtime command line functionality. False by default */
CLI_QUIET_MODE = 1 << 3, /**< The option to turn on or off quiet mode. False by default */
};

/** struct type of iota configuration */
typedef struct iota_config_s {
uint8_t milestone_depth; /**< Depth of API argument */
Expand Down Expand Up @@ -209,6 +216,15 @@ status_t ta_core_set(ta_core_t* const core);
*/
void ta_core_destroy(ta_core_t* const core);

/**
* @brief Switch logger verbose level between quiet level and verbose level
*
* @param[in] quiet Switch to quiet mode or not
* @param[in] init Initialization or not
* @param[in] conf Tangle-accelerator configuration variables
*/
void ta_logger_switch(bool quiet, bool init, ta_config_t* conf);

/**
* @brief Initializes iota_client_service
*
Expand All @@ -229,6 +245,18 @@ status_t ta_set_iota_client_service(iota_client_service_t* service, char const*
*/
void notification_trigger();

/**
* @brief Check whether a command line option is enabled or not
*
* @param[in] ta_config Tangle-accelerator configuration variables
* @param[in] option The option to be checked
*
* @return
* - true if the option is enabled
* - false if the option is not enabled
*/
bool is_option_enabled(const ta_config_t* const ta_config, uint32_t option);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions accelerator/core/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ void apis_logger_init() { logger_id = logger_helper_enable(APIS_LOGGER, LOGGER_D

int apis_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", APIS_LOGGER);
return EXIT_FAILURE;
}

return 0;
}

Expand Down
14 changes: 0 additions & 14 deletions accelerator/core/apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ extern "C" {
* different server or protocol integration with these APIs.
*/

/**
* Initialize logger
*/
void apis_logger_init();

/**
* Release logger
*
* @return
* - zero on success
* - EXIT_FAILURE on error
*/
int apis_logger_release();

/**
* @brief Dump tangle accelerator information.
*
Expand Down
7 changes: 1 addition & 6 deletions accelerator/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ void cc_logger_init() { logger_id = logger_helper_enable(CC_LOGGER, LOGGER_DEBUG

int cc_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", CC_LOGGER);
return EXIT_FAILURE;
}

return 0;
}

Expand Down Expand Up @@ -76,7 +71,7 @@ status_t ta_send_trytes(const ta_config_t* const info, const iota_config_t* cons
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
}
if (info->gtta) {
if (is_option_enabled(info, CLI_GTTA)) {
get_transactions_to_approve_req_set_depth(tx_approve_req, iconf->milestone_depth);
if (iota_client_get_transactions_to_approve(service, tx_approve_req, tx_approve_res)) {
ret = SC_CCLIENT_FAILED_RESPONSE;
Expand Down
14 changes: 0 additions & 14 deletions accelerator/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ extern "C" {
*
*/

/**
* Initialize logger
*/
void cc_logger_init();

/**
* Release logger
*
* @return
* - zero on success
* - EXIT_FAILURE on error
*/
int cc_logger_release();

/**
* @brief Send transfer to tangle.
*
Expand Down
5 changes: 0 additions & 5 deletions accelerator/core/mam_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ void ta_mam_logger_init() { logger_id = logger_helper_enable(MAM_LOGGER, LOGGER_

int ta_mam_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", MAM_LOGGER);
return EXIT_FAILURE;
}

return 0;
}

Expand Down
5 changes: 0 additions & 5 deletions accelerator/core/pow.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ void pow_logger_init() { logger_id = logger_helper_enable(POW_LOGGER, LOGGER_DEB

int pow_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", POW_LOGGER);
return EXIT_FAILURE;
}

return 0;
}

Expand Down
14 changes: 0 additions & 14 deletions accelerator/core/pow.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ extern "C" {
* @example unit-test/test_pow.c
*/

/**
* Initialize logger
*/
void pow_logger_init();

/**
* Release logger
*
* @return
* - zero on success
* - EXIT_FAILURE on error
*/
int pow_logger_release();

/**
* Initiate pow module
*/
Expand Down
7 changes: 1 addition & 6 deletions accelerator/core/proxy_apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ void proxy_apis_logger_init() { logger_id = logger_helper_enable(PROXY_APIS_LOGG

int proxy_apis_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", PROXY_APIS_LOGGER);
return EXIT_FAILURE;
}

return 0;
}

Expand Down Expand Up @@ -253,7 +248,7 @@ static status_t api_get_trytes(const iota_client_service_t* const service, const
status_t proxy_api_wrapper(const ta_config_t* const iconf, const iota_client_service_t* const service,
const char* const obj, char** json_result) {
status_t ret = SC_OK;
if (iconf->proxy_passthrough) {
if (is_option_enabled(iconf, CLI_PROXY_PASSTHROUGH)) {
char_buffer_t* res_buff = char_buffer_new();
char_buffer_t* req_buff = char_buffer_new();
char_buffer_set(req_buff, obj);
Expand Down
Loading

0 comments on commit c5f3581

Please sign in to comment.