]> SALOME platform Git repositories - modules/kernel.git/blob - src/Launcher/SALOME_Launcher.cxx
Salome HOME
Refactoring code EnableDumpYACS
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 //  Copyright (C) 2007-2010  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   return job_parameters._retn();
459 }
460
461 //=============================================================================
462 /*! CORBA Method:
463  *  Loads jobs saved in jobs_file
464  */
465 //=============================================================================
466 void
467 SALOME_Launcher::loadJobs(const char* jobs_file)
468 {
469   // Step 1: check jobs_file read access
470   FILE* xml_file = fopen(jobs_file, "r");
471   if (xml_file == NULL)
472   {
473     std::string error = "Error opening jobs_file in SALOME_Launcher::loadJobs: " + std::string(jobs_file);
474     INFOS(error);
475     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
476   }
477
478   // Step 2: read xml file
479   xmlDocPtr doc = xmlReadFile(jobs_file, NULL, 0);
480   if (doc == NULL)
481   {
482     std::string error = "Error in xmlReadFile in SALOME_Launcher::loadJobs, could not parse file: " + std::string(jobs_file);
483     INFOS(error);
484     fclose(xml_file);
485     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
486   }
487
488   // Step 3: Find jobs
489   xmlNodePtr root_node = xmlDocGetRootElement(doc);
490   xmlNodePtr xmlCurrentNode = root_node->xmlChildrenNode;
491   if (!xmlStrcmp(root_node->name, xmlCharStrdup("jobs")))
492   {
493     while(xmlCurrentNode != NULL)
494     {
495       if (!xmlStrcmp(xmlCurrentNode->name, xmlCharStrdup("job")))
496       {
497         INFOS("A job is found");
498         Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
499         xmlNodePtr job_node = xmlCurrentNode;
500
501         // Begin Job
502         if (!xmlHasProp(job_node, xmlCharStrdup("type"))  ||
503             !xmlHasProp(job_node, xmlCharStrdup("name")))
504         {
505           INFOS("A bad job is found, type or name not found");
506           break;
507         }
508         xmlChar* type = xmlGetProp(job_node, xmlCharStrdup("type"));
509         xmlChar* name = xmlGetProp(job_node, xmlCharStrdup("name"));
510         std::string job_type((const char*) type);
511         if (job_type == "command")
512           new_job = new Launcher::Job_Command();
513         else if (job_type == "yacs_file")
514           new_job = new Launcher::Job_YACSFile();
515         else if (job_type == "python_salome")
516           new_job = new Launcher::Job_PythonSALOME();
517         new_job->setJobName(std::string((const char *)name));
518         xmlFree(type);
519         xmlFree(name);
520
521         xmlNodePtr user_node = xmlFirstElementChild(job_node);
522         xmlNodePtr run_node = xmlNextElementSibling(user_node);
523         if (user_node == NULL || run_node == NULL)
524         {
525           INFOS("A bad job is found, user_part or run_part not found");
526           delete new_job;
527           break;
528         }
529         if (xmlStrcmp(user_node->name, xmlCharStrdup("user_part")) ||
530             xmlStrcmp(run_node->name, xmlCharStrdup("run_part")))
531         {
532           INFOS("A bad job is found, cannot get a correct user_part or run_part node");
533           delete new_job;
534           break;
535         }
536
537         // Add user part
538
539         // Get job_file env_file work_directory local_directory result_directory
540         xmlNodePtr job_file_node         = xmlFirstElementChild(user_node);
541         xmlNodePtr env_file_node         = xmlNextElementSibling(job_file_node);
542         xmlNodePtr work_directory_node   = xmlNextElementSibling(env_file_node);
543         xmlNodePtr local_directory_node  = xmlNextElementSibling(work_directory_node);
544         xmlNodePtr result_directory_node = xmlNextElementSibling(local_directory_node);
545         if (job_file_node         == NULL ||
546             env_file_node         == NULL ||
547             work_directory_node   == NULL ||
548             local_directory_node  == NULL ||
549             result_directory_node == NULL
550            )
551         {
552           INFOS("A bad job is found, some user_part are not found");
553           delete new_job;
554           break;
555         }
556         if (xmlStrcmp(job_file_node->name,         xmlCharStrdup("job_file"))         ||
557             xmlStrcmp(env_file_node->name,         xmlCharStrdup("env_file"))         ||
558             xmlStrcmp(work_directory_node->name,   xmlCharStrdup("work_directory"))   ||
559             xmlStrcmp(local_directory_node->name,  xmlCharStrdup("local_directory"))  ||
560             xmlStrcmp(result_directory_node->name, xmlCharStrdup("result_directory"))
561            )
562         {
563           INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name");
564           delete new_job;
565           break;
566         }
567         xmlChar* job_file         = xmlNodeGetContent(job_file_node);
568         try
569         {
570           new_job->setJobFile(std::string((const char *)job_file));
571         }
572         catch(const LauncherException &ex)
573         {
574           INFOS("Exception receice for job_file, cannot add the job" << ex.msg.c_str());
575           delete new_job;
576           xmlFree(job_file);
577           break;
578         }
579         xmlChar* env_file         = xmlNodeGetContent(env_file_node);
580         xmlChar* work_directory   = xmlNodeGetContent(work_directory_node);
581         xmlChar* local_directory  = xmlNodeGetContent(local_directory_node);
582         xmlChar* result_directory = xmlNodeGetContent(result_directory_node);
583         new_job->setEnvFile(std::string((const char *)env_file));
584         new_job->setWorkDirectory(std::string((const char *)work_directory));
585         new_job->setLocalDirectory(std::string((const char *)local_directory));
586         new_job->setResultDirectory(std::string((const char *)result_directory));
587         xmlFree(job_file);
588         xmlFree(env_file);
589         xmlFree(work_directory);
590         xmlFree(local_directory);
591         xmlFree(result_directory);
592
593         // Get in and out files
594         xmlNodePtr files_node = xmlNextElementSibling(result_directory_node);
595         if (files_node == NULL)
596         {
597           INFOS("A bad job is found, user_part files is not found");
598           delete new_job;
599           break;
600         }
601         if (xmlStrcmp(files_node->name, xmlCharStrdup("files")))
602         {
603           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");
604           delete new_job;
605           break;
606         }
607         xmlNodePtr file_node = xmlFirstElementChild(files_node);
608         while (file_node != NULL)
609         {
610           if (!xmlStrcmp(file_node->name, xmlCharStrdup("in_file")))
611           {
612             xmlChar* in_file = xmlNodeGetContent(file_node);
613             new_job->add_in_file(std::string((const char *)in_file));
614             xmlFree(in_file);
615           }
616           else if (!xmlStrcmp(file_node->name, xmlCharStrdup("out_file")))
617           {
618             xmlChar* out_file = xmlNodeGetContent(file_node);
619             new_job->add_out_file(std::string((const char *)out_file));
620             xmlFree(out_file);
621           }
622           file_node = xmlNextElementSibling(file_node);
623         }
624
625         // Get resource part
626         xmlNodePtr res_node = xmlNextElementSibling(files_node);
627         xmlNodePtr maximum_duration_node = xmlNextElementSibling(res_node);
628         xmlNodePtr queue_node = xmlNextElementSibling(maximum_duration_node);
629         if (res_node              == NULL ||
630             maximum_duration_node == NULL ||
631             queue_node            == NULL
632            )
633         {
634           INFOS("A bad job is found, some user_part are not found");
635           delete new_job;
636           break;
637         }
638         if (xmlStrcmp(res_node->name,              xmlCharStrdup("resource_params"))  ||
639             xmlStrcmp(maximum_duration_node->name, xmlCharStrdup("maximum_duration")) ||
640             xmlStrcmp(queue_node->name,            xmlCharStrdup("queue"))
641            )
642         {
643           INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name");
644           delete new_job;
645           break;
646         }
647         xmlChar* maximum_duration = xmlNodeGetContent(maximum_duration_node);
648         try
649         {
650           new_job->setMaximumDuration(std::string((const char *)maximum_duration));
651         }
652         catch(const LauncherException &ex)
653         {
654           INFOS("Exception receice for maximum_duration, cannot add the job" << ex.msg.c_str());
655           delete new_job;
656           xmlFree(maximum_duration);
657           break;
658         }
659         xmlChar* queue            = xmlNodeGetContent(queue_node);
660         new_job->setQueue(std::string((const char *)queue));
661         xmlFree(maximum_duration);
662         xmlFree(queue);
663
664         xmlNodePtr res_name_node             = xmlFirstElementChild(res_node);
665         xmlNodePtr res_hostname_node         = xmlNextElementSibling(res_name_node);
666         xmlNodePtr res_os_node               = xmlNextElementSibling(res_hostname_node);
667         xmlNodePtr res_nb_proc_node          = xmlNextElementSibling(res_os_node);
668         xmlNodePtr res_nb_node_node          = xmlNextElementSibling(res_nb_proc_node);
669         xmlNodePtr res_nb_proc_per_node_node = xmlNextElementSibling(res_nb_node_node);
670         xmlNodePtr res_cpu_clock_node        = xmlNextElementSibling(res_nb_proc_per_node_node);
671         xmlNodePtr res_mem_mb_node           = xmlNextElementSibling(res_cpu_clock_node);
672         if (res_name_node             == NULL ||
673             res_hostname_node         == NULL ||
674             res_os_node               == NULL ||
675             res_nb_proc_node          == NULL ||
676             res_nb_node_node          == NULL ||
677             res_nb_proc_per_node_node == NULL ||
678             res_cpu_clock_node        == NULL ||
679             res_mem_mb_node           == NULL
680            )
681         {
682           INFOS("A bad job is found, some resource_params user_part are not found");
683           delete new_job;
684           break;
685         }
686         if (xmlStrcmp(res_name_node->name,             xmlCharStrdup("name"))             ||
687             xmlStrcmp(res_hostname_node->name,         xmlCharStrdup("hostname"))         ||
688             xmlStrcmp(res_os_node->name,               xmlCharStrdup("OS"))               ||
689             xmlStrcmp(res_nb_proc_node->name,          xmlCharStrdup("nb_proc"))          ||
690             xmlStrcmp(res_nb_node_node->name,          xmlCharStrdup("nb_node"))          ||
691             xmlStrcmp(res_nb_proc_per_node_node->name, xmlCharStrdup("nb_proc_per_node")) ||
692             xmlStrcmp(res_cpu_clock_node->name,        xmlCharStrdup("cpu_clock"))        ||
693             xmlStrcmp(res_mem_mb_node->name,           xmlCharStrdup("mem_mb"))
694            )
695         {
696           INFOS("A bad job is found, some resource_params user_part node are not in the rigth or does not have a correct name");
697           delete new_job;
698           break;
699         }
700         xmlChar* res_name     = xmlNodeGetContent(res_name_node);
701         xmlChar* res_hostname = xmlNodeGetContent(res_hostname_node);
702         xmlChar* res_os       = xmlNodeGetContent(res_os_node);
703         resourceParams p;
704         p.name     = std::string((const char*) res_name);
705         p.hostname = std::string((const char*) res_hostname);
706         p.OS       = std::string((const char*) res_os);
707         xmlFree(res_name);
708         xmlFree(res_hostname);
709         xmlFree(res_os);
710         xmlChar* res_nb_proc          = xmlNodeGetContent(res_nb_proc_node);
711         xmlChar* res_nb_node          = xmlNodeGetContent(res_nb_node_node);
712         xmlChar* res_nb_proc_per_node = xmlNodeGetContent(res_nb_proc_per_node_node);
713         xmlChar* res_cpu_clock        = xmlNodeGetContent(res_cpu_clock_node);
714         xmlChar* res_mem_mb           = xmlNodeGetContent(res_mem_mb_node);
715         bool import_value = true;
716         std::istringstream nb_proc_stream((const char *) res_nb_proc);
717         if (!(nb_proc_stream >> p.nb_proc))
718           import_value = false;
719         std::istringstream nb_node_stream((const char *) res_nb_node);
720         if (!(nb_node_stream >> p.nb_node))
721           import_value = false;
722         std::istringstream nb_proc_per_node_stream((const char *) res_nb_proc_per_node);
723         if (!(nb_proc_per_node_stream >> p.nb_proc_per_node))
724           import_value = false;
725         std::istringstream cpu_clock_stream((const char *) res_cpu_clock);
726         if (!(cpu_clock_stream >> p.cpu_clock))
727           import_value = false;
728         std::istringstream mem_mb_stream((const char *) res_mem_mb);
729         if (!(mem_mb_stream >> p.mem_mb))
730           import_value = false;
731         xmlFree(res_nb_proc);
732         xmlFree(res_nb_node);
733         xmlFree(res_nb_proc_per_node);
734         xmlFree(res_cpu_clock);
735         xmlFree(res_mem_mb);
736         if (!import_value)
737         {
738           INFOS("A bad job is found, some resource_params value are not correct");
739           delete new_job;
740           break;
741         }
742         try
743         {
744           new_job->setResourceRequiredParams(p);
745         }
746         catch(const LauncherException &ex)
747         {
748           INFOS("A bad job is found, an error when inserting resource_params:" << ex.msg.c_str());
749           delete new_job;
750           break;
751         }
752
753         // We finally get run part to figure out what to do
754         xmlNodePtr job_state_node             = xmlFirstElementChild(run_node);
755         xmlNodePtr resource_choosed_name_node = xmlNextElementSibling(job_state_node);
756         xmlNodePtr job_reference_node         = xmlNextElementSibling(resource_choosed_name_node);
757         if (job_state_node             == NULL ||
758             resource_choosed_name_node == NULL ||
759             job_reference_node         == NULL
760            )
761         {
762           INFOS("A bad job is found, some run_part are not found");
763           delete new_job;
764           break;
765         }
766         if (xmlStrcmp(job_state_node->name,             xmlCharStrdup("job_state"))             ||
767             xmlStrcmp(resource_choosed_name_node->name, xmlCharStrdup("resource_choosed_name")) ||
768             xmlStrcmp(job_reference_node->name,         xmlCharStrdup("job_reference"))
769            )
770         {
771           INFOS("A bad job is found, some run_part nodes are not in the rigth or does not have a correct name");
772           delete new_job;
773           break;
774         }
775         xmlChar* job_state_xml             = xmlNodeGetContent(job_state_node);
776         xmlChar* resource_choosed_name_xml = xmlNodeGetContent(resource_choosed_name_node);
777         xmlChar* job_reference_xml         = xmlNodeGetContent(job_reference_node);
778         std::string job_state((const char *) job_state_xml);
779         std::string resource_choosed_name((const char *) resource_choosed_name_xml);
780         std::string job_reference((const char *) job_reference_xml);
781         xmlFree(job_state_xml);
782         xmlFree(resource_choosed_name_xml);
783         xmlFree(job_reference_xml);
784
785         if (job_state == "CREATED")
786         {
787           // In this case, we ignore run_part informations
788           try
789           {
790             _l.createJob(new_job);
791
792             std::ostringstream job_id;
793             job_id << new_job->getNumber();
794             notifyObservers("NEW_JOB", job_id.str());
795           }
796           catch(const LauncherException &ex)
797           {
798             INFOS("Load failed: " << ex.msg.c_str());
799           }
800         }
801         else if (job_state == "QUEUED"     ||
802                  job_state == "RUNNING"    ||
803                  job_state == "IN_PROCESS" ||
804                  job_state == "PAUSED")
805         {
806           try
807           {
808             // Step 1: Add the resource to the launcher C++ map
809             _l.checkFactoryForResource(resource_choosed_name);
810
811             // Step 2: We add run_part informations
812             new_job->setState(job_state);
813
814             // Step 3: We add the job to the launcher
815             ParserResourcesType resource_definition = _l._ResManager->GetResourcesDescr(resource_choosed_name);
816             new_job->setResourceDefinition(resource_definition);
817             _l.addJobDirectlyToMap(new_job, job_reference);
818
819             // Step 4: We check that the BatchManager could resume
820             // the job
821 #ifdef WITH_LIBBATCH
822             if (new_job->getBatchManagerJobId().getReference() != job_reference)
823             {
824               INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
825               new_job->setState("ERROR");
826             }
827 #endif
828             std::ostringstream job_id;
829             job_id << new_job->getNumber();
830             notifyObservers("NEW_JOB", job_id.str());
831           }
832           catch(const LauncherException &ex)
833           {
834             INFOS("Cannot load the job! Exception: " << ex.msg.c_str());
835             delete new_job;
836           }
837         }
838         else if (job_state == "FINISHED" ||
839                  job_state == "FAILED"   ||
840                  job_state == "ERROR")
841         {
842           try
843           {
844             // Step 1: Add the resource to the launcher C++ map
845             _l.checkFactoryForResource(resource_choosed_name);
846
847             // Step 2: We add run_part informations
848             new_job->setState(job_state);
849
850             // Step 3: We add the job to the launcher
851             ParserResourcesType resource_definition = _l._ResManager->GetResourcesDescr(resource_choosed_name);
852             new_job->setResourceDefinition(resource_definition);
853             _l.addJobDirectlyToMap(new_job, job_reference);
854
855             std::ostringstream job_id;
856             job_id << new_job->getNumber();
857             notifyObservers("NEW_JOB", job_id.str());
858           }
859           catch(const LauncherException &ex)
860           {
861             INFOS("Cannot load the job! Exception: " << ex.msg.c_str());
862             delete new_job;
863           }
864         }
865         else
866         {
867           INFOS("A bad job is found, state unknown " << job_state);
868           delete new_job;
869         }
870
871       }
872       xmlCurrentNode = xmlCurrentNode->next;
873     }
874   }
875   else
876   {
877     xmlFreeDoc(doc);
878     fclose(xml_file);
879     std::string error = "Error in xml file, could not find root_node named jobs: " + std::string(jobs_file);
880     INFOS(error);
881     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
882   }
883
884   // Clean
885   xmlFreeDoc(doc);
886   fclose(xml_file);
887   notifyObservers("LOAD_JOBS", jobs_file);
888 }
889
890 //=============================================================================
891 /*! CORBA Method:
892  *  Save jobs of Launcher (in any steps) in file jobs_file
893  */
894 //=============================================================================
895 void
896 SALOME_Launcher::saveJobs(const char* jobs_file)
897 {
898
899   // Step 1: check jobs_file write access
900   FILE* xml_file = fopen(jobs_file, "w");
901   if (xml_file == NULL)
902   {
903     std::string error = "Error opening jobs_file in SALOME_Launcher::saveJobs: " + std::string(jobs_file);
904     INFOS(error);
905     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
906   }
907
908   // Step 2: First lines
909   xmlKeepBlanksDefault(0);
910   xmlDocPtr doc = xmlNewDoc(xmlCharStrdup("1.0"));
911   xmlNodePtr root_node = xmlNewNode(NULL, xmlCharStrdup("jobs"));
912   xmlDocSetRootElement(doc, root_node);
913   xmlNodePtr doc_comment = xmlNewDocComment(doc, xmlCharStrdup("SALOME Launcher save jobs file"));
914   xmlAddPrevSibling(root_node, doc_comment);
915
916   // Step 3: For each job write it on the xml document
917   // We could put a mutex but are not foing to do that currently
918   std::map<int, Launcher::Job *> jobs_list = _l.getJobs();
919   std::map<int, Launcher::Job *>::const_iterator it_job;
920   for(it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
921   {
922     it_job->second->addToXmlDocument(root_node);
923   }
924
925   // Final step: write file
926   int isOk = xmlSaveFormatFile(jobs_file, doc, 1);
927   if (!isOk)
928   {
929     std::string error = "Error during xml file saving in SALOME_Launcher::saveJobs: " + std::string(jobs_file);
930     INFOS(error);
931     xmlFreeDoc(doc);
932     fclose(xml_file);
933     THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR);
934   }
935
936   // Clean
937   xmlFreeDoc(doc);
938   fclose(xml_file);
939   MESSAGE("SALOME_Launcher::saveJobs : WRITING DONE!");
940   notifyObservers("SAVE_JOBS", jobs_file);
941 }
942
943 //=============================================================================
944 /*! CORBA Method:
945  *  Add a new observer to the launcher
946  */
947 //=============================================================================
948 void
949 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
950 {
951   bool new_observer = true;
952   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
953   while(iter != _observers.end())
954   {
955     if (std::string(_orb->object_to_string(*iter)) ==
956         std::string(_orb->object_to_string(observer)))
957     {
958       new_observer = false;
959       break;
960     }
961     iter++;
962   }
963   if (new_observer)
964     _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
965
966   // We notify the new observer with all jobs that are currently in the Launcher
967   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
968   std::map<int, Launcher::Job *>::const_iterator it_job;
969   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
970   {
971     int number = it_job->first;
972     std::ostringstream job_id;
973     job_id << number;
974     try
975     {
976       observer->notify("NEW_JOB", job_id.str().c_str());
977     }
978     catch (...) 
979     {
980        MESSAGE("Notify Observer, exception catch");
981     }
982
983   }
984 }
985
986 //=============================================================================
987 /*! CORBA Method:
988  *  Add a new observer to the launcher
989  */
990 //=============================================================================
991 void
992 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
993 {
994   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
995   while(iter != _observers.end())
996   {
997     if (std::string(_orb->object_to_string(*iter)) ==
998         std::string(_orb->object_to_string(observer)))
999     {
1000       // Observer found
1001       iter =_observers.erase(iter++);
1002     }
1003     else
1004     {
1005       iter++;
1006     }
1007   }
1008 }
1009
1010 //=============================================================================
1011 /*! Internal Method:
1012  *  Notify observers on a new event
1013  */
1014 //=============================================================================
1015 void
1016 SALOME_Launcher::notifyObservers(const std::string & event_name,
1017                                  const std::string & event_data)
1018 {
1019   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
1020   while(iter != _observers.end())
1021   {
1022     try
1023     {
1024       (*iter)->notify(CORBA::string_dup(event_name.c_str()),
1025                       CORBA::string_dup(event_data.c_str()));
1026     }
1027     catch (...) 
1028     {
1029        MESSAGE("Notify Observer, exception catch");
1030     }
1031     iter++;
1032   }
1033
1034 }