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