30 #include <Ice/Communicator.h>
31 #include <Ice/Exception.h>
32 #include <Ice/Initialize.h>
33 #include <Ice/LocalException.h>
34 #include <Ice/ObjectAdapter.h>
35 #include <Ice/ProxyHandle.h>
36 #include <IceGrid/Descriptor.h>
37 #include <IceGrid/Exception.h>
38 #include <IceGrid/FileParser.h>
58 setTag(
"IceGridAdmin");
59 std::string username =
"user";
60 std::string password =
"password";
64 throw Ice::ObjectNotExistException(
65 "Ice::Communicator needed to create IceGridAdmin, NULL not allowed here", 0);
71 adminSessionProxy = registry()->createAdminSession(username, password);
73 catch (
const IceGrid::PermissionDeniedException& e)
80 std::string obsName = std::string(
"IceGridAdminSession_") + name;
85 oPrx = registerObjectWithNewAdapter(
this, obsName, iceGridAdminAdapter);
87 catch (
const std::exception& e)
89 ARMARX_ERROR <<
"Register Adapter Exception: " << e.what() <<
"\n"
90 <<
"This might be happening because the property Ice.Default.Host is "
91 "wrongly set in the config (probably ~/.armarx/default.cfg)";
95 iceGridAdminProxy = IceGrid::NodeObserverPrx::checkedCast(oPrx);
101 timer =
new IceUtil::Timer();
102 timer->scheduleRepeated(
this,
interval);
113 IceGridAdmin::~IceGridAdmin()
115 ARMARX_DEBUG <<
"*** DESTROYING IceGridAdmin <" << name <<
":" <<
this <<
"> ***";
124 IceGridAdmin::registry()
128 Ice::ObjectPrx obj = communicator->stringToProxy(
"IceGrid/Registry");
129 registryProxy = IceGrid::RegistryPrx::checkedCast(obj);
132 return registryProxy;
135 IceGrid::AdminSessionPrx
136 IceGridAdmin::adminSession()
138 return adminSessionProxy;
142 IceGridAdmin::setObservers()
148 adminSession()->setObservers(
nullptr,
nullptr,
nullptr,
nullptr,
nullptr);
149 adminSession()->setObservers(
150 nullptr, iceGridAdminProxy,
nullptr,
nullptr, this->objObserverPrx);
153 catch (
const IceGrid::ObserverAlreadyRegisteredException& e)
155 ARMARX_INFO <<
" IceGrid observer with name " << e.id.name
156 <<
" already installed. State changed messages will only processed on a "
157 "polling basis, which just means a small delay.\n"
158 <<
"ObserverAlreadyRegisteredException:\n"
164 IceGridAdmin::setObjectObserver(IceGrid::ObjectObserverPrx objObserverPrx)
166 this->objObserverPrx = objObserverPrx;
171 IceGridAdmin::removeObservers()
173 IceGrid::NodeObserverPrx adminPrx = iceGridAdminProxy;
174 iceGridAdminProxy =
nullptr;
175 IceGrid::ObjectObserverPrx objPrx = objObserverPrx;
176 objObserverPrx =
nullptr;
183 getAdmin()->removeObject(objPrx->ice_getIdentity());
188 getAdmin()->removeObject(adminPrx->ice_getIdentity());
189 iceGridAdminAdapter->destroy();
190 iceGridAdminAdapter =
nullptr;
193 catch (IceGrid::ObjectNotRegisteredException& e)
199 IceGridAdmin::getAdmin()
201 return adminSessionProxy->getAdmin();
205 IceGridAdmin::addApplication(
const std::string& xmlPath)
207 IceGrid::FileParserPrx
parser;
208 parser = IceGrid::FileParserPrx::checkedCast(communicator->stringToProxy(
"FileParser"));
209 IceGrid::ApplicationDescriptor descriptor =
parser->
parse(xmlPath, getAdmin());
210 getAdmin()->addApplication(descriptor);
214 IceGridAdmin::runTimerTask()
218 std::cout <<
"Communicator NULL" << std::endl;
225 if (communicator && communicator->isShutdown())
231 if (adminSessionProxy)
233 adminSessionProxy->keepAlive();
240 catch (Ice::ObjectNotExistException& e)
242 ARMARX_ERROR <<
"*** Caught object not exist exception: " << e.what() << endl;
249 catch (Ice::Exception& e)
251 ARMARX_ERROR <<
"*** Caught exception: " << e.ice_id() <<
":\n"
253 << e.ice_stackTrace() << endl;
261 catch (std::exception& e)
263 ARMARX_ERROR <<
"*** Caught exception: " << e.what() << endl;
267 ARMARX_ERROR <<
"*** Caught unknown exception in IceGridAdmin " << endl;
279 IceGridAdmin::cleanUpDeadObjects()
281 IceGrid::AdminPrx admin = getAdmin();
282 IceGrid::ObjectInfoSeq objects = admin->getAllObjectInfos(
"*");
285 for (IceGrid::ObjectInfo info : objects)
287 auto current = info.proxy;
294 admin->removeObject(current->ice_getIdentity());
301 const std::string& objectName,
305 <<
"' with new adapter in Ice";
306 objectAdapter = communicator->createObjectAdapterWithEndpoints(objectName,
"tcp");
309 objectAdapter->add(
object,
id);
310 objectAdapter->activate();
312 IceGrid::AdminPrx admin = getAdmin();
314 Ice::ObjectPrx proxy = objectAdapter->createProxy(
id);
318 admin->addObjectWithType(proxy, proxy->ice_id());
320 catch (
const IceGrid::ObjectExistsException&)
322 admin->updateObject(proxy);
328 IceGridAdmin::ComponentState
329 IceGridAdmin::getComponentState(std::string
id)
331 Ice::ObjectPrx proxy = communicator->stringToProxy(
id);
335 cerr <<
"No proxy for id " <<
id << endl;
341 proxy->ice_twoway()->ice_ping();
343 catch (
const Ice::Exception&)
354 IceGridAdmin::nodeInit(
const IceGrid::NodeDynamicInfoSeq& nodes,
const Ice::Current&
c)
358 for (
unsigned int i = 0; i < nodes.size(); i++)
360 ARMARX_DEBUG <<
"Node " << i <<
": " << nodes[i].info.name << endl;
365 IceGridAdmin::nodeUp(
const IceGrid::NodeDynamicInfo& node,
const Ice::Current&
c)
372 IceGridAdmin::nodeDown(
const std::string& name,
const Ice::Current&
c)
379 IceGridAdmin::notifyComponentChanged(ComponentState state, std::string
id)
384 IceGridAdmin::reportRemoteComponentStateChange(
const IceGrid::ServerDynamicInfo& updatedInfo)
386 std::unique_lock lock(mutexComponentStateUpdate);
388 std::string idServer = updatedInfo.id;
389 std::string serverString =
"Server";
392 if (idServer.compare(idServer.size() - 6, 6, serverString) == 0)
394 idServer = idServer.substr(0, idServer.size() - 6);
397 if (remoteComponentsState.find(idServer) == remoteComponentsState.end())
405 switch (updatedInfo.state)
407 case IceGrid::Inactive:
408 case IceGrid::Activating:
409 case IceGrid::ActivationTimedOut:
410 case IceGrid::Deactivating:
411 case IceGrid::Destroying:
412 case IceGrid::Destroyed:
414 if (remoteComponentsState[idServer] ==
eActivated)
423 case IceGrid::Active:
425 if (remoteComponentsState[idServer] !=
eActivated)
435 cerr << __PRETTY_FUNCTION__ <<
"Unknown state" << endl;
437 if (remoteComponentsState[idServer] ==
eActivated)
442 remoteComponentsState[idServer] = eUnknown;
448 IceGridAdmin::printServerInfo(
const IceGrid::ServerDynamicInfo& updatedInfo)
450 cout <<
"Server:" << updatedInfo.id << endl;
451 cout <<
"** enabled:";
453 if (updatedInfo.enabled)
455 cout <<
"true" << endl;
459 cout <<
"false" << endl;
462 cout <<
"** Server state:";
464 switch (updatedInfo.state)
466 case IceGrid::Inactive:
467 cout <<
"Inactive" << endl;
470 case IceGrid::Activating:
471 cout <<
"Activating" << endl;
474 case IceGrid::ActivationTimedOut:
475 cout <<
"ActivationTimedOut" << endl;
478 case IceGrid::Active:
479 cout <<
"Active" << endl;
482 case IceGrid::Deactivating:
483 cout <<
"Deactivating" << endl;
486 case IceGrid::Destroying:
487 cout <<
"Destroying" << endl;
490 case IceGrid::Destroyed:
491 cout <<
"Destroyed" << endl;
495 cout <<
"unknown" << endl;
500 IceGridAdmin::updateServer(
const std::string& node,
501 IceGrid::ServerDynamicInfo updatedInfo,
502 const Ice::Current&
c)
507 reportRemoteComponentStateChange(updatedInfo);
511 IceGridAdmin::updateAdapter(
const std::string& node,
512 IceGrid::AdapterDynamicInfo updatedInfo,
513 const Ice::Current&
c)
519 IceGridAdmin::updateServer(const ::std::string& node,
520 const ::IceGrid::ServerDynamicInfo& updateServer,
521 const ::Ice::Current&
c)
525 reportRemoteComponentStateChange(updateServer);
529 IceGridAdmin::updateAdapter(const ::std::string& node,
530 const ::IceGrid::AdapterDynamicInfo& updatedInfo,
531 const ::Ice::Current&
c)