diff options
author | Dmitry Kozlov <xeb@mail.ru> | 2017-10-17 23:17:22 +0300 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2017-10-17 23:17:22 +0300 |
commit | 0a71249f32169ad04fd6a907989578dced3ba9ec (patch) | |
tree | e316bc6ea7e56e1df8df38f85a494d4ba26a5ffd | |
parent | 05cbd57477af3b90ee43817c8db4b3ca391414fd (diff) | |
download | accel-ppp-0a71249f32169ad04fd6a907989578dced3ba9ec.tar.gz accel-ppp-0a71249f32169ad04fd6a907989578dced3ba9ec.zip |
lua: implemented interface to extend session object by modules
to access module specific object introduced new function session:module(NAME)
-rw-r--r-- | accel-pppd/lua/luasupp.h | 8 | ||||
-rw-r--r-- | accel-pppd/lua/session.c | 41 |
2 files changed, 49 insertions, 0 deletions
diff --git a/accel-pppd/lua/luasupp.h b/accel-pppd/lua/luasupp.h index ea5a646b..f7efab8c 100644 --- a/accel-pppd/lua/luasupp.h +++ b/accel-pppd/lua/luasupp.h @@ -9,4 +9,12 @@ int luaopen_bit(lua_State *L); #define LUA_AP_SESSION "ap_session" int luaopen_ap_session(lua_State *L); +struct lua_session_module { + const char *name; + void (*init)(lua_State *L); + int (*get_instance)(lua_State *L, struct ap_session *ses); +}; + +void lua_session_module_register(const struct lua_session_module *mod); + #endif diff --git a/accel-pppd/lua/session.c b/accel-pppd/lua/session.c index d2e05fff..d65a67bd 100644 --- a/accel-pppd/lua/session.c +++ b/accel-pppd/lua/session.c @@ -12,6 +12,9 @@ #include "utils.h" #include "luasupp.h" +static int mod_cnt; +static const struct lua_session_module **mods; + static int session_ifname(lua_State *L); static int session_ifindex(lua_State *L); static int session_sid(lua_State *L); @@ -24,6 +27,7 @@ static int session_ipv4(lua_State *L); static int session_ipv6(lua_State *L); static int session_rx_bytes(lua_State *L); static int session_tx_bytes(lua_State *L); +static int session_module(lua_State *L); static const struct luaL_Reg session_lib [] = { {"ifname", session_ifname}, @@ -38,11 +42,14 @@ static const struct luaL_Reg session_lib [] = { {"ipv6", session_ipv6}, {"rx_bytes", session_rx_bytes}, {"tx_bytes", session_tx_bytes}, + {"module", session_module}, {NULL, NULL} }; int __export luaopen_ap_session(lua_State *L) { + int i; + luaL_newmetatable(L, LUA_AP_SESSION); lua_pushvalue(L, -1); @@ -54,6 +61,9 @@ int __export luaopen_ap_session(lua_State *L) luaL_setfuncs(L, session_lib, 0); #endif + for (i = 0; i < mod_cnt; i++) + mods[i]->init(L); + return 1; } @@ -231,4 +241,35 @@ static int session_tx_bytes(lua_State *L) return 1; } +static int session_module(lua_State *L) +{ + int i; + struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION); + const char *name; + + if (!ses) + return 0; + + name = luaL_checkstring(L, 2); + if (!name) + return 0; + + for (i = 0; i < mod_cnt; i++) { + if (strcmp(name, mods[i]->name) == 0) + return mods[i]->get_instance(L, ses); + } + + lua_pushnil(L); + + return 1; +} + +void __export lua_session_module_register(const struct lua_session_module *mod) +{ + if (!mods) + mods = malloc(sizeof(void *)); + else + mods = realloc(mods, (mod_cnt + 1) * sizeof(void *)); + mods[mod_cnt++] = mod; +} |