diff options
-rw-r--r-- | lib/LuaTask.cc | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/lib/LuaTask.cc b/lib/LuaTask.cc index 754b136..bc6266c 100644 --- a/lib/LuaTask.cc +++ b/lib/LuaTask.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: LuaTask.cc,v 1.19 2007-12-25 15:06:12 pixel Exp $ */ +/* $Id: LuaTask.cc,v 1.20 2008-01-21 04:04:03 pixel Exp $ */ #include <LuaTask.h> #include <LuaHandle.h> @@ -34,7 +34,7 @@ htab * LuaTask::h = hcreate(1); -LuaTask::LuaTask(Lua * _L, const String & _cmd) : L(_L), cmd(_cmd), nargs(0), c(0), b(0) { +LuaTask::LuaTask(Lua * __L, const String & _cmd) : L(__L), cmd(_cmd), nargs(0), c(0), b(0) { LuaTask * top = gettop(); if (top) { @@ -46,7 +46,7 @@ LuaTask::LuaTask(Lua * _L, const String & _cmd) : L(_L), cmd(_cmd), nargs(0), c( stacktop = L->gettop() + 1; } -LuaTask::LuaTask(Lua * _L, int _nargs) throw (GeneralException) : L(_L), cmd(""), nargs(_nargs), c(0), b(0) { +LuaTask::LuaTask(Lua * __L, int _nargs) throw (GeneralException) : L(__L), cmd(""), nargs(_nargs), c(0), b(0) { if (gettop()) throw GeneralException("Can't run a stack-based LuaTask when there are other tasks waiting."); @@ -282,11 +282,13 @@ int LuaTask::Do() throw (GeneralException) { enum TaskMan_functions_t { TASKMAN_NB_TASKS = 0, TASKMAN_NB_ZOMBIES, + TASKMAN_DELAYED_TASK, }; struct lua_functypes_t TaskMan_functions[] = { { TASKMAN_NB_TASKS, "NB_Tasks", 0, 0, { } }, { TASKMAN_NB_ZOMBIES, "NB_Zombies", 0, 0, { } }, + { TASKMAN_DELAYED_TASK, "DelayedTask", 1, 2, { BLUA_STRING, BLUA_NUMBER } }, { -1, 0, 0, 0, 0 } }; @@ -294,6 +296,7 @@ class sLua_TaskMan : public Base { public: DECLARE_FUNCTION(TaskMan, TASKMAN_NB_TASKS); DECLARE_FUNCTION(TaskMan, TASKMAN_NB_ZOMBIES); + DECLARE_FUNCTION(TaskMan, TASKMAN_DELAYED_TASK); private: static int TaskMan_proceed_statics(Lua * L, int n, int caller); }; @@ -303,9 +306,40 @@ void LuaTaskMan::pushstatics(Lua * L) throw (GeneralException) { PUSH_FUNCTION(TaskMan, TASKMAN_NB_TASKS); PUSH_FUNCTION(TaskMan, TASKMAN_NB_ZOMBIES); + PUSH_FUNCTION(TaskMan, TASKMAN_DELAYED_TASK); } +class DelayedTask : public Task { + public: + DelayedTask(Lua * __L, const String & _cmd, int _delay) : L(__L), cmd(_cmd), delay(_delay) { + printm(M_INFO, "Creating object delayedtask, delay = %i seconds.\n", _delay); + SetBurst(); + } + virtual int Do() throw (GeneralException) { + timeval delay_tv = { delay, 0 }; + switch(current) { + case 0: + printm(M_INFO, "DelayTask: initializing.\n"); + WaitFor(delay); + current = 1; + return TASK_ON_HOLD; + case 1: + printm(M_INFO, "DelayTask: expired, lauching LuaTask with cmd = " + cmd + "\n"); + RemoveTimeout(); + new LuaTask(L, cmd); + return TASK_DONE; + } + } + private: + Lua * L; + String cmd; + int delay; +}; + int sLua_TaskMan::TaskMan_proceed_statics(Lua * L, int n, int caller) { + String cmd; + int delay = 0; + switch (caller) { case TASKMAN_NB_TASKS: return TaskMan::nb_tasks(); @@ -313,6 +347,13 @@ int sLua_TaskMan::TaskMan_proceed_statics(Lua * L, int n, int caller) { case TASKMAN_NB_ZOMBIES: return TaskMan::nb_zombies(); break; + case TASKMAN_DELAYED_TASK: + cmd = L->tostring(1); + if (n == 2) + delay = L->tonumber(2); + printm(M_INFO, "Creating delayed task of %i seconds.\n", delay); + new DelayedTask(L, cmd, delay); + break; } return 1; |