]> SALOME platform Git repositories - modules/kernel.git/blob - src/Launcher/SALOME_Launcher.cxx
Salome HOME
Porting functionality on Win32 Platform
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "SALOME_Launcher.hxx"
21 #include "BatchTest.hxx"
22 #include "OpUtil.hxx"
23 #include "SALOME_ContainerManager.hxx"
24 #include "Utils_CorbaException.hxx"
25
26 #ifdef WIN32
27 # include <process.h>
28 #else
29 # include <unistd.h>
30 #endif
31 #include <sys/types.h>
32 #include <vector>
33
34 using namespace std;
35
36 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
37
38 //=============================================================================
39 /*! 
40  *  Constructor
41  *  \param orb
42  *  Define a CORBA single thread policy for the server, which avoid to deal
43  *  with non thread-safe usage like Change_Directory in SALOME naming service
44  */
45 //=============================================================================
46
47 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
48 {
49   MESSAGE("SALOME_Launcher constructor");
50   _NS = new SALOME_NamingService(orb);
51   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
52   _l.SetResourcesManager(_ResManager->GetImpl());
53   _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
54   _ResManager->_remove_ref();
55   _ContManager->_remove_ref();
56
57   _orb = CORBA::ORB::_duplicate(orb) ;
58   _poa = PortableServer::POA::_duplicate(poa) ;
59   PortableServer::ObjectId_var id = _poa->activate_object(this);
60   CORBA::Object_var obj = _poa->id_to_reference(id);
61   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
62
63   _NS->Register(refContMan,_LauncherNameInNS);
64   MESSAGE("SALOME_Launcher constructor end");
65 }
66
67 //=============================================================================
68 /*! 
69  * destructor
70  */
71 //=============================================================================
72
73 SALOME_Launcher::~SALOME_Launcher()
74 {
75   MESSAGE("destructor");
76   delete _NS;
77 }
78
79 //=============================================================================
80 /*! CORBA method:
81  *  shutdown all the containers, then the ContainerManager servant
82  */
83 //=============================================================================
84
85 void SALOME_Launcher::Shutdown()
86 {
87   MESSAGE("Shutdown");
88   _NS->Destroy_Name(_LauncherNameInNS);
89   _ContManager->Shutdown();
90   _ResManager->Shutdown();
91   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
92   _poa->deactivate_object(oid);
93   //_remove_ref();
94   if(!CORBA::is_nil(_orb))
95     _orb->shutdown(0);
96 }
97
98 //=============================================================================
99 /*! CORBA Method:
100  *  Returns the PID of the process
101  */
102 //=============================================================================
103 CORBA::Long SALOME_Launcher::getPID()
104 {
105   return 
106 #ifndef WIN32
107     (CORBA::Long)getpid();
108 #else
109     (CORBA::Long)_getpid();
110 #endif
111
112 }
113
114 //=============================================================================
115 /*! CORBA Method:
116  *  Submit a batch job on a cluster and returns the JobId
117  *  \param fileToExecute      : .py/.exe/.sh/... to execute on the batch cluster
118  *  \param filesToExport      : to export on the batch cluster
119  *  \param NumberOfProcessors : Number of processors needed on the batch cluster
120  *  \param params             : Constraints for the choice of the batch cluster
121  */
122 //=============================================================================
123 CORBA::Long SALOME_Launcher::submitSalomeJob( const char * fileToExecute ,
124                                               const Engines::FilesList& filesToExport ,
125                                               const Engines::FilesList& filesToImport ,
126                                               const Engines::BatchParameters& batch_params,
127                                               const Engines::MachineParameters& params)
128 {
129   MESSAGE("BEGIN OF SALOME_Launcher::submitSalomeJob");
130   CORBA::Long jobId;
131   
132   machineParams p;
133   p.hostname = params.hostname;
134   p.OS = params.OS;
135   p.nb_node = params.nb_node;
136   p.nb_proc_per_node = params.nb_proc_per_node;
137   p.cpu_clock = params.cpu_clock;
138   p.mem_mb = params.mem_mb;
139
140   batchParams bp;
141   bp.batch_directory = batch_params.batch_directory;
142   bp.expected_during_time = batch_params.expected_during_time;
143   bp.mem = batch_params.mem;
144   bp.nb_proc = batch_params.nb_proc;
145
146   vector<string> efl;
147   for(int i=0;i<filesToExport.length();i++)
148     efl.push_back(string(filesToExport[i]));
149
150   vector<string> ifl;
151   for(int i=0;i<filesToImport.length();i++)
152     ifl.push_back(string(filesToImport[i]));
153
154   try{
155     jobId = _l.submitSalomeJob(fileToExecute,efl,ifl,bp,p);
156   }
157   catch(const LauncherException &ex){
158     INFOS(ex.msg.c_str());
159     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
160   }
161   return jobId;
162 }
163
164 //=============================================================================
165 /*! CORBA Method:
166  *  the test batch configuration 
167  *  \param params             : The batch cluster
168  */
169 //=============================================================================
170 CORBA::Boolean 
171 SALOME_Launcher::testBatch(const Engines::MachineParameters& params)
172 {
173   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
174   CORBA::Boolean rtn = false;
175   try
176   {
177     // find a cluster matching the structure params
178     Engines::CompoList aCompoList ;
179     Engines::MachineList *aMachineList = _ResManager->GetFittingResources(params, aCompoList);
180     if (aMachineList->length() == 0)
181       throw SALOME_Exception("No resources have been found with your parameters");
182
183     const Engines::MachineParameters* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
184     string clustername(p->alias);
185     INFOS("Choose cluster" <<  clustername);
186     
187     BatchTest t(*p);
188     if (t.test()) 
189     {
190       rtn = true;
191     }
192   }
193   catch(const SALOME_Exception &ex){
194     INFOS(ex.what());
195     THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::INTERNAL_ERROR);
196   }
197   return rtn;
198 }
199
200 //=============================================================================
201 /*! CORBA Method:
202  *  Query a batch job on a cluster and returns the status of job
203  *  \param jobId              : identification of Salome job
204  *  \param params             : Constraints for the choice of the batch cluster
205  */
206 //=============================================================================
207 char* SALOME_Launcher::querySalomeJob( const CORBA::Long jobId, 
208                                        const Engines::MachineParameters& params)
209 {
210   string status;
211   machineParams p;
212   p.hostname = params.hostname;
213   p.OS = params.OS;
214   p.nb_node = params.nb_node;
215   p.nb_proc_per_node = params.nb_proc_per_node;
216   p.cpu_clock = params.cpu_clock;
217   p.mem_mb = params.mem_mb;
218
219   try{
220     status =  _l.querySalomeJob(jobId,p);
221   }
222   catch(const LauncherException &ex){
223     INFOS("Caught exception.");
224     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
225   }
226   return CORBA::string_dup(status.c_str());
227 }
228
229 //=============================================================================
230 /*! CORBA Method:
231  *  Delete a batch job on a cluster 
232  *  \param jobId              : identification of Salome job
233  *  \param params             : Constraints for the choice of the batch cluster
234  */
235 //=============================================================================
236 void SALOME_Launcher::deleteSalomeJob( const CORBA::Long jobId, 
237                                        const Engines::MachineParameters& params)
238 {
239   machineParams p;
240   p.hostname = params.hostname;
241   p.OS = params.OS;
242   p.nb_node = params.nb_node;
243   p.nb_proc_per_node = params.nb_proc_per_node;
244   p.cpu_clock = params.cpu_clock;
245   p.mem_mb = params.mem_mb;
246
247   try{
248     _l.deleteSalomeJob(jobId,p);
249   }
250   catch(const LauncherException &ex){
251     INFOS("Caught exception.");
252     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
253   }
254 }
255
256 //=============================================================================
257 /*! CORBA Method:
258  *  Get result files of job on a cluster
259  *  \param jobId              : identification of Salome job
260  *  \param params             : Constraints for the choice of the batch cluster
261  */
262 //=============================================================================
263 void SALOME_Launcher::getResultSalomeJob( const char *directory,
264                                           const CORBA::Long jobId, 
265                                           const Engines::MachineParameters& params)
266 {
267   machineParams p;
268   p.hostname = params.hostname;
269   p.OS = params.OS;
270   p.nb_node = params.nb_node;
271   p.nb_proc_per_node = params.nb_proc_per_node;
272   p.cpu_clock = params.cpu_clock;
273   p.mem_mb = params.mem_mb;
274
275   try{
276     _l.getResultSalomeJob( directory, jobId, p );
277   }
278   catch(const LauncherException &ex){
279     INFOS("Caught exception.");
280     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
281   }
282 }
283