25 #include <Ice/Communicator.h>
26 #include <Ice/Exception.h>
27 #include <Ice/Initialize.h>
28 #include <Ice/LocalException.h>
29 #include <Ice/ObjectAdapter.h>
30 #include <Ice/ProxyHandle.h>
31 #include <IceGrid/Descriptor.h>
32 #include <IceGrid/Exception.h>
33 #include <IceGrid/FileParser.h>
55 void IceGridAdmin::init()
57 setTag(
"IceGridAdmin");
58 std::string username =
"user";
59 std::string password =
"password";
63 throw Ice::ObjectNotExistException(
"Ice::Communicator needed to create IceGridAdmin, NULL not allowed here", 0);
70 registry()->createAdminSession(username, password);
72 catch (
const IceGrid::PermissionDeniedException& e)
79 std::string obsName = std::string(
"IceGridAdminSession_") + name;
84 oPrx = registerObjectWithNewAdapter(
this, obsName, iceGridAdminAdapter);
86 catch (
const std::exception& e)
88 ARMARX_ERROR <<
"Register Adapter Exception: " << e.what() <<
"\n"
89 <<
"This might be happening because the property Ice.Default.Host is wrongly set in the config (probably ~/.armarx/default.cfg)";
93 iceGridAdminProxy = IceGrid::NodeObserverPrx::checkedCast(oPrx);
99 timer =
new IceUtil::Timer();
100 timer->scheduleRepeated(
this,
interval);
110 IceGridAdmin::~IceGridAdmin()
112 ARMARX_DEBUG <<
"*** DESTROYING IceGridAdmin <" << name <<
":" <<
this <<
"> ***";
124 Ice::ObjectPrx obj = communicator->stringToProxy(
"IceGrid/Registry");
125 registryProxy = IceGrid::RegistryPrx::checkedCast(obj);
128 return registryProxy;
131 IceGrid::AdminSessionPrx IceGridAdmin::adminSession()
133 return adminSessionProxy;
136 void IceGridAdmin::setObservers()
142 adminSession()->setObservers(
nullptr,
nullptr,
nullptr,
nullptr,
nullptr);
143 adminSession()->setObservers(
nullptr, iceGridAdminProxy,
nullptr,
nullptr, this->objObserverPrx);
146 catch (
const IceGrid::ObserverAlreadyRegisteredException& e)
148 ARMARX_INFO <<
" IceGrid observer with name " << e.id.name <<
" already installed. State changed messages will only processed on a polling basis, which just means a small delay.\n" <<
149 "ObserverAlreadyRegisteredException:\n" << e.what();
153 void IceGridAdmin::setObjectObserver(IceGrid::ObjectObserverPrx objObserverPrx)
155 this->objObserverPrx = objObserverPrx;
159 void IceGridAdmin::removeObservers()
161 IceGrid::NodeObserverPrx adminPrx = iceGridAdminProxy;
162 iceGridAdminProxy =
nullptr;
163 IceGrid::ObjectObserverPrx objPrx = objObserverPrx;
164 objObserverPrx =
nullptr;
171 getAdmin()->removeObject(objPrx->ice_getIdentity());
176 getAdmin()->removeObject(adminPrx->ice_getIdentity());
177 iceGridAdminAdapter->destroy();
178 iceGridAdminAdapter =
nullptr;
181 catch (IceGrid::ObjectNotRegisteredException& e)
188 IceGrid::AdminPrx IceGridAdmin::getAdmin()
190 return adminSessionProxy->getAdmin();
193 void IceGridAdmin::addApplication(
const std::string& xmlPath)
195 IceGrid::FileParserPrx
parser;
196 parser = IceGrid::FileParserPrx::checkedCast(communicator->stringToProxy(
"FileParser"));
197 IceGrid::ApplicationDescriptor descriptor =
parser->
parse(xmlPath, getAdmin());
198 getAdmin()->addApplication(descriptor);
201 void IceGridAdmin::runTimerTask()
205 std::cout <<
"Communicator NULL" << std::endl;
212 if (communicator && communicator->isShutdown())
218 if (adminSessionProxy)
220 adminSessionProxy->keepAlive();
227 catch (Ice::ObjectNotExistException& e)
229 ARMARX_ERROR <<
"*** Caught object not exist exception: " << e.what() << endl;
236 catch (Ice::Exception& e)
238 ARMARX_ERROR <<
"*** Caught exception: " << e.ice_id() <<
":\n" << e.what() <<
"\n" << e.ice_stackTrace() << endl;
246 catch (std::exception& e)
248 ARMARX_ERROR <<
"*** Caught exception: " << e.what() << endl;
252 ARMARX_ERROR <<
"*** Caught unknown exception in IceGridAdmin " << endl;
256 void IceGridAdmin::stop()
262 void IceGridAdmin::cleanUpDeadObjects()
264 IceGrid::AdminPrx admin = getAdmin();
265 IceGrid::ObjectInfoSeq objects = admin->getAllObjectInfos(
"*");
268 for (IceGrid::ObjectInfo info : objects)
270 auto current = info.proxy;
277 admin->removeObject(current->ice_getIdentity());
282 Ice::ObjectPrx IceGridAdmin::registerObjectWithNewAdapter(
284 const std::string& objectName,
287 ARMARX_VERBOSE <<
"Registering object with name '" << objectName <<
"' with new adapter in Ice";
288 objectAdapter = communicator->createObjectAdapterWithEndpoints(objectName,
"tcp");
291 objectAdapter->add(
object,
id);
292 objectAdapter->activate();
294 IceGrid::AdminPrx admin = getAdmin();
296 Ice::ObjectPrx proxy = objectAdapter->createProxy(
id);
300 admin->addObjectWithType(proxy, proxy->ice_id());
302 catch (
const IceGrid::ObjectExistsException&)
304 admin->updateObject(proxy);
310 IceGridAdmin::ComponentState IceGridAdmin::getComponentState(std::string
id)
312 Ice::ObjectPrx proxy = communicator->stringToProxy(
id);
316 cerr <<
"No proxy for id " <<
id << endl;
322 proxy->ice_twoway()->ice_ping();
324 catch (
const Ice::Exception&)
334 void IceGridAdmin::nodeInit(
const IceGrid::NodeDynamicInfoSeq& nodes,
const Ice::Current&
c)
338 for (
unsigned int i = 0; i < nodes.size(); i++)
340 ARMARX_DEBUG <<
"Node " << i <<
": " << nodes[i].info.name << endl;
344 void IceGridAdmin::nodeUp(
const IceGrid::NodeDynamicInfo& node,
const Ice::Current&
c)
350 void IceGridAdmin::nodeDown(
const std::string& name,
const Ice::Current&
c)
357 void IceGridAdmin::notifyComponentChanged(ComponentState state, std::string
id)
361 void IceGridAdmin::reportRemoteComponentStateChange(
const IceGrid::ServerDynamicInfo& updatedInfo)
363 std::unique_lock lock(mutexComponentStateUpdate);
365 std::string idServer = updatedInfo.id;
366 std::string serverString =
"Server";
369 if (idServer.compare(idServer.size() - 6, 6, serverString) == 0)
371 idServer = idServer.substr(0, idServer.size() - 6);
374 if (remoteComponentsState.find(idServer) == remoteComponentsState.end())
382 switch (updatedInfo.state)
384 case IceGrid::Inactive:
385 case IceGrid::Activating:
386 case IceGrid::ActivationTimedOut:
387 case IceGrid::Deactivating:
388 case IceGrid::Destroying:
389 case IceGrid::Destroyed:
391 if (remoteComponentsState[idServer] ==
eActivated)
400 case IceGrid::Active:
402 if (remoteComponentsState[idServer] !=
eActivated)
412 cerr << __PRETTY_FUNCTION__ <<
"Unknown state" << endl;
414 if (remoteComponentsState[idServer] ==
eActivated)
419 remoteComponentsState[idServer] = eUnknown;
425 void IceGridAdmin::printServerInfo(
const IceGrid::ServerDynamicInfo& updatedInfo)
427 cout <<
"Server:" << updatedInfo.id << endl;
428 cout <<
"** enabled:";
430 if (updatedInfo.enabled)
432 cout <<
"true" << endl;
436 cout <<
"false" << endl;
439 cout <<
"** Server state:";
441 switch (updatedInfo.state)
443 case IceGrid::Inactive:
444 cout <<
"Inactive" << endl;
447 case IceGrid::Activating:
448 cout <<
"Activating" << endl;
451 case IceGrid::ActivationTimedOut:
452 cout <<
"ActivationTimedOut" << endl;
455 case IceGrid::Active:
456 cout <<
"Active" << endl;
459 case IceGrid::Deactivating:
460 cout <<
"Deactivating" << endl;
463 case IceGrid::Destroying:
464 cout <<
"Destroying" << endl;
467 case IceGrid::Destroyed:
468 cout <<
"Destroyed" << endl;
472 cout <<
"unknown" << endl;
476 void IceGridAdmin::updateServer(
const std::string& node, IceGrid::ServerDynamicInfo updatedInfo,
const Ice::Current&
c)
481 reportRemoteComponentStateChange(updatedInfo);
485 void IceGridAdmin::updateAdapter(
const std::string& node, IceGrid::AdapterDynamicInfo updatedInfo,
const Ice::Current&
c)
490 void IceGridAdmin::updateServer(const ::std::string& node, const ::IceGrid::ServerDynamicInfo& updateServer, const ::Ice::Current&
c)
494 reportRemoteComponentStateChange(updateServer);
497 void IceGridAdmin::updateAdapter(const ::std::string& node, const ::IceGrid::AdapterDynamicInfo& updatedInfo, const ::Ice::Current&
c)