Salome HOME
merge from branch BR_V5_DEV
[modules/yacs.git] / src / Launcher / SALOME_Launcher.cxx
1 //  Copyright (C) 2007-2008  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 #include "SALOME_Launcher.hxx"
23 #include "BatchTest.hxx"
24 #include "OpUtil.hxx"
25 #include "SALOME_ContainerManager.hxx"
26 #include "Utils_CorbaException.hxx"
27
28 #ifdef WIN32
29 # include <process.h>
30 #else
31 # include <unistd.h>
32 #endif
33 #include <sys/types.h>
34 #include <vector>
35
36 using namespace std;
37
38 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
39
40 //=============================================================================
41 /*! 
42  *  Constructor
43  *  \param orb
44  *  Define a CORBA single thread policy for the server, which avoid to deal
45  *  with non thread-safe usage like Change_Directory in SALOME naming service
46  */
47 //=============================================================================
48
49 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
50 {
51   MESSAGE("SALOME_Launcher constructor");
52   _NS = new SALOME_NamingService(orb);
53   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
54   _l.SetResourcesManager(_ResManager->GetImpl());
55   _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
56   _ResManager->_remove_ref();
57   _ContManager->_remove_ref();
58
59   _orb = CORBA::ORB::_duplicate(orb) ;
60   _poa = PortableServer::POA::_duplicate(poa) ;
61   PortableServer::ObjectId_var id = _poa->activate_object(this);
62   CORBA::Object_var obj = _poa->id_to_reference(id);
63   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
64
65   _NS->Register(refContMan,_LauncherNameInNS);
66   MESSAGE("SALOME_Launcher constructor end");
67 }
68
69 //=============================================================================
70 /*! 
71  * destructor
72  */
73 //=============================================================================
74
75 SALOME_Launcher::~SALOME_Launcher()
76 {
77   MESSAGE("destructor");
78   delete _NS;
79 }
80
81 //=============================================================================
82 /*! CORBA method:
83  *  shutdown all the containers, then the ContainerManager servant
84  */
85 //=============================================================================
86
87 void SALOME_Launcher::Shutdown()
88 {
89   MESSAGE("Shutdown");
90   _NS->Destroy_Name(_LauncherNameInNS);
91   _ContManager->Shutdown();
92   _ResManager->Shutdown();
93   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
94   _poa->deactivate_object(oid);
95   //_remove_ref();
96   if(!CORBA::is_nil(_orb))
97     _orb->shutdown(0);
98 }
99
100 //=============================================================================
101 /*! CORBA Method:
102  *  Returns the PID of the process
103  */
104 //=============================================================================
105 CORBA::Long SALOME_Launcher::getPID()
106 {
107   return 
108 #ifndef WIN32
109     (CORBA::Long)getpid();
110 #else
111     (CORBA::Long)_getpid();
112 #endif
113
114 }
115
116 //=============================================================================
117 /*! CORBA Method:
118  *  Submit a batch job on a cluster and returns the JobId
119  *  \param xmlExecuteFile     : .xml to parse to execute on the batch cluster
120  *  \param clusterName        : cluster name
121  */
122 //=============================================================================
123 CORBA::Long SALOME_Launcher::submitJob(const char * xmlExecuteFile,
124                                        const char * clusterName)
125 {
126   CORBA::Long jobId;
127
128   try{
129     jobId = _l.submitJob(xmlExecuteFile,clusterName);
130   }
131   catch(const LauncherException &ex){
132     INFOS(ex.msg.c_str());
133     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
134   }
135   return jobId;
136 }
137
138 //=============================================================================
139 /*! CORBA Method:
140  *  Submit a batch job on a cluster and returns the JobId
141  *  \param fileToExecute      : .py/.exe/.sh/... to execute on the batch cluster
142  *  \param filesToExport      : to export on the batch cluster
143  *  \param NumberOfProcessors : Number of processors needed on the batch cluster
144  *  \param params             : Constraints for the choice of the batch cluster
145  */
146 //=============================================================================
147 CORBA::Long SALOME_Launcher::submitSalomeJob( const char * fileToExecute ,
148                                               const Engines::FilesList& filesToExport ,
149                                               const Engines::FilesList& filesToImport ,
150                                               const Engines::BatchParameters& batch_params,
151                                               const Engines::MachineParameters& params)
152 {
153   MESSAGE("BEGIN OF SALOME_Launcher::submitSalomeJob");
154   CORBA::Long jobId;
155   
156   machineParams p;
157   p.hostname = params.hostname;
158   p.OS = params.OS;
159   p.nb_node = params.nb_node;
160   p.nb_proc_per_node = params.nb_proc_per_node;
161   p.cpu_clock = params.cpu_clock;
162   p.mem_mb = params.mem_mb;
163
164   batchParams bp;
165   bp.batch_directory = batch_params.batch_directory;
166   bp.expected_during_time = batch_params.expected_during_time;
167   bp.mem = batch_params.mem;
168   bp.nb_proc = batch_params.nb_proc;
169
170   vector<string> efl;
171   for(int i=0;i<filesToExport.length();i++)
172     efl.push_back(string(filesToExport[i]));
173
174   vector<string> ifl;
175   for(int i=0;i<filesToImport.length();i++)
176     ifl.push_back(string(filesToImport[i]));
177
178   try{
179     jobId = _l.submitSalomeJob(fileToExecute,efl,ifl,bp,p);
180   }
181   catch(const LauncherException &ex){
182     INFOS(ex.msg.c_str());
183     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
184   }
185   return jobId;
186 }
187
188 //=============================================================================
189 /*! CORBA Method:
190  *  the test batch configuration 
191  *  \param params             : The batch cluster
192  */
193 //=============================================================================
194 CORBA::Boolean 
195 SALOME_Launcher::testBatch(const Engines::MachineParameters& params)
196 {
197   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
198   CORBA::Boolean rtn = false;
199   try
200   {
201     // find a cluster matching the structure params
202     Engines::CompoList aCompoList ;
203     Engines::MachineList *aMachineList = _ResManager->GetFittingResources(params, aCompoList);
204     if (aMachineList->length() == 0)
205       throw SALOME_Exception("No resources have been found with your parameters");
206
207     const Engines::MachineParameters* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
208     string clustername(p->alias);
209     INFOS("Choose cluster" <<  clustername);
210     
211     BatchTest t(*p);
212     if (t.test()) 
213     {
214       rtn = true;
215     }
216   }
217   catch(const LauncherException &ex){
218     INFOS(ex.msg.c_str());
219     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
220   }
221   return rtn;
222 }
223
224 //=============================================================================
225 /*! CORBA Method:
226  *  Query a batch job on a cluster and returns the status of job
227  *  \param jobId              : identification of Salome job
228  *  \param params             : Constraints for the choice of the batch cluster
229  */
230 //=============================================================================
231 char* SALOME_Launcher::queryJob( const CORBA::Long jobId, 
232                                  const Engines::MachineParameters& params)
233 {
234   string status;
235   machineParams p;
236   p.hostname = params.hostname;
237   p.OS = params.OS;
238   p.nb_node = params.nb_node;
239   p.nb_proc_per_node = params.nb_proc_per_node;
240   p.cpu_clock = params.cpu_clock;
241   p.mem_mb = params.mem_mb;
242
243   try{
244     status =  _l.queryJob(jobId,p);
245   }
246   catch(const LauncherException &ex){
247     INFOS(ex.msg.c_str());
248     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
249   }
250   return CORBA::string_dup(status.c_str());
251 }
252
253 //=============================================================================
254 /*! CORBA Method:
255  *  Delete a batch job on a cluster 
256  *  \param jobId              : identification of Salome job
257  *  \param params             : Constraints for the choice of the batch cluster
258  */
259 //=============================================================================
260 void SALOME_Launcher::deleteJob( const CORBA::Long jobId, 
261                                  const Engines::MachineParameters& params)
262 {
263   machineParams p;
264   p.hostname = params.hostname;
265   p.OS = params.OS;
266   p.nb_node = params.nb_node;
267   p.nb_proc_per_node = params.nb_proc_per_node;
268   p.cpu_clock = params.cpu_clock;
269   p.mem_mb = params.mem_mb;
270
271   try{
272     _l.deleteJob(jobId,p);
273   }
274   catch(const LauncherException &ex){
275     INFOS("Caught exception.");
276     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
277   }
278 }
279
280 //=============================================================================
281 /*! CORBA Method:
282  *  Get result files of job on a cluster
283  *  \param jobId              : identification of Salome job
284  *  \param params             : Constraints for the choice of the batch cluster
285  */
286 //=============================================================================
287 void SALOME_Launcher::getResultsJob( const char *directory,
288                                      const CORBA::Long jobId, 
289                                      const Engines::MachineParameters& params)
290 {
291   machineParams p;
292   p.hostname = params.hostname;
293   p.OS = params.OS;
294   p.nb_node = params.nb_node;
295   p.nb_proc_per_node = params.nb_proc_per_node;
296   p.cpu_clock = params.cpu_clock;
297   p.mem_mb = params.mem_mb;
298
299   try{
300     _l.getResultsJob( directory, jobId, p );
301   }
302   catch(const LauncherException &ex){
303     INFOS("Caught exception.");
304     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
305   }
306 }
307