Salome HOME
Build SALOME on win32 platform.
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
index 5b799f44e979709dececd29663e6502af3f68d34..afca47cd499eb7d734d6a9007b1ae5055e43737c 100644 (file)
@@ -1,23 +1,23 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  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 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.
+// 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
+// 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
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "SALOME_Launcher.hxx"
@@ -88,7 +88,7 @@ CORBA::Long
 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
 {
   std::string job_type = job_parameters.job_type.in();
-  
+
   if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome")
   {
     std::string message("SALOME_Launcher::createJob: bad job type: ");
@@ -104,7 +104,10 @@ SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
     new_job = new Launcher::Job_YACSFile();
   else if (job_type == "python_salome")
     new_job = new Launcher::Job_PythonSALOME();
+
+  // Name
+  new_job->setJobName(job_parameters.job_name.in());
+
   // Directories
   std::string work_directory = job_parameters.work_directory.in();
   std::string local_directory = job_parameters.local_directory.in();
@@ -132,7 +135,7 @@ SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
     new_job->add_in_file(job_parameters.in_files[i].in());
   for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
     new_job->add_out_file(job_parameters.out_files[i].in());
-  
+
   // Expected During Time
   try
   {
@@ -167,9 +170,26 @@ SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
   }
 
+  // Adding specific parameters to the job
+  for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
+    new_job->addSpecificParameter(job_parameters.specific_parameters[i].name.in(),
+                                  job_parameters.specific_parameters[i].value.in());
+  try
+  {
+    new_job->checkSpecificParameters();
+  }
+  catch(const LauncherException &ex)
+  {
+    INFOS(ex.msg.c_str());
+    THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
+  }
+
   try
   {
     _l.createJob(new_job);
+    std::ostringstream job_id;
+    job_id << new_job->getNumber();
+    notifyObservers("NEW_JOB", job_id.str());
   }
   catch(const LauncherException &ex)
   {
@@ -223,12 +243,48 @@ SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
   }
 }
 
+CORBA::Boolean
+SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
+{
+  CORBA::Boolean rtn = false;
+  try
+  {
+    rtn = _l.getJobDumpState(job_id, directory);
+  }
+  catch(const LauncherException &ex)
+  {
+    INFOS(ex.msg.c_str());
+    THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
+  }
+  return rtn;
+}
+
 void 
 SALOME_Launcher::removeJob(CORBA::Long job_id)
 {
   try
   {
     _l.removeJob(job_id);
+    std::ostringstream job_id_str;
+    job_id_str << job_id;
+    notifyObservers("REMOVE_JOB", job_id_str.str());
+  }
+  catch(const LauncherException &ex)
+  {
+    INFOS(ex.msg.c_str());
+    THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
+  }
+}
+
+void 
+SALOME_Launcher::stopJob(CORBA::Long job_id)
+{
+  try
+  {
+    _l.stopJob(job_id);
+    std::ostringstream job_id_str;
+    job_id_str << job_id;
+    notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
   }
   catch(const LauncherException &ex)
   {
@@ -279,7 +335,7 @@ SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
       throw SALOME_Exception("No resources have been found with your parameters");
 
     const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
-       std::string resource_name(p->name);
+        std::string resource_name(p->name);
     INFOS("Choose resource for test: " <<  resource_name);
     
     BatchTest t(*p);
@@ -327,16 +383,6 @@ CORBA::Long SALOME_Launcher::getPID()
 #endif
 }
 
-//=============================================================================
-/*! CORBA Method:
- *  Add a new observer to the launcher
- */
-//=============================================================================
-void
-SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
-{
-}
-
 //=============================================================================
 /*! CORBA Method:
  *  Returns current launcher jobs list
@@ -426,6 +472,22 @@ SALOME_Launcher::getJobParameters(CORBA::Long job_id)
   job_parameters->resource_required.cpu_clock        = resource_params.cpu_clock;
   job_parameters->resource_required.mem_mb           = resource_params.mem_mb;
 
+  std::map<std::string, std::string> specific_parameters = job->getSpecificParameters();
+  if (!specific_parameters.empty())
+  {
+    job_parameters->specific_parameters.length(specific_parameters.size());
+    std::map<std::string, std::string>::const_iterator it_specific;
+    CORBA::ULong i = 0;
+    for (it_specific = specific_parameters.begin() ; it_specific != specific_parameters.end(); it_specific++)
+    {
+      Engines::Parameter_var new_param = new Engines::Parameter;
+      new_param->name  = CORBA::string_dup((it_specific->first).c_str());
+      new_param->value = CORBA::string_dup((it_specific->second).c_str());
+      job_parameters->specific_parameters[i] = new_param;
+      i++;
+    }
+  }
+
   return job_parameters._retn();
 }
 
@@ -632,6 +694,61 @@ SALOME_Launcher::loadJobs(const char* jobs_file)
         xmlFree(maximum_duration);
         xmlFree(queue);
 
+        xmlNodePtr specific_node = xmlNextElementSibling(queue_node);
+        if (specific_node == NULL)
+        {
+          INFOS("A bad job is found, specific_parameters part is not found");
+          delete new_job;
+          break;
+        }
+        xmlNodePtr parameter_node = xmlFirstElementChild(specific_node);
+        while (parameter_node != NULL)
+        {
+          if (!xmlStrcmp(parameter_node->name, xmlCharStrdup("specific_parameter")))
+          {
+            xmlNodePtr name_node = xmlFirstElementChild(parameter_node);
+            xmlNodePtr value_node = xmlNextElementSibling(name_node);
+            if (name_node == NULL ||
+                value_node == NULL)
+            {
+              INFOS("A bad job is found, specific_parameter parts are not found");
+              delete new_job;
+              break;
+            }
+            if (xmlStrcmp(name_node->name, xmlCharStrdup("name")) ||
+                xmlStrcmp(value_node->name, xmlCharStrdup("value")))
+            {
+              INFOS("A bad job is found, specific_parameter bad parts are found");
+              delete new_job;
+              break;
+            }
+
+            xmlChar* name  = xmlNodeGetContent(name_node);
+            xmlChar* value = xmlNodeGetContent(value_node);
+            try
+            {
+              new_job->addSpecificParameter(std::string((const char*)name), std::string((const char*)value));
+              xmlFree(name);
+              xmlFree(value);
+            }
+            catch(const LauncherException &ex)
+            {
+              INFOS("Exception receice for a specific parameter, cannot add the job" << ex.msg.c_str());
+              delete new_job;
+              xmlFree(name);
+              xmlFree(value);
+              break;
+            }
+          }
+          else
+          {
+            INFOS("A bad job is found, specific_parameters part is bad, a node that is not a specific parameter is found");
+            delete new_job;
+            break;
+          }
+          parameter_node = xmlNextElementSibling(parameter_node);
+        }
+
         xmlNodePtr res_name_node             = xmlFirstElementChild(res_node);
         xmlNodePtr res_hostname_node         = xmlNextElementSibling(res_name_node);
         xmlNodePtr res_os_node               = xmlNextElementSibling(res_hostname_node);
@@ -752,14 +869,16 @@ SALOME_Launcher::loadJobs(const char* jobs_file)
         xmlFree(job_state_xml);
         xmlFree(resource_choosed_name_xml);
         xmlFree(job_reference_xml);
-        
-        // TODO: EVENT for observer !
+
         if (job_state == "CREATED")
         {
           // In this case, we ignore run_part informations
           try
           {
             _l.createJob(new_job);
+            std::ostringstream job_id;
+            job_id << new_job->getNumber();
+            notifyObservers("NEW_JOB", job_id.str());
           }
           catch(const LauncherException &ex)
           {
@@ -773,25 +892,21 @@ SALOME_Launcher::loadJobs(const char* jobs_file)
         {
           try
           {
-            // Step 1: Add the resource to the launcher C++ map
-            _l.checkFactoryForResource(resource_choosed_name);
-
-            // Step 2: We add run_part informations
             new_job->setState(job_state);
-
-            // Step 3: We add the job to the launcher
-            ParserResourcesType resource_definition;
-            resource_definition.Name = resource_choosed_name;
-            new_job->setResourceDefinition(resource_definition);
             _l.addJobDirectlyToMap(new_job, job_reference);
 
             // Step 4: We check that the BatchManager could resume
             // the job
+#ifdef WITH_LIBBATCH
             if (new_job->getBatchManagerJobId().getReference() != job_reference)
             {
               INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
               new_job->setState("ERROR");
             }
+#endif
+            std::ostringstream job_id;
+            job_id << new_job->getNumber();
+            notifyObservers("NEW_JOB", job_id.str());
           }
           catch(const LauncherException &ex)
           {
@@ -805,17 +920,12 @@ SALOME_Launcher::loadJobs(const char* jobs_file)
         {
           try
           {
-            // Step 1: Add the resource to the launcher C++ map
-            _l.checkFactoryForResource(resource_choosed_name);
-
             // Step 2: We add run_part informations
             new_job->setState(job_state);
-
-            // Step 3: We add the job to the launcher
-            ParserResourcesType resource_definition;
-            resource_definition.Name = resource_choosed_name;
-            new_job->setResourceDefinition(resource_definition);
             _l.addJobDirectlyToMap(new_job, job_reference);
+            std::ostringstream job_id;
+            job_id << new_job->getNumber();
+            notifyObservers("NEW_JOB", job_id.str());
           }
           catch(const LauncherException &ex)
           {
@@ -845,6 +955,7 @@ SALOME_Launcher::loadJobs(const char* jobs_file)
   // Clean
   xmlFreeDoc(doc);
   fclose(xml_file);
+  notifyObservers("LOAD_JOBS", jobs_file);
 }
 
 //=============================================================================
@@ -897,5 +1008,98 @@ SALOME_Launcher::saveJobs(const char* jobs_file)
   xmlFreeDoc(doc);
   fclose(xml_file);
   MESSAGE("SALOME_Launcher::saveJobs : WRITING DONE!");
-  //TODO: Generation evenement pour les observeurs
+  notifyObservers("SAVE_JOBS", jobs_file);
+}
+
+//=============================================================================
+/*! CORBA Method:
+ *  Add a new observer to the launcher
+ */
+//=============================================================================
+void
+SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
+{
+  bool new_observer = true;
+  std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
+  while(iter != _observers.end())
+  {
+    if (std::string(_orb->object_to_string(*iter)) ==
+        std::string(_orb->object_to_string(observer)))
+    {
+      new_observer = false;
+      break;
+    }
+    iter++;
+  }
+  if (new_observer)
+    _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
+
+  // We notify the new observer with all jobs that are currently in the Launcher
+  std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
+  std::map<int, Launcher::Job *>::const_iterator it_job;
+  for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
+  {
+    int number = it_job->first;
+    std::ostringstream job_id;
+    job_id << number;
+    try
+    {
+      observer->notify("NEW_JOB", job_id.str().c_str());
+    }
+    catch (...) 
+    {
+       MESSAGE("Notify Observer, exception catch");
+    }
+
+  }
+}
+
+//=============================================================================
+/*! CORBA Method:
+ *  Add a new observer to the launcher
+ */
+//=============================================================================
+void
+SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
+{
+  std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
+  while(iter != _observers.end())
+  {
+    if (std::string(_orb->object_to_string(*iter)) ==
+        std::string(_orb->object_to_string(observer)))
+    {
+      // Observer found
+      iter =_observers.erase(iter++);
+    }
+    else
+    {
+      iter++;
+    }
+  }
+}
+
+//=============================================================================
+/*! Internal Method:
+ *  Notify observers on a new event
+ */
+//=============================================================================
+void
+SALOME_Launcher::notifyObservers(const std::string & event_name,
+                                 const std::string & event_data)
+{
+  std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
+  while(iter != _observers.end())
+  {
+    try
+    {
+      (*iter)->notify(CORBA::string_dup(event_name.c_str()),
+                      CORBA::string_dup(event_data.c_str()));
+    }
+    catch (...) 
+    {
+       MESSAGE("Notify Observer, exception catch");
+    }
+    iter++;
+  }
+
 }