1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "yacsconfig.h"
21 #include "RuntimeSALOME.hxx"
24 #include "Exception.hxx"
25 #include "Executor.hxx"
26 #include "parsers.hxx"
27 #include "VisitorSaveState.hxx"
28 #include "VisitorSaveSalomeSchema.hxx"
29 #include "LoadState.hxx"
30 #include "Dispatcher.hxx"
31 #include "LinkInfo.hxx"
34 #include "SALOME_NamingService.hxx"
35 #include "SALOME_ModuleCatalog.hh"
36 #include "Basics_Utils.hxx"
49 using YACS::YACSLoader;
50 using namespace YACS::ENGINE;
54 // --- use of glibc argp interface for parsing unix-style arguments
56 const char *argp_program_version ="driver V0.1";
57 const char *argp_program_bug_address ="<nepal@nepal.edf.fr>";
58 static char doc[] ="driver -- a SALOME YACS graph executor";
59 static char args_doc[] = "graph.xml";
63 static struct argp_option options[] =
65 {"display", 'd', "level", 0, "Display dot files: 0=never to 3=very often (default 0)"},
66 {"verbose", 'v', 0, 0, "Produce verbose output" },
67 {"stop-on-error", 's', 0, 0, "Stop on first error" },
68 {"dump-on-error", 'e', "file", OPTION_ARG_OPTIONAL, "Stop on first error and dump state"},
69 {"dump-final", 'f', "file", OPTION_ARG_OPTIONAL, "dump final state"},
70 {"dump", 'g', "nbsec", OPTION_ARG_OPTIONAL, "dump state"},
71 {"load-state", 'l', "file", 0, "Load State from a previous partial execution"},
72 {"save-xml-schema", 'x', "file", OPTION_ARG_OPTIONAL, "dump xml schema"},
73 {"shutdown", 't', "level", 0, "Shutdown the schema: 0=no shutdown to 3=full shutdown (default 1)"},
74 {"reset", 'r', "level", 0, "Reset the schema before execution: 0=nothing, 1=reset error nodes to ready state (default 0)"},
75 {"kill-port", 'k', "port", 0, "Kill Salome application running on the specified port if the driver process is killed (with SIGINT or SIGTERM)"},
76 {"init_port", 'i', "value", OPTION_ARG_OPTIONAL, "Initialisation value of a port, specified as bloc.node.port=value."},
95 std::list<std::string> init_ports;
109 parse_opt (int key, char *arg, struct argp_state *state)
113 // Get the input argument from argp_parse, which we
114 // know is a pointer to our arguments structure.
115 struct arguments *myArgs = (arguments*)state->input;
120 myArgs->display = atoi(arg);
123 myArgs->shutdown = atoi(arg);
126 myArgs->reset = atoi(arg);
137 myArgs->dumpErrorFile = arg;
139 myArgs->dumpErrorFile = (char *)"dumpErrorState.xml";
143 myArgs->finalDump = arg;
145 myArgs->finalDump = (char *)"finalDumpState.xml";
149 myArgs->dump = atoi(arg);
154 myArgs->loadState = arg;
158 myArgs->xmlSchema = arg;
160 myArgs->xmlSchema = (char *)"saveSchema.xml";
163 myArgs->killPort = atoi(arg);
167 myArgs->init_ports.push_back(std::string(arg));
171 if (state->arg_num >=1) // Too many arguments.
173 myArgs->args[state->arg_num] = arg;
177 if (state->arg_num < 1) // Not enough arguments.
182 return ARGP_ERR_UNKNOWN;
191 static struct argp argp = { options, parse_opt, args_doc, doc };
194 void timer(std::string msg)
199 gettimeofday(&tv,NULL);
200 long t=tv.tv_sec*1000+tv.tv_usec/1000;
202 gettimeofday(&tv,NULL);
203 std::cerr << msg << tv.tv_sec*1000+tv.tv_usec/1000-t0 << " ms" << std::endl;
208 static struct arguments myArgs;
210 void Handler(int theSigId)
215 //if requested save state
216 bool isFinalDump = (strlen(myArgs.finalDump) != 0);
219 YACS::ENGINE::VisitorSaveState vst(p);
220 vst.openFileDump(myArgs.finalDump);
224 //if requested shutdown schema
225 if(myArgs.shutdown < 999)
227 p->shutdown(myArgs.shutdown);
232 ostringstream command;
233 command << "killSalomeWithPort.py " << myArgs.killPort;
234 int status = system(command.str().c_str());
236 cerr << "Salome application on port " << myArgs.killPort << " is killed" << endl;
238 cerr << "Error: Can't kill Salome application on port " << myArgs.killPort << endl;
243 void * dumpState(void *arg)
245 thread_st *st = (thread_st*)arg;
246 YACS::StatesForNode state = p->getEffectiveState();
247 while((state != YACS::DONE) && (state != YACS::LOADFAILED) && (state != YACS::EXECFAILED) && (state != YACS::INTERNALERR) && (state != YACS::DISABLED) && (state != YACS::FAILED) && (state != YACS::ERROR)){
253 string cmd = "touch " + st->lockFile;
255 YACS::ENGINE::VisitorSaveState vst(p);
256 vst.openFileDump(st->dumpFile);
259 cmd = "rm -f " + st->lockFile;
261 state = p->getEffectiveState();
268 typedef void (*sighandler_t)(int);
269 sighandler_t setsig(int sig, sighandler_t handler)
271 struct sigaction context, ocontext;
272 context.sa_handler = handler;
273 sigemptyset(&context.sa_mask);
274 context.sa_flags = 0;
275 if (sigaction(sig, &context, &ocontext) == -1)
277 return ocontext.sa_handler;
281 bool parse_init_port(const std::string& input, std::string& node, std::string& port, std::string& value)
284 size_t pos_eq = input.find('=');
285 if(pos_eq == std::string::npos || pos_eq == input.size())
287 value = input.substr(pos_eq+1);
288 size_t pos_dot = input.rfind('.', pos_eq);
289 if(!pos_dot || pos_dot == std::string::npos || pos_dot >= pos_eq-1)
291 port = input.substr(pos_dot+1, pos_eq-pos_dot-1);
292 node = input.substr(0, pos_dot);
296 int main (int argc, char* argv[])
303 myArgs.dumpErrorFile= (char *)"";
304 myArgs.finalDump = (char *)"";
306 myArgs.loadState = (char *)"";
307 myArgs.xmlSchema = (char *)"";
311 myArgs.init_ports.clear();
313 // Parse our arguments; every option seen by parse_opt will be reflected in arguments.
316 argp_parse (&argp, argc, argv, 0, 0, &myArgs);
317 std::cerr << "graph = " << myArgs.args[0];
318 std::cerr << " options: display=" << myArgs.display;
319 std::cerr << " verbose="<<myArgs.verbose;
320 std::cerr << " stop-on-error=" << myArgs.stop;
321 std::cerr << " shutdown=" << myArgs.shutdown;
322 std::cerr << " reset=" << myArgs.reset;
324 std::cerr << " kill-port=" << myArgs.killPort;
326 std::cerr << " dumpErrorFile=" << myArgs.dumpErrorFile << std::endl;
328 std::cerr << std::endl;
329 std::list<std::string>::const_iterator it;
330 for(it=myArgs.init_ports.begin(); it != myArgs.init_ports.end(); it++)
332 std::cerr << (*it) << std::endl;
337 setsig(SIGINT,&Handler);
338 setsig(SIGTERM,&Handler);
342 long flags = RuntimeSALOME::UsePython + RuntimeSALOME::UseCorba + RuntimeSALOME::UseXml + \
343 RuntimeSALOME::UseCpp + RuntimeSALOME::UseSalome;
344 RuntimeSALOME::setRuntime(flags, argc, argv);
346 // Try to load the session catalog if it exists
349 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
350 CORBA::ORB_ptr orb = runTime->getOrb();
353 SALOME_NamingService namingService(orb);
354 CORBA::Object_var obj = namingService.Resolve("/Kernel/ModulCatalog");
355 SALOME_ModuleCatalog::ModuleCatalog_var aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
356 if (! CORBA::is_nil(aModuleCatalog))
358 CORBA::String_var anIOR = orb->object_to_string( aModuleCatalog );
359 YACS::ENGINE::Catalog* aCatalog = runTime->loadCatalog( "session", anIOR.in() );
360 runTime->addCatalog(aCatalog);
364 catch(ServiceUnreachable& e)
366 //Naming service unreachable don't add catalog
374 timer("Elapsed time before load: ");
375 p=loader.load(myArgs.args[0]);
378 std::cerr << "The imported file is probably not a YACS schema file" << std::endl;
381 // Initialize the ports
382 for(it=myArgs.init_ports.begin(); it != myArgs.init_ports.end(); it++)
384 std::string node, port, value;
385 if(parse_init_port((*it), node, port, value))
387 std::cerr << "Initialization node=" << node
389 << " value=" << value << std::endl;
391 std::string init_state;
392 init_state = p->setInPortValue(node, port, value);
393 if(value.compare(init_state))
395 std::cerr << "Error on initialization:" << init_state << std::endl;
401 std::cerr << "Error on parsing initialization string:" << (*it) << std::endl;
406 //Get the parser logger
407 Logger* logger=p->getLogger("parser");
408 //Print errors logged if any
409 if(!logger->isEmpty())
411 std::cerr << "The imported file has errors" << std::endl;
412 std::cerr << logger->getStr() << std::endl;
414 //Don't execute if there are errors
415 if(logger->hasErrors())
419 std::string report=p->getErrorReport();
420 std::cerr << "The schema is not valid and can not be executed" << std::endl;
421 std::cerr << report << std::endl;
424 Runtime* r=YACS::ENGINE::getRuntime();
425 Dispatcher* disp=Dispatcher::getDispatcher();
431 timer("Elapsed time after load: ");
435 std::string report=p->getErrorReport();
436 std::cerr << "The schema is not valid and can not be executed" << std::endl;
437 std::cerr << report << std::endl;
438 Runtime* r=YACS::ENGINE::getRuntime();
439 Dispatcher* disp=Dispatcher::getDispatcher();
445 timer("Elapsed time after validation: ");
448 LinkInfo info(LinkInfo::ALL_DONT_STOP);
449 p->checkConsistency(info);
450 if(info.areWarningsOrErrors())
452 std::cerr << "The schema is not consistent and can not be executed" << std::endl;
453 std::cerr << info.getGlobalRepr() << std::endl;
454 Runtime* r=YACS::ENGINE::getRuntime();
455 Dispatcher* disp=Dispatcher::getDispatcher();
461 timer("Elapsed time after check consistency: ");
464 bool isXmlSchema = (strlen(myArgs.xmlSchema) != 0);
467 YACS::ENGINE::VisitorSaveSalomeSchema vss(p);
468 vss.openFileSchema(myArgs.xmlSchema);
470 vss.closeFileSchema();
473 bool fromScratch = (strlen(myArgs.loadState) == 0);
478 stateParser* rootParser = new stateParser();
479 stateLoader myStateLoader(rootParser, p);
480 myStateLoader.parse(myArgs.loadState);
483 p->resetState(myArgs.reset);
489 if (strlen(myArgs.dumpErrorFile) >0)
490 executor.setStopOnError(true, myArgs.dumpErrorFile);
492 executor.setStopOnError(false, myArgs.dumpErrorFile);
496 std::ofstream f("toto");
501 bool isDump = (myArgs.dump != 0);
505 thread_st *st = new thread_st;
506 st->nbsec = myArgs.dump;
507 st->dumpFile = string("dumpState_") + myArgs.args[0];
508 string rootFile = st->dumpFile.substr(0,st->dumpFile.find("."));
509 st->lockFile = rootFile + ".lock";
510 pthread_create(&th,NULL,&dumpState,(void*)st);
513 cerr << "+++++++++++++++++++ start calculation +++++++++++++++++++" << endl;
514 executor.RunW(p,myArgs.display, fromScratch);
515 cerr << "+++++++++++++++++++ end calculation +++++++++++++++++++" << endl;
516 cerr << "Proc state : " << Node::getStateName(p->getEffectiveState()) << endl;
517 timer("Elapsed time after execution: ");
519 // Return 0 if SCHEMA OK
520 // Return 1 for YACS ERROR (EXCEPTION NOT CATCHED)
521 // Return 2 for YACS SCHEMA ERROR/FAILED
522 int return_value = 0;
524 if(p->getEffectiveState() != YACS::DONE)
526 std::string report=p->getErrorReport();
527 std::cerr << "Execution has ended in error" << std::endl;
528 std::cerr << report << std::endl;
534 std::ofstream g("titi");
541 pthread_join(th,NULL);
544 bool isFinalDump = (strlen(myArgs.finalDump) != 0);
547 YACS::ENGINE::VisitorSaveState vst(p);
548 vst.openFileDump(myArgs.finalDump);
552 if(myArgs.shutdown < 999)
554 p->shutdown(myArgs.shutdown);
557 Runtime* r=YACS::ENGINE::getRuntime();
558 Dispatcher* disp=Dispatcher::getDispatcher();
564 catch (YACS::Exception& e)
566 cerr << "Caught a YACS exception" << endl;
567 cerr << e.what() << endl;
568 Runtime* r=YACS::ENGINE::getRuntime();
569 Dispatcher* disp=Dispatcher::getDispatcher();
575 catch (const std::ios_base::failure&)
577 cerr << "Caught an io failure exception" << endl;
580 catch(CORBA::SystemException& ex)
582 cerr << "Caught a CORBA::SystemException.:" << __FILE__ << ":" << __LINE__ << ":" ;
585 CORBA::TypeCode_var tc = tmp.type();
586 const char *p = tc->name();
594 catch(omniORB::fatalException& fe)
596 cerr << "Caught omniORB::fatalException:" << endl;
597 cerr << " file: " << fe.file() << endl;
598 cerr << " line: " << fe.line() << endl;
599 cerr << " mesg: " << fe.errmsg() << endl;
604 cerr << "Caught unknown exception." << endl;