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 <ArmarXCore/core/logging/Logging.h> // for Logging
27 #include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
28 #include <Ice/Handle.h> // for Handle
29 #include <Ice/ObjectAdapterF.h> // for ObjectAdapterPtr
30 #include <Ice/ObjectF.h> // for upCast
31 #include <IceUtil/Handle.h> // for Handle
32 #include <IceUtil/Shared.h> // for Shared
33 
34 #include <condition_variable>
35 #include <mutex>
36 #include <string> // for string
37 
38 #include "ArmarXCore/core/IceManager.h" // for IceManager
39 #include "ArmarXCore/core/ManagedIceObject.h" // for ManagedIceObject
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  */
75  public IceUtil::Shared,
76  virtual public Logging
77  {
78  public:
79  /**
80  * Constructs an ArmarXObjectScheduler
81  *
82  * @param armarXManager pointer to the armarXManager
83  * @param iceManager pointer to the iceManager
84  * @param object object to schedule
85  */
86  ArmarXObjectScheduler(const ArmarXManagerPtr& armarXManager, const IceManagerPtr& iceManager, const armarx::ManagedIceObjectPtr& object, Ice::ObjectAdapterPtr objectAdapterToAddTo, bool startSchedulingObject = true);
87  ~ArmarXObjectScheduler() override;
88 
89 
90  /**
91  * waits until all depenencies are resolved.
92  * @param timeoutMs If set to -1, it waits indefinitely. Otherwise throws
93  * exception after waiting the specifed time (given in milliseconds).
94  */
95  void waitForDependencies(int timeoutMs = -1);
96  void wakeupDependencyCheck();
97  bool dependsOn(const std::string& objectName);
98  void disconnected(bool reconnect);
99 
100  /**
101  * Waits until scheduler has been terminated. After termination the lifecycle of the ManagedIceObject
102  * has ceased and the thread has joined.
103  */
104  void waitForTermination();
105 
106 
107 
108  /**
109  * @brief waitForObjectStart waits (thread sleeps) until the object
110  * reached a specific state.
111  * @param stateToWaitFor State for which the calling thread should wait
112  * for.
113  * @param timeoutMs Timeout in miliseconds until this function stops
114  * waiting and returns false. Set to -1 for infinite waiting.
115  * @return Whether the object reached the state before timeout or not.
116  * @see waitForObjectStateMinimum()
117  */
118  bool waitForObjectState(ManagedIceObjectState stateToWaitFor, const long timeoutMs = -1) const;
119 
120  /**
121  * @brief waitForObjectStart waits (thread sleeps) until the object
122  * reached a specific state (or higher/later).
123  * @param minimumStateToWaitFor Minimum State for which the calling thread should wait
124  * for.
125  * @param timeoutMs Timeout in miliseconds until this function stops
126  * waiting and returns false. Set to -1 for infinite waiting.
127  * @return Whether the object reached the minimum state before timeout
128  * or not.
129  * @see waitForObjectState()
130  */
131  bool waitForObjectStateMinimum(ManagedIceObjectState minimumStateToWaitFor, const long timeoutMs = -1) const;
132 
133  /**
134  * Terminates the ManagedIceObject.
135  */
136  void terminate();
137 
138  /**
139  * Check whether the Scheduler is terminated
140  *
141  * @return terminated state
142  */
143  bool isTerminated() const;
144 
145  bool isTerminationRequested() const;
146 
147  /**
148  * Retrieve pointer to scheduled ManagedIceObject
149  *
150  * @return ManagedIceObjectPtr
151  */
152  const armarx::ManagedIceObjectPtr& getObject() const;
153  ManagedIceObjectState getObjectState() const;
154 
155 
156  bool checkDependenciesStatus() const;
157  void startScheduling();
158 
159  protected:
161  private:
162  void setInteruptConditionVariable(std::shared_ptr<std::condition_variable> interruptCondition, std::shared_ptr<bool> interruptConditionVariable);
163  // main scheduling thread functions
164  void scheduleObject();
165 
166  void waitForInterrupt();
167 
168 
169 
170 
171  // scheduling thread
173 
174 
175 
176  // phases of the object lifecycle
177  void initObject();
178  void startObject();
179  void disconnectObject();
180  void exitObject();
181 
182  // private fields
183  ArmarXManagerPtr armarXManager;
184  IceManagerPtr iceManager;
185  ManagedIceObjectPtr managedObject;
186 
187  // termination handling
188  // boost::mutex terminateMutex;
189  // boost::condition_variable terminateCondition;
190  // bool terminateConditionVariable;
191  std::mutex interruptMutex;
192  std::shared_ptr<std::condition_variable> interruptCondition;
193  std::shared_ptr<bool> interruptConditionVariable;
194  std::mutex dependencyWaitMutex;
195  std::condition_variable dependencyWaitCondition;
196  bool dependencyWaitConditionVariable;
197  bool terminateRequested;
198  bool objectedInitialized;
199  bool tryReconnect;
200  Ice::ObjectAdapterPtr objectAdapterToAddTo;
201 
203 
204  };
205 }
206 
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:312
armarx::ArmarXObjectScheduler::~ArmarXObjectScheduler
~ArmarXObjectScheduler() override
Definition: ArmarXObjectScheduler.cpp:95
armarx::ArmarXObjectScheduler::wakeupDependencyCheck
void wakeupDependencyCheck()
Definition: ArmarXObjectScheduler.cpp:431
armarx::ArmarXManagerPtr
IceUtil::Handle< ArmarXManager > ArmarXManagerPtr
Definition: ApplicationProcessFacet.h:33
armarx::ArmarXObjectScheduler::terminate
void terminate()
Terminates the ManagedIceObject.
Definition: ArmarXObjectScheduler.cpp:127
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:69
armarx::ArmarXObjectScheduler::getObjectState
ManagedIceObjectState getObjectState() const
Definition: ArmarXObjectScheduler.cpp:327
armarx::ArmarXObjectScheduler::checkDependenciesResolvement
bool checkDependenciesResolvement()
Definition: ArmarXObjectScheduler.cpp:377
IceInternal::Handle< ManagedIceObject >
ManagedIceObject.h
armarx::ArmarXObjectScheduler::checkDependenciesStatus
bool checkDependenciesStatus() const
Definition: ArmarXObjectScheduler.cpp:441
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:182
armarx::ArmarXMultipleObjectsScheduler
Definition: ArmarXMultipleObjectsScheduler.h:37
armarx::ManagedIceObjectPtr
IceInternal::Handle< ManagedIceObject > ManagedIceObjectPtr
Definition: ArmarXFwd.h:42
armarx::ArmarXObjectScheduler::startScheduling
void startScheduling()
Definition: ArmarXObjectScheduler.cpp:107
IceManager.h
armarx::ArmarXObjectScheduler::isTerminationRequested
bool isTerminationRequested() const
Definition: ArmarXObjectScheduler.cpp:318
armarx::ArmarXObjectScheduler::waitForDependencies
void waitForDependencies(int timeoutMs=-1)
waits until all depenencies are resolved.
Definition: ArmarXObjectScheduler.cpp:337
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:323
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::ArmarXObjectScheduler::disconnected
void disconnected(bool reconnect)
Definition: ArmarXObjectScheduler.cpp:492
armarx::ArmarXObjectScheduler::dependsOn
bool dependsOn(const std::string &objectName)
Definition: ArmarXObjectScheduler.cpp:472
IceUtil::Handle< ArmarXManager >
Logging.h
armarx::ArmarXObjectScheduler::waitForTermination
void waitForTermination()
Waits until scheduler has been terminated.
Definition: ArmarXObjectScheduler.cpp:149
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:239
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28