73 lines
2.8 KiB
C
73 lines
2.8 KiB
C
#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)
|
|
{
|
|
if(polling_mgr==NULL || polling_mgr->mod_mgr == NULL)return -1;
|
|
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)
|
|
{
|
|
if(polling_mgr==NULL || polling_mgr->mod_mgr == NULL)return;
|
|
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)
|
|
{
|
|
if(polling_mgr==NULL || polling_mgr->mod_mgr == NULL)return;
|
|
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;
|
|
} |