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 
28 namespace 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
50  {
51  timeServerPrx = getProxy<TimeServerInterfacePrx>(GLOBAL_TIMESERVER_NAME);
52  timeTopicPrx = armarx::ManagedIceObject::getTopic<TimeServerListenerPrx>(TIME_TOPIC_NAME);
53  }
54 
55  void
57  {
58  timeServerPrx = nullptr;
59  }
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
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
armarx::LocalTimeServer::timeServerPrx
TimeServerInterfacePrx timeServerPrx
a handle for the MasterTimeServer
Definition: LocalTimeServer.h:128
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:919
armarx::LocalTimeServer::setSpeed
void setSpeed(Ice::Float newSpeed, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:145
armarx::LocalTimeServer::reportTime
void reportTime(::Ice::Long, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:62
armarx::LocalTimeServer::setApplicationTimeServerName
static void setApplicationTimeServerName(const std::string &name)
Definition: LocalTimeServer.cpp:121
armarx::LocalTimeServer::time
IceUtil::Time time
the current time
Definition: LocalTimeServer.h:122
armarx::LocalTimeServer::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: LocalTimeServer.cpp:56
armarx::LocalTimeServer::getTime
Ice::Long getTime(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:107
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::LocalTimeServer::unregisterTimer
void unregisterTimer(CallbackReceiver *callback)
unregister a timer
Definition: LocalTimeServer.cpp:164
LocalTimeServer.h
armarx::LocalTimeServer::timeTopicPrx
TimeServerListenerPrx timeTopicPrx
a handle for the topic "Time"
Definition: LocalTimeServer.h:133
armarx::LocalTimeServer::LocalTimeServer
LocalTimeServer()
Please use LocalTimeServer::getApplicationtimeserver() to access your local timeserver.
Definition: LocalTimeServer.cpp:30
armarx::CallbackReceiver
Used by CallbackWaitLock.
Definition: LocalTimeServer.h:42
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
armarx::LocalTimeServer::stop
void stop(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:127
armarx::LocalTimeServer::onInitComponent
void onInitComponent() override
Definition: LocalTimeServer.cpp:42
armarx::LocalTimeServer::step
void step(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:139
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::CallbackReceiver::call
virtual void call()=0
armarx::LocalTimeServer::getDefaultName
std::string getDefaultName() const override
Definition: LocalTimeServer.cpp:36
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:254
armarx::LocalTimeServer::getStepTimeMS
Ice::Int getStepTimeMS(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:175
armarx::LocalTimeServer::getSpeed
Ice::Float getSpeed(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:151
armarx::LocalTimeServer::registerTimer
void registerTimer(IceUtil::Time endTime, CallbackReceiver *callback)
register a callack to call at endTime
Definition: LocalTimeServer.cpp:157
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::LocalTimeServer::getApplicationTimeServer
static LocalTimeServerPtr getApplicationTimeServer()
Get the applications LocalTimeServer instance.
Definition: LocalTimeServer.cpp:114
armarx::LocalTimeServer::scheduledTasks
std::vector< RegisteredTimer > scheduledTasks
Definition: LocalTimeServer.h:152
armarx::LocalTimeServer::onConnectComponent
void onConnectComponent() override
Definition: LocalTimeServer.cpp:49
armarx::LocalTimeServer::timeMutex
std::shared_mutex timeMutex
Definition: LocalTimeServer.h:123
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::RegisteredTimer
Definition: LocalTimeServer.h:54
armarx::LocalTimeServer::start
void start(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:133
armarx::LocalTimeServer::scheduledTasksMutex
std::mutex scheduledTasksMutex
used for locking scheduledTasks
Definition: LocalTimeServer.h:151