SystemObserver.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::Core
19 * @author Kai Welke (welke _at_ kit _dot_ edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <map>
28 #include <mutex>
29 #include <string>
30 
34 #include <ArmarXCore/interface/observers/SystemObserverInterface.h>
35 #include <ArmarXCore/interface/observers/VariantBase.h>
38 
39 namespace armarx
40 {
41 #define ARMARX_SYSTEM_OBSERVER SystemObserver
48 
49  /**
50  * @ingroup ObserversSub
51  */
53  {
54  std::string timerName;
56  int elapsedMs;
57  bool paused;
58  };
59 
60  using SystemObserverTimerMap = std::map<std::string, SystemObserverTimer>;
61 
62  /**
63  * @ingroup ObserversSub
64  */
66  {
67  std::string counterName;
68  int value;
69  };
70 
71  using SystemObserverCounterMap = std::map<std::string, SystemObserverCounter>;
72 
73  /**
74  * @defgroup Component-SystemObserver SystemObserver
75  * @ingroup ObserversSub ArmarXCore-Components
76  * The SystemObserver component offers functionality to create timers and distributed counters.
77  * Since the SystemObserver is an \ref Observers "observer" it is possible to install \ref Conditions "conditions"
78  * on the timers (e.g. for timeouts) and counters.
79  *
80  * @class SystemObserver
81  * @ingroup Component-SystemObserver
82  * @brief Provides timers for timeout events and counters
83  */
85  virtual public Observer,
86  virtual public SystemObserverInterface
87  {
88  public:
89  // framework hooks
90  void onInitObserver() override;
91 
92  void
93  onConnectObserver() override
94  {
95  }
96 
97  void
98  onExitObserver() override
99  {
100  }
101 
102  std::string
103  getDefaultName() const override
104  {
105  return "SystemObserver";
106  }
107 
108  // implementation of SystemObserverInterface
109  // timers
110  /**
111  Creates a new timer with name \p timerBaseName and starts it.
112  @param timerBaseName This is only the basename of the counter to make it human-comprehensable.
113  To get the real countername, check the channelName value of the return value
114  */
115  void startTimer_async(const AMD_SystemObserverInterface_startTimerPtr& amd,
116  const std::string& timerBaseName,
117  const Ice::Current& c = Ice::emptyCurrent) override;
118  /**
119  * @brief resetTimer sets the start time of \p timer to Time.Now() and start the timer
120  * @param timer ChannelRef obtained via SystemObserver::startTimer()
121  */
122  void resetTimer_async(const AMD_SystemObserverInterface_resetTimerPtr& amd,
123  const ChannelRefBasePtr& timer,
124  const Ice::Current& c = Ice::emptyCurrent) override;
125  /**
126  * @brief pauseTimer pauses the advance of time in \p timer. Can be resumed with SystemObserver::unpauseTimer().
127  * @param timer ChannelRef obtained via SystemObserver::startTimer()
128  * @see unpauseTimer
129  */
130  void pauseTimer_async(const AMD_SystemObserverInterface_pauseTimerPtr& amd,
131  const ChannelRefBasePtr& timer,
132  const Ice::Current& c = Ice::emptyCurrent) override;
133  /**
134  * @brief unpauseTimer resumes the advancing in time of \p timer. Call this after pausing a timer with SystemObserver::pauseTimer().
135  * @param timer ChannelRef obtained via SystemObserver::startTimer()
136  * @see pauseTimer
137  */
138  void unpauseTimer_async(const AMD_SystemObserverInterface_unpauseTimerPtr& amd,
139  const ChannelRefBasePtr& timer,
140  const Ice::Current& c = Ice::emptyCurrent) override;
141  /**
142  * @brief removeTimer stops \p timer and removes it from the SystemObserver.
143  * @param timer ChannelRef obtained via SystemObserver::startTimer()
144  */
145  void removeTimer_async(const AMD_SystemObserverInterface_removeTimerPtr& amd,
146  const ChannelRefBasePtr& timer,
147  const Ice::Current& c = Ice::emptyCurrent) override;
148 
149  // counters
150  /**
151  Creates a new counter and starts it.
152  @param counterBaseName This is only the basename of the counter to make it human-comprehensable.
153  To get the real countername, check the channelName value of the return value
154  */
155  void startCounter_async(const AMD_SystemObserverInterface_startCounterPtr& amd,
156  int initialValue,
157  const std::string& counterBaseName,
158  const Ice::Current& c = Ice::emptyCurrent) override;
159  void incrementCounter_async(const AMD_SystemObserverInterface_incrementCounterPtr& amd,
160  const ChannelRefBasePtr& counter,
161  const Ice::Current& c = Ice::emptyCurrent) override;
162  void decrementCounter_async(const AMD_SystemObserverInterface_decrementCounterPtr& amd,
163  const ChannelRefBasePtr& counter,
164  const Ice::Current& c = Ice::emptyCurrent) override;
165  void resetCounter_async(const AMD_SystemObserverInterface_resetCounterPtr& amd,
166  const ChannelRefBasePtr& counter,
167  const Ice::Current& c = Ice::emptyCurrent) override;
168  void setCounter_async(const AMD_SystemObserverInterface_setCounterPtr& amd,
169  const ChannelRefBasePtr& counter,
170  int counterValue,
171  const Ice::Current& c = Ice::emptyCurrent) override;
172  void removeCounter_async(const AMD_SystemObserverInterface_removeCounterPtr& amd,
173  const ChannelRefBasePtr& counter,
174  const Ice::Current& c = Ice::emptyCurrent) override;
175 
176  protected:
177  void postWorkerJobs() override;
178 
179  private:
180  // timer tools
181  void resetTimer(SystemObserverTimer& timer);
182  void updateTimer(SystemObserverTimer& timer);
183  int getCurrentTimeMs();
184  int getElapsedTimeMs(int referenceTimeMs);
185 
186  // counter tools
187  void updateCounter(SystemObserverCounterMap::iterator& iterCounter);
188 
189  SystemObserverTimerMap timers;
190  std::recursive_mutex timersMutex;
191 
192  SystemObserverCounterMap counters;
193  std::recursive_mutex countersMutex;
194  int maxTimerId;
195  int maxCounterId;
196  };
197 } // namespace armarx
armarx::SystemObserver::onExitObserver
void onExitObserver() override
Framework hook.
Definition: SystemObserver.h:98
armarx::SystemObserverCounter::value
int value
Definition: SystemObserver.h:68
armarx::SystemObserver
Provides timers for timeout events and counters.
Definition: SystemObserver.h:84
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:84
armarx::SystemObserverTimer::timerName
std::string timerName
Definition: SystemObserver.h:54
armarx::SystemObserverCounter::counterName
std::string counterName
Definition: SystemObserver.h:67
armarx::SystemObserverTimer::startTimeMs
int startTimeMs
Definition: SystemObserver.h:55
PeriodicTask.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::SystemObserver::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: SystemObserver.h:103
Observer.h
armarx::SystemObserverTimer
Definition: SystemObserver.h:52
armarx::SystemObserverTimerMap
std::map< std::string, SystemObserverTimer > SystemObserverTimerMap
Definition: SystemObserver.h:60
armarx::SystemObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: SystemObserver.h:93
armarx::SystemObserverCounterMap
std::map< std::string, SystemObserverCounter > SystemObserverCounterMap
Definition: SystemObserver.h:71
armarx::SystemObserverTimer::elapsedMs
int elapsedMs
Definition: SystemObserver.h:56
ARMARX_CREATE_CHECK
#define ARMARX_CREATE_CHECK(OFFERER, NEWCHECK)
Definition: ConditionCheck.h:222
armarx::SystemObserverCounter
Definition: SystemObserver.h:65
Component.h
armarx::SystemObserverTimer::paused
bool paused
Definition: SystemObserver.h:57
ImportExport.h
ConditionCheck.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
ARMARX_SYSTEM_OBSERVER
#define ARMARX_SYSTEM_OBSERVER
Definition: SystemObserver.h:41
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27