Salome HOME
Base implementation of Notebook
[modules/kernel.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   if(!CORBA::is_nil(_orb))
96     _orb->shutdown(0);
97 }
98
99 //=============================================================================
100 /*! CORBA Method:
101  *  Returns the PID of the process
102  */
103 //=============================================================================
104 CORBA::Long SALOME_Launcher::getPID()
105 {
106   return 
107 #ifndef WIN32
108     (CORBA::Long)getpid();
109 #else
110     (CORBA::Long)_getpid();
111 #endif
112
113 }
114
115 //=============================================================================
116 /*! CORBA Method:
117  *  Submit a batch job on a cluster and returns the JobId
118  *  \param xmlExecuteFile     : .xml to parse to execute on the batch cluster
119  *  \param clusterName        : cluster name
120  */
121 //=============================================================================
122 CORBA::Long SALOME_Launcher::submitJob(const char * xmlExecuteFile,
123                                        const char * clusterName)
124 {
125   CORBA::Long jobId;
126
127   try{
128     jobId = _l.submitJob(xmlExecuteFile,clusterName);
129   }
130   catch(const LauncherException &ex){
131     INFOS(ex.msg.c_str());
132     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
133   }
134   return jobId;
135 }
136
137 //=============================================================================
138 /*! CORBA Method:
139  *  Submit a batch job on a cluster and returns the JobId
140  *  \param fileToExecute      : .py/.exe/.sh/... to execute on the batch cluster
141  *  \param filesToExport      : to export on the batch cluster
142  *  \param NumberOfProcessors : Number of processors needed on the batch cluster
143  *  \param params             : Constraints for the choice of the batch cluster
144  */
145 //=============================================================================
146 CORBA::Long SALOME_Launcher::submitSalomeJob( const char * fileToExecute ,
147                                               const Engines::FilesList& filesToExport ,
148                                               const Engines::FilesList& filesToImport ,
149                                               const Engines::BatchParameters& batch_params,
150                                               const Engines::MachineParameters& params)
151 {
152   MESSAGE("BEGIN OF SALOME_Launcher::submitSalomeJob");
153   CORBA::Long jobId;
154   
155   machineParams p;
156   p.hostname = params.hostname;
157   p.OS = params.OS;
158   p.nb_node = params.nb_node;
159   p.nb_proc_per_node = params.nb_proc_per_node;
160   p.cpu_clock = params.cpu_clock;
161   p.mem_mb = params.mem_mb;
162
163   batchParams bp;
164   bp.batch_directory = batch_params.batch_directory;
165   bp.expected_during_time = batch_params.expected_during_time;
166   bp.mem = batch_params.mem;
167   bp.nb_proc = batch_params.nb_proc;
168
169   vector<string> efl;
170   for(int i=0;i<filesToExport.length();i++)
171     efl.push_back(string(filesToExport[i]));
172
173   vector<string> ifl;
174   for(int i=0;i<filesToImport.length();i++)
175     ifl.push_back(string(filesToImport[i]));
176
177   try{
178     jobId = _l.submitSalomeJob(fileToExecute,efl,ifl,bp,p);
179   }
180   catch(const LauncherException &ex){
181     INFOS(ex.msg.c_str());
182     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
183   }
184   return jobId;
185 }
186
187 //=============================================================================
188 /*! CORBA Method:
189  *  the test batch configuration 
190  *  \param params             : The batch cluster
191  */
192 //=============================================================================
193 CORBA::Boolean 
194 SALOME_Launcher::testBatch(const Engines::MachineParameters& params)
195 {
196   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
197   CORBA::Boolean rtn = false;
198   try
199   {
200     // find a cluster matching the structure params
201     Engines::MachineList *aMachineList = _ResManager->GetFittingResources(params);
202     if (aMachineList->length() == 0)
203       throw SALOME_Exception("No resources have been found with your parameters");
204
205     const Engines::MachineDefinition* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
206     string clustername(p->alias);
207     INFOS("Choose cluster" <<  clustername);
208     
209     BatchTest t(*p);
210     if (t.test()) 
211     {
212       rtn = true;
213     }
214   }
215   catch(const LauncherException &ex){
216     INFOS(ex.msg.c_str());
217     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
218   }
219   return rtn;
220 }
221
222 //=============================================================================
223 /*! CORBA Method:
224  *  Query a batch job on a cluster and returns the status of job
225  *  \param jobId              : identification of Salome job
226  *  \param params             : Constraints for the choice of the batch cluster
227  */
228 //=============================================================================
229 char* SALOME_Launcher::queryJob( const CORBA::Long jobId, 
230                                  const Engines::MachineParameters& params)
231 {
232   string status;
233   machineParams p;
234   p.hostname = params.hostname;
235   p.OS = params.OS;
236   p.nb_node = params.nb_node;
237   p.nb_proc_per_node = params.nb_proc_per_node;
238   p.cpu_clock = params.cpu_clock;
239   p.mem_mb = params.mem_mb;
240
241   try{
242     status =  _l.queryJob(jobId,p);
243   }
244   catch(const LauncherException &ex){
245     INFOS(ex.msg.c_str());
246     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
247   }
248   return CORBA::string_dup(status.c_str());
249 }
250
251 //=============================================================================
252 /*! CORBA Method:
253  *  Delete a batch job on a cluster 
254  *  \param jobId              : identification of Salome job
255  *  \param params             : Constraints for the choice of the batch cluster
256  */
257 //=============================================================================
258 void SALOME_Launcher::deleteJob( const CORBA::Long jobId, 
259                                  const Engines::MachineParameters& params)
260 {
261   machineParams p;
262   p.hostname = params.hostname;
263   p.OS = params.OS;
264   p.nb_node = params.nb_node;
265   p.nb_proc_per_node = params.nb_proc_per_node;
266   p.cpu_clock = params.cpu_clock;
267   p.mem_mb = params.mem_mb;
268
269   try{
270     _l.deleteJob(jobId,p);
271   }
272   catch(const LauncherException &ex){
273     INFOS("Caught exception.");
274     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
275   }
276 }
277
278 //=============================================================================
279 /*! CORBA Method:
280  *  Get result files of job on a cluster
281  *  \param jobId              : identification of Salome job
282  *  \param params             : Constraints for the choice of the batch cluster
283  */
284 //=============================================================================
285 void SALOME_Launcher::getResultsJob( const char *directory,
286                                      const CORBA::Long jobId, 
287                                      const Engines::MachineParameters& params)
288 {
289   machineParams p;
290   p.hostname = params.hostname;
291   p.OS = params.OS;
292   p.nb_node = params.nb_node;
293   p.nb_proc_per_node = params.nb_proc_per_node;
294   p.cpu_clock = params.cpu_clock;
295   p.mem_mb = params.mem_mb;
296
297   try{
298     _l.getResultsJob( directory, jobId, p );
299   }
300   catch(const LauncherException &ex){
301     INFOS("Caught exception.");
302     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
303   }
304 }
305