🦄 refactor(appid api): remove appid module

This commit is contained in:
yangwei
2024-11-06 13:53:03 +08:00
parent 8ddef31bb5
commit 526c110868
14 changed files with 102 additions and 143 deletions

View File

@@ -17,9 +17,7 @@
#include "stellar/packet.h"
#include "stellar/session.h"
#include "appid/appid_internal.h"
#include "lpi_plus.h"
#include "lpi_plus_internal.h"
#include "lpip_extend.h"
@@ -40,7 +38,7 @@ struct lpi_plus_mapper
struct lpi_plus_appid_info *hash_appid2name;
};
struct lpi_plus_env
struct lpi_plus
{
unsigned int max_pkts;
int lpip_session_exdata_idx;
@@ -93,13 +91,11 @@ static void lpi_plus_get_host_order_port(struct session *sess __unused, unsigned
static struct appid_message *lpi_plus_message_new(struct session *sess, int *id_array, size_t id_num)
{
struct appid_message *result=CALLOC(struct appid_message, 1);
result->origin=ORIGIN_LPI_PLUS;
result->sess=sess;
result->appid_num=id_num;
for(unsigned int i=0; i<result->appid_num; i++)
{
result->appid[i]=(int)(id_array[i]);
result->surrogate_id[i]=0;
}
return result;
@@ -136,10 +132,10 @@ static int lpi_plus_name2appid(struct lpi_plus_mapper *mapper, const char *lpi_n
return out->appid;
}
const char *lpi_plus_appid2name(struct lpi_plus_mapper *mapper, int appid)
const char *lpi_plus_appid2name(struct lpi_plus *lpip, int appid)
{
struct lpi_plus_appid_info *out=NULL;
HASH_FIND(hh_appid, mapper->hash_appid2name, &appid, sizeof(int), out);
HASH_FIND(hh_appid, lpip->mapper->hash_appid2name, &appid, sizeof(int), out);
if(out==NULL)return NULL;
return out->app_name;
}
@@ -293,7 +289,7 @@ static void lpi_plus_on_session(struct session *sess, enum session_state state,
assert(pkt == NULL);
return;
}
struct lpi_plus_env *env=(struct lpi_plus_env *)args;
struct lpi_plus *env=(struct lpi_plus *)args;
struct lpi_plus_exdata *exdata = (struct lpi_plus_exdata *)session_get_exdata(sess, env->lpip_session_exdata_idx);
if(exdata==NULL)
{
@@ -331,7 +327,7 @@ void lpi_plus_exit(struct module_manager *mod_mgr, struct module *mod)
if(mod_mgr==NULL)return;
if(mod)
{
struct lpi_plus_env *env=(struct lpi_plus_env *)module_get_ctx(mod);
struct lpi_plus *env=(struct lpi_plus *)module_get_ctx(mod);
lpi_free_library();
lpi_plus_mapper_free(env->mapper);
FREE(env);
@@ -339,11 +335,55 @@ void lpi_plus_exit(struct module_manager *mod_mgr, struct module *mod)
}
}
static void appid_message_free(void *msg, void *msg_free_arg __unused)
{
if(msg==NULL)return;
FREE(msg);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static void lpi_plus_appid_on_msg_dispatch(int topic_id __unused,
void *msg,
on_msg_cb_func* on_msg_cb,
void *on_msg_cb_arg,
void *dispatch_arg __unused)
{
on_appid_callback *appid_cb = (on_appid_callback *)on_msg_cb;
struct appid_message *appid_msg=(struct appid_message *)msg;
appid_cb(appid_msg->sess, appid_msg->appid, appid_msg->appid_num, on_msg_cb_arg);
}
int lpi_plus_appid_subscribe(struct lpi_plus *lpip, on_appid_callback *cb, void *args)
{
if(lpip==NULL)return -1;
struct module_manager *mod_mgr=lpip->mod_mgr;
int appid_topic_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC);
if(appid_topic_id<0)
{
appid_topic_id=mq_schema_create_topic(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC, lpi_plus_appid_on_msg_dispatch, mod_mgr, appid_message_free, NULL);
}
return mq_schema_subscribe(module_manager_get_mq_schema(mod_mgr), appid_topic_id, (on_msg_cb_func *)cb, args);
}
#pragma GCC diagnostic pop
int lpi_plus_create_appid_topic(struct module_manager *mod_mgr)
{
int app_topic_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC);
if(app_topic_id < 0)
{
app_topic_id=mq_schema_create_topic(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC, lpi_plus_appid_on_msg_dispatch, NULL,appid_message_free, NULL);
}
return app_topic_id;
}
struct module *lpi_plus_init(struct module_manager *mod_mgr)
{
if(mod_mgr==NULL)return NULL;
struct lpi_plus_env *env=CALLOC(struct lpi_plus_env, 1);
struct lpi_plus *env=CALLOC(struct lpi_plus, 1);
struct module *mod=module_new("LPI_PLUS", env);
env->mod_mgr=mod_mgr;
env->max_pkts=16;//TODO: load from toml
@@ -370,7 +410,7 @@ struct module *lpi_plus_init(struct module_manager *mod_mgr)
session_manager_subscribe_udp(sess_mgr, lpi_plus_on_session, env);
env->lpip_session_exdata_idx = session_manager_new_session_exdata_index(sess_mgr, "EXDATA_LPI", lpi_plus_exdata_free, NULL);
env->topic_appid=stellar_appid_create_topic(mod_mgr);
env->topic_appid=lpi_plus_create_appid_topic(mod_mgr);
if(env->topic_appid<0)
{
goto INIT_ERROR;
@@ -385,11 +425,11 @@ INIT_ERROR:
}
struct lpi_plus_mapper *module_to_lpip_mapper(struct module *mod)
struct lpi_plus *module_to_lpi_plus(struct module *mod)
{
if(mod==NULL)return NULL;
assert(strcmp(module_get_name(mod), LPI_PLUS_MODULE_NAME) == 0);
struct lpi_plus_env *lpi_p=(struct lpi_plus_env *)module_get_ctx(mod);
struct lpi_plus *lpi_p=(struct lpi_plus *)module_get_ctx(mod);
if(lpi_p==NULL)return NULL;
return lpi_p->mapper;
return lpi_p;
}