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