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

Commit

Permalink
feat(api): Add API to dump tangle accelerator info
Browse files Browse the repository at this point in the history
  • Loading branch information
yiwei01 committed Aug 23, 2019
1 parent 902478f commit e136420
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
13 changes: 13 additions & 0 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ int apis_logger_release() {
return 0;
}

status_t api_get_ta_info(char** json_result, ta_config_t* const info, iota_config_t* const tangle,
ta_cache_t* const cache, iota_client_service_t* const service) {
status_t ret = SC_OK;

ret = ta_get_info_serialize(json_result, info, tangle, cache, service);
if (ret != SC_OK) {
ret = SC_TA_OOM;
log_error(apis_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_TA_OOM");
}

return ret;
}

status_t api_get_tips(const iota_client_service_t* const service, char** json_result) {
status_t ret = SC_OK;
get_tips_res_t* res = get_tips_res_new();
Expand Down
16 changes: 16 additions & 0 deletions accelerator/apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ void apis_logger_init();
*/
int apis_logger_release();

/**
* @brief Dump tangle accelerator information.
*
* @param[out] json_result Result containing tangle accelerator information in json format
* @param info[in] Tangle-accelerator configuration variables
* @param tangle[in] iota configuration variables
* @param cache[in] redis configuration variables
* @param service[in] IRI connection configuration variables
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t api_get_ta_info(char** json_result, ta_config_t* const info, iota_config_t* const tangle,
ta_cache_t* const cache, iota_client_service_t* const service);

/**
* @brief Generate an unused address.
*
Expand Down
19 changes: 19 additions & 0 deletions accelerator/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,25 @@ int main(int argc, char* argv[]) {
cJSON_Delete(json_obj);
});

/**
* @method {get} / Dump information about a running accelerator
*
* @return {String[]} object Info of a running accelerator
*/
mux.handle("/")
.method(served::method::OPTIONS,
[&](served::response& res, const served::request& req) { set_method_header(res, HTTP_METHOD_OPTIONS); })
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result = NULL;

ret = api_get_ta_info(&json_result, &ta_core.info, &ta_core.tangle, &ta_core.cache, &ta_core.service);

set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
res << json_result;
});

std::cout << "Starting..." << std::endl;
served::net::server server(ta_core.info.host, ta_core.info.port, mux);
server.run(ta_core.info.thread_count);
Expand Down
1 change: 1 addition & 0 deletions serializer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cc_library(
copts = ["-DLOGGER_ENABLE"],
visibility = ["//visibility:public"],
deps = [
"//accelerator:ta_config",
"//accelerator:ta_errors",
"//request",
"//response",
Expand Down
32 changes: 32 additions & 0 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ int serializer_logger_release() {
return 0;
}

status_t ta_get_info_serialize(char** obj, ta_config_t* const info, iota_config_t* const tangle,
ta_cache_t* const cache, iota_client_service_t* const service) {
status_t ret = SC_OK;
cJSON* json_root = cJSON_CreateObject();
if (json_root == NULL) {
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
return SC_SERIALIZER_JSON_CREATE;
}

cJSON_AddStringToObject(json_root, "name", "tangle-accelerator");
cJSON_AddStringToObject(json_root, "host", info->host);
cJSON_AddStringToObject(json_root, "version", info->version);
cJSON_AddStringToObject(json_root, "port", info->port);
cJSON_AddNumberToObject(json_root, "thread", info->thread_count);
cJSON_AddStringToObject(json_root, "iri_host", service->http.host);
cJSON_AddNumberToObject(json_root, "iri_port", service->http.port);
cJSON_AddStringToObject(json_root, "redis_host", cache->host);
cJSON_AddNumberToObject(json_root, "redis_port", cache->port);
cJSON_AddNumberToObject(json_root, "milestone_depth", tangle->milestone_depth);
cJSON_AddNumberToObject(json_root, "mwm", tangle->mwm);
cJSON_AddBoolToObject(json_root, "verbose", verbose_mode);

*obj = cJSON_PrintUnformatted(json_root);
if (*obj == NULL) {
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_PARSE");
ret = SC_SERIALIZER_JSON_PARSE;
}

cJSON_Delete(json_root);
return ret;
}

static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack, cJSON* json_root) {
size_t array_count = 0;
hash243_stack_entry_t* s_iter = NULL;
Expand Down
17 changes: 17 additions & 0 deletions serializer/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef SERIALIZER_SERIALIZER_H_
#define SERIALIZER_SERIALIZER_H_

#include "accelerator/config.h"
#include "cJSON.h"
#include "cclient/response/responses.h"
#include "common/trinary/tryte_ascii.h"
Expand Down Expand Up @@ -56,6 +57,22 @@ void serializer_logger_init();
*/
int serializer_logger_release();

/**
* @brief Serialze tangle accelerator info into JSON
*
* @param[out] obj Tangle-accelerator info in JSON
* @param[in] info Tangle-accelerator configuration variables
* @param[in] tangle IOTA configuration variables
* @param[in] cache Redis configuration variables
* @param[in] service IRI connection configuration variables
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t ta_get_info_serialize(char** obj, ta_config_t* const info, iota_config_t* const tangle,
ta_cache_t* const cache, iota_client_service_t* const service);

/**
* @brief Serialze type of ta_generate_address_res_t to JSON string
*
Expand Down

0 comments on commit e136420

Please sign in to comment.