25 #define RUN_TIMEOUT 10 // Run timeout in seconds
26 #define STOP_TIMEOUT 30 // Stop timeout in seconds
27 #define KILL_TIMEOUT 10 // kill timeout in seconds
29 #define BROKER_LOCAL "local"
30 #define BROKER_YARPRUN "yarprun"
31 #define BROKER_YARPDEV "yarpdev"
45 bWithWatchDog = withWatchDog;
46 bAutoDependancy =
false;
50 knowledge.
createFrom(
nullptr,
nullptr,
nullptr);
55 const char* szResPath,
bool withWatchDog)
58 bWithWatchDog = withWatchDog;
59 bAutoDependancy =
false;
79 knowledge.
createFrom(pModLoad, pAppLoad, pResLoad);
93 if(find(listOfXml.begin(), listOfXml.end(),szFileName) == listOfXml.end())
94 listOfXml.emplace_back(szFileName);
104 return knowledge.
addApplication(application, szAppName_, modifyName);
116 const char* currentFile = application->
getXmlFile();
118 listOfXml.emplace_back(currentFile);
154 bool bloaded =
false;
177 if(!runnables.empty())
179 logger->
addError(
"Application cannot be removed if there is a loaded application");
182 listOfXml.erase(std::remove(listOfXml.begin(), listOfXml.end(), szFileName), listOfXml.end());
194 if(!runnables.empty())
196 logger->
addError(
"Module cannot be removed if there is a loaded application");
211 if(!runnables.empty())
213 logger->
addError(
"Resource cannot be removed if there is a loaded application");
232 logger->
addError(
"Please stop current running application first.");
236 strAppName = szAppName;
240 for(
auto& allresource : allresources)
242 auto* comp =
dynamic_cast<Computer*
>(allresource);
247 return prepare(
true);
264 bool ret = prepare(
false);
270 if (
id < runnables.size())
272 return runnables[id];
283 if (removeBroker(exe))
285 modules[id]->setHost(exe->
getHost());
286 Broker* broker = createBroker(modules[
id]);
287 if ( broker ==
nullptr)
291 broker->
setDisplay(modules[
id]->getDisplay());
302 bool Manager::prepare(
bool silent)
324 for(itr=modules.begin(); itr!=modules.end(); itr++)
326 Broker* broker = createBroker(*itr);
330 exe->setCommand((*itr)->getName());
331 exe->setParam((*itr)->getParam());
332 exe->setHost((*itr)->getHost());
333 exe->setStdio((*itr)->getStdio());
334 exe->setWorkDir((*itr)->getWorkDir());
335 exe->setPostExecWait((*itr)->getPostExecWait());
336 exe->setPostStopWait((*itr)->getPostStopWait());
337 exe->setOriginalPostExecWait((*itr)->getPostExecWait());
338 exe->setOriginalPostStopWait((*itr)->getPostStopWait());
340 if ((*itr)->getPrefix() && strlen((*itr)->getPrefix()))
341 env = string(
"YARP_PORT_PREFIX=") + string((*itr)->getPrefix());
342 if((*itr)->getEnvironment() && strlen((*itr)->getEnvironment()))
343 env += (env.length()) ? (
string(
";") + (*itr)->getEnvironment()) : (*itr)->getEnvironment();
344 exe->setEnv(env.c_str());
359 for(
auto& resource : resources)
362 if(res && (res->owner() == (*itr)))
363 exe->addResource(*res);
366 runnables.push_back(exe);
374 if(strlen(
module->getBroker()) == 0)
411 const char* szhost,
const char* szstdio,
412 const char* szworkdir,
const char* szenv )
414 if(runnables.empty())
416 logger->
addError(
"Application is not loaded.");
420 if(
id>=runnables.size())
422 logger->
addError(
"Module id is out of range.");
437 const char* to,
const char* carrier)
439 if(
id>=connections.size())
441 logger->
addError(
"Connection id is out of range.");
456 connections[id].setFrom(from);
457 connections[id].setTo(to);
458 connections[id].setCarrier(carrier);
471 if(
id>=resources.size())
473 logger->
addError(
"Resource id is out of range.");
487 string strPort = res->
getName();
488 if(strPort[0] !=
'/')
489 strPort = string(
"/") + strPort;
503 yError()<<
"Manager::Cannot contact the NameClient";
537 vector<string> ports;
541 for(
auto& allresource : allresources)
543 auto* comp =
dynamic_cast<Computer*
>(allresource);
547 for(
int i=0; i<comp->peripheralCount(); i++)
549 auto* res =
dynamic_cast<ResYarpPort*
>(&comp->getPeripheralAt(i));
555 for(
auto& port : ports)
562 for(
int i=0; i<comp->peripheralCount(); i++)
564 auto* res =
dynamic_cast<ResYarpPort*
>(&comp->getPeripheralAt(i));
565 if(res && (
string(res->getName()) ==
string(resport.
getName())))
573 comp->addPeripheral(resport);
595 auto* comp =
dynamic_cast<Computer*
>(resource);
596 if(!comp || !strlen(comp->getName()))
603 string strServer = comp->getName();
604 if(strServer[0] !=
'/')
605 strServer = string(
"/") + strServer;
609 comp->setAvailability(
false);
613 comp->setAvailability(
true);
636 comp->getProcessor().setCPULoad(load);
638 comp->getPlatform().setName(info.
platform.
name.c_str());
648 double wait = runnables[id]->getPostExecWait() +
RUN_TIMEOUT;
649 while(!timeout(base, wait))
653 msg<<
"Failed to run "<<runnables[id]->getCommand();
654 msg<<
" on "<<runnables[id]->getHost();
655 msg<<
". (State: "<<runnables[id]->state();
656 msg<<
", parameter: "<<runnables[id]->getParam()<<
")";
669 msg<<
"Failed to stop "<<runnables[id]->getCommand();
670 msg<<
" on "<<runnables[id]->getHost();
671 msg<<
". (State: "<<runnables[id]->state();
672 msg<<
", paramete: "<<runnables[id]->getParam()<<
")";
684 msg<<
"Failed to kill "<<runnables[id]->getCommand();
685 msg<<
" on "<<runnables[id]->getHost();
686 msg<<
". (State: "<<runnables[id]->state();
687 msg<<
", paramete: "<<runnables[id]->getParam()<<
")";
696 if(
id>=connections.size())
698 logger->
addError(
"Connection id is out of range.");
702 bool exists = connector.
exists(connections[
id].from());
703 connections[id].setFromExists(exists);
710 if(
id>=connections.size())
712 logger->
addError(
"Connection id is out of range.");
716 bool exists = connector.
exists(connections[
id].to());
717 connections[id].setToExists(exists);
731 for(itrRes=resources.begin(); itrRes!=resources.end(); itrRes++)
733 if(!(*itrRes)->getAvailability())
737 err<<
"Resource "<<(*itrRes)->getName()<<
" is not available!";
749 if(runnables.empty())
751 logger->
addError(
"Application is not loaded.");
755 if(
id>=runnables.size())
757 logger->
addError(
"Module id is out of range.");
761 if (runnables[
id]->shouldChangeBroker())
765 logger->
addError(
"Failing to switch broker");
770 runnables[id]->disableAutoConnect();
771 runnables[id]->start();
774 runnables[id]->startWatchDog();
785 if(runnables.empty())
787 logger->
addError(
"Application is not loaded.");
795 logger->
addError(
"Some of external ports dependency are not satisfied.");
799 logger->
addWarning(
"Some of external ports dependency are not satisfied.");
804 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
807 (*itr)->enableAutoConnect();
809 (*itr)->disableAutoConnect();
812 wait = (wait > (*itr)->getPostExecWait()) ? wait : (*itr)->getPostExecWait();
818 if(allRunning())
break;
822 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
823 (*itr)->startWatchDog();
829 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
833 msg<<
"Failed to run "<<(*itr)->getCommand();
834 msg<<
" on "<<(*itr)->getHost();
835 msg<<
". (State: "<<(*itr)->state();
836 msg<<
", parameter: "<<(*itr)->getParam()<<
")";
849 if(!connectExtraPorts())
851 logger->
addError(
"Failed to stablish some of connections.");
861 if(runnables.empty())
863 logger->
addError(
"Application is not loaded.");
867 if(
id>=runnables.size())
869 logger->
addError(
"Module id is out of range.");
873 runnables[id]->stop();
885 if(runnables.empty())
889 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
897 if(allStopped())
break;
902 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
904 ((*itr)->state() !=
DEAD))
907 msg<<
"Failed to stop "<<(*itr)->getCommand();
908 msg<<
" on "<<(*itr)->getHost();
909 msg<<
". (State: "<<(*itr)->state();
910 msg<<
", paramete: "<<(*itr)->getParam()<<
")";
921 if(runnables.empty())
923 logger->
addError(
"Application is not loaded.");
927 if(
id>=runnables.size())
929 logger->
addError(
"Module id is out of range.");
933 runnables[id]->kill();
943 if(runnables.empty())
947 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
955 if(allStopped())
break;
960 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
962 ((*itr)->state() !=
DEAD))
965 msg<<
"Failed to kill "<<(*itr)->getCommand();
966 msg<<
" on "<<(*itr)->getHost();
967 msg<<
". (State: "<<(*itr)->state();
968 msg<<
", paramete: "<<(*itr)->getParam()<<
")";
978 void Manager::clearExecutables()
981 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
992 if(
id>=connections.size())
994 logger->
addError(
"Connection id is out of range.");
1001 if( !connector.
connect(connections[
id].from(),
1002 connections[
id].to(),
1003 connections[
id].carrier(),
1004 connections[
id].isPersistent()) )
1012 return connector.
setQos(connections[
id].from(),
1013 connections[
id].to(),
1014 connections[
id].qosFrom(),
1015 connections[
id].qosTo());
1023 for(cnn=connections.begin(); cnn!=connections.end(); cnn++) {
1024 if( !(*cnn).getFromExists() ||
1025 !(*cnn).getToExists() ||
1026 !connector.
connect((*cnn).from(), (*cnn).to(),
1027 (*cnn).carrier(), (*cnn).isPersistent()) )
1036 if(! connector.
setQos((*cnn).from(), (*cnn).to(),
1037 (*cnn).qosFrom(), (*cnn).qosTo())) {
1047 if(
id>=connections.size())
1049 logger->
addError(
"Connection id is out of range.");
1056 if( !connector.
disconnect(connections[
id].from(),
1057 connections[
id].to(),
1058 connections[
id].carrier()) )
1073 for(cnn=connections.begin(); cnn!=connections.end(); cnn++)
1074 if( !connector.
disconnect((*cnn).from(), (*cnn).to(), (*cnn).carrier()) )
1086 if(
id>=connections.size())
1088 logger->
addError(
"Connection id is out of range.");
1092 if(!connector.
rmconnect(connections[
id].from(),
1093 connections[
id].to()) )
1106 for(cnn=connections.begin(); cnn!=connections.end(); cnn++)
1107 if( !connector.
rmconnect((*cnn).from(), (*cnn).to()) )
1118 if(
id>=connections.size())
1120 logger->
addError(
"Connection id is out of range.");
1124 return connections[id].getFromExists() &&
1125 connections[id].getToExists() &&
1126 connector.
connected(connections[
id].from(),
1127 connections[
id].to(),
1128 connections[
id].carrier());
1137 bool bConnected =
true;
1138 for(cnn=connections.begin(); cnn!=connections.end(); cnn++)
1139 if( !(*cnn).getFromExists() ||
1140 !(*cnn).getToExists() ||
1141 !connector.
connected((*cnn).from(), (*cnn).to(), (*cnn).carrier()) )
1146 bool Manager::checkPortsAvailable(
Broker* broker)
1149 for(itr=connections.begin(); itr!=connections.end(); itr++)
1153 if(!broker->
exists((*itr).to()) ||
1154 !broker->
exists((*itr).from()))
1162 bool Manager::connectExtraPorts()
1168 while(!timeout(base, 10.0))
1169 if(checkPortsAvailable(&connector))
1173 for(cnn=connections.begin(); cnn!=connections.end(); cnn++)
1177 if( !connector.
connect((*cnn).from(), (*cnn).to(),
1191 if(
id>=runnables.size())
1193 logger->
addError(
"Module id is out of range.");
1197 RSTATE st = runnables[id]->state();
1204 bool Manager::allRunning()
1206 if(!runnables.size())
1209 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
1211 RSTATE st = (*itr)->state();
1221 if(
id>=runnables.size())
1223 logger->
addError(
"Module id is out of range.");
1226 RSTATE st = runnables[id]->state();
1233 bool Manager::allStopped()
1235 if(!runnables.size())
1238 for(itr=runnables.begin(); itr!=runnables.end(); itr++)
1240 RSTATE st = (*itr)->state();
1249 if(
id>=runnables.size())
1251 logger->
addError(
"Module id is out of range.");
1258 msg<<
"Cannot attach to stdout of "<<runnables[id]->getCommand();
1259 msg<<
" on "<<runnables[id]->getHost();
1260 msg<<
". (State: "<<runnables[id]->state();
1261 msg<<
", paramete: "<<runnables[id]->getParam()<<
") ";
1262 msg<<
"because "<<runnables[id]->getBroker()->error();
1271 if(
id>=runnables.size())
1273 logger->
addError(
"Module id is out of range.");
1277 runnables[id]->getBroker()->detachStdout();
1281 bool Manager::timeout(
double base,
double t)