1 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "BL_SALOMEServices.hxx"
24 operator<<(std::ostream & os, const CORBA::Exception & e)
28 CORBA::TypeCode_var tc = tmp.type();
29 const char * p = tc->name ();
37 BL::SALOMEServices::SALOMEServices()
39 int nbargs = 0; char **args = 0;
40 _orb = CORBA::ORB_init (nbargs, args);
41 _salome_naming_service = NULL;
47 BL::SALOMEServices::~SALOMEServices() {}
50 BL::SALOMEServices::end()
52 if (!CORBA::is_nil(_salome_launcher))
53 _salome_launcher->removeObserver(_this());
54 if (_salome_naming_service)
55 delete _salome_naming_service;
61 BL::SALOMEServices::initNS()
63 bool return_value = true;
64 _salome_naming_service = new SALOME_NamingService(_orb);
65 _lcc = new SALOME_LifeCycleCORBA(_salome_naming_service);
66 CORBA::Object_var obj = _salome_naming_service->Resolve("/SalomeLauncher");
67 _salome_launcher = Engines::SalomeLauncher::_narrow(obj);
69 if (CORBA::is_nil(_salome_launcher))
71 DEBMSG("SALOME Launcher is not reachable!")
74 _salome_launcher->addObserver(_this());
75 _remove_ref(); // POA will automatically destroy the object
77 obj = _salome_naming_service->Resolve("/ResourcesManager");
78 _resources_manager = Engines::ResourcesManager::_narrow(obj);
79 if (CORBA::is_nil(_resources_manager))
81 DEBMSG("SALOME Resource Manager is not reachable !");
85 _state = return_value;
89 std::list<std::string>
90 BL::SALOMEServices::getResourceList(bool batch_only)
92 std::list<std::string> resource_list;
96 Engines::ResourceParameters params;
98 params.can_launch_batch_jobs = batch_only;
99 Engines::ResourceList * resourceList = NULL;
102 resourceList = _resources_manager->GetFittingResources(params);
104 catch (const SALOME::SALOME_Exception & ex)
106 DEBMSG("SALOME Exception in addResource ! " << ex.details.text.in());
108 catch (const CORBA::SystemException& ex)
110 DEBMSG("Receive SALOME System Exception: " << ex);
111 DEBMSG("Check SALOME servers...");
115 for (int i = 0; i < resourceList->length(); i++)
117 const char* aResource = (*resourceList)[i];
118 resource_list.push_back(aResource);
123 return resource_list;
127 BL::SALOMEServices::getResourceDescr(const std::string& name)
129 Engines::ResourceDefinition * resource_definition = NULL;
130 BL::ResourceDescr resource_descr;
134 resource_definition = _resources_manager-> GetResourceDefinition(name.c_str());
136 catch (const SALOME::SALOME_Exception & ex)
138 DEBMSG("SALOME Exception in addResource ! " << ex.details.text.in());
140 catch (const CORBA::SystemException& ex)
142 DEBMSG("Receive SALOME System Exception: " << ex);
143 DEBMSG("Check SALOME servers...");
146 if(resource_definition)
148 resource_descr.name = resource_definition->name.in();
149 resource_descr.hostname = resource_definition->hostname.in();
150 resource_descr.protocol = resource_definition->protocol.in();
151 resource_descr.username = resource_definition->username.in();
152 resource_descr.applipath = resource_definition->applipath.in();
153 for (int i = 0; i < resource_definition->componentList.length(); i++)
155 resource_descr.componentList.push_back(resource_definition->componentList[i].in());
158 resource_descr.OS = resource_definition->OS.in();
159 resource_descr.mem_mb = resource_definition->mem_mb;
160 resource_descr.cpu_clock = resource_definition->cpu_clock;
161 resource_descr.nb_node = resource_definition->nb_node;
162 resource_descr.nb_proc_per_node = resource_definition->nb_proc_per_node;
163 resource_descr.batch = resource_definition->batch.in();
164 resource_descr.mpiImpl = resource_definition->mpiImpl.in();
165 resource_descr.iprotocol = resource_definition->iprotocol.in();
166 resource_descr.can_launch_batch_jobs = resource_definition->can_launch_batch_jobs;
167 resource_descr.can_run_containers = resource_definition->can_run_containers;
168 resource_descr.working_directory = resource_definition->working_directory.in();
170 delete resource_definition;
172 return resource_descr;
176 BL::SALOMEServices::addResource(BL::ResourceDescr & new_resource)
178 Engines::ResourceDefinition_var resource_definition = new Engines::ResourceDefinition;
180 resource_definition->name = CORBA::string_dup(new_resource.name.c_str());
181 resource_definition->hostname = CORBA::string_dup(new_resource.hostname.c_str());
182 if (new_resource.batch == "none")
183 resource_definition->type = CORBA::string_dup("single_machine");
185 resource_definition->type = CORBA::string_dup("cluster");
186 resource_definition->protocol = CORBA::string_dup(new_resource.protocol.c_str());
187 resource_definition->username = CORBA::string_dup(new_resource.username.c_str());
188 resource_definition->applipath = CORBA::string_dup(new_resource.applipath.c_str());
191 std::list<std::string>::iterator it = new_resource.componentList.begin();
192 resource_definition->componentList.length(new_resource.componentList.size());
193 for(; it != new_resource.componentList.end(); it++)
195 resource_definition->componentList[i] = CORBA::string_dup((*it).c_str());
199 resource_definition->OS = CORBA::string_dup(new_resource.OS.c_str());
200 resource_definition->mem_mb = new_resource.mem_mb;
201 resource_definition->cpu_clock = new_resource.cpu_clock;
202 resource_definition->nb_node = new_resource.nb_node;
203 resource_definition->nb_proc_per_node = new_resource.nb_proc_per_node;
204 resource_definition->batch = CORBA::string_dup(new_resource.batch.c_str());
205 resource_definition->mpiImpl = CORBA::string_dup(new_resource.mpiImpl.c_str());
206 resource_definition->iprotocol = CORBA::string_dup(new_resource.iprotocol.c_str());
207 resource_definition->can_launch_batch_jobs = new_resource.can_launch_batch_jobs;
208 resource_definition->can_run_containers = new_resource.can_run_containers;
209 resource_definition->working_directory = CORBA::string_dup(new_resource.working_directory.c_str());
213 _resources_manager->AddResource(resource_definition, true, "");
215 catch (const SALOME::SALOME_Exception & ex)
217 DEBMSG("SALOME Exception in addResource ! " << ex.details.text.in());
218 throw(BL::Exception(ex.details.text.in()));
220 catch (const CORBA::SystemException& ex)
222 DEBMSG("Receive SALOME System Exception: " << ex);
223 DEBMSG("Check SALOME servers...");
224 throw(BL::Exception("SALOME System Exception"));
229 BL::SALOMEServices::removeResource(const std::string & name)
233 _resources_manager->RemoveResource(name.c_str(), true, "");
235 catch (const SALOME::SALOME_Exception & ex)
237 DEBMSG("SALOME Exception in removeResource ! " << ex.details.text.in());
239 catch (const CORBA::SystemException& ex)
241 DEBMSG("Receive SALOME System Exception: " << ex);
242 DEBMSG("Check SALOME servers...");
247 BL::SALOMEServices::create_job(BL::Job * job)
249 DEBMSG("Begin of create_job");
250 std::string ret = "";
251 Engines::JobParameters_var job_parameters = new Engines::JobParameters;
254 if (job->getType() == BL::Job::COMMAND)
256 job_parameters->job_type = CORBA::string_dup("command");
258 else if (job->getType() == BL::Job::YACS_SCHEMA)
260 job_parameters->job_type = CORBA::string_dup("yacs_file");
262 else if (job->getType() == BL::Job::PYTHON_SALOME)
264 job_parameters->job_type = CORBA::string_dup("python_salome");
267 // Specific parameters
268 if (job->getType() == BL::Job::YACS_SCHEMA)
270 if (job->getDumpYACSState() > 0)
272 job_parameters->specific_parameters.length(job_parameters->specific_parameters.length() + 1);
273 std::ostringstream oss;
274 oss << job->getDumpYACSState();
275 Engines::Parameter_var new_parameter = new Engines::Parameter;
276 new_parameter->name = CORBA::string_dup("EnableDumpYACS");
277 new_parameter->value = CORBA::string_dup(oss.str().c_str());
278 job_parameters->specific_parameters[job_parameters->specific_parameters.length() - 1] = new_parameter;
281 if (job->getLoadLevelerJobType() != "")
283 job_parameters->specific_parameters.length(job_parameters->specific_parameters.length() + 1);
284 Engines::Parameter_var new_parameter = new Engines::Parameter;
285 new_parameter->name = CORBA::string_dup("LoalLevelerJobType");
286 new_parameter->value = CORBA::string_dup(job->getLoadLevelerJobType().c_str());
287 job_parameters->specific_parameters[job_parameters->specific_parameters.length() - 1] = new_parameter;
291 job_parameters->job_name = CORBA::string_dup(job->getName().c_str());
292 job_parameters->job_file = CORBA::string_dup(job->getJobFile().c_str());
293 job_parameters->env_file = CORBA::string_dup(job->getEnvFile().c_str());
294 BL::Job::FilesParam files = job->getFilesParameters();
295 std::list<std::string>::iterator it;
299 job_parameters->in_files.length(files.input_files_list.size());
300 for (it = files.input_files_list.begin() ; it != files.input_files_list.end(); it++)
302 job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
306 job_parameters->out_files.length(files.output_files_list.size());
307 for (it = files.output_files_list.begin() ; it != files.output_files_list.end(); it++)
309 job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
312 job_parameters->local_directory = CORBA::string_dup("");
313 job_parameters->result_directory = CORBA::string_dup(files.result_directory.c_str());
315 BL::Job::BatchParam cpp_batch_params = job->getBatchParameters();
316 job_parameters->work_directory = CORBA::string_dup(cpp_batch_params.batch_directory.c_str());
319 job_parameters->maximum_duration = CORBA::string_dup(cpp_batch_params.maximum_duration.c_str());
320 job_parameters->resource_required.name = CORBA::string_dup(job->getResource().c_str());
321 job_parameters->resource_required.nb_proc = cpp_batch_params.nb_proc;
322 job_parameters->queue = CORBA::string_dup(job->getBatchQueue().c_str());
323 job_parameters->exclusive = cpp_batch_params.exclusive;
327 std::string ram = cpp_batch_params.expected_memory.substr(0,cpp_batch_params.expected_memory.size()-2);
328 std::istringstream iss(ram);
330 std::string unity = cpp_batch_params.expected_memory.substr(cpp_batch_params.expected_memory.size()-2, 2);
331 if((unity.find("gb") != std::string::npos))
332 memory = memory * 1024;
333 job_parameters->resource_required.mem_mb = memory;
335 // Parameters for COORM
336 job_parameters->launcher_file = CORBA::string_dup(cpp_batch_params.launcher_file.c_str());
337 job_parameters->launcher_args = CORBA::string_dup(cpp_batch_params.launcher_args.c_str());
342 int job_id = _salome_launcher->createJob(job_parameters);
343 job->setSalomeLauncherId(job_id);
345 catch (const SALOME::SALOME_Exception & ex)
347 DEBMSG("SALOME Exception in createJob !");
348 ret = ex.details.text.in();
350 catch (const CORBA::SystemException& ex)
352 DEBMSG("Receive SALOME System Exception: " << ex);
353 DEBMSG("Check SALOME servers...");
354 ret = "SALOME System Exception - see logs";
360 BL::SALOMEServices::start_job(BL::Job * job)
362 std::string ret = "";
366 _salome_launcher->launchJob(job->getSalomeLauncherId());
368 catch (const SALOME::SALOME_Exception & ex)
370 DEBMSG("SALOME Exception in launchJob !");
371 ret = ex.details.text.in();
373 catch (const CORBA::SystemException& ex)
375 DEBMSG("Receive SALOME System Exception: " << ex);
376 DEBMSG("Check SALOME servers...");
377 ret = "SALOME System Exception - see logs";
383 BL::SALOMEServices::refresh_job(BL::Job * job)
385 std::string ret = "";
390 CORBA::String_var result = _salome_launcher->getJobState(job->getSalomeLauncherId());
393 catch (const SALOME::SALOME_Exception & ex)
395 DEBMSG("SALOME Exception in getJobState !");
396 ret = ex.details.text.in();
398 catch (const CORBA::SystemException& ex)
400 DEBMSG("Receive SALOME System Exception: " << ex);
401 DEBMSG("Check SALOME servers...");
402 ret = "SALOME System Exception - see logs";
408 BL::SALOMEServices::delete_job(BL::Job * job)
410 std::string ret = "";
414 _salome_launcher->removeJob(job->getSalomeLauncherId());
416 catch (const SALOME::SALOME_Exception & ex)
418 DEBMSG("SALOME Exception in removeJob !");
419 ret = ex.details.text.in();
421 catch (const CORBA::SystemException& ex)
423 DEBMSG("Receive SALOME System Exception: " << ex);
424 DEBMSG("Check SALOME servers...");
425 ret = "SALOME System Exception - see logs";
431 BL::SALOMEServices::stop_job(BL::Job * job)
433 std::string ret = "";
436 _salome_launcher->stopJob(job->getSalomeLauncherId());
438 catch (const SALOME::SALOME_Exception & ex)
440 DEBMSG("SALOME Exception in stopJob !");
441 ret = ex.details.text.in();
443 catch (const CORBA::SystemException& ex)
445 DEBMSG("Receive SALOME System Exception: " << ex);
446 DEBMSG("Check SALOME servers...");
447 ret = "SALOME System Exception - see logs";
453 BL::SALOMEServices::get_results_job(BL::Job * job)
455 std::string ret = "";
457 BL::Job::FilesParam files = job->getFilesParameters();
458 CORBA::String_var directory = CORBA::string_dup(files.result_directory.c_str());
463 _salome_launcher->getJobResults(job->getSalomeLauncherId(), directory);
465 catch (const SALOME::SALOME_Exception & ex)
467 DEBMSG("SALOME Exception in refresh_job !");
468 ret = ex.details.text.in();
470 catch (const CORBA::SystemException& ex)
472 DEBMSG("Receive SALOME System Exception: " << ex);
473 DEBMSG("Check SALOME servers...");
474 ret = "SALOME System Exception - see logs";
479 // Get names or ids of hosts assigned to the job
481 BL::SALOMEServices::get_assigned_hostnames(BL::Job * job)
483 std::string ret = "";
487 CORBA::String_var result = _salome_launcher->getAssignedHostnames(job->getSalomeLauncherId());
490 catch (const SALOME::SALOME_Exception & ex)
492 DEBMSG("SALOME Exception in get_assigned_hostnames !");
493 ret = ex.details.text.in();
495 catch (const CORBA::SystemException& ex)
497 DEBMSG("Receive SALOME System Exception: " << ex);
498 DEBMSG("Check SALOME servers...");
499 ret = "SALOME System Exception - see logs";
505 BL::SALOMEServices::save_jobs(const std::string & file_name)
507 CORBA::String_var file = CORBA::string_dup(file_name.c_str());
508 std::string ret = "";
511 _salome_launcher->saveJobs(file);
513 catch (const SALOME::SALOME_Exception & ex)
515 DEBMSG("SALOME Exception in saveJobs !");
516 ret = ex.details.text.in();
518 catch (const CORBA::SystemException& ex)
520 DEBMSG("Receive CORBA System Exception: " << ex);
521 DEBMSG("Check SALOME servers...");
522 ret = "CORBA System Exception - see SALOME logs";
528 BL::SALOMEServices::load_jobs(const std::string & file_name)
530 CORBA::String_var file = CORBA::string_dup(file_name.c_str());
531 std::string ret = "";
534 _salome_launcher->loadJobs(file);
536 catch (const SALOME::SALOME_Exception & ex)
538 DEBMSG("SALOME Exception in loadJobs !");
539 ret = ex.details.text.in();
541 catch (const CORBA::SystemException& ex)
543 DEBMSG("Receive CORBA System Exception: " << ex);
544 DEBMSG("Check SALOME servers...");
545 ret = "CORBA System Exception - see SALOME logs";
551 BL::SALOMEServices::notify(const char* event_name, const char * event_data)
553 DEBMSG("Launcher event received " << event_name << " " << event_data);
555 std::string event(event_name);
556 std::string data(event_data);
558 if (event == "SAVE_JOBS")
560 _manager->launcher_event_save_jobs(data);
562 else if (event == "LOAD_JOBS")
564 _manager->launcher_event_load_jobs(data);
566 else if (event == "NEW_JOB")
568 _manager->launcher_event_new_job(data);
570 else if (event == "REMOVE_JOB")
572 _manager->launcher_event_remove_job(data);
574 else if (event == "UPDATE_JOB_STATE")
576 _manager->launcher_event_update_job_state(data);
580 DEBMSG("Unkown launcher event received");
585 BL::SALOMEServices::get_new_job(int job_number)
587 DEBMSG("Start of BL::SALOMEServices::get_new_job");
588 BL::Job * job_return = NULL;
589 Engines::JobParameters * job_parameters = NULL;
592 job_parameters = _salome_launcher->getJobParameters(job_number);
594 catch (const SALOME::SALOME_Exception & ex)
596 DEBMSG("SALOME Exception in saveJobs !");
598 catch (const CORBA::SystemException& ex)
600 DEBMSG("Receive CORBA System Exception: " << ex);
601 DEBMSG("Check SALOME servers...");
606 job_return = new BL::Job();
607 job_return->setSalomeLauncherId(job_number);
609 job_return->setName(job_parameters->job_name.in());
610 job_return->setType(job_parameters->job_type.in());
611 job_return->setJobFile(job_parameters->job_file.in());
612 job_return->setEnvFile(job_parameters->env_file.in());
613 job_return->setBatchQueue(job_parameters->queue.in());
615 BL::Job::FilesParam param;
616 param.result_directory = job_parameters->result_directory.in();
617 for (CORBA::ULong i = 0; i < job_parameters->in_files.length(); i++)
618 param.input_files_list.push_back(job_parameters->in_files[i].in());
619 for (CORBA::ULong i = 0; i < job_parameters->out_files.length(); i++)
620 param.output_files_list.push_back(job_parameters->out_files[i].in());
621 job_return->setFilesParameters(param);
623 BL::Job::BatchParam batch_param;
624 batch_param.batch_directory = job_parameters->work_directory.in();
625 batch_param.maximum_duration = job_parameters->maximum_duration.in();
626 batch_param.nb_proc = job_parameters->resource_required.nb_proc;
627 batch_param.exclusive = job_parameters->exclusive;
628 std::ostringstream mem_stream;
629 mem_stream << job_parameters->resource_required.mem_mb << "mb";
630 batch_param.expected_memory = mem_stream.str();
632 // Parameters for COORM
633 batch_param.launcher_file = job_parameters->launcher_file.in();
634 batch_param.launcher_args = job_parameters->launcher_args.in();
636 job_return->setBatchParameters(batch_param);
638 job_return->setResource(job_parameters->resource_required.name.in());
640 // Specific parameters
641 for (CORBA::ULong i = 0; i < job_parameters->specific_parameters.length(); i++)
643 if (std::string(job_parameters->specific_parameters[i].name.in()) == "EnableDumpYACS")
645 std::string user_value = job_parameters->specific_parameters[i].value.in();
646 std::istringstream iss(user_value);
649 job_return->setDumpYACSState(value);
651 if (std::string(job_parameters->specific_parameters[i].name.in()) == "LoalLevelerJobType")
653 std::string user_value = job_parameters->specific_parameters[i].value.in();
654 job_return->setLoadLevelerJobType(user_value);
659 std::string result_job = job_return->setStringState(refresh_job(job_return));
660 if (result_job != "RefreshError") {}
663 // Error in getting state
664 DEBMSG("Error in getting state of the new job!");
668 delete job_parameters;