ArmarXObjectScheduler.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 Kai Welke (kai dot welke at kit dot edu)
20  * @date 2012
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 #include <condition_variable>
27 #include <mutex>
28 #include <string> // for string
29 
30 #include <Ice/Handle.h> // for Handle
31 #include <Ice/ObjectAdapterF.h> // for ObjectAdapterPtr
32 #include <Ice/ObjectF.h> // for upCast
33 #include <IceUtil/Handle.h> // for Handle
34 #include <IceUtil/Shared.h> // for Shared
35 
36 #include "ArmarXCore/core/IceManager.h" // for IceManager
37 #include "ArmarXCore/core/ManagedIceObject.h" // for ManagedIceObject
38 #include <ArmarXCore/core/logging/Logging.h> // for Logging
39 #include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
40 
41 namespace armarx
42 {
43  /**
44  * forward declarations
45  */
46  class ArmarXManager;
47 
51 
52  /**
53  * ArmarXObjectScheduler shared pointer for convenience
54  */
55  class ArmarXObjectScheduler;
56  template <class T>
57  class RunningTask;
58 
60 
61  /**
62  * @class ArmarXObjectScheduler
63  * @brief Takes care of the lifecycle management of ManagedIceObjects.
64  * @ingroup DistributedProcessingSub
65  *
66  * The ArmarXObjectScheduler schedules the lifecycle of a ManagedIceObject in a single thread.
67  * According to the current state, the ManagedIceObject framework hooks are called.
68  * It provides all necessary functionality for a clean initialisation of ManagedIceObjects
69  * as well as dependency-resolution, dependency-monitoring, and exception handling.
70  * ArmarXObjectSchedulers are created by the ArmarXManager.
71 
72  * @image html Lifecycle-UML.svg Lifecyle of a ManagedIceObject managed by the ArmarXObjectScheduler
73  */
74  class ArmarXObjectScheduler : public IceUtil::Shared, virtual public Logging
75  {
76  public:
77  /**
78  * Constructs an ArmarXObjectScheduler
79  *
80  * @param armarXManager pointer to the armarXManager
81  * @param iceManager pointer to the iceManager
82  * @param object object to schedule
83  */
84  ArmarXObjectScheduler(const ArmarXManagerPtr& armarXManager,
85  const IceManagerPtr& iceManager,
86  const armarx::ManagedIceObjectPtr& object,
87  Ice::ObjectAdapterPtr objectAdapterToAddTo,
88  bool startSchedulingObject = true);
89  ~ArmarXObjectScheduler() override;
90 
91 
92  /**
93  * waits until all depenencies are resolved.
94  * @param timeoutMs If set to -1, it waits indefinitely. Otherwise throws
95  * exception after waiting the specifed time (given in milliseconds).
96  */
97  void waitForDependencies(int timeoutMs = -1);
98  void wakeupDependencyCheck();
99  bool dependsOn(const std::string& objectName);
100  void disconnected(bool reconnect);
101 
102  /**
103  * Waits until scheduler has been terminated. After termination the lifecycle of the ManagedIceObject
104  * has ceased and the thread has joined.
105  */
106  void waitForTermination();
107 
108 
109  /**
110  * @brief waitForObjectStart waits (thread sleeps) until the object
111  * reached a specific state.
112  * @param stateToWaitFor State for which the calling thread should wait
113  * for.
114  * @param timeoutMs Timeout in miliseconds until this function stops
115  * waiting and returns false. Set to -1 for infinite waiting.
116  * @return Whether the object reached the state before timeout or not.
117  * @see waitForObjectStateMinimum()
118  */
119  bool waitForObjectState(ManagedIceObjectState stateToWaitFor,
120  const long timeoutMs = -1) const;
121 
122  /**
123  * @brief waitForObjectStart waits (thread sleeps) until the object
124  * reached a specific state (or higher/later).
125  * @param minimumStateToWaitFor Minimum State for which the calling thread should wait
126  * for.
127  * @param timeoutMs Timeout in miliseconds until this function stops
128  * waiting and returns false. Set to -1 for infinite waiting.
129  * @return Whether the object reached the minimum state before timeout
130  * or not.
131  * @see waitForObjectState()
132  */
133  bool waitForObjectStateMinimum(ManagedIceObjectState minimumStateToWaitFor,
134  const long timeoutMs = -1) const;
135 
136  /**
137  * Terminates the ManagedIceObject.
138  */
139  void terminate();
140 
141  /**
142  * Check whether the Scheduler is terminated
143  *
144  * @return terminated state
145  */
146  bool isTerminated() const;
147 
148  bool isTerminationRequested() const;
149 
150  /**
151  * Retrieve pointer to scheduled ManagedIceObject
152  *
153  * @return ManagedIceObjectPtr
154  */
155  const armarx::ManagedIceObjectPtr& getObject() const;
156  ManagedIceObjectState getObjectState() const;
157 
158 
159  bool checkDependenciesStatus() const;
160  void startScheduling();
161 
162  protected:
164 
165  private:
166  void
167  setInteruptConditionVariable(std::shared_ptr<std::condition_variable> interruptCondition,
168  std::shared_ptr<bool> interruptConditionVariable);
169  // main scheduling thread functions
170  void scheduleObject();
171 
172  void waitForInterrupt();
173 
174 
175  // scheduling thread
177 
178 
179  // phases of the object lifecycle
180  void initObject();
181  void startObject();
182  void disconnectObject();
183  void exitObject();
184 
185  // private fields
186  ArmarXManagerPtr armarXManager;
187  IceManagerPtr iceManager;
188  ManagedIceObjectPtr managedObject;
189 
190  // termination handling
191  // boost::mutex terminateMutex;
192  // boost::condition_variable terminateCondition;
193  // bool terminateConditionVariable;
194  std::mutex interruptMutex;
195  std::shared_ptr<std::condition_variable> interruptCondition;
196  std::shared_ptr<bool> interruptConditionVariable;
197  std::mutex dependencyWaitMutex;
198  std::condition_variable dependencyWaitCondition;
199  bool dependencyWaitConditionVariable;
200  bool terminateRequested;
201  bool objectedInitialized;
202  bool tryReconnect;
203  Ice::ObjectAdapterPtr objectAdapterToAddTo;
204 
206  };
207 } // namespace armarx
armarx::IceManagerPtr
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition: ArmarXFwd.h:39
armarx::ArmarXObjectScheduler::isTerminated
bool isTerminated() const
Check whether the Scheduler is terminated.
Definition: ArmarXObjectScheduler.cpp:346
armarx::ArmarXObjectScheduler::~ArmarXObjectScheduler
~ArmarXObjectScheduler() override
Definition: ArmarXObjectScheduler.cpp:97
armarx::ArmarXObjectScheduler::wakeupDependencyCheck
void wakeupDependencyCheck()
Definition: ArmarXObjectScheduler.cpp:473
armarx::ArmarXManagerPtr
IceUtil::Handle< ArmarXManager > ArmarXManagerPtr
Definition: ApplicationProcessFacet.h:33
armarx::ArmarXObjectScheduler::terminate
void terminate()
Terminates the ManagedIceObject.
Definition: ArmarXObjectScheduler.cpp:134
armarx::ArmarXObjectScheduler::ArmarXObjectScheduler
ArmarXObjectScheduler(const ArmarXManagerPtr &armarXManager, const IceManagerPtr &iceManager, const armarx::ManagedIceObjectPtr &object, Ice::ObjectAdapterPtr objectAdapterToAddTo, bool startSchedulingObject=true)
Constructs an ArmarXObjectScheduler.
Definition: ArmarXObjectScheduler.cpp:70
armarx::ArmarXObjectScheduler::getObjectState
ManagedIceObjectState getObjectState() const
Definition: ArmarXObjectScheduler.cpp:365
armarx::ArmarXObjectScheduler::checkDependenciesResolvement
bool checkDependenciesResolvement()
Definition: ArmarXObjectScheduler.cpp:413
IceInternal::Handle< ManagedIceObject >
ManagedIceObject.h
armarx::ArmarXObjectScheduler::checkDependenciesStatus
bool checkDependenciesStatus() const
Definition: ArmarXObjectScheduler.cpp:484
armarx::ArmarXObjectScheduler
Takes care of the lifecycle management of ManagedIceObjects.
Definition: ArmarXObjectScheduler.h:74
armarx::ArmarXObjectScheduler::waitForObjectState
bool waitForObjectState(ManagedIceObjectState stateToWaitFor, const long timeoutMs=-1) const
waitForObjectStart waits (thread sleeps) until the object reached a specific state.
Definition: ArmarXObjectScheduler.cpp:193
armarx::ArmarXMultipleObjectsScheduler
Definition: ArmarXMultipleObjectsScheduler.h:38
armarx::ManagedIceObjectPtr
IceInternal::Handle< ManagedIceObject > ManagedIceObjectPtr
Definition: ArmarXFwd.h:42
armarx::ArmarXObjectScheduler::startScheduling
void startScheduling()
Definition: ArmarXObjectScheduler.cpp:110
IceManager.h
armarx::ArmarXObjectScheduler::isTerminationRequested
bool isTerminationRequested() const
Definition: ArmarXObjectScheduler.cpp:353
armarx::ArmarXObjectScheduler::waitForDependencies
void waitForDependencies(int timeoutMs=-1)
waits until all depenencies are resolved.
Definition: ArmarXObjectScheduler.cpp:375
armarx::ArmarXObjectSchedulerPtr
IceUtil::Handle< ArmarXObjectScheduler > ArmarXObjectSchedulerPtr
Definition: ArmarXFwd.h:33
armarx::ArmarXObjectScheduler::getObject
const armarx::ManagedIceObjectPtr & getObject() const
Retrieve pointer to scheduled ManagedIceObject.
Definition: ArmarXObjectScheduler.cpp:359
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
armarx::ArmarXObjectScheduler::disconnected
void disconnected(bool reconnect)
Definition: ArmarXObjectScheduler.cpp:535
armarx::ArmarXObjectScheduler::dependsOn
bool dependsOn(const std::string &objectName)
Definition: ArmarXObjectScheduler.cpp:513
IceUtil::Handle< ArmarXManager >
Logging.h
armarx::ArmarXObjectScheduler::waitForTermination
void waitForTermination()
Waits until scheduler has been terminated.
Definition: ArmarXObjectScheduler.cpp:157
armarx::ArmarXObjectScheduler::waitForObjectStateMinimum
bool waitForObjectStateMinimum(ManagedIceObjectState minimumStateToWaitFor, const long timeoutMs=-1) const
waitForObjectStart waits (thread sleeps) until the object reached a specific state (or higher/later).
Definition: ArmarXObjectScheduler.cpp:261
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27