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 <algorithm>
25 
26 #include "LocalTimeServer.h"
27 
28 
29 namespace armarx
30 {
32  {
33  time = IceUtil::Time::milliSeconds(0);
34  }
35 
36  std::string LocalTimeServer::getDefaultName() const
37  {
38  return "LocalTimeServer";
39  }
40 
42  {
43  usingProxy(GLOBAL_TIMESERVER_NAME);
44  usingTopic(TIME_TOPIC_NAME);
45  }
46 
47 
49  {
50  timeServerPrx = getProxy<TimeServerInterfacePrx>(GLOBAL_TIMESERVER_NAME);
51  timeTopicPrx = armarx::ManagedIceObject::getTopic<TimeServerListenerPrx>(TIME_TOPIC_NAME);
52  }
53 
55  {
56  timeServerPrx = nullptr;
57  }
58 
59 
60  void LocalTimeServer::reportTime(::Ice::Long timestamp, const Ice::Current&)
61  {
62  IceUtil::Time tempTime = IceUtil::Time::milliSeconds(timestamp);
63  // if the new time is before the current time we have to subtract the delta from all threads sleeping
64  // to prevent them from waiting too long.
65  // if we don't do this, the following can happen:
66  // global time = 10min
67  // thread sleeps for 10ms (-> until time is 10min + 10ms)
68  // reset of timeserver -> global time = 0
69  // thread wakes up after 10min + 10ms
70  IceUtil::Time timeDeduction;
71  {
72  std::unique_lock lock(timeMutex);
73  timeDeduction = tempTime - this->time;
74  this->time = tempTime;
75  }
76  if (timeDeduction.toMicroSeconds() > 0)
77  {
78  //the time progresses monotonically
79  timeDeduction = IceUtil::Time::microSeconds(0);
80  }
81 
82  if (scheduledTasksMutex.try_lock())
83  {
84  for (std::vector<RegisteredTimer>::iterator it = scheduledTasks.begin(); it != scheduledTasks.end();)
85  {
86  it->endTime += timeDeduction;
87  if (it->endTime < tempTime)
88  {
89  CallbackReceiver* callback = it->callback;
90  it = scheduledTasks.erase(it);
91  callback->call();
92  }
93  else
94  {
95  ++it;
96  }
97  }
98 
99  scheduledTasksMutex.unlock();
100  }
101  }
102 
103  Ice::Long LocalTimeServer::getTime(const ::Ice::Current&)
104  {
105  std::shared_lock lock(timeMutex);
106  return static_cast<Ice::Long>(time.toMilliSeconds());
107  }
108 
110  {
111  static LocalTimeServerPtr applicationTimeServer(new LocalTimeServer());
112  return applicationTimeServer;
113  }
114 
115  void LocalTimeServer::setApplicationTimeServerName(const std::string& name)
116  {
117  getApplicationTimeServer()->setName(name);
118  }
119 
120  void LocalTimeServer::stop(const ::Ice::Current&)
121  {
122  timeServerPrx->stop();
123  }
124 
125  void LocalTimeServer::start(const ::Ice::Current&)
126  {
127  timeServerPrx->start();
128  }
129 
130  void LocalTimeServer::step(const ::Ice::Current&)
131  {
132  timeServerPrx->step();
133  }
134 
135  void LocalTimeServer::setSpeed(Ice::Float newSpeed, const ::Ice::Current&)
136  {
137  timeServerPrx->setSpeed(newSpeed);
138  }
139 
141  {
142  return timeServerPrx->getSpeed();
143  }
144 
146  {
147  std::unique_lock lock(scheduledTasksMutex);
148  scheduledTasks.push_back(RegisteredTimer {endTime, callback});
149  }
150 
152  {
153  std::unique_lock lock(scheduledTasksMutex);
154  scheduledTasks.erase(std::remove_if(scheduledTasks.begin(), scheduledTasks.end(), [callback](RegisteredTimer t)
155  {
156  return t.callback == callback;
157  }), scheduledTasks.end());
158  }
159 
161  {
162  return timeServerPrx->getStepTimeMS();
163  }
164 }
armarx::LocalTimeServer::timeServerPrx
TimeServerInterfacePrx timeServerPrx
a handle for the MasterTimeServer
Definition: LocalTimeServer.h:127
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::LocalTimeServer::setSpeed
void setSpeed(Ice::Float newSpeed, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:135
armarx::LocalTimeServer::reportTime
void reportTime(::Ice::Long, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:60
armarx::LocalTimeServer::setApplicationTimeServerName
static void setApplicationTimeServerName(const std::string &name)
Definition: LocalTimeServer.cpp:115
armarx::LocalTimeServer::time
IceUtil::Time time
the current time
Definition: LocalTimeServer.h:121
armarx::LocalTimeServer::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: LocalTimeServer.cpp:54
armarx::LocalTimeServer::getTime
Ice::Long getTime(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:103
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::LocalTimeServer::unregisterTimer
void unregisterTimer(CallbackReceiver *callback)
unregister a timer
Definition: LocalTimeServer.cpp:151
LocalTimeServer.h
armarx::LocalTimeServer::timeTopicPrx
TimeServerListenerPrx timeTopicPrx
a handle for the topic "Time"
Definition: LocalTimeServer.h:132
armarx::LocalTimeServer::LocalTimeServer
LocalTimeServer()
Please use LocalTimeServer::getApplicationtimeserver() to access your local timeserver.
Definition: LocalTimeServer.cpp:31
armarx::CallbackReceiver
Used by CallbackWaitLock.
Definition: LocalTimeServer.h:42
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::LocalTimeServer::stop
void stop(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:120
armarx::LocalTimeServer::onInitComponent
void onInitComponent() override
Definition: LocalTimeServer.cpp:41
armarx::LocalTimeServer::step
void step(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:130
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:248
armarx::LocalTimeServer::getStepTimeMS
Ice::Int getStepTimeMS(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:160
armarx::LocalTimeServer::getSpeed
Ice::Float getSpeed(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:140
armarx::LocalTimeServer::registerTimer
void registerTimer(IceUtil::Time endTime, CallbackReceiver *callback)
register a callack to call at endTime
Definition: LocalTimeServer.cpp:145
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::LocalTimeServer::getApplicationTimeServer
static LocalTimeServerPtr getApplicationTimeServer()
Get the applications LocalTimeServer instance.
Definition: LocalTimeServer.cpp:109
armarx::LocalTimeServer::scheduledTasks
std::vector< RegisteredTimer > scheduledTasks
Definition: LocalTimeServer.h:151
armarx::LocalTimeServer::onConnectComponent
void onConnectComponent() override
Definition: LocalTimeServer.cpp:48
armarx::LocalTimeServer::timeMutex
std::shared_mutex timeMutex
Definition: LocalTimeServer.h:122
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:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RegisteredTimer
Definition: LocalTimeServer.h:51
armarx::LocalTimeServer::start
void start(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:125
armarx::LocalTimeServer::scheduledTasksMutex
std::mutex scheduledTasksMutex
used for locking scheduledTasks
Definition: LocalTimeServer.h:150