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