IceManager.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 Nils Adermann (naderman at naderman dot de)
20  * @author Jan Issac (jan dot issac at gmx dot de)
21  * @date 2010
22  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23  * GNU General Public License
24  */
25 
26 #pragma once
27 
28 #include <ArmarXCore/core/logging/Logging.h> // for Logging
29 #include <ArmarXCore/core/system/ImportExport.h> // for ARMARXCORE_IMPORT_EXPORT
31 
32 #include <Ice/Object.h> // for ObjectPtr
33 #include <Ice/Proxy.h> // for ObjectPrx
34 
35 #include <IceUtil/Handle.h> // for Handle, HandleBase
36 #include <IceUtil/Shared.h> // for Shared
37 
38 #include <stdexcept> // for out_of_range
39 #include <string> // for string, allocator, etc
40 #include <typeinfo> // for type_info
41 #include <utility> // for pair
42 #include <vector> // for vector
43 
44 namespace Ice
45 {
46  class ConnectionRefusedException;
47 
48  class Communicator;
49  typedef ::IceInternal::Handle< ::Ice::Communicator> CommunicatorPtr;
50 
51  class ObjectAdapter;
52  typedef ::IceInternal::Handle< ::Ice::ObjectAdapter> ObjectAdapterPtr;
53 
54 }
55 
57 {
58  class Registry;
59 }
60 namespace IceProxy::IceStorm
61 {
62  class TopicManager;
63  class Topic;
64 }
65 
66 namespace IceStorm
67 {
68  using TopicManagerPrx = ::IceInternal::ProxyHandle< ::IceProxy::IceStorm::TopicManager>;
69  using TopicPrx = ::IceInternal::ProxyHandle< ::IceProxy::IceStorm::Topic>;
70 }
71 
72 namespace IceGrid
73 {
74  using RegistryPrx = ::IceInternal::ProxyHandle< ::IceProxy::IceGrid::Registry>;
75 }
76 
81 namespace armarx
82 {
83  class IceGridAdmin;
85 
86  class IceManager;
87 
92 
96  using ObjectHandles = std::pair<Ice::ObjectPrx, Ice::ObjectAdapterPtr>;
97 
98 
106  public IceUtil::Shared,
107  virtual public Logging
108  {
109  friend class Component;
110 
111  public:
127  IceManager(const Ice::CommunicatorPtr& communicator,
128  std::string name = "", const std::string topicSuffix = "");
129 
130  ~IceManager() override;
131 
142  ObjectHandles registerObject(const Ice::ObjectPtr& object,
143  const std::string& objectName, const Ice::ObjectAdapterPtr& adapterToAddTo = nullptr);
144 
150  void removeObject(const std::string& objectName);
151 
160  template <class ProxyType>
161  ProxyType getProxy(const std::string& name,
162  const std::string& endpoints = std::string())
163  {
164  std::string proxyString = name;
165 
166  if (!endpoints.empty())
167  {
168  proxyString += std::string(":") + endpoints;
169  }
170 
171  std::string proxyTypedId =
172  proxyString
173  + std::string(":")
174  + std::string(typeid(ProxyType).name());
175 
176  try
177  {
178  return ProxyType::uncheckedCast(implGetCheckedProxy(proxyTypedId));
179  }
180  catch (std::out_of_range& uncheckedProxiesException)
181  {
182  }
183 
184  Ice::ObjectPrx base = communicator_stringToProxy(proxyString);
185 
186  ProxyType proxy;
187 
188  try
189  {
190  proxy = ProxyType::checkedCast(base);
191  implSetCheckedProxy(proxyTypedId, proxy);
192  }
193  catch (const Ice::ConnectionRefusedException&)
194  {
195  std::stringstream exceptionText;
196  exceptionText << "Connection refused for proxy of type '" << GetTypeString<ProxyType>()
197  << "': " << name << " ProxyString: " << proxyString << " Endpoints: " << endpoints << std::endl;
198  throwUserException(exceptionText.str());
199  }
200 
201  if (!proxy)
202  {
203  std::stringstream exceptionText;
204  exceptionText << "Invalid Proxy. Searched for: " << name << " ProxyString: " << proxyString << " Endpoints: " << endpoints << std::endl;
205  throwUserException(exceptionText.str());
206  }
207 
208  return proxy;
209  }
210 
211  void throwUserException(std::string const& message);
212 
220  template <class ProxyType>
221  bool removeProxyFromCache(const std::string& name,
222  const std::string& endpoints = std::string())
223  {
224  return removeProxyFromCache(name, typeid(ProxyType).name(), endpoints);
225  }
226 
227  bool removeProxyFromCache(const std::string& name,
228  const std::string& typeName,
229  const std::string& endpoints = std::string());
230 
238  bool removeProxyFromCache(const Ice::ObjectPrx& proxy);
239 
246  IceStorm::TopicManagerPrx getTopicManager();
247  static IceStorm::TopicManagerPrx GetTopicManager(Ice::CommunicatorPtr communicator);
248 
256  template <class TopicProxy>
257  TopicProxy getTopic(const std::string& topicName, bool useUDP = false)
258  {
259  Ice::ObjectPrx pub = __getTopic(topicName, useUDP);
260  TopicProxy castTopic = TopicProxy::uncheckedCast(pub);
261 
262  return castTopic;
263  }
264 
278  void subscribeTopic(Ice::ObjectPrx subscriber,
279  const std::string& topicName, bool orderedPublishing = false);
280  void registerAndSubscribeTopic(Ice::ObjectPtr subscriber,
281  const std::string& topicName, bool orderedPublishing = false);
282 
289  void unsubscribeTopic(Ice::ObjectPrx subscriberProxy,
290  const std::string& topicName);
291 
302  void registerObjectDependency(
303  const std::string& registrantName,
304  const std::string& dependencyObjectName);
305 
313  void registerDelayedTopicSubscription(
314  const std::string& registrantName,
315  const std::string& topicName);
316 
323  void registerDelayedTopicRetrieval(
324  const std::string& registrantName,
325  const std::string& topicName);
326 
333  bool isObjectReachable(std::string objectName);
334 
341  template <typename ProxyType>
342  bool isObjectReachable(std::string objectName);
343 
349  const Ice::CommunicatorPtr& getCommunicator();
350 
357  IceGrid::RegistryPrx getIceGridRegistry();
358 
364  const armarx::IceGridAdminPtr& getIceGridSession();
365 
369  void setName(std::string name);
370 
375  void shutdown();
376 
380  void waitForShutdown();
381 
385  bool isShutdown();
386 
390  void destroy();
391 
395  std::string getTopicSuffix() const;
396 
397  private:
398 
399  Ice::ObjectPrx communicator_stringToProxy(std::string const& proxyString);
400 
401  Ice::ObjectPrx __getTopic(const std::string& topicName, bool useUDP = false);
402 
403  struct ObjectEntry;
404 
405  struct DependencyObjectEntry: public IceUtil::Shared
406  {
407  DependencyObjectEntry(std::string name, Ice::ObjectPrx proxy) :
408  name(name),
409  proxy(proxy),
410  resolved(false)
411  {
412 
413  }
414 
415  std::string name;
416  Ice::ObjectPrx proxy;
417  bool resolved;
418  };
419 
420  using ObjectEntryPtr = IceUtil::Handle<ObjectEntry>;
421  using DependencyObjectEntryPtr = IceUtil::Handle<DependencyObjectEntry>;
422 
423  using TopicList = std::vector<std::string>;
424  using DependencyList = std::vector<DependencyObjectEntryPtr>;
425 
429  void subscribeTopics(Ice::ObjectPrx subscriber,
430  const TopicList& topics, bool orderedPublishing = false);
431 
435  void retrieveTopics(const TopicList& topics);
436 
442  void resolveObjectDependencies();
443 
452  ObjectEntryPtr getOrCreateObjectEntry(const std::string& objectName);
453 
457  void cleanUp();
458 
468  IceStorm::TopicPrx retrieveTopic(const std::string& topicName);
469 
481  IceStorm::TopicPrx stormTopicRetrieve(const std::string& topicName);
482 
483  Ice::ObjectPrx implGetCheckedProxy(std::string const& proxyTypedId);
484  void implSetCheckedProxy(std::string const& proxyTypedId, Ice::ObjectPrx const& proxy);
485 
486 
487  struct Impl;
488  std::unique_ptr<Impl> impl;
489  };
490 
491 
492  template <typename ProxyType>
493  bool IceManager::isObjectReachable(std::string objectName)
494  {
495  try
496  {
497  ProxyType prx = getProxy<ProxyType>(objectName);
498  prx->ice_timeout(500)->ice_ping();
499  // if ping'able, object is already registered
500  return true;
501  }
502  catch (...)
503  {
504 
505  return false;
506  }
507  }
508 
509 }
510 
armarx::IceManagerPtr
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition: ArmarXFwd.h:39
armarx::IceManager::getProxy
ProxyType getProxy(const std::string &name, const std::string &endpoints=std::string())
Retrieves a proxy object.
Definition: IceManager.h:161
armarx::DependencyList
std::map< std::string, ManagedIceObjectDependencyPtr > DependencyList
Map of dependency names and dependecies.
Definition: ManagedIceObjectDependency.h:50
IceStorm
Definition: DBTypes.ice:22
armarx::IceManager
The IceManager class provides simplified access to commonly used Ice features.
Definition: IceManager.h:105
GetTypeString.h
IceProxy::IceGrid
Definition: IceManager.h:56
armarx::IceManager::isObjectReachable
bool isObjectReachable(std::string objectName)
creates a proxy to the object specified with parameter objectName and tries to ping it.
Definition: IceManager.cpp:684
armarx::IceManager::removeProxyFromCache
bool removeProxyFromCache(const std::string &name, const std::string &endpoints=std::string())
This functions removes the given proxy from the proxy cache.
Definition: IceManager.h:221
Ice::CommunicatorPtr
::IceInternal::Handle< ::Ice::Communicator > CommunicatorPtr
Definition: IceManager.h:48
IceInternal::Handle< ::Ice::Communicator >
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
IceStorm::TopicManagerPrx
::IceInternal::ProxyHandle< ::IceProxy::IceStorm::TopicManager > TopicManagerPrx
Definition: IceManager.h:68
armarx::IceManager::getTopic
TopicProxy getTopic(const std::string &topicName, bool useUDP=false)
Gets an Ice Storm topic for publishing messages.
Definition: IceManager.h:257
IceProxy::IceStorm
Definition: IceStormInternal.h:381
IceStorm::TopicPrx
::IceInternal::ProxyHandle< ::IceProxy::IceStorm::Topic > TopicPrx
Definition: IceManager.h:69
armarx::ProxyType
ProxyType
Definition: ProxyPropertyDefinition.h:41
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::IceGridAdminPtr
IceUtil::Handle< IceGridAdmin > IceGridAdminPtr
Definition: ArmarXFwd.h:48
Ice
Definition: DBTypes.cpp:64
Ice::ObjectAdapterPtr
::IceInternal::Handle< ::Ice::ObjectAdapter > ObjectAdapterPtr
Definition: IceManager.h:51
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
IceUtil::Handle< IceGridAdmin >
IceGrid::RegistryPrx
::IceInternal::ProxyHandle< ::IceProxy::IceGrid::Registry > RegistryPrx
Definition: IceManager.h:74
ImportExport.h
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:37
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
IceGrid
Definition: IceManager.cpp:51
Logging.h
armarx::ObjectHandles
std::pair< Ice::ObjectPrx, Ice::ObjectAdapterPtr > ObjectHandles
Object handles pair which contains the object proxy and its adapter.
Definition: IceManager.h:96
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28