24 bHasUpdateCallback(false),
25 bHasUpdateReplyCallback(false),
39 registerExtraFunctions();
52 if(getLocalFunction(
"destroy"))
54 if(lua_pcall(L, 0, 0, 0) != 0)
65 if(luaL_loadfile(L, options.
find(
"filename").
asString().c_str()))
75 if(lua_pcall(L,0, LUA_MULTRET, 0))
88 lua_pushlightuserdata(L,
this);
89 lua_setglobal(L,
"PortMonitor_Owner");
91 lua_getglobal(L,
"PortMonitor");
92 if(lua_istable(L, -1) == 0)
104 if(getLocalFunction(
"create"))
117 if(lua_pcall(L, 1, 1, 0) != 0)
127 result = lua_toboolean(L, -1);
132 bHasAcceptCallback = getLocalFunction(
"accept");
136 bHasUpdateCallback = getLocalFunction(
"update");
140 bHasUpdateReplyCallback = getLocalFunction(
"update_reply");
149 if(getLocalFunction(
"accept"))
163 if(lua_pcall(L, 1, 1, 0) != 0)
172 bool result = lua_toboolean(L, -1);
187 if(getLocalFunction(
"update"))
201 if(lua_pcall(L, 1, 1, 0) != 0)
234 if(getLocalFunction(
"update_reply"))
248 if(lua_pcall(L, 1, 1, 0) != 0)
281 if(getLocalFunction(
"setparam"))
295 if(lua_pcall(L, 1, 0, 0) != 0)
314 if(getLocalFunction(
"getparam"))
327 if(lua_pcall(L, 0, 1, 0) != 0)
361 if(getLocalFunction(
"trig"))
363 if(lua_pcall(L, 0, 0, 0) != 0)
380 bool MonitorLua::getLocalFunction(
const char *name)
382 lua_pushstring(L, name);
384 return (lua_isfunction(L, -1) == 1);
388 bool MonitorLua::registerExtraFunctions()
390 #if LUA_VERSION_NUM > 501
392 luaL_setfuncs (L, MonitorLua::portMonitorLib, 0);
393 lua_pushvalue(L, -1);
394 lua_setglobal(L,
"PortMonitor");
398 luaL_register(L,
"PortMonitor", MonitorLua::portMonitorLib);
415 string strConstraint = constraint;
416 string strDummy = strConstraint;
417 searchReplace(strDummy,
"(",
" ");
418 searchReplace(strDummy,
")",
" ");
420 strDummy =
" " + strDummy +
" ";
421 string delimiter =
" ";
424 while ((pos = strDummy.find(delimiter)) != string::npos)
426 token = strDummy.substr(0, pos);
427 if(token.size() && !isKeyword(token.c_str()))
430 string value = (record.
hasEvent(token.c_str())) ?
"true" :
"false";
432 searchReplace(strConstraint, token, value);
434 strDummy.erase(0, pos + delimiter.length());
443 strConstraint =
"return " + strConstraint;
445 if(luaL_dostring(L, strConstraint.c_str()) != 0)
451 if(!lua_isboolean(L, -1))
457 bool accepted = (lua_toboolean(L,-1) == 1);
463 inline void MonitorLua::searchReplace(
string& str,
const string& oldStr,
const string& newStr)
466 while((pos = str.find(oldStr, pos)) != string::npos)
468 str.replace(pos, oldStr.length(), newStr);
469 pos += newStr.length();
473 inline void MonitorLua::trimString(
string& str)
475 string::size_type pos = str.find_last_not_of(
' ');
476 if(pos != string::npos) {
478 pos = str.find_first_not_of(
' ');
479 if(pos != string::npos) str.erase(0, pos);
481 else str.erase(str.begin(), str.end());
484 inline bool MonitorLua::isKeyword(
const char* str)
490 if((token ==
"true") || (token ==
"false") ||
491 (token ==
"and") || (token ==
"or") || (token ==
"not") )
501 int MonitorLua::setConstraint(lua_State* L)
503 const char *cst = luaL_checkstring(L, 1);
506 lua_getglobal(L,
"PortMonitor_Owner");
507 if(!lua_islightuserdata(L, -1))
513 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
515 owner->setAcceptConstraint(cst);
520 int MonitorLua::getConstraint(lua_State* L)
522 lua_getglobal(L,
"PortMonitor_Owner");
523 if(!lua_islightuserdata(L, -1))
529 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
531 lua_pushstring(L, owner->getAcceptConstraint());
536 int MonitorLua::setEvent(lua_State* L)
538 double lifetime = -1.0;
539 int n_args = lua_gettop(L);
540 const char *event_name = luaL_checkstring(L, 1);
546 if(lua_isnumber(L,2))
547 lifetime = (double) luaL_checknumber(L,2);
555 lua_getglobal(L,
"PortMonitor_Owner");
556 if(!lua_islightuserdata(L, -1))
561 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
563 if(owner->isKeyword(event_name))
567 record.
setEvent(event_name, owner, lifetime);
573 int MonitorLua::unsetEvent(lua_State* L)
575 const char *event_name = luaL_checkstring(L, 1);
578 lua_getglobal(L,
"PortMonitor_Owner");
579 if(!lua_islightuserdata(L, -1))
584 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
586 if(owner->isKeyword(event_name))
596 int MonitorLua::setTrigInterval(lua_State* L)
599 int n_args = lua_gettop(L);
601 if(lua_isnumber(L, 1))
602 period = (double) luaL_checknumber(L,1);
613 lua_getglobal(L,
"PortMonitor_Owner");
614 if(!lua_islightuserdata(L, -1))
620 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
624 if(owner->trigger ==
nullptr) {
626 owner->trigger->start();
632 #if LUA_VERSION_NUM > 501
633 const struct luaL_Reg
MonitorLua::portMonitorLib [] = {
637 {
"setConstraint", MonitorLua::setConstraint},
638 {
"getConstraint", MonitorLua::getConstraint},
639 {
"setEvent", MonitorLua::setEvent},
640 {
"unsetEvent", MonitorLua::unsetEvent},
641 {
"setTrigInterval", MonitorLua::setTrigInterval},