r11115 - prelude-correlator/trunk/plugins/lua

[email protected]
Newsgroups gmane.comp.security.ids.prelude.cvs
Message-ID <[email protected]>
Author: yoann
Date: 2009-04-06 12:49:32 +0200 (Mon, 06 Apr 2009)
New Revision: 11115

Modified:
   prelude-correlator/trunk/plugins/lua/lib.lua
   prelude-correlator/trunk/plugins/lua/lua-timer.c
   prelude-correlator/trunk/plugins/lua/lua-timer.h
Log:
Complete Lua Timer() implementation. An Lua callback function can
now be associated with the Lua Timer. Additionally, the start() and reset()
method has been simplified.

Modified: prelude-correlator/trunk/plugins/lua/lib.lua
===================================================================
--- prelude-correlator/trunk/plugins/lua/lib.lua	2009-04-06 10:49:26 UTC (rev 11114)
+++ prelude-correlator/trunk/plugins/lua/lib.lua	2009-04-06 10:49:32 UTC (rev 11115)
@@ -24,17 +24,15 @@
 Context = {}
 Context.__index = Context
 
-function _del_context_(name)
-        c = Context.get(name)
-
-        if c._alert_on_expire then
-            c:alert()
-        end
-
-        c:del()
+function _timer_cb(timer, ctx)
+    ctx:del()
 end
 
 function Context:del()
+    if self._alert_on_expire then
+        self:alert()
+    end
+
     self._timer = nil
     self._idmef = nil
     __C[self._name] = nil
@@ -67,7 +65,6 @@
     return __C[name]
 end
 
-
 function Context.new(name, options)
     local ctx = {}
 
@@ -85,8 +82,8 @@
     end
 
     if ctx._expire ~= nil then
-        ctx._timer = Timer.new(name)
-        ctx._timer:start(ctx._expire)
+        ctx._timer = Timer.new(ctx._expire, _timer_cb, ctx)
+        ctx._timer:start()
     end
 
     return ctx

Modified: prelude-correlator/trunk/plugins/lua/lua-timer.c
===================================================================
--- prelude-correlator/trunk/plugins/lua/lua-timer.c	2009-04-06 10:49:26 UTC (rev 11114)
+++ prelude-correlator/trunk/plugins/lua/lua-timer.c	2009-04-06 10:49:32 UTC (rev 11115)
@@ -45,8 +45,11 @@
 struct lua_timer {
         prelude_bool_t is_active;
         prelude_timer_t timer;
-        char *data;
         lua_State *lstate;
+
+        int cb_timer_ref;
+        int cb_func_ref;
+        int cb_data_ref;
 };
 
 
@@ -56,18 +59,18 @@
         int ret;
         lua_timer_t *timer = data;
 
-        lua_getglobal(timer->lstate, "_del_context_");
-        lua_pushstring(timer->lstate, timer->data);
+        lua_rawgeti(timer->lstate, LUA_REGISTRYINDEX, timer->cb_func_ref);
+        lua_rawgeti(timer->lstate, LUA_REGISTRYINDEX, timer->cb_timer_ref);
 
-        ret = lua_pcall(timer->lstate, 1, 0, 0);
-        if ( ret != 0 )
-                prelude_log(PRELUDE_LOG_ERR, "LUA error: %s.\n", lua_tostring(timer->lstate, -1));
+        if ( timer->cb_data_ref )
+                lua_rawgeti(timer->lstate, LUA_REGISTRYINDEX, timer->cb_data_ref);
 
-
-        //lua_gc(timer->lstate, LUA_GCCOLLECT, 0);
-
         timer->is_active = FALSE;
         prelude_timer_destroy(&timer->timer);
+
+        ret = lua_pcall(timer->lstate, (timer->cb_data_ref) ? 2 : 1, 0, 0);
+        if ( ret != 0 )
+                prelude_log(PRELUDE_LOG_ERR, "timer callback problem: %s.\n", lua_tostring(timer->lstate, -1));
 }
 
 
@@ -98,23 +101,27 @@
 }
 
 
-lua_timer_t *pushTimer(lua_State *lstate, const char *cname)
+lua_timer_t *pushTimer(lua_State *lstate, int expire, int func_ref, int data_ref)
 {
-        char *dup;
         lua_timer_t *timer;
 
-        dup = strdup(cname);
-        if ( ! dup )
-                return NULL;
-
         timer = lua_newuserdata(lstate, sizeof(*timer));
         timer->is_active = FALSE;
-        timer->data = dup;
         timer->lstate = lstate;
 
+        timer->cb_func_ref = func_ref;
+        timer->cb_data_ref = data_ref;
+
+        prelude_timer_set_data(&timer->timer, timer);
+        prelude_timer_set_expire(&timer->timer, expire);
+        prelude_timer_set_callback(&timer->timer, timer_cb);
+
         luaL_getmetatable(lstate, TIMER_CLASS);
         lua_setmetatable(lstate, -2);
 
+        lua_pushvalue(lstate, -1);
+        timer->cb_timer_ref = luaL_ref(lstate, LUA_REGISTRYINDEX);
+
         return timer;
 }
 
@@ -123,19 +130,33 @@
 {
         int ret;
         lua_timer_t *timer;
+        int data_ref = 0, func_ref;
 
         ret = lua_gettop(lstate);
-        if ( ret != 1 ) {
-                prelude_log(PRELUDE_LOG_ERR, "timer_start(): require 1 arguments, got %d.\n", ret);
+        if ( ret < 2 || ret > 3 ) {
+                prelude_log(PRELUDE_LOG_ERR, "TimerNew(): require 3 arguments, got %d.\n", ret);
                 return -1;
         }
 
-        if ( ! lua_isstring(lstate, 1) ) {
-                prelude_log(PRELUDE_LOG_ERR, "timer_init(): First argument should be 'string'.\n");
+        if ( ! lua_isnumber(lstate, 1) ) {
+                prelude_log(PRELUDE_LOG_ERR, "TimerNew(): First argument should be 'number'.\n");
                 return -1;
         }
 
-        timer = pushTimer(lstate, lua_tostring(lstate, 1));
+        if ( ! lua_isfunction(lstate, 2) ) {
+                prelude_log(PRELUDE_LOG_ERR, "TimerNew(): Second argument should be 'function'.\n");
+                return -1;
+        }
+
+        lua_pushvalue(lstate, 2);
+        func_ref = luaL_ref(lstate, LUA_REGISTRYINDEX);
+
+        if ( ret == 3 ) {
+                lua_pushvalue(lstate, 3);
+                data_ref = luaL_ref(lstate, LUA_REGISTRYINDEX);
+        }
+
+        timer = pushTimer(lstate, lua_tonumber(lstate, 1), func_ref, data_ref);
         return 1;
 }
 
@@ -146,8 +167,8 @@
         lua_timer_t *timer;
 
         ret = lua_gettop(lstate);
-        if ( ret != 2 ) {
-                prelude_log(PRELUDE_LOG_ERR, "timer_start(): require 2 arguments, got %d.\n", ret);
+        if ( ret != 1 ) {
+                prelude_log(PRELUDE_LOG_ERR, "timer_start(): require 1 arguments, got %d.\n", ret);
                 return -1;
         }
 
@@ -157,15 +178,7 @@
                 return -1;
         }
 
-        if ( ! lua_isnumber(lstate, 2) ) {
-                prelude_log(PRELUDE_LOG_ERR, "timer_init(): First argument should be a 'number'.\n");
-                return -1;
-        }
-
         timer->is_active = TRUE;
-        prelude_timer_set_data(&timer->timer, timer);
-        prelude_timer_set_expire(&timer->timer, lua_tonumber(lstate, 2));
-        prelude_timer_set_callback(&timer->timer, timer_cb);
         prelude_timer_init(&timer->timer);
 
         return 0;
@@ -178,8 +191,8 @@
         lua_timer_t *timer;
 
         ret = lua_gettop(lstate);
-        if ( ret != 2 ) {
-                prelude_log(PRELUDE_LOG_ERR, "timer_reset(): require 2 arguments, got %d.\n", ret);
+        if ( ret < 1 || ret > 2 ) {
+                prelude_log(PRELUDE_LOG_ERR, "timer_reset(): require 1 or 2 arguments, got %d.\n", ret);
                 return -1;
         }
 
@@ -189,19 +202,23 @@
                 return -1;
         }
 
-        if ( ! lua_isnumber(lstate, 2) ) {
-                prelude_log(PRELUDE_LOG_ERR, "timer_reset(): Second argument should be a 'number'.\n");
-                return -1;
+        if ( ret == 2 ) {
+                if ( ! lua_isnumber(lstate, 2) ) {
+                        prelude_log(PRELUDE_LOG_ERR, "timer_reset(): Second argument should be a 'number'.\n");
+                        return -1;
+                }
+
+                prelude_timer_set_expire(&timer->timer, lua_tonumber(lstate, 2));
         }
 
         timer->is_active = TRUE;
-        prelude_timer_set_expire(&timer->timer, lua_tonumber(lstate, 2));
         prelude_timer_reset(&timer->timer);
 
         return 0;
 }
 
 
+
 static int Timer_stop(lua_State *lstate)
 {
         int ret;
@@ -240,11 +257,14 @@
         lua_timer_t *timer;
 
         timer = toTimer(lstate, 1);
+
+        luaL_unref(timer->lstate, LUA_REGISTRYINDEX, timer->cb_func_ref);
+        luaL_unref(timer->lstate, LUA_REGISTRYINDEX, timer->cb_data_ref);
+        luaL_unref(timer->lstate, LUA_REGISTRYINDEX, timer->cb_timer_ref);
+
         if ( timer && timer->is_active )
                 prelude_timer_destroy(&timer->timer);
 
-        free(timer->data);
-
         prelude_log_debug(1, "[gc] TIMER at %p\n", timer);
         return 0;
 }

Modified: prelude-correlator/trunk/plugins/lua/lua-timer.h
===================================================================
--- prelude-correlator/trunk/plugins/lua/lua-timer.h	2009-04-06 10:49:26 UTC (rev 11114)
+++ prelude-correlator/trunk/plugins/lua/lua-timer.h	2009-04-06 10:49:32 UTC (rev 11115)
@@ -23,6 +23,6 @@
 
 typedef struct lua_timer lua_timer_t;
 
-lua_timer_t *pushTimer(lua_State *lstate, const char *cname);
+lua_timer_t *pushTimer(lua_State *lstate, int expire, int func_ref, int data_ref);
 
 int Timer_register(lua_State *lstate);

_______________________________________________
Prelude-cvslog site list
[email protected]
http://lists.prelude-ids.org/mailman/listinfo/prelude-cvslog
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.