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