Skip to content

Commit

Permalink
adding <ensure> tag to ensure some post-condition for running the mod…
Browse files Browse the repository at this point in the history
…ule. e.g., wait for few seconds after launching a module
  • Loading branch information
apaikan committed May 23, 2015
1 parent 678f347 commit becdbbe
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/libYARP_manager/include/yarp/manager/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ModuleInterface{
ModuleInterface( const char* szName) {
if(szName) strName = szName;
iRank = -1;
wait = 0.0;
}

ModuleInterface(Module* module);
Expand Down Expand Up @@ -188,6 +189,9 @@ class ModuleInterface{
void addResource(ResYarpPort &res) { resources.push_back(res); }
ResourceContainer& getResources(void) { return resources; }

void setPostExecWait(double t) { wait = t; }
double getPostExecWait() { return wait; }

int portmapCount(void) { return portmaps.size(); }
Portmap& getPortmapAt(int index){ return portmaps[index]; }
bool addPortmap(Portmap &portmap);
Expand All @@ -200,6 +204,7 @@ class ModuleInterface{
void setModelBase(GraphicModel& mdl) { modelBase = mdl; };



protected:

private:
Expand All @@ -212,6 +217,7 @@ class ModuleInterface{
string strPrefix;
int iRank;
ResourceContainer resources;
double wait;
PortmapContainer portmaps;
PortmapIterator findPortmap(Portmap& portmap);
string strTag;
Expand Down
4 changes: 4 additions & 0 deletions src/libYARP_manager/include/yarp/manager/executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class Executable : public BrokerEventSink
const char* getEnv(void) { return strEnv.c_str(); }
int getID(void) { return theID; }

void setPostExecWait(double t) { wait = t; }
double getPostExecWait() { return wait; }

void enableAutoConnect(void) { bAutoConnect = true; }
void disableAutoConnect(void) { bAutoConnect = false; }
bool autoConnect(void) { return bAutoConnect; }
Expand All @@ -119,6 +122,7 @@ class Executable : public BrokerEventSink
string strWorkdir;
string strEnv;
int theID;
double wait;

bool bWatchDog;
Broker* broker;
Expand Down
2 changes: 1 addition & 1 deletion src/libYARP_manager/include/yarp/manager/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Manager : public MEvent{
bool oneRunning(void);
bool allStopped(void);
bool prepare(bool silent=true);
bool timeout(double base, double timeout);
bool timeout(double base, double t);
bool updateResource(GenericResource* resource);
Broker* createBroker(Module* module);
};
Expand Down
5 changes: 4 additions & 1 deletion src/libYARP_manager/include/yarp/manager/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class Module : public Node{
bool addResource(GenericResource& res);
bool removeResource(GenericResource& res);

void setPostExecWait(double t) { wait = t; }
double getPostExecWait() { return wait; }

void clearInputs(void) { inputs.clear(); }
void clearOutputs(void) { outputs.clear(); }
void clearResources(void) { resources.clear(); }
Expand Down Expand Up @@ -210,7 +213,7 @@ class Module : public Node{
bool bNeedDeployer;
string strPrefix;
string strBasePrefix;

double wait;
GraphicModel modelBase;

ArgumentIterator findArgument(Argument& argument);
Expand Down
1 change: 1 addition & 0 deletions src/libYARP_manager/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ModuleInterface::ModuleInterface(Module* module)
strPrefix = module->strPrefix;
iRank = module->iRank;
strTag = module->getLabel();
wait = module->getPostExecWait();
if(module->getModel())
modelBase = *module->getModel();
else
Expand Down
1 change: 1 addition & 0 deletions src/libYARP_manager/src/execstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ void Ready::startModule(void)
}
else
{
yarp::os::Time::delay(executable->getPostExecWait());
castEvent(EventFactory::startModuleEventOk);
executable->getEvent()->onExecutableStart(executable);
}
Expand Down
1 change: 1 addition & 0 deletions src/libYARP_manager/src/executable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Executable::Executable(Broker* _broker, MEvent* _event,
broker = _broker;
event = _event;
bWatchDog = _bWatchDog;
wait = 0.0;
logger = ErrorLogger::Instance();
broker->setEventSink(dynamic_cast<BrokerEventSink*>(this));
execMachine = new ExecMachine(this);
Expand Down
2 changes: 1 addition & 1 deletion src/libYARP_manager/src/kbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ bool KnowledgeBase::updateModule(Module* module, ModuleInterface* imod )
module->setStdio(imod->getStdio());
if(strlen(imod->getWorkDir()))
module->setWorkDir(imod->getWorkDir());

module->setPostExecWait(imod->getPostExecWait());
module->setModelBase(imod->getModelBase());


Expand Down
21 changes: 12 additions & 9 deletions src/libYARP_manager/src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ bool Manager::prepare(bool silent)
exe->setParam((*itr)->getParam());
exe->setHost((*itr)->getHost());
exe->setStdio((*itr)->getStdio());
exe->setWorkDir((*itr)->getWorkDir());
exe->setWorkDir((*itr)->getWorkDir());
exe->setPostExecWait((*itr)->getPostExecWait());
string env = string("YARP_PORT_PREFIX=") +
string((*itr)->getPrefix());
exe->setEnv(env.c_str());
Expand Down Expand Up @@ -612,14 +613,15 @@ bool Manager::run(unsigned int id, bool async)

// waiting for running
double base = yarp::os::Time::now();
while(!timeout(base, RUN_TIMEOUT))
double wait = runnables[id]->getPostExecWait() + RUN_TIMEOUT;
while(!timeout(base, wait))
if(running(id)) return true;

OSTRINGSTREAM msg;
msg<<"Failed to run "<<runnables[id]->getCommand();
msg<<" on "<<runnables[id]->getHost();
msg<<". (State: "<<runnables[id]->state();
msg<<", paramete: "<<runnables[id]->getParam()<<")";
msg<<", parameter: "<<runnables[id]->getParam()<<")";
logger->addError(msg);
return false;
}
Expand All @@ -644,21 +646,22 @@ bool Manager::run(void)
}

ExecutablePIterator itr;
double wait = 0.0;
for(itr=runnables.begin(); itr!=runnables.end(); itr++)
{
if(bAutoConnect)
(*itr)->enableAutoConnect();
else
(*itr)->disableAutoConnect();
(*itr)->start();
(*itr)->start();
yarp::os::Time::delay(0.2);
wait = (wait > (*itr)->getPostExecWait()) ? wait : (*itr)->getPostExecWait();
}

// waiting for running
double base = yarp::os::Time::now();
while(!timeout(base, RUN_TIMEOUT))
while(!timeout(base, wait + RUN_TIMEOUT))
if(allRunning()) break;

if(!allRunning())
{
ExecutablePIterator itr;
Expand All @@ -669,7 +672,7 @@ bool Manager::run(void)
msg<<"Failed to run "<<(*itr)->getCommand();
msg<<" on "<<(*itr)->getHost();
msg<<". (State: "<<(*itr)->state();
msg<<", paramete: "<<(*itr)->getParam()<<")";
msg<<", parameter: "<<(*itr)->getParam()<<")";
logger->addError(msg);
}

Expand Down Expand Up @@ -1117,10 +1120,10 @@ bool Manager::detachStdout(unsigned int id)
return true;
}

bool Manager::timeout(double base, double timeout)
bool Manager::timeout(double base, double t)
{
yarp::os::Time::delay(1.0);
if((yarp::os::Time::now()-base) > timeout)
if((yarp::os::Time::now()-base) > t)
return true;
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/libYARP_manager/src/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void Module::swap(const Module &mod)
strPrefix = mod.strPrefix;
strBasePrefix = mod.strBasePrefix;
modOwner = mod.modOwner;
wait = mod.wait;
// deep copy
for(int i=0; i<mod.resourceCount(); i++)
addResource(mod.getResourceAt(i));
Expand Down Expand Up @@ -234,6 +235,7 @@ void Module::clear(void)
*itr = NULL;
}
resources.clear();
wait = 0.0;
}

bool Module::setParam(const char* szParam)
Expand Down
22 changes: 22 additions & 0 deletions src/libYARP_manager/src/xmlapploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,28 @@ Application* XmlAppLoader::parsXml(const char* szFile)
}
}
}

/* retrieving resources information*/
TiXmlElement* ensure;
if((ensure = (TiXmlElement*) mod->FirstChild("ensure")))
{
for(TiXmlElement* res = ensure->FirstChildElement(); res;
res = res->NextSiblingElement())
{
if(compareString(res->Value(), "wait"))
{
if(res->GetText())
module.setPostExecWait(atof(res->GetText()));
}
else
{
OSTRINGSTREAM war;
war<<"Unrecognized tag from "<<szFile<<" at line "\
<<res->Row()<<".";
logger->addWarning(war);
}
}
}
/* retrieving portmaps */
for(TiXmlElement* map = mod->FirstChildElement(); map;
map = map->NextSiblingElement())
Expand Down

0 comments on commit becdbbe

Please sign in to comment.