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
39namespace armarx
40{
41#define ARMARX_SYSTEM_OBSERVER SystemObserver
48
49 /**
50 * @ingroup ObserversSub
51 */
53 {
54 std::string timerName;
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
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
190 std::recursive_mutex timersMutex;
191
193 std::recursive_mutex countersMutex;
194 int maxTimerId;
195 int maxCounterId;
196 };
197} // namespace armarx
#define ARMARX_CREATE_CHECK(OFFERER, NEWCHECK)
#define ARMARXCORE_IMPORT_EXPORT
#define ARMARX_SYSTEM_OBSERVER
constexpr T c
Provides timers for timeout events and counters.
void onConnectObserver() override
Framework hook.
void onExitObserver() override
Framework hook.
void onInitObserver() override
Framework hook.
std::string getDefaultName() const override
Retrieve default name of component.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::map< std::string, SystemObserverTimer > SystemObserverTimerMap
std::map< std::string, SystemObserverCounter > SystemObserverCounterMap