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