YARP
Yet Another Robot Platform
MonitorLua.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef MONITORLUA_INC
10 #define MONITORLUA_INC
11 
12 #include <string>
13 #include <string>
14 #include <yarp/os/PeriodicThread.h>
15 #include "MonitorBinding.h"
16 #include "swigluarun.h"
17 #include <mutex>
18 
19 class MonitorTrigger;
20 
21 class MonitorLua : public MonitorBinding
22 {
23 
24 public:
25  MonitorLua();
26  ~MonitorLua() override;
27 
28  bool load(const yarp::os::Property& options) override;
29  bool setParams(const yarp::os::Property& params) override;
30  bool getParams(yarp::os::Property& params) override;
31 
32  bool acceptData(yarp::os::Things& thing) override;
35 
36  bool peerTrigged() override;
37  bool canAccept() override;
38 
39  bool setAcceptConstraint(const char* constraint) override {
40  if(!constraint) {
41  return false;
42  }
43  MonitorLua::constraint = constraint;
44  trimString(MonitorLua::constraint);
45  return true;
46  }
47 
48  const char* getAcceptConstraint() override {
49  return constraint.c_str();
50  }
51 
52  bool hasAccept() override {
53  return bHasAcceptCallback;
54  }
55 
56  bool hasUpdate() override {
57  return bHasUpdateCallback;
58  }
59 
60  bool hasUpdateReply() override {
61  return bHasUpdateReplyCallback;
62  }
63 
64 private:
65  lua_State *L;
66  std::string constraint;
67  bool bHasAcceptCallback;
68  bool bHasUpdateCallback;
69  bool bHasUpdateReplyCallback;
70  std::recursive_mutex luaMutex;
71 
72 public:
74 
75 private:
76  bool getLocalFunction(const char *name);
77  bool registerExtraFunctions();
78  void trimString(std::string& str);
79  void searchReplace(std::string& str,
80  const std::string& oldStr, const std::string& newStr);
81  bool isKeyword(const char* str);
82 
83  /* lua accessible function*/
84  static int setConstraint(lua_State* L);
85  static int getConstraint(lua_State* L);
86  static int setEvent(lua_State* L);
87  static int unsetEvent(lua_State* L);
88  static int setTrigInterval(lua_State* L);
89 #if LUA_VERSION_NUM > 501
90  static const struct luaL_Reg portMonitorLib[];
91 #else
92  static const struct luaL_reg portMonitorLib[];
93 #endif
94 
95 };
96 
98 public:
99  MonitorTrigger(MonitorLua* monitor, double period)
100  : yarp::os::PeriodicThread(period) {
101  MonitorTrigger::monitor = monitor;
102  }
103  virtual ~MonitorTrigger() { }
104 
105  // inherited from the yarp::os::RateThread
106  void run () override {
107  monitor->peerTrigged();
108  }
109 
110 private:
111  MonitorLua* monitor;
112 };
113 
114 
115 #endif //_MONITORLUA_INC_
MonitorBinding
Definition: MonitorBinding.h:19
MonitorLua::canAccept
bool canAccept() override
Definition: MonitorLua.cpp:404
MonitorLua::~MonitorLua
~MonitorLua() override
Definition: MonitorLua.cpp:42
MonitorLua::getParams
bool getParams(yarp::os::Property &params) override
Definition: MonitorLua.cpp:311
MonitorLua::load
bool load(const yarp::os::Property &options) override
Definition: MonitorLua.cpp:62
MonitorTrigger::MonitorTrigger
MonitorTrigger(MonitorLua *monitor, double period)
Definition: MonitorLua.h:99
yarp::os::PeriodicThread::PeriodicThread
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No)
Constructor.
Definition: PeriodicThread.cpp:271
yarp::os::Things
Base class for generic things.
Definition: Things.h:22
MonitorTrigger
Definition: MonitorLua.h:97
MonitorLua
Definition: MonitorLua.cpp:635
swigluarun.h
MonitorLua::hasAccept
bool hasAccept() override
Definition: MonitorLua.h:52
MonitorLua::updateData
yarp::os::Things & updateData(yarp::os::Things &thing) override
Definition: MonitorLua.cpp:184
MonitorTrigger::run
void run() override
Loop function.
Definition: MonitorLua.h:106
MonitorTrigger::~MonitorTrigger
virtual ~MonitorTrigger()
Definition: MonitorLua.h:103
MonitorLua::MonitorLua
MonitorLua()
Class MonitorLua.
Definition: MonitorLua.cpp:23
MonitorLua::getAcceptConstraint
const char * getAcceptConstraint() override
Definition: MonitorLua.h:48
MonitorLua::setParams
bool setParams(const yarp::os::Property &params) override
Definition: MonitorLua.cpp:278
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
PeriodicThread.h
MonitorLua::setAcceptConstraint
bool setAcceptConstraint(const char *constraint) override
Definition: MonitorLua.h:39
MonitorLua::hasUpdateReply
bool hasUpdateReply() override
Definition: MonitorLua.h:60
MonitorLua::peerTrigged
bool peerTrigged() override
Definition: MonitorLua.cpp:358
MonitorLua::hasUpdate
bool hasUpdate() override
Definition: MonitorLua.h:56
MonitorLua::acceptData
bool acceptData(yarp::os::Things &thing) override
Definition: MonitorLua.cpp:146
MonitorLua::updateReply
yarp::os::Things & updateReply(yarp::os::Things &thing) override
Definition: MonitorLua.cpp:231
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
MonitorLua::trigger
MonitorTrigger * trigger
Definition: MonitorLua.h:73
MonitorBinding.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37