IceGridAdmin.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 Nikolaus Vahrenkamp (vahrenkamp at kit dot edu)
20 * @date 2011
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <ArmarXCore/core/logging/Logging.h> // for Logging
29 #include <Ice/CommunicatorF.h> // for CommunicatorPtr
30 #include <Ice/Current.h> // for Current
31 #include <Ice/Identity.h> // for Identity
32 #include <Ice/ObjectAdapterF.h> // for ObjectAdapterPtr
33 #include <Ice/ObjectF.h> // for ObjectPtr
34 #include <Ice/Proxy.h> // for Object
35 #include <Ice/ProxyF.h> // for ObjectPrx
36 #include <IceGrid/Admin.h> // for ObjectInfoSeq, AdminPrx, etc
37 #include <IceGrid/IceGrid.h>
38 #include <IceGrid/Admin.h>
39 #include <IceGrid/Registry.h> // for RegistryPrx
40 #include <IceUtil/Handle.h> // for HandleBase, Handle
41 #include <IceUtil/Time.h> // for Time
42 #include <IceUtil/Timer.h> // for TimerPtr, TimerTask
43 
44 #include <string> // for string
45 #include <vector> // for vector, vector<>::iterator
46 #include <mutex>
47 #include <map> // for map
48 
49 namespace boost
50 {
51  template <class Y> class shared_ptr;
52 } // namespace boost
53 
54 namespace armarx
55 {
56  class Component;
57  class IceGridAdmin;
58 
59  /**
60  * Typedef of IceUtil::Handle for convenience.
61  */
63 
64  /**
65  * @class IceGridAdmin
66  * @brief Provides simplified access to commonly used IceGrid features.
67  * @ingroup DistributedProcessingSub
68  *
69  * It is possible to create an IceGridAdmin instance in your code in addition to the
70  * default one used for dependency checking.
71  * You will want to do this for registering your own IceGrid observers.
72  * However, the following steps must be taken care of if you want your custom IceGridAdmin
73  * instance to be destroyed.
74  *
75  * \li stop updating the IceGrid::AdminSession
76  @code
77  iceGridAdmin->stop();
78  @endcode
79  * \li remove installed observers so the IceGridAmin does not hold any more references to them
80  @code
81  iceGridAdmin->adminSession()->removeObservers((NULL, NULL, NULL, NULL, NULL)));
82  @endcode
83  * \li destroy the object adapters returned by IceGridManager::registerObjectWithNewAdapter() to remove the observer objects
84  @code
85  observerAdapter->destroy();
86  @encode
87  */
88  class ARMARXCORE_IMPORT_EXPORT IceGridAdmin :
89  virtual public IceUtil::TimerTask,
90  virtual public IceGrid::NodeObserver,
91  virtual public Logging
92  {
93  protected:
94  /**
95  * Create an IceGrid::AdminSession and starts a
96  * keepAlive thread.
97  *
98  * @param c An Ice communicator.
99  * @param name A unique name which is used to create IceGridObservers which must have unique names.
100  * In case the name is not unique no state changed messages can be processed with this IceGridAdmin session.
101  */
102  IceGridAdmin(Ice::CommunicatorPtr c, std::string name);
103  void init();
104  public:
105  static IceGridAdminPtr Create(Ice::CommunicatorPtr c, std::string name);
106  ~IceGridAdmin() override;
107 
108  /**
109  * Provides access to the IceGrid::AdminSession.
110  *
111  * @return A proxy of the IceGrid::AdminSession.
112  */
113  IceGrid::AdminSessionPrx adminSession();
114 
115  /**
116  * Provides access to the IceGrid::Admin.
117  *
118  * @return A proxy of the IceGrid::Admin.
119  */
120  IceGrid::AdminPrx getAdmin();
121 
122  /**
123  * Adds an application based on a path to an XML file
124  *
125  * @param xmlPath Path to XML configuration file for the application
126  */
127  void addApplication(const std::string& xmlPath);
128 
129  /**
130  * Provides access to the IceGrid::Registry.
131  * A proxy is created if necessary.
132  *
133  * @return A proxy of the IceGrid::Registy.
134  */
135  IceGrid::RegistryPrx registry();
136 
137  /**
138  * @brief setObjectObserver changes the ObjectObserver to the instance passed as parameter
139  * @param objObserverPrx
140  */
141  void setObjectObserver(IceGrid::ObjectObserverPrx objObserverPrx);
142 
143  /**
144  * @brief removeObservers removes all observers set on this IceGridAdmin instance
145  */
146  void removeObservers();
147 
148  /**
149  * Register an object with Ice for being accessed through IceGrid.
150  * First, an Ice::ObjectAdapter is created, the \p object added to
151  * it and then it is activated.
152  * Afterwards a proxy to the \p object is created and returned
153  *
154  * @param object The object to be registered, implementing an Ice
155  * interface.
156  * @param objectName The name this object should be available as.
157  * @param objectAdapter instance of the newly created Ice::ObjectAdapter
158  *
159  * @return The registered object proxy.
160  */
161  Ice::ObjectPrx registerObjectWithNewAdapter(
162  Ice::ObjectPtr object,
163  const std::string& objectName,
164  Ice::ObjectAdapterPtr& objectAdapter);
165 
166  /**
167  * @brief stops the timertask which calls the keepAlive() method on the admin session
168  */
169  void stop();
170 
171  /**
172  * Retrieve all well-known objects with name matching the regExp and of
173  * proxy type given as template parameter
174  *
175  * @param regExp regular expression to use for filtering
176  * @return list of object infos corresponding to all well-known objects registred in Ice Grid which match the ObjectType and the regExp
177  */
178  template <class ObjectType>
179  IceGrid::ObjectInfoSeq getRegisteredObjects(std::string regExp = "*")
180  {
181  IceGrid::AdminPrx admin = getAdmin();
182  IceGrid::ObjectInfoSeq objects = admin->getAllObjectInfos(regExp);
183  IceGrid::ObjectInfoSeq::iterator iter = objects.begin();
184  IceGrid::ObjectInfoSeq result;
185 
186  while (iter != objects.end())
187  {
188  Ice::ObjectPrx current = iter->proxy;
189 
190  ObjectType object;
191 
192  // if objects are hangig we might get connection refused
193  try
194  {
195  object = ObjectType::checkedCast(current->ice_timeout(60));
196  }
197  catch (...)
198  {
199  }
200 
201  if (object)
202  {
203  result.push_back(*iter);
204  }
205 
206  ++iter;
207  }
208 
209  return result;
210  }
211 
212  /**
213  * Retrieve names of all well-known objects with name matching the regExp and of
214  * proxy type given as template parameter
215  *
216  * @param regExp regular expression to use for filtering
217  * @return list of object names corresponding to all well-known objects registred in Ice Grid which match the ObjectType and the regExp
218  */
219  template <class ObjectType>
220  std::vector<std::string> getRegisteredObjectNames(std::string regExp = "*")
221  {
222  IceGrid::ObjectInfoSeq objects = getRegisteredObjects<ObjectType>(regExp);
223 
224  IceGrid::ObjectInfoSeq::iterator iter = objects.begin();
225  std::vector<std::string> result;
226 
227  while (iter != objects.end())
228  {
229  result.push_back(iter->proxy->ice_getIdentity().name);
230  ++iter;
231  }
232 
233  return result;
234  }
235 
236  /**
237  * @brief Removes all dead objects from the registry.
238  */
239  void cleanUpDeadObjects();
240 
241  /**
242  * Node Observer callbacks.
243  */
244  void nodeInit(const IceGrid::NodeDynamicInfoSeq& nodes, const Ice::Current& c = Ice::emptyCurrent) override;
245  void nodeUp(const IceGrid::NodeDynamicInfo& node, const Ice::Current& c = Ice::emptyCurrent) override;
246  void nodeDown(const std::string& name, const Ice::Current& c = Ice::emptyCurrent) override;
247  void updateServer(const std::string& node, IceGrid::ServerDynamicInfo updatedInfo, const Ice::Current& c = Ice::emptyCurrent);
248  void updateAdapter(const std::string& node, IceGrid::AdapterDynamicInfo updatedInfo, const Ice::Current& c = Ice::emptyCurrent);
249  void updateServer(const ::std::string& node, const ::IceGrid::ServerDynamicInfo& updatedInfo, const ::Ice::Current& c = Ice::emptyCurrent) override;
250  void updateAdapter(const ::std::string& node, const ::IceGrid::AdapterDynamicInfo& updatedInfo, const ::Ice::Current& c = Ice::emptyCurrent) override;
251 
252  enum ComponentState
253  {
254  eUnknown,
255  eActivated, // running
256  eDeactivated // stopped, crashed or not yet started
257  };
258 
259  /**
260  * Checks the state of a remote component.
261  * Don't use this method in your control loop, since it may introduce some network overhead.
262  * It's better to use callbacks for state updates (@see BaseComponent::requestComponentStateChangeNotification())
263  *
264  * @return The state of the component: If the component is running eActivated will be returned.
265  */
266  ComponentState getComponentState(std::string id);
267 
268  protected:
269  /**
270  * This method is executed periodically by IceGridAdmin::timer in
271  * the frequency specified by IceGridAdmin::interval.
272  */
273  void runTimerTask() override;
274 
275  void printServerInfo(const IceGrid::ServerDynamicInfo& updatedInfo);
276  void reportRemoteComponentStateChange(const IceGrid::ServerDynamicInfo& updatedInfo);
277  //! All components that have registered for notification with this id are notified
278  void notifyComponentChanged(ComponentState state, std::string id);
279  void setObservers();
280  private:
281  Ice::CommunicatorPtr communicator;
282  IceGrid::RegistryPrx registryProxy;
283  IceGrid::AdminSessionPrx adminSessionProxy;
284  Ice::ObjectAdapterPtr iceGridAdminAdapter;
285 
286  IceGrid::NodeObserverPrx iceGridAdminProxy;
287  IceGrid::ObjectObserverPrx objObserverPrx;
288 
289  std::map<std::string, std::vector< std::shared_ptr<Component> > > stateChangeNotifications;
290  std::map<std::string, ComponentState> remoteComponentsState;
291 
292  std::string name;
293 
294  IceUtil::TimerPtr timer;
296 
297  std::mutex mutexComponentStateUpdate;
298 
299  };
300 }
301 
boost::shared_ptr
Definition: IceGridAdmin.h:51
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
boost
Definition: ApplicationOptions.h:37
IceInternal::Handle< ::Ice::Communicator >
armarx::interval
Interval< T > interval(T lo, T hi)
Definition: OccupancyGrid.h:26
armarx::statechartmodel::eActivated
@ eActivated
Definition: SignalType.h:38
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::IceGridAdminPtr
IceUtil::Handle< IceGridAdmin > IceGridAdminPtr
Definition: ArmarXFwd.h:48
IceUtil::Handle< IceGridAdmin >
IceInternal::ProxyHandle
Definition: ObjectPoseClientWidget.h:39
armarx::statechartmodel::eDeactivated
@ eDeactivated
Definition: SignalType.h:39
ImportExport.h
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
Logging.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28