diff options
| author | 童宗振 <[email protected]> | 2024-04-19 04:33:01 +0000 |
|---|---|---|
| committer | 童宗振 <[email protected]> | 2024-04-19 04:33:01 +0000 |
| commit | cb9760c17b16835f0d3f4404c8c767b676faa7e9 (patch) | |
| tree | 520d0ed37cb43dbfd53598493102f1834ab54a6d | |
| parent | f23ca6048de819961182e15ce8e0fd8de0b9cfe3 (diff) | |
| parent | d2ca0568ef45016f45eec16f788dcfa1d0bcca52 (diff) | |
Merge branch 'add_keep_alive_port' into 'master'
Add keep alive port
See merge request tsg/dp_telemetry_app!17
| -rw-r--r-- | etc/dp_trace.conf | 5 | ||||
| -rw-r--r-- | include/config.h | 5 | ||||
| -rw-r--r-- | include/http_serv.h | 5 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/config.c | 10 | ||||
| -rw-r--r-- | src/http_serv.c | 98 | ||||
| -rw-r--r-- | src/main.c | 3 | ||||
| -rw-r--r-- | test/CMakeLists.txt | 1 |
8 files changed, 127 insertions, 1 deletions
diff --git a/etc/dp_trace.conf b/etc/dp_trace.conf index 9fad227..6e96c00 100644 --- a/etc/dp_trace.conf +++ b/etc/dp_trace.conf @@ -5,6 +5,11 @@ dp_trace_dir=./ device_group= monit_file_path=/var/run/mrzcpd/mrmonit.app.dp_trace_telemetry.saving +[http_server] +listen_addr=127.0.0.1 +listen_port=10000 +keep_alive_path=/probe + [kafka] borker_list="192.168.44.12" topic_name="datapath-telemetry-record-test" diff --git a/include/config.h b/include/config.h index 7bd942c..967d337 100644 --- a/include/config.h +++ b/include/config.h @@ -23,6 +23,11 @@ struct config char * sled_ip; char device_group[MR_SYMBOL_MAX]; + // server + char str_listen_addr[INET6_ADDRSTRLEN]; + unsigned int listen_port; + char keep_alive_path[MR_SYMBOL_MAX]; + // kafka char topic_name[MR_SYMBOL_MAX]; char broker_list[1024]; diff --git a/include/http_serv.h b/include/http_serv.h new file mode 100644 index 0000000..918d1c1 --- /dev/null +++ b/include/http_serv.h @@ -0,0 +1,5 @@ +#pragma once +#include "common.h" + +int http_serv_init(); +int http_serv_deinit();
\ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 645d2dc..f9069dd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ set(DP_TELEMETRY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/maat.c ${CMAKE_CURRENT_SOURCE_DIR}/monit.c ${CMAKE_CURRENT_SOURCE_DIR}/mocking.c + ${CMAKE_CURRENT_SOURCE_DIR}/http_serv.c ${CMAKE_SOURCE_DIR}/support/mpack/mpack.c) add_executable(${PROJECT_NAME} ${DP_TELEMETRY_SRC}) diff --git a/src/config.c b/src/config.c index 6f970a2..0f89db1 100644 --- a/src/config.c +++ b/src/config.c @@ -76,9 +76,17 @@ void config_load() sizeof(g_conf->monit_file_path), "/var/run/mrzcpd/mrmonit.app.dp_trace_telemetry.saving"); - MESA_load_profile_string_def(g_conf->device_group, "global", "device_group", g_conf->device_group, + MESA_load_profile_string_def(config_path, "global", "device_group", g_conf->device_group, sizeof(g_conf->device_group), "unknow"); + MESA_load_profile_string_def(config_path, "http_server", "listen_addr", g_conf->str_listen_addr, + sizeof(g_conf->str_listen_addr), "127.0.0.1"); + + MESA_load_profile_uint_def(config_path, "http_server", "listen_port", &g_conf->listen_port, 10000); + + MESA_load_profile_string_def(config_path, "http_server", "keep_alive_path", g_conf->keep_alive_path, + sizeof(g_conf->keep_alive_path), "/probe"); + g_conf->sled_ip = getenv("SLED_IP"); if (g_conf->sled_ip == NULL) { diff --git a/src/http_serv.c b/src/http_serv.c new file mode 100644 index 0000000..c9a3234 --- /dev/null +++ b/src/http_serv.c @@ -0,0 +1,98 @@ +#include "http_serv.h" +#include "config.h" + +#include <MESA_prof_load.h> +#include <assert.h> +#include <event2/buffer.h> +#include <event2/event.h> +#include <event2/http.h> +#include <pthread.h> +#include <stdio.h> + +struct http_serv_main * http_server_main = NULL; + +struct http_serv_main +{ + struct event_base * ev_base; + struct evhttp * ev_http; + pthread_t ev_dispatch_tid; +}; + +static void http_serv_probe_cb(struct evhttp_request * req, void * arg) +{ + struct evbuffer * evb = evbuffer_new(); + if (!evb) + { + dzlog_error("failed to create response buffer"); + return; + } + + evbuffer_add_printf(evb, "Probe successfully, dp_trace_telemetry is running."); + evhttp_send_reply(req, HTTP_OK, "OK", evb); + evbuffer_free(evb); +} + +/* event_base dispatch thread */ +static void * http_serv_dispatch(void * arg) +{ + struct event_base * base = (struct event_base *)arg; + event_base_dispatch(base); + return NULL; +} + +int http_serv_deinit() +{ + assert(http_server_main != NULL); + + /* stop event_base dispatch thread */ + event_base_loopexit(http_server_main->ev_base, NULL); + pthread_join(http_server_main->ev_dispatch_tid, NULL); + + /* release all ctx */ + evhttp_free(http_server_main->ev_http); + event_base_free(http_server_main->ev_base); + + /* free http_serv_main */ + free(http_server_main); + return 0; +} + +int http_serv_init() +{ + int ret = 0; + http_server_main = calloc(1, sizeof(struct http_serv_main)); + DP_TRACE_VERIFY(http_server_main, "http_server_main calloc failed"); + + struct event_config * cfg = event_config_new(); + struct event_base * base = event_base_new_with_config(cfg); + DP_TRACE_VERIFY(base, "Couldn't create an event_base."); + + event_config_free(cfg); + cfg = NULL; + + /* Create a new evhttp object to socket requests. */ + struct evhttp * http = evhttp_new(base); + DP_TRACE_VERIFY(http, "couldn't create evhttp. Exiting."); + + const struct config * conf = global_config_get(); + + evhttp_set_cb(http, conf->keep_alive_path, http_serv_probe_cb, (void *)http_server_main); + + struct evhttp_bound_socket * socket = + evhttp_bind_socket_with_handle(http, conf->str_listen_addr, conf->listen_port); + DP_TRACE_VERIFY(socket, "couldn't bind to %s:%d.", conf->str_listen_addr, conf->listen_port); + + /* create a thread to do the event_base dispatch */ + pthread_t tid; + ret = pthread_create(&tid, NULL, http_serv_dispatch, base); + DP_TRACE_VERIFY(ret == 0, "failed to create thread for monit_loop.return value:%d", ret); + + /* save the event_base, ev_http handles to http_serv_main */ + http_server_main->ev_base = base; + http_server_main->ev_http = http; + http_server_main->ev_dispatch_tid = tid; + + dzlog_info("HTTP Server is listening on %s:%u", conf->str_listen_addr, conf->listen_port); + dzlog_info("keep alive path:%s", conf->keep_alive_path); + return 0; +} @@ -1,5 +1,6 @@ #include "common.h" #include "config.h" +#include "http_serv.h" #include "maat.h" #include "monit.h" #include "trace_output.h" @@ -165,6 +166,8 @@ int main(int argc, char * argv[]) pthread_create(&tmp_pid[i], NULL, dp_trace_process_thread, (void *)(uintptr_t)i); } + http_serv_init(); + for (int i = 0; i < nr_thread; i++) { pthread_join(tmp_pid[i], NULL); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1a6b3bd..a924ffc 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,6 +8,7 @@ set(DP_TRACE_TELEMETRY_SOURCES ${CMAKE_SOURCE_DIR}/src/kafka.c ${CMAKE_SOURCE_DIR}/src/mocking.c ${CMAKE_SOURCE_DIR}/src/monit.c + ${CMAKE_SOURCE_DIR}/src/http_serv.c ${CMAKE_SOURCE_DIR}/support/mpack/mpack.c) add_executable(cmocka_test cmocka_test.c ${DP_TRACE_TELEMETRY_SOURCES}) |
