36 #include <IceUtil/Time.h>
59 std::unique_lock lock(timersMutex);
60 SystemObserverTimerMap::iterator iter = timers.begin();
62 while (iter != timers.end())
67 updateTimer(iter->second);
70 setDataField(iter->second.timerName,
"elapsedMs",
int(iter->second.elapsedMs));
73 catch (armarx::LocalException& e)
77 catch (armarx::UserException& e)
89 const AMD_SystemObserverInterface_startTimerPtr& amd,
90 const std::string& timerBaseName,
91 const Ice::Current&
c)
93 addWorkerJob(
"SystemObserver::startTimer", [
this, amd, timerBaseName]
95 std::stringstream temp;
96 temp << ++maxTimerId <<
"_" << timerBaseName;
97 std::string timerName = temp.str();
99 std::unique_lock lock(timersMutex);
101 if (timers.find(timerName) != timers.end())
103 std::string reason =
"Timer " + timerName +
" already exists";
104 amd->ice_exception(InvalidTimerException(reason.c_str()));
112 std::pair<std::string, SystemObserverTimer> entry;
113 entry.first = timerName;
114 entry.second = timer;
116 timers.insert(entry);
126 amd->ice_response(
new ChannelRef(
this, timerName));
131 const AMD_SystemObserverInterface_resetTimerPtr& amd,
132 const ChannelRefBasePtr& timer,
133 const Ice::Current&
c)
135 addWorkerJob(
"SystemObserver::resetTimer", [
this, amd, timer]
137 std::unique_lock lock(timersMutex);
138 std::string timerName = ChannelRefPtr::dynamicCast(timer)->getChannelName();
140 SystemObserverTimerMap::iterator iter = timers.find(timerName);
142 if (iter != timers.end())
144 resetTimer(iter->second);
148 std::string reason =
"Timer " + timerName +
" unknown";
149 amd->ice_exception(InvalidTimerException(reason.c_str()));
156 const AMD_SystemObserverInterface_pauseTimerPtr& amd,
157 const ChannelRefBasePtr& timer,
158 const Ice::Current&
c)
160 addWorkerJob(
"SystemObserver::pauseTimer", [
this, amd, timer]
162 std::unique_lock lock(timersMutex);
163 std::string timerName = ChannelRefPtr::dynamicCast(timer)->getChannelName();
165 SystemObserverTimerMap::iterator iter = timers.find(timerName);
167 if (iter != timers.end())
169 iter->second.paused =
true;
173 std::string reason =
"Timer " + timerName +
" unknown";
174 amd->ice_exception(InvalidTimerException(reason.c_str()));
181 const AMD_SystemObserverInterface_unpauseTimerPtr& amd,
182 const ChannelRefBasePtr& timer,
183 const Ice::Current&
c)
185 addWorkerJob(
"SystemObserver::unpauseTimer", [
this, amd, timer]
187 std::unique_lock lock(timersMutex);
188 std::string timerName = ChannelRefPtr::dynamicCast(timer)->getChannelName();
190 SystemObserverTimerMap::iterator iter = timers.find(timerName);
192 if (iter != timers.end())
194 iter->second.paused =
false;
195 iter->second.startTimeMs += getElapsedTimeMs(iter->second.startTimeMs + iter->second.elapsedMs);
199 std::string reason =
"Timer " + timerName +
" unknown";
200 amd->ice_exception(InvalidTimerException(reason.c_str()));
207 const AMD_SystemObserverInterface_removeTimerPtr& amd,
208 const ChannelRefBasePtr& timer,
209 const Ice::Current&
c)
211 addWorkerJob(
"SystemObserver::removeTimer", [
this, amd, timer]
213 std::unique_lock lock(timersMutex);
214 std::string timerName = ChannelRefPtr::dynamicCast(timer)->getChannelName();
216 SystemObserverTimerMap::iterator iter = timers.find(timerName);
218 if (iter != timers.end())
233 const AMD_SystemObserverInterface_startCounterPtr& amd,
235 const std::string& counterBaseName,
236 const Ice::Current&
c)
238 addWorkerJob(
"SystemObserver::startCounter", [
this, amd, initialValue, counterBaseName]
240 std::stringstream temp;
241 temp << ++maxCounterId <<
"_" << counterBaseName;
242 std::string counterName = temp.str();
244 std::unique_lock lock(countersMutex);
246 if (counters.find(counterName) != counters.end())
248 std::string reason =
"Counter " + counterName +
" already exists";
249 amd->ice_exception(InvalidCounterException(reason.c_str()));
257 counter.
value = initialValue;
259 std::pair<std::string, SystemObserverCounter> entry;
260 entry.first = counterName;
261 entry.second = counter;
264 SystemObserverCounterMap::iterator iter;
265 iter = (counters.insert(entry)).first;
275 amd->ice_response(
new ChannelRef(
this, counterName));
281 const AMD_SystemObserverInterface_incrementCounterPtr& amd,
282 const ChannelRefBasePtr& counter,
283 const Ice::Current&
c)
285 addWorkerJob(
"SystemObserver::incrementCounter", [
this, amd, counter]
287 std::unique_lock lock(countersMutex);
288 std::string counterName = ChannelRefPtr::dynamicCast(counter)->getChannelName();
290 SystemObserverCounterMap::iterator iter = counters.find(counterName);
292 if (iter != counters.end())
294 iter->second.value++;
298 std::string reason =
"Counter " + counterName +
" unknown";
299 amd->ice_exception(InvalidCounterException(reason.c_str()));
303 amd->ice_response(iter->second.value);
308 const AMD_SystemObserverInterface_decrementCounterPtr& amd,
309 const ChannelRefBasePtr& counter,
310 const Ice::Current&
c)
312 addWorkerJob(
"SystemObserver::decrementCounter", [
this, amd, counter]
314 std::unique_lock lock(countersMutex);
315 std::string counterName = ChannelRefPtr::dynamicCast(counter)->getChannelName();
317 SystemObserverCounterMap::iterator iter = counters.find(counterName);
319 if (iter != counters.end())
321 iter->second.value--;
325 std::string reason =
"Counter " + counterName +
" unknown";
326 amd->ice_exception(InvalidCounterException(reason.c_str()));
330 amd->ice_response(iter->second.value);
335 const AMD_SystemObserverInterface_resetCounterPtr& amd,
336 const ChannelRefBasePtr& counter,
337 const Ice::Current&
c)
339 addWorkerJob(
"SystemObserver::resetCounter", [
this, amd, counter]
341 std::unique_lock lock(countersMutex);
342 std::string counterName = ChannelRefPtr::dynamicCast(counter)->getChannelName();
344 SystemObserverCounterMap::iterator iter = counters.find(counterName);
346 if (iter != counters.end())
348 iter->second.value = 0;
352 std::string reason =
"Counter " + counterName +
" unknown";
353 amd->ice_exception(InvalidCounterException(reason.c_str()));
362 const AMD_SystemObserverInterface_setCounterPtr& amd,
363 const ChannelRefBasePtr& counter,
365 const Ice::Current&
c)
367 addWorkerJob(
"SystemObserver::setCounter", [
this, amd, counter, counterValue]
369 std::unique_lock lock(countersMutex);
370 std::string counterName = ChannelRefPtr::dynamicCast(counter)->getChannelName();
372 SystemObserverCounterMap::iterator iter = counters.find(counterName);
374 if (iter != counters.end())
376 iter->second.value = counterValue;
380 std::string reason =
"Counter " + counterName +
" unknown";
381 amd->ice_exception(InvalidCounterException(reason.c_str()));
390 const AMD_SystemObserverInterface_removeCounterPtr& amd,
391 const ChannelRefBasePtr& counter,
392 const Ice::Current&
c)
394 addWorkerJob(
"SystemObserver::removeCounter", [
this, amd, counter]
396 std::unique_lock lock(countersMutex);
397 std::string counterName = ChannelRefPtr::dynamicCast(counter)->getChannelName();
399 SystemObserverCounterMap::iterator iter = counters.find(counterName);
401 if (iter != counters.end())
404 counters.erase(iter);
423 void SystemObserver::updateTimer(SystemObserverTimer& timer)
427 timer.elapsedMs = getElapsedTimeMs(timer.startTimeMs);
431 int SystemObserver::getCurrentTimeMs()
435 return current.toMilliSeconds();
438 int SystemObserver::getElapsedTimeMs(
int referenceTimeMs)
441 IceUtil::Time reference = IceUtil::Time::milliSeconds(referenceTimeMs);
448 void SystemObserver::updateCounter(SystemObserverCounterMap::iterator& iterCounter)
451 setDataField(iterCounter->second.counterName,
"value", iterCounter->second.value);