LocalTimeServer.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 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 
25 #pragma once
26 
27 
28 #include <mutex>
29 #include <shared_mutex>
30 
31 #include <ArmarXCore/interface/core/TimeServerInterface.h>
32 
33 #include "../ManagedIceObject.h"
34 
35 namespace armarx
36 {
37  /**
38  * @class CallbackReceiver
39  * @brief Used by CallbackWaitLock
40  * @ingroup VirtualTime
41  */
43  {
44  public:
46  {
47  }
48 
49  virtual void call() = 0;
50  };
51 
52  using CallbackReceiverPtr = std::shared_ptr<CallbackReceiver>;
53 
54  typedef struct
55  {
59 
60  class LocalTimeServer;
62 
63  /**
64  * @class LocalTimeServer
65  * @brief A local time server that gets its time from the MasterTimeServer
66  * @ingroup VirtualTime
67  *
68  * The LocalTimeServer runs as a component in every application and offers the time it gets from the MasterTimeServer to the other compnents.
69  */
70  class LocalTimeServer : virtual public TimeServerRelay, virtual public ManagedIceObject
71  {
72  public:
73  /**
74  * @see armarx::ManagedIceObject::getDefaultName()
75  */
76  std::string getDefaultName() const override;
77 
78  void reportTime(::Ice::Long, const ::Ice::Current& = Ice::emptyCurrent) override;
79 
80  Ice::Long getTime(const ::Ice::Current& = Ice::emptyCurrent) override;
81  void stop(const ::Ice::Current& = Ice::emptyCurrent) override;
82  void start(const ::Ice::Current& = Ice::emptyCurrent) override;
83  void step(const ::Ice::Current& = Ice::emptyCurrent) override;
84  void setSpeed(Ice::Float newSpeed, const ::Ice::Current& = Ice::emptyCurrent) override;
85  Ice::Float getSpeed(const ::Ice::Current& = Ice::emptyCurrent) override;
86  Ice::Int getStepTimeMS(const ::Ice::Current& = Ice::emptyCurrent) override;
87 
88  /**
89  * @brief register a callack to call at endTime
90  * @param endTime when to call
91  * @param callback whom to call (has to implement CallbackReceiver)
92  *
93  * Register a callback that is called when the endTime is reached. Always takes at least one timeserver tick when virtual time is active
94  *
95  * A registered timer _must_ be unregistered if it is destroyed before the endTime is reached!
96  */
97  void registerTimer(IceUtil::Time endTime, CallbackReceiver* callback);
98 
99  /**
100  * @brief unregister a timer
101  * @param the CallbackReceiver to unregister
102  *
103  * Cancels all scheduled callbacks registered by the given CallbackReceiver
104  */
105  void unregisterTimer(CallbackReceiver* callback);
106 
107  /**
108  * @brief Get the applications LocalTimeServer instance
109  * @return The applications LocalTimeServer instance
110  */
112 
113  protected:
114  /**
115  * Please use LocalTimeServer::getApplicationtimeserver() to access your local timeserver.
116  */
117  LocalTimeServer();
118 
119  /**
120  * @brief the current time
121  */
123  std::shared_mutex timeMutex;
124 
125  /**
126  * @brief a handle for the MasterTimeServer
127  */
128  TimeServerInterfacePrx timeServerPrx;
129 
130  /**
131  * @brief a handle for the topic "Time"
132  */
133  TimeServerListenerPrx timeTopicPrx;
134 
135  /**
136  * @see armarx::ManagedIceObject::onInitComponent()
137  */
138  void onInitComponent() override;
139 
140  /**
141  * @see armarx::ManagedIceObject::onConnectComponent()
142  */
143  void onConnectComponent() override;
144  void onDisconnectComponent() override;
145 
146  static void setApplicationTimeServerName(const std::string& name);
147 
148  /**
149  * @brief used for locking scheduledTasks
150  */
152  std::vector<RegisteredTimer> scheduledTasks;
153 
154  friend class ArmarXManager;
155  };
156 } // namespace armarx
armarx::RegisteredTimer::endTime
IceUtil::Time endTime
Definition: LocalTimeServer.h:56
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
A local time server that gets its time from the MasterTimeServer.
Definition: LocalTimeServer.h:70
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
armarx::CallbackReceiverPtr
std::shared_ptr< CallbackReceiver > CallbackReceiverPtr
Definition: LocalTimeServer.h:52
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::RegisteredTimer::callback
CallbackReceiver * callback
Definition: LocalTimeServer.h:57
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::LocalTimeServer::getStepTimeMS
Ice::Int getStepTimeMS(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LocalTimeServer.cpp:175
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
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::CallbackReceiver::~CallbackReceiver
virtual ~CallbackReceiver()
Definition: LocalTimeServer.h:45
armarx::ArmarXManager
Main class of an ArmarX process.
Definition: ArmarXManager.h:97
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