]> SALOME platform Git repositories - modules/kernel.git/blob - src/Launcher/SALOME_Launcher.cxx
Salome HOME
Merge from V5_1_main 14/05/2010
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 //  Copyright (C) 2007-2010  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
23 #include "SALOME_Launcher.hxx"
24 #include "BatchTest.hxx"
25 #include "OpUtil.hxx"
26 #include "SALOME_ContainerManager.hxx"
27 #include "Utils_CorbaException.hxx"
28
29
30 #include "Launcher_Job_Command.hxx"
31 #include "Launcher_Job_YACSFile.hxx"
32 #include "Launcher_Job_PythonSALOME.hxx"
33
34 #ifdef WIN32
35 # include <process.h>
36 #else
37 # include <unistd.h>
38 #endif
39 #include <sys/types.h>
40 #include <vector>
41
42 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
43
44 //=============================================================================
45 /*! 
46  *  Constructor
47  *  \param orb
48  */
49 //=============================================================================
50 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
51 {
52   MESSAGE("SALOME_Launcher constructor");
53   _NS = new SALOME_NamingService(orb);
54   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
55   _l.SetResourcesManager(_ResManager->GetImpl());
56   _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
57   _ResManager->_remove_ref();
58   _ContManager->_remove_ref();
59
60   _orb = CORBA::ORB::_duplicate(orb) ;
61   _poa = PortableServer::POA::_duplicate(poa) ;
62   PortableServer::ObjectId_var id = _poa->activate_object(this);
63   CORBA::Object_var obj = _poa->id_to_reference(id);
64   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
65
66   _NS->Register(refContMan,_LauncherNameInNS);
67   MESSAGE("SALOME_Launcher constructor end");
68 }
69
70 //=============================================================================
71 /*! 
72  * destructor
73  */
74 //=============================================================================
75 SALOME_Launcher::~SALOME_Launcher()
76 {
77   MESSAGE("SALOME_Launcher destructor");
78   delete _NS;
79   MESSAGE("SALOME_Launcher destructor end");
80 }
81
82
83 CORBA::Long 
84 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
85 {
86   std::string job_type = job_parameters.job_type.in();
87   
88   if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome")
89   {
90     std::string message("SALOME_Launcher::createJob: bad job type: ");
91     message += job_type;
92     THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR);
93   }
94
95   Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
96
97   if (job_type == "command")
98     new_job = new Launcher::Job_Command();
99   else if (job_type == "yacs_file")
100     new_job = new Launcher::Job_YACSFile();
101   else if (job_type == "python_salome")
102     new_job = new Launcher::Job_PythonSALOME();
103  
104   // Directories
105   std::string work_directory = job_parameters.work_directory.in();
106   std::string local_directory = job_parameters.local_directory.in();
107   std::string result_directory = job_parameters.result_directory.in();
108   new_job->setWorkDirectory(work_directory);
109   new_job->setLocalDirectory(local_directory);
110   new_job->setResultDirectory(result_directory);
111
112   // Job File
113   std::string job_file = job_parameters.job_file.in();
114   try
115   {
116     new_job->setJobFile(job_file);
117   }
118   catch(const LauncherException &ex)
119   {
120     INFOS(ex.msg.c_str());
121     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
122   }
123
124   // Files
125   std::string env_file = job_parameters.env_file.in();
126   new_job->setEnvFile(env_file);
127   for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
128     new_job->add_in_file(job_parameters.in_files[i].in());
129   for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
130     new_job->add_out_file(job_parameters.out_files[i].in());
131   
132   // Expected During Time
133   try
134   {
135     std::string maximum_duration = job_parameters.maximum_duration.in();
136     new_job->setMaximumDuration(maximum_duration);
137   }
138   catch(const LauncherException &ex){
139     INFOS(ex.msg.c_str());
140     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
141   }
142
143   // Queue
144   std::string queue = job_parameters.queue.in();
145   if (queue != "")
146     new_job->setQueue(queue);
147    
148   // Resources requirements
149   try
150   {
151     resourceParams p;
152     p.name = job_parameters.resource_required.name;
153     p.hostname = job_parameters.resource_required.hostname;
154     p.OS = job_parameters.resource_required.OS;
155     p.nb_proc = job_parameters.resource_required.nb_proc;
156     p.nb_node = job_parameters.resource_required.nb_node;
157     p.nb_proc_per_node = job_parameters.resource_required.nb_proc_per_node;
158     p.cpu_clock = job_parameters.resource_required.cpu_clock;
159     p.mem_mb = job_parameters.resource_required.mem_mb;
160     new_job->setResourceRequiredParams(p);
161   }
162   catch(const LauncherException &ex){
163     INFOS(ex.msg.c_str());
164     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
165   }
166
167   try
168   {
169     _l.createJob(new_job);
170   }
171   catch(const LauncherException &ex)
172   {
173     INFOS(ex.msg.c_str());
174     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
175   }
176   return new_job->getNumber();
177 }
178
179 void 
180 SALOME_Launcher::launchJob(CORBA::Long job_id)
181 {
182   try
183   {
184     _l.launchJob(job_id);
185   }
186   catch(const LauncherException &ex)
187   {
188     INFOS(ex.msg.c_str());
189     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
190   }
191 }
192
193 char *
194 SALOME_Launcher::getJobState(CORBA::Long job_id)
195 {
196   std::string result;
197   try
198   {
199     result = _l.getJobState(job_id);
200   }
201   catch(const LauncherException &ex)
202   {
203     INFOS(ex.msg.c_str());
204     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
205   }
206   return CORBA::string_dup(result.c_str());
207 }
208
209 void
210 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
211 {
212   try
213   {
214     _l.getJobResults(job_id, directory);
215   }
216   catch(const LauncherException &ex)
217   {
218     INFOS(ex.msg.c_str());
219     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
220   }
221 }
222
223 void 
224 SALOME_Launcher::removeJob(CORBA::Long job_id)
225 {
226   try
227   {
228     _l.removeJob(job_id);
229   }
230   catch(const LauncherException &ex)
231   {
232     INFOS(ex.msg.c_str());
233     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
234   }
235 }
236
237 //=============================================================================
238 /*! CORBA Method:
239  *  Create a job in the launcher with a file
240  *  \param xmlExecuteFile     : .xml to parse that contains job description
241  *  \param clusterName        : machine choosed
242  */
243 //=============================================================================
244 CORBA::Long 
245 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
246                                    const char * clusterName)
247 {
248   CORBA::Long jobId;
249   try{
250     jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
251   }
252   catch(const LauncherException &ex){
253     INFOS(ex.msg.c_str());
254     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
255   }
256
257   return jobId;
258 }
259
260 //=============================================================================
261 /*! CORBA Method:
262  *  the test batch configuration 
263  *  \param params             : The batch cluster
264  */
265 //=============================================================================
266 CORBA::Boolean 
267 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
268 {
269   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
270   CORBA::Boolean rtn = false;
271   try
272   {
273     // find a cluster matching the structure params
274     Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(params);
275     if (aMachineList->length() == 0)
276       throw SALOME_Exception("No resources have been found with your parameters");
277
278     const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
279         std::string resource_name(p->name);
280     INFOS("Choose resource for test: " <<  resource_name);
281     
282     BatchTest t(*p);
283     if (t.test()) 
284     {
285       rtn = true;
286     }
287   }
288   catch(const LauncherException &ex){
289     INFOS(ex.msg.c_str());
290     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
291   }
292   return rtn;
293 }
294
295 //=============================================================================
296 /*! CORBA method:
297  *  shutdown all the containers, then the ContainerManager servant
298  */
299 //=============================================================================
300 void SALOME_Launcher::Shutdown()
301 {
302   MESSAGE("Shutdown");
303   _NS->Destroy_Name(_LauncherNameInNS);
304   _ContManager->Shutdown();
305   _ResManager->Shutdown();
306   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
307   _poa->deactivate_object(oid);
308   if(!CORBA::is_nil(_orb))
309     _orb->shutdown(0);
310 }
311
312 //=============================================================================
313 /*! CORBA Method:
314  *  Returns the PID of the process
315  */
316 //=============================================================================
317 CORBA::Long SALOME_Launcher::getPID()
318 {
319   return 
320 #ifndef WIN32
321     (CORBA::Long)getpid();
322 #else
323     (CORBA::Long)_getpid();
324 #endif
325 }