From ff480424abb09c1816463c7bdb38524e09aa4891 Mon Sep 17 00:00:00 2001 From: Thierry Fournier Date: Thu, 25 Feb 2016 08:36:46 +0100 Subject: [PATCH] MINOR: lua: add class listener This class provides the access to the listener struct, it allows some manipulations and retrieve informations. --- doc/lua-api/index.rst | 18 +++++++++++ include/types/hlua.h | 1 + src/hlua_fcn.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index 07ade6915..2bca4d07f 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -597,6 +597,11 @@ Proxy class Contain an array with the attached servers. Each server entry is an object of type :ref:`server_class`. +.. js:attribute:: Proxy.listeners + + Contain an array with the attached listeners. Each listeners entry is an + object of type :ref:`listener_class`. + .. js:function:: Proxy.pause(px) Pause the proxy. See the management socket documentation for more information. @@ -811,6 +816,19 @@ Server class :param class_server sv: A :ref:`server_class` which indicates the manipulated server. +.. _listener_class: + +Listener class +============== + +.. js:function:: Listener.get_stats(ls) + + Returns server statistics. + + :param class_listener ls: A :ref:`listener_class` which indicates the + manipulated listener. + :returns: a key/value array containing stats + .. _concat_class: Concat class diff --git a/include/types/hlua.h b/include/types/hlua.h index afe870a37..43e9f1a6c 100644 --- a/include/types/hlua.h +++ b/include/types/hlua.h @@ -21,6 +21,7 @@ #define CLASS_APPLET_HTTP "AppletHTTP" #define CLASS_PROXY "Proxy" #define CLASS_SERVER "Server" +#define CLASS_LISTENER "Listener" struct stream; diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index 0e5a33d37..2ee339355 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -33,6 +33,7 @@ static int class_concat_ref; static int class_proxy_ref; static int class_server_ref; +static int class_listener_ref; #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS)) @@ -435,6 +436,48 @@ static int hlua_concat_init(lua_State *L) return 1; } +int hlua_fcn_new_listener(lua_State *L, struct listener *lst) +{ + lua_newtable(L); + + /* Pop a class sesison metatable and affect it to the userdata. */ + lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref); + lua_setmetatable(L, -2); + + lua_pushlightuserdata(L, lst); + lua_rawseti(L, -2, 0); + return 1; +} + +static struct listener *hlua_check_listener(lua_State *L, int ud) +{ + return (struct listener *)(hlua_checkudata(L, ud, class_listener_ref)); +} + +int hlua_listener_get_stats(lua_State *L) +{ + struct listener *li; + int i; + + li = hlua_check_listener(L, 1); + + if (!li->frontend) { + lua_pushnil(L); + return 1; + } + + stats_fill_li_stats(li->frontend, li, ST_SHLGNDS, stats, STATS_LEN); + + lua_newtable(L); + for (i=0; iconf.listeners, by_fe) { + if (lst->name) + lua_pushstring(L, lst->name); + else { + snprintf(buffer, 10, "sock-%d", lid); + lid++; + lua_pushstring(L, buffer); + } + hlua_fcn_new_listener(L, lst); + lua_settable(L, -3); + } + lua_settable(L, -3); + return 1; } @@ -856,6 +919,14 @@ int hlua_fcn_reg_core_fcn(lua_State *L) hlua_class_function(L, "concat", hlua_concat_new); hlua_class_function(L, "get_info", hlua_get_info); + /* Create listener object. */ + lua_newtable(L); + lua_pushstring(L, "__index"); + lua_newtable(L); + hlua_class_function(L, "get_stats", hlua_listener_get_stats); + lua_settable(L, -3); /* -> META["__index"] = TABLE */ + class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER); + /* Create server object. */ lua_newtable(L); lua_pushstring(L, "__index");