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