LocalTimeServer.cpp
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 ArmarXCore::core
19 * @author Clemens Wallrath ( uagzs at student dot kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl.txt
22 * GNU General Public License
23 */
24#include "LocalTimeServer.h"
25
26#include <algorithm>
27
28namespace armarx
29{
31 {
32 time = IceUtil::Time::milliSeconds(0);
33 }
34
35 std::string
37 {
38 return "LocalTimeServer";
39 }
40
41 void
43 {
44 usingProxy(GLOBAL_TIMESERVER_NAME);
45 usingTopic(TIME_TOPIC_NAME);
46 }
47
48 void
54
55 void
60
61 void
62 LocalTimeServer::reportTime(::Ice::Long timestamp, const Ice::Current&)
63 {
64 IceUtil::Time tempTime = IceUtil::Time::milliSeconds(timestamp);
65 // if the new time is before the current time we have to subtract the delta from all threads sleeping
66 // to prevent them from waiting too long.
67 // if we don't do this, the following can happen:
68 // global time = 10min
69 // thread sleeps for 10ms (-> until time is 10min + 10ms)
70 // reset of timeserver -> global time = 0
71 // thread wakes up after 10min + 10ms
72 IceUtil::Time timeDeduction;
73 {
74 std::unique_lock lock(timeMutex);
75 timeDeduction = tempTime - this->time;
76 this->time = tempTime;
77 }
78 if (timeDeduction.toMicroSeconds() > 0)
79 {
80 //the time progresses monotonically
81 timeDeduction = IceUtil::Time::microSeconds(0);
82 }
83
84 if (scheduledTasksMutex.try_lock())
85 {
86 for (std::vector<RegisteredTimer>::iterator it = scheduledTasks.begin();
87 it != scheduledTasks.end();)
88 {
89 it->endTime += timeDeduction;
90 if (it->endTime < tempTime)
91 {
92 CallbackReceiver* callback = it->callback;
93 it = scheduledTasks.erase(it);
94 callback->call();
95 }
96 else
97 {
98 ++it;
99 }
100 }
101
102 scheduledTasksMutex.unlock();
103 }
104 }
105
106 Ice::Long
107 LocalTimeServer::getTime(const ::Ice::Current&)
108 {
109 std::shared_lock lock(timeMutex);
110 return static_cast<Ice::Long>(time.toMilliSeconds());
111 }
112
115 {
116 static LocalTimeServerPtr applicationTimeServer(new LocalTimeServer());
117 return applicationTimeServer;
118 }
119
120 void
122 {
123 getApplicationTimeServer()->setName(name);
124 }
125
126 void
127 LocalTimeServer::stop(const ::Ice::Current&)
128 {
129 timeServerPrx->stop();
130 }
131
132 void
133 LocalTimeServer::start(const ::Ice::Current&)
134 {
135 timeServerPrx->start();
136 }
137
138 void
139 LocalTimeServer::step(const ::Ice::Current&)
140 {
141 timeServerPrx->step();
142 }
143
144 void
145 LocalTimeServer::setSpeed(Ice::Float newSpeed, const ::Ice::Current&)
146 {
147 timeServerPrx->setSpeed(newSpeed);
148 }
149
150 Ice::Float
151 LocalTimeServer::getSpeed(const Ice::Current&)
152 {
153 return timeServerPrx->getSpeed();
154 }
155
156 void
157 LocalTimeServer::registerTimer(IceUtil::Time endTime, CallbackReceiver* callback)
158 {
159 std::unique_lock lock(scheduledTasksMutex);
160 scheduledTasks.push_back(RegisteredTimer{endTime, callback});
161 }
162
163 void
165 {
166 std::unique_lock lock(scheduledTasksMutex);
167 scheduledTasks.erase(std::remove_if(scheduledTasks.begin(),
168 scheduledTasks.end(),
169 [callback](RegisteredTimer t)
170 { return t.callback == callback; }),
171 scheduledTasks.end());
172 }
173
174 Ice::Int
175 LocalTimeServer::getStepTimeMS(const ::Ice::Current&)
176 {
177 return timeServerPrx->getStepTimeMS();
178 }
179} // namespace armarx
std::string timestamp()
Used by CallbackWaitLock.
virtual void call()=0
void onInitComponent() override
void setSpeed(Ice::Float newSpeed, const ::Ice::Current &=Ice::emptyCurrent) override
LocalTimeServer()
Please use LocalTimeServer::getApplicationtimeserver() to access your local timeserver.
Ice::Int getStepTimeMS(const ::Ice::Current &=Ice::emptyCurrent) override
std::mutex scheduledTasksMutex
used for locking scheduledTasks
void onDisconnectComponent() override
Hook for subclass.
void start(const ::Ice::Current &=Ice::emptyCurrent) override
static void setApplicationTimeServerName(const std::string &name)
void registerTimer(IceUtil::Time endTime, CallbackReceiver *callback)
register a callack to call at endTime
void unregisterTimer(CallbackReceiver *callback)
unregister a timer
std::vector< RegisteredTimer > scheduledTasks
TimeServerInterfacePrx timeServerPrx
a handle for the MasterTimeServer
std::shared_mutex timeMutex
void onConnectComponent() override
void reportTime(::Ice::Long, const ::Ice::Current &=Ice::emptyCurrent) override
IceUtil::Time time
the current time
Ice::Float getSpeed(const ::Ice::Current &=Ice::emptyCurrent) override
static LocalTimeServerPtr getApplicationTimeServer()
Get the applications LocalTimeServer instance.
TimeServerListenerPrx timeTopicPrx
a handle for the topic "Time"
void stop(const ::Ice::Current &=Ice::emptyCurrent) override
Ice::Long getTime(const ::Ice::Current &=Ice::emptyCurrent) override
std::string getDefaultName() const override
void step(const ::Ice::Current &=Ice::emptyCurrent) override
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< LocalTimeServer > LocalTimeServerPtr