YARP
Yet Another Robot Platform
MonitorEvent.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 MONITOREVENT_INC
10 #define MONITOREVENT_INC
11 
12 #include <string>
13 #include <mutex>
14 #include <yarp/os/Time.h>
15 #include <vector>
16 
17 
18 class MonitorBinding;
19 
21 {
22 public:
23  MonitorEvent(const char* _name, MonitorBinding* _owner, double lf=-1.0) :
24  owner(_owner),
25  name(_name ? _name : ""),
26  lifetime(lf), // default: infinite life time
27  create_time(yarp::os::Time::now())
28  {
29  }
30 
31  MonitorBinding* owner; // event's owner
32  std::string name; // event's symbolic name
33  double lifetime; // event's life time in second. negative value means infinite life time.
34  double create_time; // event's creation or updating time
35 };
36 
37 
42 {
43 public:
44  typedef std::vector<MonitorEvent> vector_type;
45  typedef vector_type::iterator iterator;
46  typedef vector_type::const_iterator const_iterator;
47 
48  void setEvent(const char* name, MonitorBinding* owner, double lifetime=-1.0)
49  {
50  // if event already exists just update the create_time and lifetime
51  MonitorEventRecord::iterator itr = findEvent(name, owner);
52  if(itr != events.end())
53  {
54  (*itr).create_time = yarp::os::Time::now();
55  (*itr).lifetime = lifetime;
56  return;
57  }
58  events.push_back(MonitorEvent(name, owner, lifetime));
59  }
60 
61  void unsetEvent(const char* name, MonitorBinding* owner)
62  {
63  MonitorEventRecord::iterator itr = findEvent(name, owner);
64  if(itr == events.end()) {
65  return;
66  }
67  events.erase(itr);
68  }
69 
70  bool hasEvent(const char* name)
71  {
73  for(itr=events.begin(); itr<events.end(); itr++) {
74  if((*itr).name == name)
75  {
76  if((*itr).lifetime < 0.0) {
77  return true;
78  }
79  if((yarp::os::Time::now() - (*itr).create_time) < (*itr).lifetime) {
80  return true;
81  }
82  events.erase(itr); // remove expired event
83  return false;
84  }
85  }
86  return false;
87  }
88 
89  void lock()
90  {
91  mutex.lock();
92  }
93 
94  void unlock()
95  {
96  mutex.unlock();
97  }
98 
100  {
101  static MonitorEventRecord __instance_MonitorEventRecord;
102  return __instance_MonitorEventRecord;
103  }
104 
105 private:
106  MonitorEventRecord() = default;
107  MonitorEventRecord(MonitorEventRecord const &) = delete;
108  void operator=(MonitorEventRecord const &) = delete;
109 
110  MonitorEventRecord::iterator findEvent(const char* name, MonitorBinding* owner)
111  {
113  for(itr=events.begin(); itr<events.end(); itr++) {
114  if (((*itr).name == name) && ((*itr).owner == owner)) {
115  return itr;
116  }
117  }
118  return events.end();
119  }
120 
121 
122 private:
123  vector_type events;
124  std::mutex mutex;
125 };
126 
127 #endif // MONITOREVENT_INC
MonitorBinding
Definition: MonitorBinding.h:19
std_msgs::Time
yarp::rosmsg::std_msgs::Time Time
Definition: Time.h:24
MonitorEvent::name
std::string name
Definition: MonitorEvent.h:32
MonitorEventRecord::unsetEvent
void unsetEvent(const char *name, MonitorBinding *owner)
Definition: MonitorEvent.h:61
MonitorEvent::MonitorEvent
MonitorEvent(const char *_name, MonitorBinding *_owner, double lf=-1.0)
Definition: MonitorEvent.h:23
MonitorEventRecord
A singleton class to record the port monitor events.
Definition: MonitorEvent.h:42
MonitorEvent
Definition: MonitorEvent.h:21
MonitorEventRecord::unlock
void unlock()
Definition: MonitorEvent.h:94
MonitorEventRecord::hasEvent
bool hasEvent(const char *name)
Definition: MonitorEvent.h:70
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
MonitorEvent::create_time
double create_time
Definition: MonitorEvent.h:34
MonitorEvent::owner
MonitorBinding * owner
Definition: MonitorEvent.h:31
MonitorEventRecord::setEvent
void setEvent(const char *name, MonitorBinding *owner, double lifetime=-1.0)
Definition: MonitorEvent.h:48
MonitorEventRecord::getInstance
static MonitorEventRecord & getInstance()
Definition: MonitorEvent.h:99
MonitorEventRecord::vector_type
std::vector< MonitorEvent > vector_type
Definition: MonitorEvent.h:44
MonitorEvent::lifetime
double lifetime
Definition: MonitorEvent.h:33
MonitorEventRecord::iterator
vector_type::iterator iterator
Definition: MonitorEvent.h:45
MonitorEventRecord::const_iterator
vector_type::const_iterator const_iterator
Definition: MonitorEvent.h:46
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Time.h
MonitorEventRecord::lock
void lock()
Definition: MonitorEvent.h:89