36 XmlAppLoader::XmlAppLoader(
const char* szPath,
const char* szAppName)
41 strAppName = szAppName;
47 if((strPath.rfind(directorySeparator)==string::npos) ||
48 (strPath.rfind(directorySeparator)!=strPath.size()-1))
49 strPath = strPath + string(directorySeparator);
56 XmlAppLoader::XmlAppLoader(
const char* szFileName)
61 strFileName = szFileName;
65 XmlAppLoader::~XmlAppLoader()
74 bool XmlAppLoader::init()
83 if(!strFileName.empty())
85 fileNames.push_back(strFileName);
91 logger->
addError(
"No application path is introduced.");
97 if ((dir = opendir(strPath.c_str())) ==
nullptr)
100 err<<
"Cannot access "<<strPath;
106 while((entry = readdir(dir)))
108 string name = entry->d_name;
111 string ext = name.substr(name.size()-3,3);
113 fileNames.push_back(strPath+name);
129 void XmlAppLoader::reset()
136 void XmlAppLoader::fini()
145 if(strAppName.empty())
150 if(fileNames.empty())
152 string fname = fileNames.back();
153 fileNames.pop_back();
154 app = parsXml(fname.c_str());
160 vector<string>::iterator itr;
161 for(itr=fileNames.begin(); itr<fileNames.end(); itr++)
164 if(app && (
string(app->
getName())==strAppName))
171 Application* XmlAppLoader::parsXml(
const char* szFile)
177 TiXmlDocument doc(szFile);
181 err<<
"Syntax error while loading "<<szFile<<
" at line "\
182 <<doc.ErrorRow()<<
": ";
183 err<<doc.ErrorDesc();
189 TiXmlElement *root = doc.RootElement();
193 err<<
"Syntax error while loading "<<szFile<<
" . ";
194 err<<
"No root element.";
208 auto* name = (TiXmlElement*) root->FirstChild(
"name");
209 if(!name || !name->GetText())
212 err<<
"Module from "<<szFile<<
" has no name.";
217 for(TiXmlElement* var = root->FirstChildElement(
"var"); var; var = var->NextSiblingElement())
219 if(var->Attribute(
"name") && var->GetText())
221 parser->addVariable(var->Attribute(
"name"), var->GetText());
225 app.setXmlFile(szFile);
229 string strname = parser->parseText(name->GetText());
230 for(
char& i : strname)
233 app.setName(strname.c_str());
238 if((desc = (TiXmlElement*) root->FirstChild(
"description")))
239 app.setDescription(parser->parseText(desc->GetText()).c_str());
243 if((ver = (TiXmlElement*) root->FirstChild(
"version")))
244 app.setVersion(parser->parseText(ver->GetText()).c_str());
258 TiXmlElement* authors;
259 if((authors = (TiXmlElement*) root->FirstChild(
"authors")))
260 for(TiXmlElement* ath = authors->FirstChildElement(); ath;
261 ath = ath->NextSiblingElement())
267 author.
setName(parser->parseText(ath->GetText()).c_str());
268 if(ath->Attribute(
"email"))
269 author.
setEmail(ath->Attribute(
"email"));
270 app.addAuthor(author);
275 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
282 TiXmlElement* resources;
283 if((resources = (TiXmlElement*) root->FirstChild(
"dependencies")))
284 for(TiXmlElement* res = resources->FirstChildElement(); res;
285 res = res->NextSiblingElement())
291 ResYarpPort resource(parser->parseText(res->GetText()).c_str());
292 resource.setPort(parser->parseText(res->GetText()).c_str());
293 app.addResource(resource);
299 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
308 vector<pair<const char*, setter> > modList;
309 pair<const char*, setter> pairNode;
311 pairNode.first =
"node"; pairNode.second = &ModuleInterface::setHost; modList.push_back(pairNode);
312 pairNode.first =
"parameters"; pairNode.second = &ModuleInterface::setParam; modList.push_back(pairNode);
313 pairNode.first =
"stdio"; pairNode.second = &ModuleInterface::setStdio; modList.push_back(pairNode);
314 pairNode.first =
"workdir"; pairNode.second = &ModuleInterface::setWorkDir; modList.push_back(pairNode);
315 pairNode.first =
"deployer"; pairNode.second = &ModuleInterface::setBroker; modList.push_back(pairNode);
316 pairNode.first =
"prefix"; pairNode.second = &ModuleInterface::setPrefix; modList.push_back(pairNode);
318 pairNode.first =
"display"; pairNode.second = &ModuleInterface::setDisplay; modList.push_back(pairNode);
319 for(TiXmlElement* mod = root->FirstChildElement(); mod; mod = mod->NextSiblingElement())
323 TiXmlElement* element;
324 if((element = (TiXmlElement*) mod->FirstChild(
"name")))
331 if(element->GetText())
333 elemText = parser->parseText(element->GetText());
334 text = elemText.c_str();
339 for(
auto& i : modList)
341 if((element = (TiXmlElement*) mod->FirstChild(i.first)))
344 if(element->GetText())
346 elemText = parser->parseText(element->GetText());
347 text = elemText.c_str();
350 (
module.*(i.second))(text);
354 if((element = (TiXmlElement*) mod->FirstChild(
"rank")))
356 if(element->GetText())
358 elemText = parser->parseText(element->GetText());
359 text = elemText.c_str();
362 module.setRank(atoi(text));
366 element = (TiXmlElement*) mod->FirstChild(
"geometry");
367 if(element && element->GetText())
372 if(prop.check(
"Pos"))
374 pt.
x = prop.findGroup(
"Pos").find(
"x").asFloat64();
375 pt.
y = prop.findGroup(
"Pos").find(
"y").asFloat64();
376 model.
points.push_back(pt);
377 module.setModelBase(model);
382 TiXmlElement* resources;
383 if((resources = (TiXmlElement*) mod->FirstChild(
"dependencies")))
385 for(TiXmlElement* res = resources->FirstChildElement(); res;
386 res = res->NextSiblingElement())
392 ResYarpPort resource(parser->parseText(res->GetText()).c_str());
393 resource.setPort(parser->parseText(res->GetText()).c_str());
394 if(res->Attribute(
"timeout"))
395 resource.setTimeout(atof(res->Attribute(
"timeout")));
396 if(res->Attribute(
"request"))
397 resource.setRequest(res->Attribute(
"request"));
398 if(res->Attribute(
"reply"))
399 resource.setReply(res->Attribute(
"reply"));
400 module.addResource(resource);
406 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
414 TiXmlElement* ensure;
415 if((ensure = (TiXmlElement*) mod->FirstChild(
"ensure")))
417 for(TiXmlElement* res = ensure->FirstChildElement(); res;
418 res = res->NextSiblingElement())
422 if (res->Attribute(
"when") &&
compareString(res->Attribute(
"when"),
"start")) {
423 if(parser->parseText(res->GetText()).c_str())
424 module.setPostExecWait(atof(parser->parseText(res->GetText()).c_str()));
426 else if (res->Attribute(
"when") &&
compareString(res->Attribute(
"when"),
"stop")) {
427 if(parser->parseText(res->GetText()).c_str())
428 module.setPostStopWait(atof(parser->parseText(res->GetText()).c_str()));
430 else if (res->Attribute(
"when") && strlen(res->Attribute(
"when"))) {
432 war <<
"Unrecognized value for 'when' property from " << szFile <<
" at line "<< res->Row() <<
".";
436 if(parser->parseText(res->GetText()).c_str())
437 module.setPostExecWait(atof(parser->parseText(res->GetText()).c_str()));
443 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
450 for(TiXmlElement* map = mod->FirstChildElement(); map;
451 map = map->NextSiblingElement())
455 TiXmlElement* second;
456 if((first=(TiXmlElement*) map->FirstChild(
"old")) &&
457 (second=(TiXmlElement*) map->FirstChild(
"new")) )
459 Portmap portmap(parser->parseText(first->GetText()).c_str(), parser->parseText(second->GetText()).c_str());
460 module.addPortmap(portmap);
469 war<<
"Module from "<<szFile<<
" at line "\
470 <<mod->Row()<<
" has not name tag.";
478 for(TiXmlElement* embApp = root->FirstChildElement(); embApp;
479 embApp = embApp->NextSiblingElement())
484 TiXmlElement* prefix;
485 if((name=(TiXmlElement*) embApp->FirstChild(
"name")))
488 if((prefix=(TiXmlElement*) embApp->FirstChild(
"prefix")))
489 IApp.setPrefix(parser->parseText(prefix->GetText()).c_str());
491 auto* element = (TiXmlElement*) embApp->FirstChild(
"geometry");
492 if(element && element->GetText())
497 if(prop.check(
"Pos"))
499 pt.
x = prop.findGroup(
"Pos").find(
"x").asFloat64();
500 pt.
y = prop.findGroup(
"Pos").find(
"y").asFloat64();
501 model.
points.push_back(pt);
502 IApp.setModelBase(model);
506 app.addIapplication(IApp);
511 war<<
"Incomplete application tag from "<<szFile<<
" at line "\
512 <<embApp->Row()<<
". (no name)";
520 for(TiXmlElement* arb = root->FirstChildElement(); arb;
521 arb = arb->NextSiblingElement())
525 auto* port = (TiXmlElement*) arb->FirstChild(
"port");
526 if(port && port->GetText())
528 Arbitrator arbitrator(parser->parseText(port->GetText()).c_str());
531 for(TiXmlElement* rule = arb->FirstChildElement(); rule;
532 rule = rule->NextSiblingElement())
536 if(rule->Attribute(
"connection"))
537 arbitrator.addRule(rule->Attribute(
"connection"), parser->parseText(rule->GetText()).c_str());
541 auto* geometry = (TiXmlElement*) arb->FirstChild(
"geometry");
542 if(geometry && geometry->GetText())
546 if(prop.check(
"Pos"))
549 for(
size_t i=1; i<pos.
size(); i++)
554 model.
points.push_back(pt);
556 arbitrator.setModelBase(model);
560 app.addArbitrator(arbitrator);
565 war<<
"Incomplete arbitrator tag from "<<szFile<<
" at line "\
573 for(TiXmlElement* cnn = root->FirstChildElement(); cnn;
574 cnn = cnn->NextSiblingElement())
578 auto* from = (TiXmlElement*) cnn->FirstChild(
"from");
579 auto* to = (TiXmlElement*) cnn->FirstChild(
"to");
582 from = (TiXmlElement*) cnn->FirstChild(
"output");
584 to = (TiXmlElement*) cnn->FirstChild(
"input");
586 TiXmlElement* protocol;
590 if((protocol=(TiXmlElement*) cnn->FirstChild(
"protocol")) &&
592 strCarrier = parser->parseText(protocol->GetText());
593 Connection connection(parser->parseText(from->GetText()).c_str(),
594 parser->parseText(to->GetText()).c_str(),
598 if(cnn->Attribute(
"qos")) {
599 connection.setQosTo(cnn->Attribute(
"qos"));
600 connection.setQosFrom(cnn->Attribute(
"qos"));
603 if(from->Attribute(
"external") &&
606 connection.setFromExternal(
true);
609 ResYarpPort resource(parser->parseText(from->GetText()).c_str());
610 resource.setPort(parser->parseText(from->GetText()).c_str());
611 app.addResource(resource);
614 if(from->Attribute(
"qos"))
615 connection.setQosFrom(from->Attribute(
"qos"));
616 if(to->Attribute(
"external") &&
621 connection.setToExternal(
true);
622 ResYarpPort resource(parser->parseText(to->GetText()).c_str());
623 resource.setPort(parser->parseText(to->GetText()).c_str());
624 app.addResource(resource);
627 if(to->Attribute(
"qos"))
628 connection.setQosTo(to->Attribute(
"qos"));
632 for(
int i=0; i<app.resourcesCount(); i++)
636 connection.setFromExternal(
true);
638 connection.setToExternal(
true);
641 if(cnn->Attribute(
"id"))
642 connection.setId(cnn->Attribute(
"id"));
644 if(cnn->Attribute(
"persist") &&
646 connection.setPersistent(
true);
649 auto* geometry = (TiXmlElement*) cnn->FirstChild(
"geometry");
650 if(geometry && geometry->GetText())
654 if(prop.check(
"Pos"))
657 for(
size_t i=1; i<pos.
size(); i++)
662 model.
points.push_back(pt);
664 connection.setModelBase(model);
669 app.addConnection(connection);
674 war<<
"Incomplete connection tag from "<<szFile<<
" at line "\