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 
30 
31 #include <ArmarXCore/interface/observers/VariantBase.h>
32 #include <ArmarXCore/interface/observers/SystemObserverInterface.h>
33 
36 
37 #include <string>
38 #include <mutex>
39 #include <map>
40 
41 
42 namespace armarx
43 {
44 #define ARMARX_SYSTEM_OBSERVER SystemObserver
51 
52  /**
53  * @ingroup ObserversSub
54  */
56  {
57  std::string timerName;
59  int elapsedMs;
60  bool paused;
61  };
62 
63  using SystemObserverTimerMap = std::map<std::string, SystemObserverTimer>;
64 
65  /**
66  * @ingroup ObserversSub
67  */
69  {
70  std::string counterName;
71  int value;
72  };
73 
74  using SystemObserverCounterMap = std::map<std::string, SystemObserverCounter>;
75 
76  /**
77  * @defgroup Component-SystemObserver SystemObserver
78  * @ingroup ObserversSub ArmarXCore-Components
79  * The SystemObserver component offers functionality to create timers and distributed counters.
80  * Since the SystemObserver is an \ref Observers "observer" it is possible to install \ref Conditions "conditions"
81  * on the timers (e.g. for timeouts) and counters.
82  *
83  * @class SystemObserver
84  * @ingroup Component-SystemObserver
85  * @brief Provides timers for timeout events and counters
86  */
88  virtual public Observer,
89  virtual public SystemObserverInterface
90  {
91  public:
92  // framework hooks
93  void onInitObserver() override;
94  void onConnectObserver() override {}
95  void onExitObserver() override {}
96 
97  std::string getDefaultName() const override
98  {
99  return "SystemObserver";
100  }
101 
102  // implementation of SystemObserverInterface
103  // timers
104  /**
105  Creates a new timer with name \p timerBaseName and starts it.
106  @param timerBaseName This is only the basename of the counter to make it human-comprehensable.
107  To get the real countername, check the channelName value of the return value
108  */
109  void startTimer_async(
110  const AMD_SystemObserverInterface_startTimerPtr& amd,
111  const std::string& timerBaseName,
112  const Ice::Current& c = Ice::emptyCurrent) override;
113  /**
114  * @brief resetTimer sets the start time of \p timer to Time.Now() and start the timer
115  * @param timer ChannelRef obtained via SystemObserver::startTimer()
116  */
117  void resetTimer_async(
118  const AMD_SystemObserverInterface_resetTimerPtr& amd,
119  const ChannelRefBasePtr& timer,
120  const Ice::Current& c = Ice::emptyCurrent) override;
121  /**
122  * @brief pauseTimer pauses the advance of time in \p timer. Can be resumed with SystemObserver::unpauseTimer().
123  * @param timer ChannelRef obtained via SystemObserver::startTimer()
124  * @see unpauseTimer
125  */
126  void pauseTimer_async(
127  const AMD_SystemObserverInterface_pauseTimerPtr& amd,
128  const ChannelRefBasePtr& timer,
129  const Ice::Current& c = Ice::emptyCurrent) override;
130  /**
131  * @brief unpauseTimer resumes the advancing in time of \p timer. Call this after pausing a timer with SystemObserver::pauseTimer().
132  * @param timer ChannelRef obtained via SystemObserver::startTimer()
133  * @see pauseTimer
134  */
135  void unpauseTimer_async(
136  const AMD_SystemObserverInterface_unpauseTimerPtr& amd,
137  const ChannelRefBasePtr& timer,
138  const Ice::Current& c = Ice::emptyCurrent) override;
139  /**
140  * @brief removeTimer stops \p timer and removes it from the SystemObserver.
141  * @param timer ChannelRef obtained via SystemObserver::startTimer()
142  */
143  void removeTimer_async(
144  const AMD_SystemObserverInterface_removeTimerPtr& amd,
145  const ChannelRefBasePtr& timer,
146  const Ice::Current& c = Ice::emptyCurrent) override;
147 
148  // counters
149  /**
150  Creates a new counter and starts it.
151  @param counterBaseName This is only the basename of the counter to make it human-comprehensable.
152  To get the real countername, check the channelName value of the return value
153  */
154  void startCounter_async(
155  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(
160  const AMD_SystemObserverInterface_incrementCounterPtr& amd,
161  const ChannelRefBasePtr& counter,
162  const Ice::Current& c = Ice::emptyCurrent) override;
163  void decrementCounter_async(
164  const AMD_SystemObserverInterface_decrementCounterPtr& amd,
165  const ChannelRefBasePtr& counter,
166  const Ice::Current& c = Ice::emptyCurrent) override;
167  void resetCounter_async(
168  const AMD_SystemObserverInterface_resetCounterPtr& amd,
169  const ChannelRefBasePtr& counter,
170  const Ice::Current& c = Ice::emptyCurrent) override;
171  void setCounter_async(
172  const AMD_SystemObserverInterface_setCounterPtr& amd,
173  const ChannelRefBasePtr& counter,
174  int counterValue,
175  const Ice::Current& c = Ice::emptyCurrent) override;
176  void removeCounter_async(
177  const AMD_SystemObserverInterface_removeCounterPtr& amd,
178  const ChannelRefBasePtr& counter,
179  const Ice::Current& c = Ice::emptyCurrent) override;
180 
181  protected:
182  void postWorkerJobs() override;
183 
184  private:
185  // timer tools
186  void resetTimer(SystemObserverTimer& timer);
187  void updateTimer(SystemObserverTimer& timer);
188  int getCurrentTimeMs();
189  int getElapsedTimeMs(int referenceTimeMs);
190 
191  // counter tools
192  void updateCounter(SystemObserverCounterMap::iterator& iterCounter);
193 
194  SystemObserverTimerMap timers;
195  std::recursive_mutex timersMutex;
196 
197  SystemObserverCounterMap counters;
198  std::recursive_mutex countersMutex;
199  int maxTimerId;
200  int maxCounterId;
201  };
202 }
203 
armarx::SystemObserver::onExitObserver
void onExitObserver() override
Framework hook.
Definition: SystemObserver.h:95
armarx::SystemObserverCounter::value
int value
Definition: SystemObserver.h:71
armarx::SystemObserver
Provides timers for timeout events and counters.
Definition: SystemObserver.h:87
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:80
armarx::SystemObserverTimer::timerName
std::string timerName
Definition: SystemObserver.h:57
armarx::SystemObserverCounter::counterName
std::string counterName
Definition: SystemObserver.h:70
armarx::SystemObserverTimer::startTimeMs
int startTimeMs
Definition: SystemObserver.h:58
PeriodicTask.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::SystemObserver::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: SystemObserver.h:97
Observer.h
armarx::SystemObserverTimer
Definition: SystemObserver.h:55
armarx::SystemObserverTimerMap
std::map< std::string, SystemObserverTimer > SystemObserverTimerMap
Definition: SystemObserver.h:63
armarx::SystemObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: SystemObserver.h:94
armarx::SystemObserverCounterMap
std::map< std::string, SystemObserverCounter > SystemObserverCounterMap
Definition: SystemObserver.h:74
armarx::SystemObserverTimer::elapsedMs
int elapsedMs
Definition: SystemObserver.h:59
ARMARX_CREATE_CHECK
#define ARMARX_CREATE_CHECK(OFFERER, NEWCHECK)
Definition: ConditionCheck.h:205
armarx::SystemObserverCounter
Definition: SystemObserver.h:68
Component.h
armarx::SystemObserverTimer::paused
bool paused
Definition: SystemObserver.h:60
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:44
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28