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