20 bHasUpdateCallback(false),
21 bHasUpdateReplyCallback(false),
35 registerExtraFunctions();
48 if(getLocalFunction(
"destroy"))
50 if (lua_pcall(L, 0, 0, 0) != 0) {
62 if(luaL_loadfile(L, options.
find(
"filename").
asString().c_str()))
72 if(lua_pcall(L,0, LUA_MULTRET, 0))
85 lua_pushlightuserdata(L,
this);
86 lua_setglobal(L,
"PortMonitor_Owner");
88 lua_getglobal(L,
"PortMonitor");
89 if(lua_istable(L, -1) == 0)
101 if(getLocalFunction(
"create"))
114 if(lua_pcall(L, 1, 1, 0) != 0)
123 result = lua_toboolean(L, -1);
129 bHasAcceptCallback = getLocalFunction(
"accept");
133 bHasUpdateCallback = getLocalFunction(
"update");
137 bHasUpdateReplyCallback = getLocalFunction(
"update_reply");
146 if(getLocalFunction(
"accept"))
160 if(lua_pcall(L, 1, 1, 0) != 0)
169 bool result = lua_toboolean(L, -1);
184 if(getLocalFunction(
"update"))
198 if(lua_pcall(L, 1, 1, 0) != 0)
231 if(getLocalFunction(
"update_reply"))
245 if(lua_pcall(L, 1, 1, 0) != 0)
278 if(getLocalFunction(
"setparam"))
292 if(lua_pcall(L, 1, 0, 0) != 0)
311 if(getLocalFunction(
"getparam"))
324 if(lua_pcall(L, 0, 1, 0) != 0)
358 if(getLocalFunction(
"trig"))
360 if(lua_pcall(L, 0, 0, 0) != 0)
377 bool MonitorLua::getLocalFunction(
const char *name)
379 lua_pushstring(L, name);
381 return (lua_isfunction(L, -1) == 1);
385 bool MonitorLua::registerExtraFunctions()
387 #if LUA_VERSION_NUM > 501
389 luaL_setfuncs (L, MonitorLua::portMonitorLib, 0);
390 lua_pushvalue(L, -1);
391 lua_setglobal(L,
"PortMonitor");
395 luaL_register(L,
"PortMonitor", MonitorLua::portMonitorLib);
403 if (constraint ==
"") {
413 std::string strConstraint = constraint;
414 std::string strDummy = strConstraint;
415 searchReplace(strDummy,
"(",
" ");
416 searchReplace(strDummy,
")",
" ");
418 strDummy =
" " + strDummy +
" ";
419 std::string delimiter =
" ";
422 while ((pos = strDummy.find(delimiter)) != std::string::npos)
424 token = strDummy.substr(0, pos);
425 if(token.size() && !isKeyword(token.c_str()))
428 std::string value = (record.
hasEvent(token.c_str())) ?
"true" :
"false";
430 searchReplace(strConstraint, token, value);
432 strDummy.erase(0, pos + delimiter.length());
441 strConstraint =
"return " + strConstraint;
443 if(luaL_dostring(L, strConstraint.c_str()) != 0)
449 if(!lua_isboolean(L, -1))
455 bool accepted = (lua_toboolean(L,-1) == 1);
461 inline void MonitorLua::searchReplace(std::string& str,
const std::string& oldStr,
const std::string& newStr)
464 while((pos = str.find(oldStr, pos)) != std::string::npos)
466 str.replace(pos, oldStr.length(), newStr);
467 pos += newStr.length();
471 inline void MonitorLua::trimString(std::string& str)
473 std::string::size_type pos = str.find_last_not_of(
' ');
474 if(pos != std::string::npos) {
476 pos = str.find_first_not_of(
' ');
477 if (pos != std::string::npos) {
481 str.erase(str.begin(), str.end());
485 inline bool MonitorLua::isKeyword(
const char* str)
491 std::string token = str;
492 if ((token ==
"true") || (token ==
"false") || (token ==
"and") || (token ==
"or") || (token ==
"not")) {
503 int MonitorLua::setConstraint(lua_State* L)
505 const char *cst = luaL_checkstring(L, 1);
508 lua_getglobal(L,
"PortMonitor_Owner");
509 if(!lua_islightuserdata(L, -1))
515 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
517 owner->setAcceptConstraint(cst);
522 int MonitorLua::getConstraint(lua_State* L)
524 lua_getglobal(L,
"PortMonitor_Owner");
525 if(!lua_islightuserdata(L, -1))
531 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
533 lua_pushstring(L, owner->getAcceptConstraint());
538 int MonitorLua::setEvent(lua_State* L)
540 double lifetime = -1.0;
541 int n_args = lua_gettop(L);
542 const char *event_name = luaL_checkstring(L, 1);
548 if (lua_isnumber(L, 2)) {
549 lifetime = (double) luaL_checknumber(L,2);
556 lua_getglobal(L,
"PortMonitor_Owner");
557 if(!lua_islightuserdata(L, -1))
562 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
564 if (owner->isKeyword(event_name)) {
569 record.
setEvent(event_name, owner, lifetime);
575 int MonitorLua::unsetEvent(lua_State* L)
577 const char *event_name = luaL_checkstring(L, 1);
580 lua_getglobal(L,
"PortMonitor_Owner");
581 if(!lua_islightuserdata(L, -1))
586 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
588 if (owner->isKeyword(event_name)) {
599 int MonitorLua::setTrigInterval(lua_State* L)
602 int n_args = lua_gettop(L);
604 if (lua_isnumber(L, 1)) {
605 period = (double) luaL_checknumber(L,1);
616 lua_getglobal(L,
"PortMonitor_Owner");
617 if(!lua_islightuserdata(L, -1))
623 auto* owner =
static_cast<MonitorLua*
>(lua_touserdata(L, -1));
627 if(owner->trigger ==
nullptr) {
629 owner->trigger->start();
635 #if LUA_VERSION_NUM > 501
636 const struct luaL_Reg
MonitorLua::portMonitorLib [] = {
640 {
"setConstraint", MonitorLua::setConstraint},
641 {
"getConstraint", MonitorLua::getConstraint},
642 {
"setEvent", MonitorLua::setEvent},
643 {
"unsetEvent", MonitorLua::unsetEvent},
644 {
"setTrigInterval", MonitorLua::setTrigInterval},
const yarp::os::LogComponent & PORTMONITORCARRIER()
A singleton class to record the port monitor events.
void setEvent(const char *name, MonitorBinding *owner, double lifetime=-1.0)
void unsetEvent(const char *name, MonitorBinding *owner)
static MonitorEventRecord & getInstance()
bool hasEvent(const char *name)
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for storing options and configuration information.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Base class for generic things.
virtual std::string asString() const
Get string value.
#define yCError(component,...)
#define yCAssert(component, x)
#define yCTrace(component,...)
An interface to the operating system, including Port based communication.
MonitorLua()
Class MonitorLua.
bool getParams(yarp::os::Property ¶ms) override
bool setParams(const yarp::os::Property ¶ms) override
bool canAccept() override
bool acceptData(yarp::os::Things &thing) override
yarp::os::Things & updateReply(yarp::os::Things &thing) override
bool peerTrigged() override
yarp::os::Things & updateData(yarp::os::Things &thing) override
bool load(const yarp::os::Property &options) override
static int SWIG_Lua_ConvertPtr(lua_State *L, int index, void **ptr, swig_type_info *type, int flags)
#define SWIG_NewPointerObj(L, ptr, type, owner)
static swig_type_info * SWIG_TypeQuery(lua_State *clientdata, const char *name)