ArmarXManager.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 
29 #include <ArmarXCore/core/logging/Logging.h> // for Logging
31 #include <ArmarXCore/interface/core/ArmarXManagerInterface.h>
32 #include <ArmarXCore/interface/core/BasicTypes.h>
33 //#include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
34 
35 #include <Ice/BuiltinSequences.h> // for StringSeq
36 #include <Ice/CommunicatorF.h> // for CommunicatorPtr
37 #include <Ice/Current.h> // for Current
38 #include <Ice/ObjectAdapterF.h> // for ObjectAdapterPtr
39 #include <Ice/Properties.h>
40 #include <IceUtil/Handle.h> // for Handle
41 
42 #include <condition_variable>
43 #include <cstddef> // for size_t
44 #include <string> // for string
45 #include <vector> // for vector
46 #include <mutex>
47 #include <map> // for map, map<>::value_compare
48 
49 namespace armarx
50 {
51  // forward declaration
52  class ArmarXObjectScheduler;
53 
55 
56  class IceManager;
57 
59 
60  class ArmarXMultipleObjectsScheduler;
62 
66  class ArmarXManager;
67  template <class T>
68  class PeriodicTask;
69 
71 
72 
74 
96  virtual public Logging,
97  virtual public ArmarXManagerInterface,
99  {
101  friend class ArmarXObjectObserver;
102 
103  enum ArmarXManagerState
104  {
105  eCreated,
106  eRunning,
107  eShutdownInProgress,
108  eShutdown
109  };
110 
111  public:
123  ArmarXManager(std::string applicationName, int port = 4061, std::string host = "localhost", std::string locatorName = "IceGrid/Locator", Ice::StringSeq args = Ice::StringSeq());
124 
132  ArmarXManager(std::string applicationName, const Ice::CommunicatorPtr& communicator);
133 
134  ~ArmarXManager() override;
135 
136  bool checkIceConnection(bool printHint = true) const;
137  static bool CheckIceConnection(const Ice::CommunicatorPtr& communicator, bool printHint);
138 
144  void enableLogging(bool enable);
145 
149  void enableProfiling(bool enable);
150 
158 
164  void setDataPaths(std::string dataPaths);
165 
176  void addObject(const ManagedIceObjectPtr& object, bool addWithOwnAdapter = true, const std::string& objectName = "", bool useOwnScheduleThread = true) override;
177  virtual void addObject(const ManagedIceObjectPtr& object, const std::string& objectName, bool addWithOwnAdapter = true, bool useOwnScheduleThread = true);
178 
179  virtual void addObject(const ManagedIceObjectPtr& object, Ice::ObjectAdapterPtr objectAdapterToAddTo, const std::string& objectName = "", bool useOwnScheduleThread = true);
180 
181  virtual void addObjectAsync(const ManagedIceObjectPtr& object, const std::string& objectName, bool addWithOwnAdapter = true, bool useOwnScheduleThread = true);
182 
191  void removeObjectBlocking(const ManagedIceObjectPtr& object) override;
192 
201  void removeObjectBlocking(const std::string& objectName) override;
202 
211  void removeObjectNonBlocking(const ManagedIceObjectPtr& object) override;
212 
221  void removeObjectNonBlocking(const std::string& objectName) override;
222 
226  void waitForShutdown();
227 
231  void shutdown();
232 
237  void asyncShutdown(std::size_t timeoutMs = 0);
238 
244  bool isShutdown();
245 
251  std::vector<ManagedIceObjectPtr> getManagedObjects() override;
252 
261  ManagedIceObjectState getObjectState(const std::string& objectName, const Ice::Current& c = Ice::emptyCurrent) override;
262 
271  ManagedIceObjectConnectivity getObjectConnectivity(const std::string& objectName, const Ice::Current& c = Ice::emptyCurrent) override;
272 
277  ::armarx::StringStringDictionary getObjectProperties(const ::std::string& objectName, const ::Ice::Current& = Ice::emptyCurrent) override;
278  ObjectPropertyInfos getObjectPropertyInfos(const::std::string& objectName, const::Ice::Current&) override;
279  ObjectPropertyInfos getApplicationPropertyInfos(const::Ice::Current&) override;
280 
281 
282  Ice::PropertiesAdminPrx getPropertiesAdmin(const Ice::Current& = Ice::emptyCurrent) override;
283 
288  void setComponentIceProperties(const Ice::PropertiesPtr& properties);
289 
290  void updateComponentIceProperties(const Ice::PropertyDict& properties);
291 
299  Ice::StringSeq getManagedObjectNames(const Ice::Current& c = Ice::emptyCurrent) override;
300 
306  const IceManagerPtr& getIceManager() const;
307  const Ice::CommunicatorPtr& getCommunicator() const;
308 
319 
325 
326  const Ice::ObjectAdapterPtr& getAdapter() const;
327 
328  const std::shared_ptr<SharedRemoteHandleState>& getSharedRemoteHandleState() const;
329 
335  void increaseSchedulers(int increaseBy);
336 
337  template<class Function, class Data>
339  Function f, Data d, const std::string& id = "",
340  IceUtil::Time deletionDelay = IceUtil::Time::seconds(3),
341  IceUtil::Time orphantDeletionDelay = IceUtil::Time::seconds(6),
342  long heartBeatMs = 1000)
343  {
346  (
347  this, id, std::forward<decltype(f)>(f), std::forward<decltype(d)>(d),
348  deletionDelay, orphantDeletionDelay, heartBeatMs
349  );
350  remoteReferenceCountControlBlockManager->add(ptr);
351  return ptr;
352  }
353  template<class Function>
355  Function f, const std::string& id = "",
356  IceUtil::Time deletionDelay = IceUtil::Time::seconds(3),
357  IceUtil::Time orphantDeletionDelay = IceUtil::Time::seconds(6),
358  long heartBeatMs = 1000)
359  {
362  (
363  this, id, std::forward<decltype(f)>(f),
364  deletionDelay, orphantDeletionDelay, heartBeatMs
365  );
366  remoteReferenceCountControlBlockManager->add(ptr);
367  return ptr;
368  }
369 
370  template<class Function, class Data>
373  Function f, Data d, const std::string& id = "", IceUtil::Time deletionDelay = IceUtil::Time::seconds(3))
374  {
377  (
378  this, id, std::forward<decltype(f)>(f), std::forward<decltype(d)>(d), deletionDelay
379  );
380  remoteReferenceCountControlBlockManager->add(ptr);
381  return ptr;
382  }
383  template<class Function>
386  Function f, const std::string& id = "", IceUtil::Time deletionDelay = IceUtil::Time::seconds(3))
387  {
390  (
391  this, id, std::forward<decltype(f)>(f), deletionDelay
392  );
393  remoteReferenceCountControlBlockManager->add(ptr);
394  return ptr;
395  }
396 
397  StringVariantBaseMap getMetaInfo(const std::string&, const Ice::Current&) override;
398 
399 
400  bool loadLibFromPath(const std::string& path, const Ice::Current& = Ice::emptyCurrent) override;
401  bool loadLibFromPackage(const std::string& package, const std::string& lib, const Ice::Current& = Ice::emptyCurrent) override;
402  private:
403  // typedefs
404  using ObjectSchedulerMap = std::map<std::string, ArmarXObjectSchedulerPtr>;
405  using ObjectSchedulerList = std::vector<ArmarXObjectSchedulerPtr>;
406 
407  // internal init used by both constructors
408  void init(std::string applicationName, const Ice::CommunicatorPtr& communicator);
409 
410  // thread method to cleanup terminated schedulers
411  void cleanupSchedulers();
412 
417  void disconnectDependees(const std::string& object);
418  void disconnectAllObjects();
419  std::vector<std::string> getDependendees(const std::string& removedObject);
420 
426  void wakeupWaitingSchedulers();
427 
428  // remove all managed objects
429  void removeAllObjects(bool blocking);
430 
431  // remove specific managed objects
432  bool removeObject(const ArmarXObjectSchedulerPtr& objectScheduler, bool blocking);
433 
434  ArmarXObjectSchedulerPtr findObjectScheduler(const std::string& objectName) const;
435 
436  void checkDependencies();
437 
438  // synchronization
439  // Ugly: Returing a smart pointer to a lock is just frigging insanity
440  // Why do we even have this weird pattern?
441  using ScopedRecursiveLockPtr = std::unique_ptr<std::scoped_lock<std::recursive_mutex>>;
442  ScopedRecursiveLockPtr acquireManagedObjectsMutex();
443 
453  void installProcessFacet();
454 
455  // name of the application
456  std::string applicationName;
457 
458  // state of the manager
459  ArmarXManagerState managerState;
460  std::mutex managerStateMutex;
461 
462  // ice manager
463  IceManagerPtr iceManager;
464 
465  ArmarXObjectObserverPtr objObserver;
466 
467  // object scheduler registry
468  ObjectSchedulerMap managedObjects;
469  ObjectSchedulerList terminatingObjects;
470  mutable std::recursive_mutex managedObjectsMutex;
471  std::mutex terminatingObjectsMutex;
472 
473  std::mutex schedulerListMutex;
474  std::vector<ArmarXMultipleObjectsSchedulerPtr> singleThreadedSchedulers;
475 
476  // shutdown handling
477  std::mutex shutdownMutex;
478  std::condition_variable shutdownCondition;
479 
480  // cleanup task
481  IceUtil::Handle<PeriodicTask<ArmarXManager> > cleanupSchedulersTask;
482 
483  IceUtil::Handle<PeriodicTask<ArmarXManager> > checkDependencyStatusTask;
484  Ice::ObjectAdapterPtr armarxManagerAdapter;
485 
486  //remotehandles
487  std::shared_ptr<SharedRemoteHandleState> sharedRemoteHandleState;
488  std::shared_ptr<RemoteReferenceCountControlBlockManager> remoteReferenceCountControlBlockManager;
489  };
490 }
491 
armarx::ArmarXManager::setComponentIceProperties
void setComponentIceProperties(const Ice::PropertiesPtr &properties)
Calls Component::setIceProperties() on all components assigend to this ArmarXManager Instance.
Definition: ArmarXManager.cpp:673
armarx::IceManagerPtr
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition: ArmarXFwd.h:39
armarx::ArmarXManager::getApplicationPropertyInfos
ObjectPropertyInfos getApplicationPropertyInfos(const ::Ice::Current &) override
Definition: ArmarXManager.cpp:649
armarx::ArmarXManager::addObjectAsync
virtual void addObjectAsync(const ManagedIceObjectPtr &object, const std::string &objectName, bool addWithOwnAdapter=true, bool useOwnScheduleThread=true)
Definition: ArmarXManager.cpp:312
armarx::ArmarXManager::getObjectPropertyInfos
ObjectPropertyInfos getObjectPropertyInfos(const ::std::string &objectName, const ::Ice::Current &) override
Definition: ArmarXManager.cpp:622
armarx::MessageTypeT
MessageTypeT
Definition: LogSender.h:45
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
armarx::Logging::minimumLoggingLevel
MessageTypeT minimumLoggingLevel
Definition: Logging.h:272
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:113
armarx::ArmarXManager::waitForShutdown
void waitForShutdown()
Wait for shutdown.
Definition: ArmarXManager.cpp:397
armarx::ArmarXManager::ArmarXManager
ArmarXManager(std::string applicationName, int port=4061, std::string host="localhost", std::string locatorName="IceGrid/Locator", Ice::StringSeq args=Ice::StringSeq())
ArmarXManager constructor.
Definition: ArmarXManager.cpp:95
armarx::SharedRemoteHandleState
This holds the shared state of all RemoteHandleControlBlocks for one armarx manager.
Definition: RemoteHandleControlBlock.h:52
armarx::ArmarXManager::enableProfiling
void enableProfiling(bool enable)
Enable or disable profiling of CPU Usage.
Definition: ArmarXManager.cpp:209
armarx::ArmarXManager::getAdapter
const Ice::ObjectAdapterPtr & getAdapter() const
Definition: ArmarXManager.cpp:1181
armarx::ArmarXObjectObserver
The ArmarXObjectObserver inherits from IceGrid::ObjectObserver and signals its associated ArmarXManag...
Definition: ArmarXObjectObserver.h:37
armarx::ArmarXManager::createSimpleRemoteReferenceCount
SimpleRemoteReferenceCountControlBlockPtr< Function, Data > createSimpleRemoteReferenceCount(Function f, Data d, const std::string &id="", IceUtil::Time deletionDelay=IceUtil::Time::seconds(3))
Definition: ArmarXManager.h:372
armarx::ArmarXManager::CheckIceConnection
static bool CheckIceConnection(const Ice::CommunicatorPtr &communicator, bool printHint)
Definition: ArmarXManager.cpp:129
armarx::ArmarXManager::increaseSchedulers
void increaseSchedulers(int increaseBy)
increased the number of single threaded schedulers.
Definition: ArmarXManager.cpp:1191
armarx::ArmarXManager::isShutdown
bool isShutdown()
Whether ArmarXManager shutdown has been finished.
Definition: ArmarXManager.cpp:513
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ArmarXManager::createSimpleRemoteReferenceCount
SimpleRemoteReferenceCountControlBlockPtr< Function > createSimpleRemoteReferenceCount(Function f, const std::string &id="", IceUtil::Time deletionDelay=IceUtil::Time::seconds(3))
Definition: ArmarXManager.h:385
armarx::ArmarXManager::getMetaInfo
StringVariantBaseMap getMetaInfo(const std::string &, const Ice::Current &) override
Definition: ArmarXManager.cpp:1275
ArmarXObjectObserver.h
armarx::ArmarXManager::loadLibFromPath
bool loadLibFromPath(const std::string &path, const Ice::Current &=Ice::emptyCurrent) override
Definition: ArmarXManager.cpp:1233
armarx::ArmarXManager::getManagedObjects
std::vector< ManagedIceObjectPtr > getManagedObjects() override
Retrieve pointers to all ManagedIceObject.
Definition: ArmarXManager.cpp:534
RemoteReferenceCount.h
IceInternal::Handle< ::Ice::Communicator >
armarx::ArmarXManager::getSharedRemoteHandleState
const std::shared_ptr< SharedRemoteHandleState > & getSharedRemoteHandleState() const
Definition: ArmarXManager.cpp:1186
armarx::ArmarXManager::updateComponentIceProperties
void updateComponentIceProperties(const Ice::PropertyDict &properties)
Definition: ArmarXManager.cpp:685
armarx::ArmarXManager::getPropertiesAdmin
Ice::PropertiesAdminPrx getPropertiesAdmin(const Ice::Current &=Ice::emptyCurrent) override
Definition: ArmarXManager.cpp:666
armarx::ArmarXManager::removeObjectNonBlocking
void removeObjectNonBlocking(const ManagedIceObjectPtr &object) override
Removes an object from the manager.
Definition: ArmarXManager.cpp:389
armarx::ArmarXManager::addObject
void addObject(const ManagedIceObjectPtr &object, bool addWithOwnAdapter=true, const std::string &objectName="", bool useOwnScheduleThread=true) override
Add a ManagedIceObject to the manager.
Definition: ArmarXManager.cpp:230
armarx::SimpleRemoteReferenceCountControlBlock
Definition: RemoteReferenceCount.h:210
armarx::ManagedIceObjectRegistryInterface
The registery interface is implemented by ArmarXManagers.
Definition: ManagedIceObjectRegistryInterface.h:48
armarx::ArmarXManager::enableLogging
void enableLogging(bool enable)
Enable or disable logging.
Definition: ArmarXManager.cpp:203
armarx::ArmarXManager::loadLibFromPackage
bool loadLibFromPackage(const std::string &package, const std::string &lib, const Ice::Current &=Ice::emptyCurrent) override
Definition: ArmarXManager.cpp:1243
armarx::RemoteReferenceCountControlBlock
Definition: RemoteReferenceCount.h:162
armarx::ArmarXManager::checkIceConnection
bool checkIceConnection(bool printHint=true) const
Definition: ArmarXManager.cpp:124
armarx::ArmarXObjectSchedulerPtr
IceUtil::Handle< ArmarXObjectScheduler > ArmarXObjectSchedulerPtr
Definition: ArmarXFwd.h:33
armarx::ArmarXManager::getObjectState
ManagedIceObjectState getObjectState(const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
Retrieve state of a ManagedIceObject.
Definition: ArmarXManager.cpp:559
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::ArmarXManager::getObjectProperties
::armarx::StringStringDictionary getObjectProperties(const ::std::string &objectName, const ::Ice::Current &=Ice::emptyCurrent) override
getObjectProperties is used to retrieve the properties of an object
Definition: ArmarXManager.cpp:601
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::ArmarXManager::setDataPaths
void setDataPaths(std::string dataPaths)
Set data paths used to search for datafiles.
Definition: ArmarXManager.cpp:220
armarx::ArmarXManager::createRemoteReferenceCount
RemoteReferenceCountControlBlockPtr< Function > createRemoteReferenceCount(Function f, const std::string &id="", IceUtil::Time deletionDelay=IceUtil::Time::seconds(3), IceUtil::Time orphantDeletionDelay=IceUtil::Time::seconds(6), long heartBeatMs=1000)
Definition: ArmarXManager.h:354
armarx::ArmarXManager::getObjectConnectivity
ManagedIceObjectConnectivity getObjectConnectivity(const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
Retrieve connectivity of a ManagedIceObject.
Definition: ArmarXManager.cpp:579
IceUtil::Handle< ArmarXObjectScheduler >
armarx::ArmarXManager::getManagedObjectNames
Ice::StringSeq getManagedObjectNames(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the names of all ManagedIceObject.
Definition: ArmarXManager.cpp:698
ManagedIceObjectRegistryInterface.h
armarx::ArmarXManager::removeObjectBlocking
void removeObjectBlocking(const ManagedIceObjectPtr &object) override
Removes an object from the manager.
Definition: ArmarXManager.cpp:384
armarx::ArmarXManager::registerKnownObjectFactoriesWithIce
void registerKnownObjectFactoriesWithIce()
non static convenience version of ArmarXManager::RegisterKnownObjectFactoriesWithIce()
Definition: ArmarXManager.cpp:1176
armarx::PeriodicTask
Definition: ArmarXManager.h:68
armarx::ArmarXManager::createRemoteReferenceCount
RemoteReferenceCountControlBlockPtr< Function, Data > createRemoteReferenceCount(Function f, Data d, const std::string &id="", IceUtil::Time deletionDelay=IceUtil::Time::seconds(3), IceUtil::Time orphantDeletionDelay=IceUtil::Time::seconds(6), long heartBeatMs=1000)
Definition: ArmarXManager.h:338
Logging.h
armarx::ArmarXManager::shutdown
void shutdown()
Shuts down the ArmarXManager.
Definition: ArmarXManager.cpp:415
armarx::ScopedRecursiveLockPtr
std::shared_ptr< ScopedRecursiveLock > ScopedRecursiveLockPtr
Definition: Synchronization.h:143
armarx::ArmarXManager::~ArmarXManager
~ArmarXManager() override
Definition: ArmarXManager.cpp:118
armarx::ArmarXManager::getCommunicator
const Ice::CommunicatorPtr & getCommunicator() const
Definition: ArmarXManager.cpp:529
armarx::ArmarXManager
Main class of an ArmarX process.
Definition: ArmarXManager.h:95
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ArmarXManager::RegisterKnownObjectFactoriesWithIce
static void RegisterKnownObjectFactoriesWithIce(const Ice::CommunicatorPtr &ic)
Registers all object factories that are known with Ice.
Definition: ArmarXManager.cpp:1157
armarx::ArmarXManager::asyncShutdown
void asyncShutdown(std::size_t timeoutMs=0)
Calls shutdown() after a timeout.
Definition: ArmarXManager.cpp:504
armarx::ArmarXManager::setGlobalMinimumLoggingLevel
void setGlobalMinimumLoggingLevel(MessageTypeT minimumLoggingLevel)
Set minimum logging level to output in log stream.
Definition: ArmarXManager.cpp:215
armarx::ArmarXManager::getIceManager
const IceManagerPtr & getIceManager() const
Retrieve the instance of the icemanager.
Definition: ArmarXManager.cpp:524