Salome HOME
10bae23e7129a6941484207fdad401f53505360b
[modules/smesh.git] / src / Tools / padder / meshjob / impl / MeshJobManager_i.cxx
1 // Copyright (C) 2011-2021  EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Authors : Guillaume Boulant (EDF) - 01/03/2011
21
22 #ifdef WIN32
23 #include <winsock2.h>
24 #include <windows.h> 
25 #else
26 #include <sys/time.h>
27 #endif
28
29 #include "MeshJobManager_i.hxx"
30
31 #include <SALOMEconfig.h>
32 #include CORBA_SERVER_HEADER(SALOME_Exception)
33
34
35 #include "Basics_Utils.hxx"         // For standard logging
36 #undef LOG
37 #include "SALOME_KernelServices.hxx"   // For CORBA logging
38 #undef LOG
39
40 #define LOG STDLOG
41
42 //
43 // ====================================================================
44 // General purpose helper functions (to put elsewhere at least)
45 // ====================================================================
46 //
47
48 /*!
49  * This function must be used to associate a datetime tag to a job
50  */
51
52 #ifndef WIN32
53 static long timetag() {
54   timeval tv;
55   gettimeofday(&tv,0);
56   long tag = tv.tv_usec + tv.tv_sec*1000000;
57   return tag;
58 }
59 #endif
60
61 /*!
62  * This function returns true if the string text starts with the string
63  * token.
64  */
65 static bool myStartsWith(const std::string& text,const std::string& token){     
66   if(text.length() < token.length())
67     return false;
68   return (text.compare(0, token.length(), token) == 0);
69 }
70
71 /*!
72  * This function returns true if the file exists on the local file
73  * system.
74  */
75 #include <iostream>
76 #include <fstream>
77 static bool fexists(const char *filename)
78 {
79   std::ifstream ifile(filename);
80   if ((bool)ifile && ifile.good()) {
81     return true;
82   }
83   return false;
84 }
85
86 Engines::EngineComponent_var RetrievePadderMeshJobManagerInstance()
87 {
88
89 }
90
91 //
92 // ====================================================================
93 // Constructor/Destructor
94 // ====================================================================
95 //
96 MeshJobManager_i::MeshJobManager_i(CORBA::ORB_ptr orb,
97                                    PortableServer::POA_ptr poa,
98                                    PortableServer::ObjectId * contId,
99                                    const char *instanceName,
100                                    const char *interfaceName,
101                                    bool checkNS, bool regist)
102   : Engines_Component_i(orb, poa, contId, instanceName, interfaceName, checkNS, regist)
103 {
104   LOG("Activating MESHJOB::MeshJobManager object");
105   _thisObj = this ;
106   _id = _poa->activate_object(_thisObj);
107
108   _salomeLauncher   = KERNEL::getSalomeLauncher();
109   if(CORBA::is_nil(_salomeLauncher)){
110     LOG("The SALOME launcher can't be reached ==> STOP");
111     throw KERNEL::createSalomeException("SALOME launcher can't be reached");
112   }
113
114   _resourcesManager = KERNEL::getResourcesManager();
115   if(CORBA::is_nil(_resourcesManager)){
116     LOG("The SALOME resource manager can't be reached ==> STOP");
117     throw KERNEL::createSalomeException("The SALOME resource manager can't be reached");
118   }
119
120   _lastErrorMessage = "";
121 }
122
123 MeshJobManager_i::~MeshJobManager_i() {
124   LOG("MeshJobManager_i::~MeshJobManager_i()");
125 }
126
127 //
128 // ====================================================================
129 // Helper functions to deals with the local and remote file systems
130 // ====================================================================
131 //
132 #include <fstream>     // to get the file streams
133 #ifdef WIN32
134 #include <stdlib.h>    // to get _splitpath
135 #include <direct.h>    // to get _mkdir
136 #else
137 #include <unistd.h>    // to get basename
138 #include <libgen.h>    // to get basename - as per posix
139 #include <sys/stat.h>  // to get mkdir
140 #include <sys/types.h> // to get mkdir options
141 #endif
142
143 #include <stdlib.h>    // to get system and getenv
144
145 static std::string OUTPUTFILE("output.med");
146 static std::string DATAFILE("data.txt");
147 static std::string SCRIPTFILE("padder.sh");
148 static std::string SEPARATOR(" ");
149
150 #ifdef WIN32
151 static std::string USER(getenv("USERNAME"));
152 #else
153 static std::string USER(getenv("USER"));
154 #endif
155
156 static std::string LOCAL_INPUTDIR("/tmp/spadder.local.inputdir."+USER);
157 static std::string LOCAL_RESULTDIR("/tmp/spadder.local.resultdir."+USER);
158 static std::string REMOTE_WORKDIR("/tmp/spadder.remote.workdir."+USER);
159
160 /*!
161  * This function creates the padder text input file containing the
162  * input data (list of filenames and groupnames) and returns the path
163  * of the created file. This function is the one that knows the format
164  * of the padder input file. If the input file format changes, then
165  * this function (and only this one) should be updated. The file
166  * format is the following ([] means that the variable is optional):
167  *
168  * [<concreteMeshFile>   <concreteGroupName>]
169  * nbSteelBarMeshes <N>
170  * <steelBarMeshFile_1>   <steelBarGroupName_1>
171  * <steelBarMeshFile_2>   <steelBarGroupName_2>
172  * ...
173  * <steelBarMeshFile_N>   <steelBarGroupName_N>
174  * <outputMedFile>
175  */
176 const char * MeshJobManager_i::_writeDataFile(std::vector<MESHJOB::MeshJobFile> listConcreteMesh,
177                                               std::vector<MESHJOB::MeshJobFile> listSteelBarMesh,
178                                               const MESHJOB::MeshJobParameterList & meshJobParameterList) {
179 #ifdef WIN32
180   _mkdir(LOCAL_INPUTDIR.c_str());
181 #else
182   mkdir(LOCAL_INPUTDIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
183 #endif
184
185   // Make it static so that it's allocated once (constant name)
186   static std::string * dataFilename = new std::string(LOCAL_INPUTDIR+"/"+DATAFILE);
187   std::ofstream dataFile(dataFilename->c_str());
188
189   // Note that we use here the basename of the files because the files
190   // are supposed to be copied in the REMOTE_WORKDIR for execution.
191   std::string line;
192
193   // We first specify the concrete mesh data (filename and groupname)
194   if ( listConcreteMesh.size() > 0 ) {
195 #ifdef WIN32
196     char fname[ _MAX_FNAME ];
197     _splitpath( listConcreteMesh[0].file_name, NULL, NULL, fname, NULL );
198     char* bname = &fname[0];
199 #else
200     char* bname = basename(listConcreteMesh[0].file_name);
201 #endif
202     line = std::string(bname) + " " + std::string(listConcreteMesh[0].group_name);
203     dataFile << line.c_str() << std::endl;
204   }
205   // Then, we can specify the steelbar mesh data, starting by the
206   // number of meshes
207   int nbSteelBarMeshes=listSteelBarMesh.size();
208   line = std::string("nbSteelBarMeshes") + SEPARATOR + ToString(nbSteelBarMeshes);
209   dataFile << line.c_str() << std::endl;
210   for (int i=0; i<nbSteelBarMeshes; i++) {
211 #ifdef WIN32
212         char fname[ _MAX_FNAME ];
213         _splitpath( listSteelBarMesh[i].file_name, NULL, NULL, fname, NULL );
214         char* bname = &fname[0];
215 #else
216         char* bname = basename(listSteelBarMesh[i].file_name);
217 #endif
218     line = std::string(bname) + " " + std::string(listSteelBarMesh[i].group_name);
219     dataFile << line.c_str() << std::endl;
220   }
221   
222   // We conclude the list of files with the name of the output file
223   line = OUTPUTFILE;
224   dataFile << line.c_str() << std::endl;
225
226   // We put the numerical parameters at the end of the data file
227   for(CORBA::ULong i=0; i<meshJobParameterList.length(); i++) {
228     MESHJOB::MeshJobParameter param = meshJobParameterList[i];
229     line = std::string(param.name) + " " + std::string(param.value);
230     dataFile << line.c_str() << std::endl;
231   }
232
233   dataFile.close();
234   return dataFilename->c_str();  
235 }
236
237 /*!
238  * This function creates a shell script that runs padder with the
239  * specified data file, and returns the path of the created script
240  * file. The config id is used to retrieve the path to the binary file
241  * and other required files.
242  */
243 const char* MeshJobManager_i::_writeScriptFile(const char * dataFileName, const char * configId) {
244 #ifdef WIN32
245   _mkdir(LOCAL_INPUTDIR.c_str());
246 #else
247   mkdir(LOCAL_INPUTDIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
248 #endif
249
250   // Make it static so that it's allocated once (constant name)
251   static std::string * scriptFilename = new std::string(LOCAL_INPUTDIR+"/"+SCRIPTFILE);
252
253   char * binpath = _configMap[configId].binpath;
254   char * envpath = _configMap[configId].envpath;
255
256 #ifdef WIN32
257         char fname[ _MAX_FNAME ];
258         _splitpath( dataFileName, NULL, NULL, fname, NULL );
259         const char* bname = &fname[0];
260 #else
261         const char* bname = basename(const_cast<char *>(dataFileName));
262 #endif
263
264
265   std::ofstream script(scriptFilename->c_str());
266   script << "#!/bin/sh"                                   << std::endl;
267   script << "here=$(dirname $0)"                          << std::endl;
268   script << ". " << envpath                               << std::endl;
269   script << binpath << " $here/" << bname                 << std::endl;
270   // Note that we use the basename of the datafile because all data
271   // files are supposed to have been copied in the REMOTE_WORKDIR.
272   script.close();
273   return scriptFilename->c_str();
274 }
275
276 //
277 // ====================================================================
278 // Functions to initialize and supervise the mesh computation job
279 // ====================================================================
280 //
281 bool MeshJobManager_i::configure(const char *configId,
282                                  const MESHJOB::ConfigParameter & configParameter)
283 {
284   beginService("MeshJobManager_i::configure");
285   
286   _configMap[configId] = configParameter;
287
288   LOG("Adding configuration for " << configId);
289   LOG("- binpath = " << _configMap[configId].binpath);
290   LOG("- envpath = " << _configMap[configId].envpath);
291
292   endService("MeshJobManager_i::configure");
293   return true;
294 }
295
296 long MeshJobManager_i::JOBID_UNDEFINED = -1;
297
298 /*! Initialize a smesh computation job and return the job identifier */
299 CORBA::Long MeshJobManager_i::initialize(const MESHJOB::MeshJobFileList & meshJobFileList,
300                                          const MESHJOB::MeshJobParameterList & meshJobParameterList,
301                                          const char * configId)
302 {
303   beginService("MeshJobManager_i::initialize");
304   //
305   // We first analyse the CORBA sequence to store data in C++ vectors
306   //
307   std::vector<MESHJOB::MeshJobFile> listConcreteMesh;
308   std::vector<MESHJOB::MeshJobFile> listSteelBarMesh;
309   for(CORBA::ULong i=0; i<meshJobFileList.length(); i++) {
310     MESHJOB::MeshJobFile currentMesh = meshJobFileList[i];
311     switch ( currentMesh.file_type ) {
312     case MESHJOB::MED_CONCRETE:
313       listConcreteMesh.push_back(currentMesh);
314       break;
315     case MESHJOB::MED_STEELBAR:
316       listSteelBarMesh.push_back(currentMesh);
317       break;
318     default:
319       _lastErrorMessage =
320         std::string("The type of the file ")+
321         std::string(currentMesh.file_name)+
322         std::string(" is not recognized");
323       LOG(_lastErrorMessage);
324       return JOBID_UNDEFINED;
325     }
326   }
327   
328   // It is not possible to specify more than one concrete
329   // file. Converselly, it is possible to specify no concrete file.
330   if ( listConcreteMesh.size() > 1 ) {
331     // Not consistent with the specification
332     _lastErrorMessage = std::string("You specify more than one concrete mesh (not authorized)");
333     LOG(_lastErrorMessage);
334     return JOBID_UNDEFINED;
335   }
336   
337   LOG("Nb. concrete mesh = " << listConcreteMesh.size());
338   LOG("Nb. steelbar mesh = " << listSteelBarMesh.size());
339
340   // We initiate here a datetime to tag the files and folder
341   // associated to this job.
342 #ifdef WIN32
343   DWORD jobDatetimeTag = timeGetTime();
344 #else
345   long jobDatetimeTag = timetag();
346 #endif
347   // And a MESHJOB::MeshJobPaths structure to hold the directories
348   // where to find data
349   MESHJOB::MeshJobPaths * jobPaths = new MESHJOB::MeshJobPaths();
350   jobPaths->local_inputdir  = LOCAL_INPUTDIR.c_str();
351   jobPaths->local_resultdir = (LOCAL_RESULTDIR + "." + ToString(jobDatetimeTag)).c_str();
352   jobPaths->remote_workdir  = (REMOTE_WORKDIR + "." + ToString(jobDatetimeTag)).c_str();  
353
354   //
355   // Then, we have to create the padder input data file. This input
356   // data is a text file containing the list of file names and group
357   // names.
358   //
359   const char * dataFilename = this->_writeDataFile(listConcreteMesh, listSteelBarMesh, meshJobParameterList);
360   LOG("dataFilename = " << dataFilename);
361   const char * scriptFilename = this->_writeScriptFile(dataFilename, configId);
362   LOG("scriptFilename = " << scriptFilename);
363
364   //
365   // Then, the following instructions consists in preparing the job
366   // parameters to request the SALOME launcher for creating a new
367   // job.
368   //
369   Engines::JobParameters_var jobParameters = new Engines::JobParameters;
370   jobParameters->job_type = CORBA::string_dup("command");
371   // CAUTION: the job_file must be a single filename specifying a
372   // self-consistent script to be executed without any argument on the
373   // remote host.
374   jobParameters->job_file = CORBA::string_dup(scriptFilename);
375
376   //
377   // Specification of the working spaces:
378   //
379   // - local_directory: can be used to specify where to find the input
380   //   files on the local resource. It's optional if you specify the
381   //   absolute path name of input files.
382   //
383   // - result_directory: must be used to specify where to download the
384   //   output files on the local resources
385   //
386   // - work_directory: must be used to specify the remote directory
387   //   where to put all the stuff to run the job. Note that the job
388   //   will be executed from within this directory, i.e. a change
389   //   directory toward this working directory is done by the batch
390   //   system before running the specified job script.
391   //
392   jobParameters->local_directory  = CORBA::string_dup("");
393   jobParameters->result_directory = CORBA::string_dup(jobPaths->local_resultdir);
394   jobParameters->work_directory   = CORBA::string_dup(jobPaths->remote_workdir);
395
396   // We specify the input files that are required to execute the
397   // job_file. If basenames are specified, then the files are supposed
398   // to be located in local_directory.
399   int nbcmesh = listConcreteMesh.size();
400   int nbsmesh = listSteelBarMesh.size();
401   int nbFiles = nbsmesh+nbcmesh+1;
402   // The number of input file is: 
403   //   (nb. of steelbar meshfile)
404   // + (1 or 0 concrete meshfile)
405   // + (1 padder input file)
406   jobParameters->in_files.length(nbFiles);
407   for (int i=0; i<nbcmesh; i++) {
408     jobParameters->in_files[i] = CORBA::string_dup(listConcreteMesh[i].file_name);
409   }
410   for (int i=0; i<nbsmesh; i++) {
411     jobParameters->in_files[nbcmesh+i] = CORBA::string_dup(listSteelBarMesh[i].file_name);
412   }
413   jobParameters->in_files[nbcmesh+nbsmesh] = CORBA::string_dup(dataFilename);
414   // Note that all these input files will be copied in the
415   // REMOTE_WORKDIR on the remote host. At this step, they should
416   // all exist, so we can check their presence on the local
417   // filesystem.
418   for (int i=0; i<nbFiles; i++) {
419     if ( fexists(jobParameters->in_files[i]) != true ) {
420       _lastErrorMessage = std::string("The input file ") + std::string(jobParameters->in_files[i]);
421       _lastErrorMessage+= std::string(" does not exists. Can't initialize the job");
422       LOG(_lastErrorMessage);
423       return JOBID_UNDEFINED;      
424     }
425   }
426
427   // Then, we have to specify the existence of an output filename. The
428   // path is supposed to be a path on the remote resource, i.e. where
429   // the job is executed.
430   jobParameters->out_files.length(1);
431   std::string outputfile_name = std::string(jobPaths->remote_workdir)+"/"+OUTPUTFILE;
432   jobParameters->out_files[0] = CORBA::string_dup(outputfile_name.c_str());
433
434   // CAUTION: the maximum duration has to be set with a format like "hh:mm"
435   //jobParameters->maximum_duration = CORBA::string_dup("01:00");
436   jobParameters->queue = CORBA::string_dup("");
437
438   // Setting resource and additional properties (if needed)
439   // The resource parameters can be initiated from scratch, for
440   // example by specifying the values in hard coding:
441   // >>>
442   //jobParameters->resource_required.name = CORBA::string_dup("localhost");
443   //jobParameters->resource_required.hostname = CORBA::string_dup("localhost");
444   //jobParameters->resource_required.mem_mb = 1024 * 10;
445   //jobParameters->resource_required.nb_proc = 1;
446   // <<<
447   // But it's better to initiate these parameters from a resource
448   // definition known by the resource manager. This ensures that the
449   // resource will be available:
450   //const char * resourceName = "localhost";
451   //const char * resourceName = "boulant@claui2p1";
452   //const char * resourceName = "nepal@nepal";
453   const char * resourceName = _configMap[configId].resname;
454   
455   Engines::ResourceDefinition * resourceDefinition;
456   try {
457     resourceDefinition = _resourcesManager->GetResourceDefinition(resourceName);
458   }
459   catch (const CORBA::SystemException& ex) {
460     _lastErrorMessage = std::string("We can not access the resource ") + std::string(resourceName);
461     _lastErrorMessage+= std::string("(check the file CatalogResource.xml)");
462     LOG(_lastErrorMessage);
463     return JOBID_UNDEFINED;
464   }
465   // CAUTION: This resource should have been defined in the
466   // CatalogResource.xml associated to the SALOME application.
467   //
468   // Then, the values can be used to initiate the resource parameters
469   // of the job:
470   jobParameters->resource_required.name     = CORBA::string_dup(resourceDefinition->name.in());
471   // CAUTION: the additional two following parameters MUST be
472   // specified explicitly, because they are not provided by the
473   // resource definition:
474   jobParameters->resource_required.mem_mb   = resourceDefinition->mem_mb;
475   jobParameters->resource_required.nb_proc  = resourceDefinition->nb_proc_per_node;
476   // CAUTION: the parameter mem_mb specifies the maximum memory value
477   // that could be allocated for executing the job. This takes into
478   // account not only the data that could be loaded by the batch
479   // process but also the linked dynamic library.
480   //
481   // A possible problem, for example in the case where you use the ssh
482   // emulation of a batch system, is to get an error message as below
483   // when libBatch try to run the ssh command:
484   //
485   // ## /usr/bin/ssh: error while loading shared libraries: libcrypto.so.0.9.8: failed
486   // ## to map segment from shared object: Cannot allocate memory
487   //
488   // In this example, the mem_mb was set to 1MB, value that is not
489   // sufficient to load the dynamic libraries linked to the ssh
490   // executable (libcrypto.so in the error message).
491   //
492   // So, even in the case of a simple test shell script, you should
493   // set this value at least to a standard threshold as 500MB
494   int jobId = JOBID_UNDEFINED;
495   try {
496     jobId = _salomeLauncher->createJob(jobParameters);
497     // We register the datetime tag of this job
498     _jobDateTimeMap[jobId]=jobDatetimeTag;
499     _jobPathsMap[jobId] = jobPaths;
500   }
501   catch (const SALOME::SALOME_Exception & ex) {
502     LOG("SALOME Exception at initialization step !" <<ex.details.text.in());
503     _lastErrorMessage = ex.details.text.in();
504     return JOBID_UNDEFINED;
505   }
506   catch (const CORBA::SystemException& ex) {
507     LOG("Receive SALOME System Exception: "<<ex);
508     LOG("Check SALOME servers...");
509     _lastErrorMessage = "Check the SALOME servers (or try to restart SALOME)";
510     return JOBID_UNDEFINED;
511   }
512   
513   endService("MeshJobManager_i::initialize");
514   return jobId;
515 }
516
517 /*! Submit the job execution and return true if submission is OK */
518 bool MeshJobManager_i::start(CORBA::Long jobId) {
519   beginService("MeshJobManager_i::start");
520
521   try {
522     _salomeLauncher->launchJob(jobId);
523   }
524   catch (const SALOME::SALOME_Exception & ex) {
525     LOG("SALOME Exception in launchjob !" <<ex.details.text.in());
526     _lastErrorMessage = ex.details.text.in();
527     return false;
528   }
529   catch (const CORBA::SystemException& ex) {
530     LOG("Receive SALOME System Exception: "<<ex);
531     LOG("Check SALOME servers...");
532     _lastErrorMessage = "Check the SALOME servers (or try to restart SALOME)";
533     return false;
534   }
535
536   endService("MeshJobManager_i::initialize");
537   return true;
538 }
539
540 /*! Request the launch manager for the state of the specified job */
541 char* MeshJobManager_i::getState(CORBA::Long jobId) {
542   beginService("MeshJobManager_i::getState");
543
544   std::string state;
545   try
546   {
547     state = _salomeLauncher->getJobState(jobId);
548   }
549   catch (const SALOME::SALOME_Exception & ex)
550   {
551     LOG("SALOME Exception in getJobState !");
552     _lastErrorMessage = ex.details.text.in();
553     state = ex.details.text;
554   }
555   catch (const CORBA::SystemException& ex)
556   {
557     LOG("Receive SALOME System Exception: " << ex);
558     state="SALOME System Exception - see logs";
559   }
560   LOG("jobId="<<ToString(jobId)<<" state="<<state);
561   endService("MeshJobManager_i::getState");
562   return CORBA::string_dup(state.c_str());
563 }
564
565 MESHJOB::MeshJobPaths * MeshJobManager_i::getPaths(CORBA::Long jobId) {
566
567   MESHJOB::MeshJobPaths * jobPaths = _jobPathsMap[jobId];
568   if ( jobPaths == NULL ) {
569     LOG("You request the working paths for an undefined job (jobId="<<ToString(jobId)<<")");
570     return NULL; // Maybe raise an exception?
571   }
572   return jobPaths;
573 }
574
575
576 MESHJOB::MeshJobResults * MeshJobManager_i::finalize(CORBA::Long jobId) {
577   beginService("MeshJobManager_i::getResults");
578   MESHJOB::MeshJobResults * result = new MESHJOB::MeshJobResults();
579
580   MESHJOB::MeshJobPaths * jobPaths = this->getPaths(jobId);
581   std::string local_resultdir(jobPaths->local_resultdir);
582   result->results_dirname = local_resultdir.c_str();  
583   try
584   {
585     _salomeLauncher->getJobResults(jobId, local_resultdir.c_str());
586  
587     // __BUG__: to prevent from a bug of the MED driver (SALOME
588     // 5.1.5), we change the basename of the output file to force the
589     // complete reloading of data by the med driver.
590     long jobDatetimeTag = _jobDateTimeMap[jobId];
591     std::string outputFileName = "output"+ToString(jobDatetimeTag)+".med";
592     rename((local_resultdir+"/"+OUTPUTFILE).c_str(), (local_resultdir+"/"+outputFileName).c_str());
593
594     result->outputmesh_filename = outputFileName.c_str();
595     
596     if ( fexists( (local_resultdir+"/"+outputFileName).c_str()  ) != true ) {
597       _lastErrorMessage = std::string("The result file ")+
598         std::string((local_resultdir+"/"+outputFileName).c_str())+
599         std::string(" has not been created.");
600       result->status = false;
601     }
602     else {
603       result->status = true;
604     }
605  }
606   catch (const SALOME::SALOME_Exception & ex)
607   {
608     _lastErrorMessage = ex.details.text.in();
609     LOG(_lastErrorMessage);
610     result->status = false;
611   }
612   catch (const CORBA::SystemException& ex)
613   {
614     _lastErrorMessage = "The SALOME launcher can not retrieve the result data";
615     LOG(_lastErrorMessage);
616     result->status = false;
617   }
618   endService("MeshJobManager_i::getResults");
619   return result;
620 }
621
622
623 /*! Clean all data associated to this job and remove the job from the launch manager */
624 bool MeshJobManager_i::clean(CORBA::Long jobId) {
625   beginService("MeshJobManager_i::clean");
626   
627   // __GBO__ WORK IN PROGRESS: we just clean the temporary local
628   // directories. The remote working directories are tag with the
629   // execution datetime and the we prevent the task from conflict
630   // with files of another task.
631   MESHJOB::MeshJobPaths * jobPaths = this->getPaths(jobId);
632   if ( jobPaths == NULL ) return false;
633
634   // WARN: !!!!!
635   // For safety reason (and prevent from bug that could erase the
636   // filesystem), we cancel the operation in the case where the
637   // directories to delete are not in the /tmp folder.
638   std::string shell_command("rm -rf ");
639   std::string inputdir(jobPaths->local_inputdir);
640   std::string resultdir(jobPaths->local_resultdir);
641   if ( !myStartsWith(inputdir,"/tmp/") )  {
642     LOG("WRN: The directory "<<inputdir<<" is not in /tmp. NO DELETE is done");
643   } else {
644     shell_command+=inputdir+" ";
645   }
646   if ( !myStartsWith(resultdir,"/tmp/"))  {
647     LOG("WRN: The directory "<<resultdir<<" is not in /tmp. NO DELETE is done");
648   } else {
649     shell_command+=resultdir;
650   }
651
652   LOG("DBG: clean shell command = "<<shell_command);
653
654   bool cleanOk = false;
655   int error = system(shell_command.c_str());
656   if (error == 0) cleanOk = true;
657
658   endService("MeshJobManager_i::clean");
659   return cleanOk;
660 }
661
662
663 std::vector<std::string> * MeshJobManager_i::_getResourceNames() {
664
665   //
666   // These part is just to control the available resources
667   //
668   Engines::ResourceParameters params;
669   KERNEL::getLifeCycleCORBA()->preSet(params);
670
671   Engines::ResourceList * resourceList = _resourcesManager->GetFittingResources(params);
672   Engines::ResourceDefinition * resourceDefinition = NULL;
673   LOG("### resource list:");
674   std::vector<std::string>* resourceNames = new std::vector<std::string>();
675   if (resourceList) {
676     for ( size_t i = 0; i < resourceList->length(); i++) {
677       const char* aResourceName = (*resourceList)[i];
678       resourceNames->push_back(std::string(aResourceName));
679       LOG("resource["<<i<<"] = "<<aResourceName);
680       resourceDefinition = _resourcesManager->GetResourceDefinition(aResourceName);
681       LOG("protocol["<<i<<"] = "<<resourceDefinition->protocol);
682       (void)resourceDefinition; // unused in release mode
683     }
684   }
685
686   // Note: a ResourceDefinition is used to create a batch configuration
687   // in the Launcher. This operation is done at Launcher startup from
688   // the configuration file CatalogResources.xml provided by the
689   // SALOME application.
690   // In the code instructions, you just have to choose a resource
691   // configuration by its name and then define the ResourceParameters
692   // that specify additional properties for a specific job submission
693   // (use the attribute resource_required of the JobParameters).
694
695   return resourceNames;
696 }
697
698 char* MeshJobManager_i::getLastErrorMessage() {
699   beginService("MeshJobManager_i::getState");
700   endService("MeshJobManager_i::getState");
701   return CORBA::string_dup(_lastErrorMessage.c_str());
702 }
703
704 //
705 // ==========================================================================
706 // Factory services
707 // ==========================================================================
708 //
709 extern "C"
710 {
711   MESHJOBMANAGERENGINE_EXPORT
712   PortableServer::ObjectId * MeshJobManagerEngine_factory( CORBA::ORB_ptr orb,
713                                                            PortableServer::POA_ptr poa,
714                                                            PortableServer::ObjectId * contId,
715                                                            const char *instanceName,
716                                                            const char *interfaceName)
717   {
718     LOG("PortableServer::ObjectId * MeshJobManagerEngine_factory()");
719     MeshJobManager_i * myEngine = new MeshJobManager_i(orb, poa, contId, instanceName, interfaceName);
720     return myEngine->getId() ;
721   }
722 }