diff options
Diffstat (limited to 'src/lua_binding_functions.c')
| -rw-r--r-- | src/lua_binding_functions.c | 127 |
1 files changed, 82 insertions, 45 deletions
diff --git a/src/lua_binding_functions.c b/src/lua_binding_functions.c index 383bbb0..77b2058 100644 --- a/src/lua_binding_functions.c +++ b/src/lua_binding_functions.c @@ -28,12 +28,17 @@ * int lua_mq_publish_message * int lua_mq_ignore_message * int lua_mq_unignore_message + * + * 08-29 + * 修改create_topic逻辑, 允许创建一个空的topic ************************************************************************/ #include "lua_plugin_manage_internal.h" #include "stellar/session.h" #include "stellar/session_mq.h" +int global_max_plugin_id = 0; + /* ***** ***** ***** ***** ***** ***** */ int lua_plugin_manage_regist(lua_State *state) { @@ -123,6 +128,9 @@ int lua_plugin_manage_regist(lua_State *state) return 1; } } + /* 统计记录一下当前最大的plugin_id编号 */ + if (plugin_id > global_max_plugin_id) + global_max_plugin_id = plugin_id; /* 将注册完成的新插件插入到队列中 */ struct lua_plugin new_plugin; @@ -132,6 +140,7 @@ int lua_plugin_manage_regist(lua_State *state) new_plugin.ctx_free_ref = ctx_free_id; utarray_push_back(plugin_env->plugin_array, &new_plugin); plugin_env->plugin_count += 1; + global_schema->plugin_count += 1; lua_settop(state, 0); lua_pushinteger(state, plugin_id); @@ -265,7 +274,8 @@ int lua_mq_create_topic(lua_State *state) } /* 参数类型检查 */ - if (lua_type(state, -1) != LUA_TTABLE || lua_type(state, -2) != LUA_TFUNCTION || + if (((lua_type(state, -1) != LUA_TTABLE || lua_type(state, -2) != LUA_TFUNCTION) && + (lua_type(state, -1) != LUA_TNIL || lua_type(state, -2) != LUA_TNIL)) || lua_type(state, -3) != LUA_TSTRING || lua_type(state, -4) != LUA_TLIGHTUSERDATA) { lua_settop(state, 0); @@ -273,20 +283,28 @@ int lua_mq_create_topic(lua_State *state) } /* 创建对该table的引用, 防止该table被回收 */ - int private_ref = luaL_ref(state, LUA_REGISTRYINDEX); /* stack top function */ - if (private_ref == LUA_REFNIL) + int private_ref = 0; + if (lua_type(state, -1) != LUA_TNIL) { - lua_settop(state, 0); - return 0; + private_ref = luaL_ref(state, LUA_REGISTRYINDEX); /* stack top function */ + if (private_ref == LUA_REFNIL) + { + lua_settop(state, 0); + return 0; + } } /* 导出free message的调用函数 */ - int free_ref = luaL_ref(state, LUA_REGISTRYINDEX); - if (free_ref == LUA_REFNIL) + int free_ref = 0; + if (lua_type(state, -1) != LUA_TFUNCTION) { - luaL_unref(state, LUA_REGISTRYINDEX, private_ref); - lua_settop(state, 0); - return 0; + free_ref = luaL_ref(state, LUA_REGISTRYINDEX); + if (free_ref == LUA_REFNIL) + { + luaL_unref(state, LUA_REGISTRYINDEX, private_ref); + lua_settop(state, 0); + return 0; + } } /* 出栈队列名称 */ @@ -307,29 +325,33 @@ int lua_mq_create_topic(lua_State *state) lua_settop(state, 0); /* 插入新的元素 */ - struct lua_message_mq new_mq; - memset(&new_mq, 0, sizeof(new_mq)); - utarray_push_back(global_schema->message_mq_array, &new_mq); - /* 从队列尾部取出最后一个元素 */ - struct lua_message_mq *mq = utarray_eltptr(global_schema->message_mq_array, (utarray_len(global_schema->message_mq_array) - 1)); - mq->freemessage_ref = free_ref; - mq->mq_private_ref = private_ref; - /* 调用stellar中mq的topic创建函数 */ - /* BugFix: 仔细看了代码, 在stellar中没有对name做拷贝处理, 这里name不能释放 */ - int topic_id = stellar_session_mq_create_topic(st, (const char *)name, lpm_message_free_func, mq); - /* 创建topic失败, 还原创建topic之前的状态 */ - if (topic_id < 0) - { - utarray_pop_back(global_schema->message_mq_array); - luaL_unref(state, LUA_REGISTRYINDEX, free_ref); - luaL_unref(state, LUA_REGISTRYINDEX, private_ref); - if (name) - free(name); - return 0; - } - global_schema->mq_count += 1; + int topic_id = -1; + if (private_ref && free_ref) + { + struct lua_message_mq new_mq; + memset(&new_mq, 0, sizeof(new_mq)); + utarray_push_back(global_schema->message_mq_array, &new_mq); + /* 从队列尾部取出最后一个元素 */ + struct lua_message_mq *mq = utarray_eltptr(global_schema->message_mq_array, (utarray_len(global_schema->message_mq_array) - 1)); + mq->freemessage_ref = free_ref; + mq->mq_private_ref = private_ref; + /* 调用stellar中mq的topic创建函数 */ + /* BugFix: 仔细看了代码, 在stellar中没有对name做拷贝处理, 这里name不能释放 */ + topic_id = stellar_session_mq_create_topic(st, (const char *)name, lpm_message_free_func, mq); + + /* 创建topic失败, 还原创建topic之前的状态 */ + if (topic_id < 0) + { + utarray_pop_back(global_schema->message_mq_array); + luaL_unref(state, LUA_REGISTRYINDEX, free_ref); + luaL_unref(state, LUA_REGISTRYINDEX, private_ref); + if (name) + free(name); + return 0; + } + global_schema->mq_count += 1; - lua_rawgeti(state, LUA_REGISTRYINDEX, mq->mq_private_ref); + lua_rawgeti(state, LUA_REGISTRYINDEX, mq->mq_private_ref); #if 0 /* 不检查了, 直接覆盖 */ /* 在传入的table中检查是否存在与mq_private_env同名的元素 */ @@ -340,10 +362,22 @@ int lua_mq_create_topic(lua_State *state) } lua_pop(state, 1); /* stack top table */ #endif - /* 在该table中加入新元素 */ - lua_pushlightuserdata(state, (void *)mq); /* stack top new_mq */ - lua_setfield(state, -2, LUA_MQ_ENV_DEFAULT_KEY); /* stack top table */ - mq->topic_id = topic_id; + /* 在该table中加入新元素 */ + lua_pushlightuserdata(state, (void *)mq); /* stack top new_mq */ + lua_setfield(state, -2, LUA_MQ_ENV_DEFAULT_KEY); /* stack top table */ + mq->topic_id = topic_id; + } + else + { + /* 只是创建一个新的空topic */ + topic_id = stellar_session_mq_create_topic(st, (const char *)name, NULL, NULL); + if (topic_id < 0) + { + if (name) + free(name); + return 0; + } + } lua_settop(state, 0); lua_pushinteger(state, topic_id); @@ -695,9 +729,10 @@ int lua_mq_ignore_message(lua_State *state) int topic_id = lua_tointeger(state, -1); lua_pop(state, 1); - - struct session * sess = (struct session *)lua_topointer(state, -1); - if ( !sess ) { + + struct session *sess = (struct session *)lua_topointer(state, -1); + if (!sess) + { lua_settop(state, 0); return 0; } @@ -734,9 +769,10 @@ int lua_mq_unignore_message(lua_State *state) int topic_id = lua_tointeger(state, -1); lua_pop(state, 1); - - struct session * sess = (struct session *)lua_topointer(state, -1); - if ( !sess ) { + + struct session *sess = (struct session *)lua_topointer(state, -1); + if (!sess) + { lua_settop(state, 0); return 0; } @@ -769,9 +805,10 @@ int lua_mq_topic_is_active(lua_State *state) /* 倒序获取参数 */ int topic_id = lua_tointeger(state, -1); lua_pop(state, 1); - - struct session * sess = (struct session *)lua_topointer(state, -1); - if ( !sess ) { + + struct session *sess = (struct session *)lua_topointer(state, -1); + if (!sess) + { lua_settop(state, 0); return 0; } |
