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