Salome HOME
Fix Libbatch usage with CMake compilation
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SALOME_Launcher.hxx"
24 #include "BatchTest.hxx"
25 #include "OpUtil.hxx"
26 #include "SALOME_ContainerManager.hxx"
27 #include "Utils_CorbaException.hxx"
28
29
30 #include "Launcher_Job_Command.hxx"
31 #include "Launcher_Job_YACSFile.hxx"
32 #include "Launcher_Job_PythonSALOME.hxx"
33
34 #ifdef WIN32
35 # include <process.h>
36 #else
37 # include <unistd.h>
38 #endif
39 #include <sys/types.h>
40 #include <vector>
41
42 #include <libxml/parser.h>
43 #include <stdio.h>
44 #include <sstream>
45
46 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
47
48 //=============================================================================
49 /*! 
50  *  Constructor
51  *  \param orb
52  */
53 //=============================================================================
54 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
55 {
56   MESSAGE("SALOME_Launcher constructor");
57   _NS = new SALOME_NamingService(orb);
58   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
59   _l.SetResourcesManager(_ResManager->GetImpl());
60   _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
61   _ResManager->_remove_ref();
62   _ContManager->_remove_ref();
63
64   _orb = CORBA::ORB::_duplicate(orb) ;
65   _poa = PortableServer::POA::_duplicate(poa) ;
66   PortableServer::ObjectId_var id = _poa->activate_object(this);
67   CORBA::Object_var obj = _poa->id_to_reference(id);
68   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
69
70   _NS->Register(refContMan,_LauncherNameInNS);
71   MESSAGE("SALOME_Launcher constructor end");
72 }
73
74 //=============================================================================
75 /*! 
76  * destructor
77  */
78 //=============================================================================
79 SALOME_Launcher::~SALOME_Launcher()
80 {
81   MESSAGE("SALOME_Launcher destructor");
82   delete _NS;
83   MESSAGE("SALOME_Launcher destructor end");
84 }
85
86
87 CORBA::Long 
88 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
89 {
90   std::string job_type = job_parameters.job_type.in();
91
92   if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome")
93   {
94     std::string message("SALOME_Launcher::createJob: bad job type: ");
95     message += job_type;
96     THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR);
97   }
98
99   Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
100
101   if (job_type == "command")
102     new_job = new Launcher::Job_Command();
103   else if (job_type == "yacs_file")
104     new_job = new Launcher::Job_YACSFile();
105   else if (job_type == "python_salome")
106     new_job = new Launcher::Job_PythonSALOME();
107
108   // Name
109   new_job->setJobName(job_parameters.job_name.in());
110
111   // Directories
112   std::string work_directory = job_parameters.work_directory.in();
113   std::string local_directory = job_parameters.local_directory.in();
114   std::string result_directory = job_parameters.result_directory.in();
115   new_job->setWorkDirectory(work_directory);
116   new_job->setLocalDirectory(local_directory);
117   new_job->setResultDirectory(result_directory);
118
119   // Parameters for COORM
120   std::string launcher_file = job_parameters.launcher_file.in();
121   std::string launcher_args = job_parameters.launcher_args.in();
122   new_job->setLauncherFile(launcher_file);
123   new_job->setLauncherArgs(launcher_args);
124
125   // Job File
126   std::string job_file = job_parameters.job_file.in();
127   try
128   {
129     new_job->setJobFile(job_file);
130   }
131   catch(const LauncherException &ex)
132   {
133     INFOS(ex.msg.c_str());
134     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
135   }
136
137   // Files
138   std::string env_file = job_parameters.env_file.in();
139   new_job->setEnvFile(env_file);
140   for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
141     new_job->add_in_file(job_parameters.in_files[i].in());
142   for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
143     new_job->add_out_file(job_parameters.out_files[i].in());
144
145   // Expected During Time
146   try
147   {
148     std::string maximum_duration = job_parameters.maximum_duration.in();
149     new_job->setMaximumDuration(maximum_duration);
150   }
151   catch(const LauncherException &ex){
152     INFOS(ex.msg.c_str());
153     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
154   }
155
156   // Queue
157   std::string queue = job_parameters.queue.in();
158   new_job->setQueue(queue);
159
160   // Resources requirements
161   try
162   {
163     resourceParams p;
164     p.name = job_parameters.resource_required.name;
165     p.hostname = job_parameters.resource_required.hostname;
166     p.OS = job_parameters.resource_required.OS;
167     p.nb_proc = job_parameters.resource_required.nb_proc;
168     p.nb_node = job_parameters.resource_required.nb_node;
169     p.nb_proc_per_node = job_parameters.resource_required.nb_proc_per_node;
170     p.cpu_clock = job_parameters.resource_required.cpu_clock;
171     p.mem_mb = job_parameters.resource_required.mem_mb;
172     new_job->setResourceRequiredParams(p);
173   }
174   catch(const LauncherException &ex){
175     INFOS(ex.msg.c_str());
176     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
177   }
178
179   // Adding specific parameters to the job
180   for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
181     new_job->addSpecificParameter(job_parameters.specific_parameters[i].name.in(),
182                                   job_parameters.specific_parameters[i].value.in());
183   try
184   {
185     new_job->checkSpecificParameters();
186   }
187   catch(const LauncherException &ex)
188   {
189     INFOS(ex.msg.c_str());
190     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
191   }
192
193   try
194   {
195     _l.createJob(new_job);
196     std::ostringstream job_id;
197     job_id << new_job->getNumber();
198     notifyObservers("NEW_JOB", job_id.str());
199   }
200   catch(const LauncherException &ex)
201   {
202     INFOS(ex.msg.c_str());
203     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
204   }
205   return new_job->getNumber();
206 }
207
208 void 
209 SALOME_Launcher::launchJob(CORBA::Long job_id)
210 {
211   try
212   {
213     _l.launchJob(job_id);
214   }
215   catch(const LauncherException &ex)
216   {
217     INFOS(ex.msg.c_str());
218     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
219   }
220 }
221
222 char *
223 SALOME_Launcher::getJobState(CORBA::Long job_id)
224 {
225   std::string result;
226   try
227   {
228     result = _l.getJobState(job_id);
229   }
230   catch(const LauncherException &ex)
231   {
232     INFOS(ex.msg.c_str());
233     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
234   }
235   return CORBA::string_dup(result.c_str());
236 }
237
238 // Get names or ids of hosts assigned to the job
239 char *
240 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
241 {
242   std::string result;
243   try
244   {
245     result = _l.getAssignedHostnames(job_id);
246   }
247   catch(const LauncherException &ex)
248   {
249     INFOS(ex.msg.c_str());
250     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
251   }
252   return CORBA::string_dup(result.c_str());
253 }
254
255 void
256 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
257 {
258   try
259   {
260     _l.getJobResults(job_id, directory);
261   }
262   catch(const LauncherException &ex)
263   {
264     INFOS(ex.msg.c_str());
265     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
266   }
267 }
268
269 CORBA::Boolean
270 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
271 {
272   CORBA::Boolean rtn = false;
273   try
274   {
275     rtn = _l.getJobDumpState(job_id, directory);
276   }
277   catch(const LauncherException &ex)
278   {
279     INFOS(ex.msg.c_str());
280     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
281   }
282   return rtn;
283 }
284
285 void 
286 SALOME_Launcher::removeJob(CORBA::Long job_id)
287 {
288   try
289   {
290     _l.removeJob(job_id);
291     std::ostringstream job_id_str;
292     job_id_str << job_id;
293     notifyObservers("REMOVE_JOB", job_id_str.str());
294   }
295   catch(const LauncherException &ex)
296   {
297     INFOS(ex.msg.c_str());
298     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
299   }
300 }
301
302 void 
303 SALOME_Launcher::stopJob(CORBA::Long job_id)
304 {
305   try
306   {
307     _l.stopJob(job_id);
308     std::ostringstream job_id_str;
309     job_id_str << job_id;
310     notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
311   }
312   catch(const LauncherException &ex)
313   {
314     INFOS(ex.msg.c_str());
315     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
316   }
317 }
318
319 //=============================================================================
320 /*! CORBA Method:
321  *  Create a job in the launcher with a file
322  *  \param xmlExecuteFile     : .xml to parse that contains job description
323  *  \param clusterName        : machine choosed
324  */
325 //=============================================================================
326 CORBA::Long 
327 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
328                                    const char * clusterName)
329 {
330   CORBA::Long jobId;
331   try{
332     jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
333   }
334   catch(const LauncherException &ex){
335     INFOS(ex.msg.c_str());
336     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
337   }
338
339   return jobId;
340 }
341
342 //=============================================================================
343 /*! CORBA Method:
344  *  the test batch configuration 
345  *  \param params             : The batch cluster
346  */
347 //=============================================================================
348 CORBA::Boolean 
349 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
350 {
351   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
352   CORBA::Boolean rtn = false;
353   try
354   {
355     // Consider only resources that can run batch jobs
356     Engines::ResourceParameters new_params(params);
357     new_params.can_launch_batch_jobs = true;
358
359     // find a resource matching the required parameters
360     Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
361     if (aMachineList->length() == 0)
362       throw SALOME_Exception("No resources have been found with your parameters");
363
364     const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
365         std::string resource_name(p->name);
366     INFOS("Choose resource for test: " <<  resource_name);
367     
368     BatchTest t(*p);
369     if (t.test()) 
370     {
371       rtn = true;
372     }
373   }
374   catch(const LauncherException &ex){
375     INFOS(ex.msg.c_str());
376     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
377   }
378   return rtn;
379 }
380
381 //=============================================================================
382 /*! CORBA method:
383  *  shutdown all the containers, then the ContainerManager servant
384  */
385 //=============================================================================
386 void SALOME_Launcher::Shutdown()
387 {
388   MESSAGE("Shutdown");
389   _NS->Destroy_Name(_LauncherNameInNS);
390   _ContManager->Shutdown();
391   _ResManager->Shutdown();
392   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
393   _poa->deactivate_object(oid);
394   if(!CORBA::is_nil(_orb))
395     _orb->shutdown(0);
396 }
397
398 //=============================================================================
399 /*! CORBA Method:
400  *  Returns the PID of the process
401  */
402 //=============================================================================
403 CORBA::Long SALOME_Launcher::getPID()
404 {
405   return 
406 #ifndef WIN32
407     (CORBA::Long)getpid();
408 #else
409     (CORBA::Long)_getpid();
410 #endif
411 }
412
413 //=============================================================================
414 /*! CORBA Method:
415  *  Returns current launcher jobs list
416  */
417 //=============================================================================
418 Engines::JobsList *
419 SALOME_Launcher::getJobsList()
420 {
421   Engines::JobsList_var jobs_list = new Engines::JobsList();
422   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
423   std::map<int, Launcher::Job *>::const_iterator it_job;
424   int list_id = 0;
425   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
426   {
427     int number          = it_job->first;
428     try
429     {
430       // Prepare CORBA job description
431       Engines::JobDescription_var job_descr = new Engines::JobDescription();
432       Engines::JobParameters_var job_parameters = getJobParameters(number);
433       job_descr->job_id = number;
434       job_descr->job_parameters = job_parameters;
435
436       // Add job description to the sequence
437       jobs_list->length(list_id + 1);
438       jobs_list[list_id] = job_descr;
439       list_id++;
440     }
441     catch (...) {}
442   }
443   return jobs_list._retn();
444 }
445
446 //=============================================================================
447 /*! CORBA Method:
448  * Returns the job description
449  */
450 //=============================================================================
451 Engines::JobParameters *
452 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
453 {
454   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
455   std::map<int, Launcher::Job *>::const_iterator it_job = cpp_jobs.find(job_id);
456   if (it_job == cpp_jobs.end())
457   {
458     INFOS("Cannot find the job, is it created ? job number: " << job_id);
459     THROW_SALOME_CORBA_EXCEPTION("Job does not exist", SALOME::INTERNAL_ERROR);
460   }
461
462   Launcher::Job * job = it_job->second;
463   Engines::JobParameters_var job_parameters = new Engines::JobParameters;
464   job_parameters->job_name         = CORBA::string_dup(job->getJobName().c_str());
465   job_parameters->job_type         = CORBA::string_dup(job->getJobType().c_str());
466   job_parameters->job_file         = CORBA::string_dup(job->getJobFile().c_str());
467   job_parameters->env_file         = CORBA::string_dup(job->getEnvFile().c_str());
468   job_parameters->work_directory   = CORBA::string_dup(job->getWorkDirectory().c_str());
469   job_parameters->local_directory  = CORBA::string_dup(job->getLocalDirectory().c_str());
470   job_parameters->result_directory = CORBA::string_dup(job->getResultDirectory().c_str());
471
472   // Parameters for COORM
473   job_parameters->launcher_file = CORBA::string_dup(job->getLauncherFile().c_str());
474   job_parameters->launcher_args = CORBA::string_dup(job->getLauncherArgs().c_str());
475
476   int i = 0;
477   int j = 0;
478   std::list<std::string> in_files  = job->get_in_files();
479   std::list<std::string> out_files = job->get_out_files();
480   job_parameters->in_files.length(in_files.size());
481   for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
482   {
483     job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
484     i++;
485   }
486   job_parameters->out_files.length(out_files.size());
487   for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
488   {
489     job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
490     j++;
491   }
492
493   job_parameters->maximum_duration = CORBA::string_dup(job->getMaximumDuration().c_str());
494   job_parameters->queue            = CORBA::string_dup(job->getQueue().c_str());
495
496   resourceParams resource_params = job->getResourceRequiredParams();
497   job_parameters->resource_required.name             = CORBA::string_dup(resource_params.name.c_str());
498   job_parameters->resource_required.hostname         = CORBA::string_dup(resource_params.hostname.c_str());
499   job_parameters->resource_required.OS               = CORBA::string_dup(resource_params.OS.c_str());
500   job_parameters->resource_required.nb_proc          = resource_params.nb_proc;
501   job_parameters->resource_required.nb_node          = resource_params.nb_node;
502   job_parameters->resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
503   job_parameters->resource_required.cpu_clock        = resource_params.cpu_clock;
504   job_parameters->resource_required.mem_mb           = resource_params.mem_mb;
505
506   std::map<std::string, std::string> specific_parameters = job->getSpecificParameters();
507   if (!specific_parameters.empty())
508   {
509     job_parameters->specific_parameters.length(specific_parameters.size());
510     std::map<std::string, std::string>::const_iterator it_specific;
511     CORBA::ULong i = 0;
512     for (it_specific = specific_parameters.begin() ; it_specific != specific_parameters.end(); it_specific++)
513     {
514       Engines::Parameter_var new_param = new Engines::Parameter;
515       new_param->name  = CORBA::string_dup((it_specific->first).c_str());
516       new_param->value = CORBA::string_dup((it_specific->second).c_str());
517       job_parameters->specific_parameters[i] = new_param;
518       i++;
519     }
520   }
521
522   return job_parameters._retn();
523 }
524
525 //=============================================================================
526 /*! CORBA Method:
527  *  Loads jobs saved in jobs_file
528  */
529 //=============================================================================
530 void
531 SALOME_Launcher::loadJobs(const char* jobs_file)
532 {
533   // Step 1: check jobs_file read access
534   FILE* xml_file = fopen(jobs_file, "r");
535   if (xml_file == NULL)
536   {
537     std::string error = "Error opening jobs_file in SALOME_Launcher::loadJobs: " + std::string(jobs_file);
538     INFOS(error);
539     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
540   }
541
542   // Step 2: read xml file
543   xmlDocPtr doc = xmlReadFile(jobs_file, NULL, 0);
544   if (doc == NULL)
545   {
546     std::string error = "Error in xmlReadFile in SALOME_Launcher::loadJobs, could not parse file: " + std::string(jobs_file);
547     INFOS(error);
548     fclose(xml_file);
549     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
550   }
551
552   // Step 3: Find jobs
553   xmlNodePtr root_node = xmlDocGetRootElement(doc);
554   xmlNodePtr xmlCurrentNode = root_node->xmlChildrenNode;
555   if (!xmlStrcmp(root_node->name, xmlCharStrdup("jobs")))
556   {
557     while(xmlCurrentNode != NULL)
558     {
559       if (!xmlStrcmp(xmlCurrentNode->name, xmlCharStrdup("job")))
560       {
561         INFOS("A job is found");
562         Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
563         xmlNodePtr job_node = xmlCurrentNode;
564
565         // Begin Job
566         if (!xmlHasProp(job_node, xmlCharStrdup("type"))  ||
567             !xmlHasProp(job_node, xmlCharStrdup("name")))
568         {
569           INFOS("A bad job is found, type or name not found");
570           break;
571         }
572         xmlChar* type = xmlGetProp(job_node, xmlCharStrdup("type"));
573         xmlChar* name = xmlGetProp(job_node, xmlCharStrdup("name"));
574         std::string job_type((const char*) type);
575         if (job_type == "command")
576           new_job = new Launcher::Job_Command();
577         else if (job_type == "yacs_file")
578           new_job = new Launcher::Job_YACSFile();
579         else if (job_type == "python_salome")
580           new_job = new Launcher::Job_PythonSALOME();
581         new_job->setJobName(std::string((const char *)name));
582         xmlFree(type);
583         xmlFree(name);
584
585         xmlNodePtr user_node = xmlFirstElementChild(job_node);
586         xmlNodePtr run_node = xmlNextElementSibling(user_node);
587         if (user_node == NULL || run_node == NULL)
588         {
589           INFOS("A bad job is found, user_part or run_part not found");
590           delete new_job;
591           break;
592         }
593         if (xmlStrcmp(user_node->name, xmlCharStrdup("user_part")) ||
594             xmlStrcmp(run_node->name, xmlCharStrdup("run_part")))
595         {
596           INFOS("A bad job is found, cannot get a correct user_part or run_part node");
597           delete new_job;
598           break;
599         }
600
601         // Add user part
602
603         // Get job_file env_file work_directory local_directory result_directory
604         xmlNodePtr job_file_node         = xmlFirstElementChild(user_node);
605         xmlNodePtr env_file_node         = xmlNextElementSibling(job_file_node);
606         xmlNodePtr work_directory_node   = xmlNextElementSibling(env_file_node);
607         xmlNodePtr local_directory_node  = xmlNextElementSibling(work_directory_node);
608         xmlNodePtr result_directory_node = xmlNextElementSibling(local_directory_node);
609
610                 // Parameters for COORM
611         xmlNodePtr launcher_file_node = xmlNextElementSibling(result_directory_node);
612
613         if (job_file_node         == NULL ||
614             env_file_node         == NULL ||
615             work_directory_node   == NULL ||
616             local_directory_node  == NULL ||
617             result_directory_node == NULL ||
618                         // For COORM
619             launcher_file_node    == NULL
620            )
621         {
622           INFOS("A bad job is found, some user_part are not found");
623           delete new_job;
624           break;
625         }
626         if (xmlStrcmp(job_file_node->name,         xmlCharStrdup("job_file"))         ||
627             xmlStrcmp(env_file_node->name,         xmlCharStrdup("env_file"))         ||
628             xmlStrcmp(work_directory_node->name,   xmlCharStrdup("work_directory"))   ||
629             xmlStrcmp(local_directory_node->name,  xmlCharStrdup("local_directory"))  ||
630             xmlStrcmp(result_directory_node->name, xmlCharStrdup("result_directory")) ||
631                         // For COORM
632             xmlStrcmp(launcher_file_node->name, xmlCharStrdup("launcher_file"))
633            )
634         {
635           INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name");
636           delete new_job;
637           break;
638         }
639         xmlChar* job_file         = xmlNodeGetContent(job_file_node);
640         try
641         {
642           new_job->setJobFile(std::string((const char *)job_file));
643         }
644         catch(const LauncherException &ex)
645         {
646           INFOS("Exception receice for job_file, cannot add the job" << ex.msg.c_str());
647           delete new_job;
648           xmlFree(job_file);
649           break;
650         }
651         xmlChar* env_file         = xmlNodeGetContent(env_file_node);
652         xmlChar* work_directory   = xmlNodeGetContent(work_directory_node);
653         xmlChar* local_directory  = xmlNodeGetContent(local_directory_node);
654         xmlChar* result_directory = xmlNodeGetContent(result_directory_node);
655
656                 // Parameters for COORM
657         xmlChar* launcher_file = xmlNodeGetContent(launcher_file_node);
658
659         new_job->setEnvFile(std::string((const char *)env_file));
660         new_job->setWorkDirectory(std::string((const char *)work_directory));
661         new_job->setLocalDirectory(std::string((const char *)local_directory));
662         new_job->setResultDirectory(std::string((const char *)result_directory));
663
664                 // Parameters for COORM
665         new_job->setLauncherFile(std::string((const char *)launcher_file));
666
667         xmlFree(job_file);
668         xmlFree(env_file);
669         xmlFree(work_directory);
670         xmlFree(local_directory);
671         xmlFree(result_directory);
672
673                 // Parameters for COORM
674                 xmlFree(launcher_file);
675
676         // Get in and out files
677         xmlNodePtr files_node = xmlNextElementSibling(launcher_file_node);
678         if (files_node == NULL)
679         {
680           INFOS("A bad job is found, user_part files is not found");
681           delete new_job;
682           break;
683         }
684         if (xmlStrcmp(files_node->name, xmlCharStrdup("files")))
685         {
686           INFOS("A bad job is found, files node are not in the rigth place or does not have a correct name or does not exist");
687           delete new_job;
688           break;
689         }
690         xmlNodePtr file_node = xmlFirstElementChild(files_node);
691         while (file_node != NULL)
692         {
693           if (!xmlStrcmp(file_node->name, xmlCharStrdup("in_file")))
694           {
695             xmlChar* in_file = xmlNodeGetContent(file_node);
696             new_job->add_in_file(std::string((const char *)in_file));
697             xmlFree(in_file);
698           }
699           else if (!xmlStrcmp(file_node->name, xmlCharStrdup("out_file")))
700           {
701             xmlChar* out_file = xmlNodeGetContent(file_node);
702             new_job->add_out_file(std::string((const char *)out_file));
703             xmlFree(out_file);
704           }
705           file_node = xmlNextElementSibling(file_node);
706         }
707
708         // Get resource part
709         xmlNodePtr res_node = xmlNextElementSibling(files_node);
710         xmlNodePtr maximum_duration_node = xmlNextElementSibling(res_node);
711         xmlNodePtr queue_node = xmlNextElementSibling(maximum_duration_node);
712         xmlNodePtr launcher_args_node = xmlNextElementSibling(queue_node);
713         if (res_node              == NULL ||
714             maximum_duration_node == NULL ||
715             queue_node            == NULL ||
716                         // For COORM
717             launcher_args_node    == NULL
718            )
719         {
720           INFOS("A bad job is found, some user_part are not found");
721           delete new_job;
722           break;
723         }
724         if (xmlStrcmp(res_node->name,              xmlCharStrdup("resource_params"))  ||
725             xmlStrcmp(maximum_duration_node->name, xmlCharStrdup("maximum_duration")) ||
726             xmlStrcmp(queue_node->name,            xmlCharStrdup("queue")) ||
727                         // For COORM
728             xmlStrcmp(launcher_args_node->name,    xmlCharStrdup("launcher_args"))
729            )
730         {
731           INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name");
732           delete new_job;
733           break;
734         }
735         xmlChar* maximum_duration = xmlNodeGetContent(maximum_duration_node);
736         try
737         {
738           new_job->setMaximumDuration(std::string((const char *)maximum_duration));
739         }
740         catch(const LauncherException &ex)
741         {
742           INFOS("Exception receice for maximum_duration, cannot add the job" << ex.msg.c_str());
743           delete new_job;
744           xmlFree(maximum_duration);
745           break;
746         }
747         xmlChar* queue            = xmlNodeGetContent(queue_node);
748         new_job->setQueue(std::string((const char *)queue));
749         xmlFree(maximum_duration);
750         xmlFree(queue);
751
752                 // For COORM
753         xmlChar* launcher_args           = xmlNodeGetContent(launcher_args_node);
754         new_job->setLauncherArgs(std::string((const char *)launcher_args));
755         xmlFree(launcher_args);
756
757         xmlNodePtr specific_node = xmlNextElementSibling(launcher_args_node);
758         if (specific_node == NULL)
759         {
760           INFOS("A bad job is found, specific_parameters part is not found");
761           delete new_job;
762           break;
763         }
764         xmlNodePtr parameter_node = xmlFirstElementChild(specific_node);
765         while (parameter_node != NULL)
766         {
767           if (!xmlStrcmp(parameter_node->name, xmlCharStrdup("specific_parameter")))
768           {
769             xmlNodePtr name_node = xmlFirstElementChild(parameter_node);
770             xmlNodePtr value_node = xmlNextElementSibling(name_node);
771             if (name_node == NULL ||
772                 value_node == NULL)
773             {
774               INFOS("A bad job is found, specific_parameter parts are not found");
775               delete new_job;
776               break;
777             }
778             if (xmlStrcmp(name_node->name, xmlCharStrdup("name")) ||
779                 xmlStrcmp(value_node->name, xmlCharStrdup("value")))
780             {
781               INFOS("A bad job is found, specific_parameter bad parts are found");
782               delete new_job;
783               break;
784             }
785
786             xmlChar* name  = xmlNodeGetContent(name_node);
787             xmlChar* value = xmlNodeGetContent(value_node);
788             try
789             {
790               new_job->addSpecificParameter(std::string((const char*)name), std::string((const char*)value));
791               xmlFree(name);
792               xmlFree(value);
793             }
794             catch(const LauncherException &ex)
795             {
796               INFOS("Exception receice for a specific parameter, cannot add the job" << ex.msg.c_str());
797               delete new_job;
798               xmlFree(name);
799               xmlFree(value);
800               break;
801             }
802           }
803           else
804           {
805             INFOS("A bad job is found, specific_parameters part is bad, a node that is not a specific parameter is found");
806             delete new_job;
807             break;
808           }
809           parameter_node = xmlNextElementSibling(parameter_node);
810         }
811
812         xmlNodePtr res_name_node             = xmlFirstElementChild(res_node);
813         xmlNodePtr res_hostname_node         = xmlNextElementSibling(res_name_node);
814         xmlNodePtr res_os_node               = xmlNextElementSibling(res_hostname_node);
815         xmlNodePtr res_nb_proc_node          = xmlNextElementSibling(res_os_node);
816         xmlNodePtr res_nb_node_node          = xmlNextElementSibling(res_nb_proc_node);
817         xmlNodePtr res_nb_proc_per_node_node = xmlNextElementSibling(res_nb_node_node);
818         xmlNodePtr res_cpu_clock_node        = xmlNextElementSibling(res_nb_proc_per_node_node);
819         xmlNodePtr res_mem_mb_node           = xmlNextElementSibling(res_cpu_clock_node);
820         if (res_name_node             == NULL ||
821             res_hostname_node         == NULL ||
822             res_os_node               == NULL ||
823             res_nb_proc_node          == NULL ||
824             res_nb_node_node          == NULL ||
825             res_nb_proc_per_node_node == NULL ||
826             res_cpu_clock_node        == NULL ||
827             res_mem_mb_node           == NULL
828            )
829         {
830           INFOS("A bad job is found, some resource_params user_part are not found");
831           delete new_job;
832           break;
833         }
834         if (xmlStrcmp(res_name_node->name,             xmlCharStrdup("name"))             ||
835             xmlStrcmp(res_hostname_node->name,         xmlCharStrdup("hostname"))         ||
836             xmlStrcmp(res_os_node->name,               xmlCharStrdup("OS"))               ||
837             xmlStrcmp(res_nb_proc_node->name,          xmlCharStrdup("nb_proc"))          ||
838             xmlStrcmp(res_nb_node_node->name,          xmlCharStrdup("nb_node"))          ||
839             xmlStrcmp(res_nb_proc_per_node_node->name, xmlCharStrdup("nb_proc_per_node")) ||
840             xmlStrcmp(res_cpu_clock_node->name,        xmlCharStrdup("cpu_clock"))        ||
841             xmlStrcmp(res_mem_mb_node->name,           xmlCharStrdup("mem_mb"))
842            )
843         {
844           INFOS("A bad job is found, some resource_params user_part node are not in the rigth or does not have a correct name");
845           delete new_job;
846           break;
847         }
848         xmlChar* res_name     = xmlNodeGetContent(res_name_node);
849         xmlChar* res_hostname = xmlNodeGetContent(res_hostname_node);
850         xmlChar* res_os       = xmlNodeGetContent(res_os_node);
851         resourceParams p;
852         p.name     = std::string((const char*) res_name);
853         p.hostname = std::string((const char*) res_hostname);
854         p.OS       = std::string((const char*) res_os);
855         xmlFree(res_name);
856         xmlFree(res_hostname);
857         xmlFree(res_os);
858         xmlChar* res_nb_proc          = xmlNodeGetContent(res_nb_proc_node);
859         xmlChar* res_nb_node          = xmlNodeGetContent(res_nb_node_node);
860         xmlChar* res_nb_proc_per_node = xmlNodeGetContent(res_nb_proc_per_node_node);
861         xmlChar* res_cpu_clock        = xmlNodeGetContent(res_cpu_clock_node);
862         xmlChar* res_mem_mb           = xmlNodeGetContent(res_mem_mb_node);
863         bool import_value = true;
864         std::istringstream nb_proc_stream((const char *) res_nb_proc);
865         if (!(nb_proc_stream >> p.nb_proc))
866           import_value = false;
867         std::istringstream nb_node_stream((const char *) res_nb_node);
868         if (!(nb_node_stream >> p.nb_node))
869           import_value = false;
870         std::istringstream nb_proc_per_node_stream((const char *) res_nb_proc_per_node);
871         if (!(nb_proc_per_node_stream >> p.nb_proc_per_node))
872           import_value = false;
873         std::istringstream cpu_clock_stream((const char *) res_cpu_clock);
874         if (!(cpu_clock_stream >> p.cpu_clock))
875           import_value = false;
876         std::istringstream mem_mb_stream((const char *) res_mem_mb);
877         if (!(mem_mb_stream >> p.mem_mb))
878           import_value = false;
879         xmlFree(res_nb_proc);
880         xmlFree(res_nb_node);
881         xmlFree(res_nb_proc_per_node);
882         xmlFree(res_cpu_clock);
883         xmlFree(res_mem_mb);
884         if (!import_value)
885         {
886           INFOS("A bad job is found, some resource_params value are not correct");
887           delete new_job;
888           break;
889         }
890         try
891         {
892           new_job->setResourceRequiredParams(p);
893         }
894         catch(const LauncherException &ex)
895         {
896           INFOS("A bad job is found, an error when inserting resource_params:" << ex.msg.c_str());
897           delete new_job;
898           break;
899         }
900
901         // We finally get run part to figure out what to do
902         xmlNodePtr job_state_node             = xmlFirstElementChild(run_node);
903         xmlNodePtr resource_choosed_name_node = xmlNextElementSibling(job_state_node);
904         xmlNodePtr job_reference_node         = xmlNextElementSibling(resource_choosed_name_node);
905         if (job_state_node             == NULL ||
906             resource_choosed_name_node == NULL ||
907             job_reference_node         == NULL
908            )
909         {
910           INFOS("A bad job is found, some run_part are not found");
911           delete new_job;
912           break;
913         }
914         if (xmlStrcmp(job_state_node->name,             xmlCharStrdup("job_state"))             ||
915             xmlStrcmp(resource_choosed_name_node->name, xmlCharStrdup("resource_choosed_name")) ||
916             xmlStrcmp(job_reference_node->name,         xmlCharStrdup("job_reference"))
917            )
918         {
919           INFOS("A bad job is found, some run_part nodes are not in the rigth or does not have a correct name");
920           delete new_job;
921           break;
922         }
923         xmlChar* job_state_xml             = xmlNodeGetContent(job_state_node);
924         xmlChar* resource_choosed_name_xml = xmlNodeGetContent(resource_choosed_name_node);
925         xmlChar* job_reference_xml         = xmlNodeGetContent(job_reference_node);
926         std::string job_state((const char *) job_state_xml);
927         std::string resource_choosed_name((const char *) resource_choosed_name_xml);
928         std::string job_reference((const char *) job_reference_xml);
929         xmlFree(job_state_xml);
930         xmlFree(resource_choosed_name_xml);
931         xmlFree(job_reference_xml);
932
933         if (job_state == "CREATED")
934         {
935           // In this case, we ignore run_part informations
936           try
937           {
938             _l.createJob(new_job);
939             std::ostringstream job_id;
940             job_id << new_job->getNumber();
941             notifyObservers("NEW_JOB", job_id.str());
942           }
943           catch(const LauncherException &ex)
944           {
945             INFOS("Load failed: " << ex.msg.c_str());
946           }
947         }
948         else if (job_state == "QUEUED"     ||
949                  job_state == "RUNNING"    ||
950                  job_state == "IN_PROCESS" ||
951                  job_state == "PAUSED")
952         {
953           try
954           {
955             new_job->setState(job_state);
956             _l.addJobDirectlyToMap(new_job, job_reference);
957
958             // Step 4: We check that the BatchManager could resume
959             // the job
960 #ifdef WITH_LIBBATCH
961             if (new_job->getBatchManagerJobId().getReference() != job_reference)
962             {
963               INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
964               new_job->setState("ERROR");
965             }
966 #endif
967             std::ostringstream job_id;
968             job_id << new_job->getNumber();
969             notifyObservers("NEW_JOB", job_id.str());
970           }
971           catch(const LauncherException &ex)
972           {
973             INFOS("Cannot load the job! Exception: " << ex.msg.c_str());
974             delete new_job;
975           }
976         }
977         else if (job_state == "FINISHED" ||
978                  job_state == "FAILED"   ||
979                  job_state == "ERROR")
980         {
981           try
982           {
983             // Step 2: We add run_part informations
984             new_job->setState(job_state);
985             _l.addJobDirectlyToMap(new_job, job_reference);
986             std::ostringstream job_id;
987             job_id << new_job->getNumber();
988             notifyObservers("NEW_JOB", job_id.str());
989           }
990           catch(const LauncherException &ex)
991           {
992             INFOS("Cannot load the job! Exception: " << ex.msg.c_str());
993             delete new_job;
994           }
995         }
996         else
997         {
998           INFOS("A bad job is found, state unknown " << job_state);
999           delete new_job;
1000         }
1001
1002       }
1003       xmlCurrentNode = xmlCurrentNode->next;
1004     }
1005   }
1006   else
1007   {
1008     xmlFreeDoc(doc);
1009     fclose(xml_file);
1010     std::string error = "Error in xml file, could not find root_node named jobs: " + std::string(jobs_file);
1011     INFOS(error);
1012     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
1013   }
1014
1015   // Clean
1016   xmlFreeDoc(doc);
1017   fclose(xml_file);
1018   notifyObservers("LOAD_JOBS", jobs_file);
1019 }
1020
1021 //=============================================================================
1022 /*! CORBA Method:
1023  *  Save jobs of Launcher (in any steps) in file jobs_file
1024  */
1025 //=============================================================================
1026 void
1027 SALOME_Launcher::saveJobs(const char* jobs_file)
1028 {
1029
1030   // Step 1: check jobs_file write access
1031   FILE* xml_file = fopen(jobs_file, "w");
1032   if (xml_file == NULL)
1033   {
1034     std::string error = "Error opening jobs_file in SALOME_Launcher::saveJobs: " + std::string(jobs_file);
1035     INFOS(error);
1036     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
1037   }
1038
1039   // Step 2: First lines
1040   xmlKeepBlanksDefault(0);
1041   xmlDocPtr doc = xmlNewDoc(xmlCharStrdup("1.0"));
1042   xmlNodePtr root_node = xmlNewNode(NULL, xmlCharStrdup("jobs"));
1043   xmlDocSetRootElement(doc, root_node);
1044   xmlNodePtr doc_comment = xmlNewDocComment(doc, xmlCharStrdup("SALOME Launcher save jobs file"));
1045   xmlAddPrevSibling(root_node, doc_comment);
1046
1047   // Step 3: For each job write it on the xml document
1048   // We could put a mutex but are not foing to do that currently
1049   std::map<int, Launcher::Job *> jobs_list = _l.getJobs();
1050   std::map<int, Launcher::Job *>::const_iterator it_job;
1051   for(it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
1052   {
1053     it_job->second->addToXmlDocument(root_node);
1054   }
1055
1056   // Final step: write file
1057   int isOk = xmlSaveFormatFile(jobs_file, doc, 1);
1058   if (!isOk)
1059   {
1060     std::string error = "Error during xml file saving in SALOME_Launcher::saveJobs: " + std::string(jobs_file);
1061     INFOS(error);
1062     xmlFreeDoc(doc);
1063     fclose(xml_file);
1064     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
1065   }
1066
1067   // Clean
1068   xmlFreeDoc(doc);
1069   fclose(xml_file);
1070   MESSAGE("SALOME_Launcher::saveJobs : WRITING DONE!");
1071   notifyObservers("SAVE_JOBS", jobs_file);
1072 }
1073
1074 //=============================================================================
1075 /*! CORBA Method:
1076  *  Add a new observer to the launcher
1077  */
1078 //=============================================================================
1079 void
1080 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
1081 {
1082   bool new_observer = true;
1083   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
1084   while(iter != _observers.end())
1085   {
1086     if (std::string(_orb->object_to_string(*iter)) ==
1087         std::string(_orb->object_to_string(observer)))
1088     {
1089       new_observer = false;
1090       break;
1091     }
1092     iter++;
1093   }
1094   if (new_observer)
1095     _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
1096
1097   // We notify the new observer with all jobs that are currently in the Launcher
1098   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
1099   std::map<int, Launcher::Job *>::const_iterator it_job;
1100   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
1101   {
1102     int number = it_job->first;
1103     std::ostringstream job_id;
1104     job_id << number;
1105     try
1106     {
1107       observer->notify("NEW_JOB", job_id.str().c_str());
1108     }
1109     catch (...) 
1110     {
1111        MESSAGE("Notify Observer, exception catch");
1112     }
1113
1114   }
1115 }
1116
1117 //=============================================================================
1118 /*! CORBA Method:
1119  *  Add a new observer to the launcher
1120  */
1121 //=============================================================================
1122 void
1123 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
1124 {
1125   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
1126   while(iter != _observers.end())
1127   {
1128     if (std::string(_orb->object_to_string(*iter)) ==
1129         std::string(_orb->object_to_string(observer)))
1130     {
1131       // Observer found
1132       iter =_observers.erase(iter++);
1133     }
1134     else
1135     {
1136       iter++;
1137     }
1138   }
1139 }
1140
1141 //=============================================================================
1142 /*! Internal Method:
1143  *  Notify observers on a new event
1144  */
1145 //=============================================================================
1146 void
1147 SALOME_Launcher::notifyObservers(const std::string & event_name,
1148                                  const std::string & event_data)
1149 {
1150   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
1151   while(iter != _observers.end())
1152   {
1153     try
1154     {
1155       (*iter)->notify(CORBA::string_dup(event_name.c_str()),
1156                       CORBA::string_dup(event_data.c_str()));
1157     }
1158     catch (...) 
1159     {
1160        MESSAGE("Notify Observer, exception catch");
1161     }
1162     iter++;
1163   }
1164
1165 }