Salome HOME
BUG: Installation procedure: libyacsloader_echo.so library has to be installed in...
[modules/yacs.git] / src / yacsloader / driver.cxx
index 558324d7d035828e4cf80263fd760f95eab05cc9..146fdda9aeffd2fd01c75a70809bdd01d766d085 100644 (file)
@@ -1,3 +1,21 @@
+// Copyright (C) 2006-2013  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "yacsconfig.h"
 #include "RuntimeSALOME.hxx"
 #ifdef SALOME_KERNEL
 #include "SALOME_NamingService.hxx"
 #include "SALOME_ModuleCatalog.hh"
+#include "Basics_Utils.hxx"
 #endif
 
 #include <iostream>
 #include <fstream>
+#include <signal.h>
+
+#ifdef WIN32
+#else
 #include <argp.h>
+#endif
 
 using YACS::YACSLoader;
 using namespace YACS::ENGINE;
@@ -33,17 +57,24 @@ const char *argp_program_bug_address ="<nepal@nepal.edf.fr>";
 static char doc[] ="driver -- a SALOME YACS graph executor";
 static char args_doc[] = "graph.xml";
 
+#ifdef WIN32
+#else
 static struct argp_option options[] =
   {
-    {"display",         'd', "level", 0,                   "Display dot files: 0=never to 3=very often"},
+    {"display",         'd', "level", 0,                   "Display dot files: 0=never to 3=very often (default 0)"},
     {"verbose",         'v', 0,       0,                   "Produce verbose output" },
     {"stop-on-error",   's', 0,       0,                   "Stop on first error" },
     {"dump-on-error",   'e', "file",  OPTION_ARG_OPTIONAL, "Stop on first error and dump state"},
     {"dump-final",      'f', "file",  OPTION_ARG_OPTIONAL, "dump final state"},
+    {"dump",            'g', "nbsec", OPTION_ARG_OPTIONAL, "dump state"},
     {"load-state",      'l', "file",  0,                   "Load State from a previous partial execution"},
     {"save-xml-schema", 'x', "file",  OPTION_ARG_OPTIONAL, "dump xml schema"},
+    {"shutdown",        't', "level", 0,                   "Shutdown the schema: 0=no shutdown to 3=full shutdown (default 1)"},
+    {"reset",           'r', "level", 0,                   "Reset the schema before execution: 0=nothing, 1=reset error nodes to ready state (default 0)"},
+    {"kill-port",       'k', "port",  0,                   "Kill Salome application running on the specified port if the driver process is killed (with SIGINT or SIGTERM)"},
     { 0 }
   };
+#endif
 
 struct arguments
 {
@@ -53,13 +84,29 @@ struct arguments
   int stop;
   char *dumpErrorFile;
   char *finalDump;
+  int dump;
   char *xmlSchema;
   char *loadState;
+  int shutdown;
+  int reset;
+  int killPort;
 };
 
+typedef struct {
+  int nbsec;
+  string dumpFile;
+  string lockFile;
+} thread_st;
+
+#ifdef WIN32
+static int
+#else
 static error_t
+#endif
 parse_opt (int key, char *arg, struct argp_state *state)
 {
+#ifdef WIN32
+#else
   // Get the input argument from argp_parse, which we
   // know is a pointer to our arguments structure. 
   struct arguments *myArgs = (arguments*)state->input;
@@ -69,6 +116,12 @@ parse_opt (int key, char *arg, struct argp_state *state)
     case 'd':
       myArgs->display = atoi(arg);
       break;
+    case 't':
+      myArgs->shutdown = atoi(arg);
+      break;
+    case 'r':
+      myArgs->reset = atoi(arg);
+      break;
     case 'v':
       myArgs->verbose = 1;
       break;
@@ -80,13 +133,19 @@ parse_opt (int key, char *arg, struct argp_state *state)
       if (arg)
         myArgs->dumpErrorFile = arg;
       else
-        myArgs->dumpErrorFile = "dumpErrorState.xml";
+        myArgs->dumpErrorFile = (char *)"dumpErrorState.xml";
       break;
     case 'f':
       if (arg)
         myArgs->finalDump = arg;
       else
-        myArgs->finalDump = "finalDumpState.xml";
+        myArgs->finalDump = (char *)"finalDumpState.xml";
+      break;      
+    case 'g':
+      if (arg)
+        myArgs->dump = atoi(arg);
+      else
+        myArgs->dump = 60;
       break;      
     case 'l':
       myArgs->loadState = arg;
@@ -95,8 +154,11 @@ parse_opt (int key, char *arg, struct argp_state *state)
       if (arg)
         myArgs->xmlSchema = arg;
       else
-        myArgs->xmlSchema = "saveSchema.xml";
+        myArgs->xmlSchema = (char *)"saveSchema.xml";
       break;      
+    case 'k':
+      myArgs->killPort = atoi(arg);
+      break;
 
     case ARGP_KEY_ARG:
       if (state->arg_num >=1) // Too many arguments.
@@ -112,37 +174,144 @@ parse_opt (int key, char *arg, struct argp_state *state)
     default:
       return ARGP_ERR_UNKNOWN;
     }
+#endif
   return 0;
 }
 
 // Our argp parser.
+#ifdef WIN32
+#else
 static struct argp argp = { options, parse_opt, args_doc, doc };
+#endif
+
+void timer(std::string msg)
+{
+#ifdef WIN32
+#else
+  struct timeval tv;
+  gettimeofday(&tv,NULL);
+  long t=tv.tv_sec*1000+tv.tv_usec/1000;
+  static long t0=t;
+  gettimeofday(&tv,NULL);
+  std::cerr << msg << tv.tv_sec*1000+tv.tv_usec/1000-t0 << " ms" << std::endl;
+#endif
+}
+
+Proc* p=0;
+static struct arguments myArgs;
+
+void Handler(int theSigId)
+{
+  if(p)
+    {
+      p->cleanNodes();
+      //if requested save state
+      bool isFinalDump = (strlen(myArgs.finalDump) != 0);
+      if (isFinalDump)
+        {
+          YACS::ENGINE::VisitorSaveState vst(p);
+          vst.openFileDump(myArgs.finalDump);
+          p->accept(&vst);
+          vst.closeFileDump();
+        }
+      //if requested shutdown schema
+      if(myArgs.shutdown < 999)
+        {
+          p->shutdown(myArgs.shutdown);
+        }
+    }
+  if (myArgs.killPort)
+    {
+      ostringstream command;
+      command << "killSalomeWithPort.py " << myArgs.killPort;
+      int status = system(command.str().c_str());
+      if (status == 0)
+        cerr << "Salome application on port " << myArgs.killPort << " is killed" << endl;
+      else
+        cerr << "Error: Can't kill Salome application on port " << myArgs.killPort << endl;
+    }
+  _exit(1);
+}
+
+void * dumpState(void *arg)
+{
+  thread_st *st = (thread_st*)arg;
+  YACS::StatesForNode state = p->getEffectiveState();
+  while((state != YACS::DONE) && (state != YACS::LOADFAILED) && (state != YACS::EXECFAILED) && (state != YACS::INTERNALERR) && (state != YACS::DISABLED) && (state != YACS::FAILED) && (state != YACS::ERROR)){
+#ifdef WIN32
+    Sleep(st->nbsec);
+#else 
+    sleep(st->nbsec);
+#endif
+    string cmd = "touch " + st->lockFile;
+    system(cmd.c_str());
+    YACS::ENGINE::VisitorSaveState vst(p);
+    vst.openFileDump(st->dumpFile);
+    p->accept(&vst);
+    vst.closeFileDump();
+    cmd = "rm -f " + st->lockFile;
+    system(cmd.c_str());
+    state = p->getEffectiveState();
+  }
+  delete st;
+  return NULL;
+}
+
+#ifndef WIN32
+typedef void (*sighandler_t)(int);
+sighandler_t setsig(int sig, sighandler_t handler)
+{
+  struct sigaction context, ocontext;
+  context.sa_handler = handler;
+  sigemptyset(&context.sa_mask);
+  context.sa_flags = 0;
+  if (sigaction(sig, &context, &ocontext) == -1)
+    return SIG_ERR;
+  return ocontext.sa_handler;
+}
+#endif
 
 
-main (int argc, char* argv[])
+int main (int argc, char* argv[])
 {
-  struct arguments myArgs;
      
   // Default values.
   myArgs.display = 0;
   myArgs.verbose = 0;
   myArgs.stop = 0;
-  myArgs.dumpErrorFile= "";
-  myArgs.finalDump = "";
-  myArgs.loadState = "";
-  myArgs.xmlSchema = "";
+  myArgs.dumpErrorFile= (char *)"";
+  myArgs.finalDump = (char *)"";
+  myArgs.dump = 0;
+  myArgs.loadState = (char *)"";
+  myArgs.xmlSchema = (char *)"";
+  myArgs.shutdown = 1;
+  myArgs.reset = 0;
+  myArgs.killPort = 0;
 
   // Parse our arguments; every option seen by parse_opt will be reflected in arguments.
+#ifdef WIN32
+#else
   argp_parse (&argp, argc, argv, 0, 0, &myArgs);
-    cerr << "graph = " << myArgs.args[0] 
-         << " options: display=" << myArgs.display 
-         << " verbose="<<myArgs.verbose
-         << " stop-on-error=" << myArgs.stop;
+  std::cerr << "graph = " << myArgs.args[0];
+  std::cerr << " options: display=" << myArgs.display;
+  std::cerr << " verbose="<<myArgs.verbose;
+  std::cerr << " stop-on-error=" << myArgs.stop;
+  std::cerr << " shutdown=" << myArgs.shutdown;
+  std::cerr << " reset=" << myArgs.reset;
+  if (myArgs.killPort)
+    std::cerr << " kill-port=" << myArgs.killPort;
   if (myArgs.stop)
-    cerr << " dumpErrorFile=" << myArgs.dumpErrorFile << endl;
+    std::cerr << " dumpErrorFile=" << myArgs.dumpErrorFile << std::endl;
   else
-    cerr << endl;
+    std::cerr << std::endl;
+#endif
+
+#ifndef WIN32
+  setsig(SIGINT,&Handler);
+  setsig(SIGTERM,&Handler);
+#endif
 
+  timer("Starting ");
   RuntimeSALOME::setRuntime();
 
   // Try to load the session catalog if it exists
@@ -173,7 +342,8 @@ main (int argc, char* argv[])
 
   try
     {
-      Proc* p=loader.load(myArgs.args[0]);
+      timer("Elapsed time before load: ");
+      p=loader.load(myArgs.args[0]);
       if(p==0)
         {
           std::cerr << "The imported file is probably not a YACS schema file" << std::endl;
@@ -190,6 +360,12 @@ main (int argc, char* argv[])
       //Don't execute if there are errors
       if(logger->hasErrors())
         {
+          if(!p->isValid())
+            {
+              std::string report=p->getErrorReport();
+              std::cerr << "The schema is not valid and can not be executed" << std::endl;
+              std::cerr << report << std::endl;
+            }
           delete p;
           Runtime* r=YACS::ENGINE::getRuntime();
           Dispatcher* disp=Dispatcher::getDispatcher();
@@ -198,6 +374,7 @@ main (int argc, char* argv[])
           delete disp;
           return 1;
         }
+      timer("Elapsed time after load: ");
 
       if(!p->isValid())
         {
@@ -211,6 +388,7 @@ main (int argc, char* argv[])
           delete disp;
           return 1;
         }
+      timer("Elapsed time after validation: ");
 
       // Check consistency
       LinkInfo info(LinkInfo::ALL_DONT_STOP);
@@ -226,6 +404,7 @@ main (int argc, char* argv[])
           delete disp;
           return 1;
         }
+      timer("Elapsed time after check consistency: ");
 
       //execution
       bool isXmlSchema = (strlen(myArgs.xmlSchema) != 0);
@@ -245,6 +424,11 @@ main (int argc, char* argv[])
           stateParser* rootParser = new stateParser();
           stateLoader myStateLoader(rootParser, p);
           myStateLoader.parse(myArgs.loadState);
+          if(myArgs.reset>0)
+            {
+              p->resetState(myArgs.reset);
+              p->exUpdateState();
+            }
         }
 
       if (myArgs.stop)
@@ -253,25 +437,55 @@ main (int argc, char* argv[])
         else
           executor.setStopOnError(false, myArgs.dumpErrorFile);
 
-      std::ofstream f("toto");
-      p->writeDot(f);
-      f.close();
+      if(myArgs.display>0)
+        {
+          std::ofstream f("toto");
+          p->writeDot(f);
+          f.close();
+        }
+
+      bool isDump = (myArgs.dump != 0);
+      pthread_t th;
+      if (isDump)
+        {
+          thread_st *st = new thread_st;
+          st->nbsec = myArgs.dump;
+          st->dumpFile = string("dumpState_") + myArgs.args[0];
+          string rootFile = st->dumpFile.substr(0,st->dumpFile.find("."));
+          st->lockFile = rootFile + ".lock";
+          pthread_create(&th,NULL,&dumpState,(void*)st);
+        }
 
       cerr << "+++++++++++++++++++ start calculation +++++++++++++++++++" << endl;
       executor.RunW(p,myArgs.display, fromScratch);
       cerr << "+++++++++++++++++++  end calculation  +++++++++++++++++++" << endl;
-      cerr << "Proc state : " << p->getEffectiveState() << endl;
+      cerr << "Proc state : " << Node::getStateName(p->getEffectiveState()) << endl;
+      timer("Elapsed time after execution: ");
+
+      // Return 0 if SCHEMA OK
+      // Return 1 for YACS ERROR (EXCEPTION NOT CATCHED)
+      // Return 2 for YACS SCHEMA ERROR/FAILED
+      int return_value = 0;
 
       if(p->getEffectiveState() != YACS::DONE)
         {
           std::string report=p->getErrorReport();
           std::cerr << "Execution has ended in error" << std::endl;
           std::cerr << report << std::endl;
+          return_value = 2;
+        }
+
+      if(myArgs.display>0)
+        {
+          std::ofstream g("titi");
+          p->writeDot(g);
+          g.close();
         }
 
-      std::ofstream g("titi");
-      p->writeDot(g);
-      g.close();
+      if (isDump)
+        {
+          pthread_join(th,NULL);
+        }
 
       bool isFinalDump = (strlen(myArgs.finalDump) != 0);
       if (isFinalDump)
@@ -281,13 +495,17 @@ main (int argc, char* argv[])
           p->accept(&vst);
           vst.closeFileDump();
         }
+      if(myArgs.shutdown < 999)
+        {
+          p->shutdown(myArgs.shutdown);
+        }
       delete p;
       Runtime* r=YACS::ENGINE::getRuntime();
       Dispatcher* disp=Dispatcher::getDispatcher();
       r->fini();
       delete r;
       delete disp;
-      return 0;
+      return return_value;
     }
   catch (YACS::Exception& e)
     {
@@ -307,7 +525,7 @@ main (int argc, char* argv[])
     }
   catch(CORBA::SystemException& ex) 
     {
-      cerr << "Caught a CORBA::SystemException." ;
+      cerr << "Caught a CORBA::SystemException.:" << __FILE__ << ":" << __LINE__ << ":" ;
       CORBA::Any tmp;
       tmp <<= ex;
       CORBA::TypeCode_var tc = tmp.type();