diff options
| author | yangwei <[email protected]> | 2024-09-25 18:18:02 +0800 |
|---|---|---|
| committer | yangwei <[email protected]> | 2024-09-25 18:18:02 +0800 |
| commit | 74f77f34112170d081868203441e71b2c78a5452 (patch) | |
| tree | 7134993b9f9013d620777e03f8727648c79e404b | |
| parent | 7291db59693c271f582cc5428306d74a007cb4b9 (diff) | |
✨ feat(polling manager): support polling manager
| -rw-r--r-- | include/stellar/module_manager.h | 6 | ||||
| -rw-r--r-- | include/stellar/polling_manager.h | 21 | ||||
| -rw-r--r-- | infra/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | infra/module_manager/module_manager.c | 38 | ||||
| -rw-r--r-- | infra/module_manager/module_manager_interna.h | 1 | ||||
| -rw-r--r-- | infra/module_manager/test/gtest_module_manager_main.cpp | 55 | ||||
| -rw-r--r-- | infra/polling_manager/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | infra/polling_manager/polling_manager.c | 70 | ||||
| -rw-r--r-- | infra/polling_manager/polling_manager_internal.h | 22 | ||||
| -rw-r--r-- | infra/polling_manager/test/CMakeLists.txt | 18 | ||||
| -rw-r--r-- | infra/polling_manager/test/gtest_polling_manager_main.cpp | 85 |
11 files changed, 224 insertions, 100 deletions
diff --git a/include/stellar/module_manager.h b/include/stellar/module_manager.h index 7d99bec..283e948 100644 --- a/include/stellar/module_manager.h +++ b/include/stellar/module_manager.h @@ -37,11 +37,7 @@ int stellar_module_manager_get_max_thread_num(struct stellar_module_manager* mod const char *stellar_module_manager_get_toml_path(struct stellar_module_manager *mod_mgr); struct mq_schema *stellar_module_manager_get_mq_schema(struct stellar_module_manager *mod_mgr); -typedef void module_on_polling_func(struct stellar_module_manager* mod_mgr, void *polling_arg); -//return 0 if success, otherwise return -1. -int stellar_module_manager_polling_subscribe(struct stellar_module_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg); -void stellar_module_manager_polling_dispatch(struct stellar_module_manager *mod_mgr); -void stellar_module_manager_polling_active(struct stellar_module_manager *mod_mgr); + #ifdef __cplusplus } diff --git a/include/stellar/polling_manager.h b/include/stellar/polling_manager.h new file mode 100644 index 0000000..641c03e --- /dev/null +++ b/include/stellar/polling_manager.h @@ -0,0 +1,21 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "stellar/module_manager.h" + +struct stellar_polling_manager; + +struct stellar_polling_manager *stellar_module_get_polling_manager(struct stellar_module_manager *mod_mgr); + +typedef void module_on_polling_func(struct stellar_polling_manager* mod_mgr, void *polling_arg); +//return 0 if success, otherwise return -1. +int stellar_polling_subscribe(struct stellar_polling_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg); +void stellar_polling_active(struct stellar_polling_manager *mod_mgr); + +#ifdef __cplusplus +} +#endif
\ No newline at end of file diff --git a/infra/CMakeLists.txt b/infra/CMakeLists.txt index 0ad1bb2..008713e 100644 --- a/infra/CMakeLists.txt +++ b/infra/CMakeLists.txt @@ -1,4 +1,4 @@ -set(INFRA exdata mq tuple packet_manager packet_io ip_reassembly tcp_reassembly session_manager module_manager) +set(INFRA exdata mq tuple packet_manager packet_io ip_reassembly tcp_reassembly session_manager module_manager polling_manager) set(DEPS bitmap dablooms interval_tree logger nmx_pool rbtree timeout toml) #set(DECODERS http lpi) set(WHOLE_ARCHIVE ${DEPS} ${INFRA} ${DECODERS}) diff --git a/infra/module_manager/module_manager.c b/infra/module_manager/module_manager.c index 38e2135..471564b 100644 --- a/infra/module_manager/module_manager.c +++ b/infra/module_manager/module_manager.c @@ -237,42 +237,4 @@ void stellar_module_set_name(struct stellar_module* mod, const char *name) memcpy(mod->name, name, MIN(NAME_MAX, strlen(name))); return; } -#define TOPIC_POLLING "POLLING" -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-function-type" -static void stellar_module_manager_on_polling_dispatch(int topic_id __unused, - void *msg __unused, - on_msg_cb_func* on_msg_cb, - void *on_msg_cb_arg, - void *dispatch_arg) -{ - struct stellar_module_manager *mod_mgr=(struct stellar_module_manager *)dispatch_arg; - module_on_polling_func *polling = (module_on_polling_func *)on_msg_cb; - polling(mod_mgr, on_msg_cb_arg); -} - -int stellar_module_manager_polling_subscribe(struct stellar_module_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg) -{ - mod_mgr->schema.polling_topic_id=mq_schema_get_topic_id(mod_mgr->schema.mq_schema, TOPIC_POLLING); - if(mod_mgr->schema.polling_topic_id<0) - { - mod_mgr->schema.polling_topic_id=mq_schema_create_topic(mod_mgr->schema.mq_schema, TOPIC_POLLING, stellar_module_manager_on_polling_dispatch, mod_mgr, NULL, NULL); - } - return mq_schema_subscribe(mod_mgr->schema.mq_schema, mod_mgr->schema.polling_topic_id, (on_msg_cb_func *)on_polling, polling_arg); -} - -#pragma GCC diagnostic pop - -void stellar_module_manager_polling_active(struct stellar_module_manager *mod_mgr) -{ - mq_runtime_publish_message(local_mq_rt, mod_mgr->schema.polling_topic_id, NULL); -} - -void stellar_module_manager_polling_dispatch(struct stellar_module_manager *mod_mgr) -{ - stellar_module_manager_polling_active(mod_mgr); - mq_runtime_dispatch(local_mq_rt); - mq_runtime_clean(local_mq_rt); - return; -} diff --git a/infra/module_manager/module_manager_interna.h b/infra/module_manager/module_manager_interna.h index 5d571a6..7d28b50 100644 --- a/infra/module_manager/module_manager_interna.h +++ b/infra/module_manager/module_manager_interna.h @@ -40,7 +40,6 @@ struct stellar_module_manager int load_module_num; int max_thread_num; struct mq_schema *mq_schema; - int polling_topic_id; }schema; }__attribute__((aligned(sizeof(void*)))); diff --git a/infra/module_manager/test/gtest_module_manager_main.cpp b/infra/module_manager/test/gtest_module_manager_main.cpp index e46037b..74aa611 100644 --- a/infra/module_manager/test/gtest_module_manager_main.cpp +++ b/infra/module_manager/test/gtest_module_manager_main.cpp @@ -200,61 +200,6 @@ TEST(module_manager, basic_module) { } -/*********************************** - * TEST MODULE MANAGER POLLING API * - ***********************************/ - -struct test_module_polling_env -{ - int N_round; - int polling_count; - int polling_active_count; -}; - - void test_module_on_polling(struct stellar_module_manager* mod_mgr, void *polling_arg) - { - struct test_module_polling_env *env = (struct test_module_polling_env*)polling_arg; - env->polling_count++; - if(env->polling_count%2==0) - { - stellar_module_manager_polling_active(mod_mgr); - env->polling_active_count++; - } - } - -TEST(module_manager_polling, basic_polling_module) { - - struct mq_schema *mq_schema=mq_schema_new(); - - struct stellar_module_manager *mod_mgr=stellar_module_manager_new(NULL, 10, mq_schema); - EXPECT_TRUE(mod_mgr!=NULL); - - - EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10); - EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema); - - struct test_module_polling_env env={}; - env.N_round=10; - - stellar_module_manager_polling_subscribe(mod_mgr, test_module_on_polling, &env); - - struct mq_runtime *mq_rt = mq_runtime_new(mq_schema); - stellar_module_manager_register_thread(mod_mgr, 1, mq_rt); - - EXPECT_EQ((long)stellar_module_manager_get_thread_id(mod_mgr), 1); - EXPECT_EQ(stellar_module_manager_get_mq_runtime(mod_mgr), mq_rt); - - for(int i=0; i<env.N_round; i++) - { - stellar_module_manager_polling_dispatch(mod_mgr); - } - - stellar_module_manager_free(mod_mgr); - - EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count); - -} - /********************************************** * GTEST MAIN * **********************************************/ diff --git a/infra/polling_manager/CMakeLists.txt b/infra/polling_manager/CMakeLists.txt new file mode 100644 index 0000000..a85368b --- /dev/null +++ b/infra/polling_manager/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(polling_manager polling_manager.c) +target_include_directories(polling_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +target_include_directories(polling_manager PUBLIC ${CMAKE_SOURCE_DIR}/include/) +target_link_libraries(polling_manager PUBLIC module_manager ${CMAKE_DL_LIBS}) + +add_subdirectory(test)
\ No newline at end of file diff --git a/infra/polling_manager/polling_manager.c b/infra/polling_manager/polling_manager.c new file mode 100644 index 0000000..5d9013a --- /dev/null +++ b/infra/polling_manager/polling_manager.c @@ -0,0 +1,70 @@ +#include "polling_manager_internal.h" + +#include "stellar/utils.h" + + +struct stellar_polling_manager *stellar_module_get_polling_manager(struct stellar_module_manager *mod_mgr) +{ + if(mod_mgr==NULL)return NULL; + struct stellar_module *mod=stellar_module_manager_get_module(mod_mgr, MODULE_POLLING); + if(mod==NULL)return NULL; + return (struct stellar_polling_manager *)stellar_module_get_ctx(mod); +} + + +struct stellar_module *polling_manager_on_init(struct stellar_module_manager *mod_mgr) +{ + if(mod_mgr==NULL)return NULL; + struct stellar_polling_manager *polling_mgr=CALLOC(struct stellar_polling_manager, 1); + polling_mgr->mod_mgr=mod_mgr; + struct stellar_module *mod=stellar_module_new(MODULE_POLLING, polling_mgr); + return mod; +} + +void polling_manager_on_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod) +{ + if(mod_mgr==NULL || mod==NULL)return; + struct stellar_polling_manager *polling_mgr=(struct stellar_polling_manager *)stellar_module_get_ctx(mod); + if(polling_mgr==NULL)return; + FREE(polling_mgr); + stellar_module_free(mod); + return; +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-function-type" +static void on_polling_dispatch(int topic_id __unused, + void *msg __unused, + on_msg_cb_func* on_msg_cb, + void *on_msg_cb_arg, + void *dispatch_arg) +{ + struct stellar_polling_manager *polling_mgr=(struct stellar_polling_manager *)dispatch_arg; + module_on_polling_func *polling = (module_on_polling_func *)on_msg_cb; + polling(polling_mgr, on_msg_cb_arg); +} + +int stellar_polling_subscribe(struct stellar_polling_manager* polling_mgr, module_on_polling_func on_polling, void *polling_arg) +{ + polling_mgr->polling_topic_id=mq_schema_get_topic_id(stellar_module_manager_get_mq_schema(polling_mgr->mod_mgr), TOPIC_POLLING); + if(polling_mgr->polling_topic_id<0) + { + polling_mgr->polling_topic_id=mq_schema_create_topic(stellar_module_manager_get_mq_schema(polling_mgr->mod_mgr), TOPIC_POLLING, on_polling_dispatch, polling_mgr, NULL, NULL); + } + return mq_schema_subscribe(stellar_module_manager_get_mq_schema(polling_mgr->mod_mgr), polling_mgr->polling_topic_id, (on_msg_cb_func *)on_polling, polling_arg); +} + +#pragma GCC diagnostic pop + +void stellar_polling_active(struct stellar_polling_manager *polling_mgr) +{ + mq_runtime_publish_message(stellar_module_manager_get_mq_runtime(polling_mgr->mod_mgr), polling_mgr->polling_topic_id, NULL); +} + +void stellar_polling_dispatch(struct stellar_polling_manager *polling_mgr) +{ + stellar_polling_active(polling_mgr); + mq_runtime_dispatch(stellar_module_manager_get_mq_runtime(polling_mgr->mod_mgr)); + mq_runtime_clean(stellar_module_manager_get_mq_runtime(polling_mgr->mod_mgr)); + return; +}
\ No newline at end of file diff --git a/infra/polling_manager/polling_manager_internal.h b/infra/polling_manager/polling_manager_internal.h new file mode 100644 index 0000000..e6c02dd --- /dev/null +++ b/infra/polling_manager/polling_manager_internal.h @@ -0,0 +1,22 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "stellar/polling_manager.h" + +#define TOPIC_POLLING "POLLING" +#define MODULE_POLLING "POLLING" +struct stellar_polling_manager +{ + struct stellar_module_manager *mod_mgr; + int polling_topic_id; +}; + +void stellar_polling_dispatch(struct stellar_polling_manager *polling_mgr); + +#ifdef __cplusplus +} +#endif
\ No newline at end of file diff --git a/infra/polling_manager/test/CMakeLists.txt b/infra/polling_manager/test/CMakeLists.txt new file mode 100644 index 0000000..1a77472 --- /dev/null +++ b/infra/polling_manager/test/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(gtest_polling_manager + gtest_polling_manager_main.cpp +) + + +include_directories(${CMAKE_SOURCE_DIR}/infra/module_manager/) + +target_link_libraries( + gtest_polling_manager + polling_manager + dl + "-rdynamic" + gtest + gmock +) + +include(GoogleTest) +gtest_discover_tests(gtest_polling_manager)
\ No newline at end of file diff --git a/infra/polling_manager/test/gtest_polling_manager_main.cpp b/infra/polling_manager/test/gtest_polling_manager_main.cpp new file mode 100644 index 0000000..e6953f9 --- /dev/null +++ b/infra/polling_manager/test/gtest_polling_manager_main.cpp @@ -0,0 +1,85 @@ +#pragma GCC diagnostic ignored "-Wunused-parameter" + +#include <gtest/gtest.h> + + +#include "polling_manager/polling_manager_internal.h" +#include "module_manager/module_manager_interna.h" + +/*********************************** + * TEST POLLING MANAGER POLLING API * + ***********************************/ + +struct test_module_polling_env +{ + int N_round; + int polling_count; + int polling_active_count; +}; + + void test_module_on_polling(struct stellar_polling_manager* mod_mgr, void *polling_arg) + { + struct test_module_polling_env *env = (struct test_module_polling_env*)polling_arg; + env->polling_count++; + if(env->polling_count%2==0) + { + stellar_polling_active(mod_mgr); + env->polling_active_count++; + } + } + +const char *gtest_mock_spec_toml = + "[[module]]\n" + "path = \"\"\n" + "init = \"polling_manager_on_init\"\n" + "exit = \"polling_manager_on_exit\"\n"; + +TEST(polling_manager, basic_polling_module) { + + struct mq_schema *mq_schema=mq_schema_new(); + toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0); + EXPECT_TRUE(conf!=NULL); + + struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema); + EXPECT_TRUE(mod_mgr!=NULL); + + struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr); + EXPECT_TRUE(polling_mgr!=NULL); + + EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10); + EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema); + + struct test_module_polling_env env={}; + env.N_round=10; + + stellar_polling_subscribe(polling_mgr, test_module_on_polling, &env); + + struct mq_runtime *mq_rt = mq_runtime_new(mq_schema); + stellar_module_manager_register_thread(mod_mgr, 1, mq_rt); + + EXPECT_EQ((long)stellar_module_manager_get_thread_id(mod_mgr), 1); + EXPECT_EQ(stellar_module_manager_get_mq_runtime(mod_mgr), mq_rt); + + for(int i=0; i<env.N_round; i++) + { + stellar_polling_dispatch(polling_mgr); + } + + stellar_module_manager_free(mod_mgr); + toml_free(conf); + + EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count); + +} + +/********************************************** + * GTEST MAIN * + **********************************************/ + +int main(int argc, char ** argv) +{ + int ret=0; + ::testing::InitGoogleTest(&argc, argv); + ret=RUN_ALL_TESTS(); + return ret; +}
\ No newline at end of file |
