1 // Copyright (C) 2006-2008 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
22 #define private public
23 #define protected public
24 #include <omniORB4/CORBA.h>
25 #include <omniORB4/internal/typecode.h>
28 #include "RuntimeSALOME.hxx"
29 #include "SalomeContainer.hxx"
30 #include "SalomeComponent.hxx"
32 #include "SALOME_NamingService.hxx"
33 #include "SALOME_LifeCycleCORBA.hxx"
34 #include "SALOME_ContainerManager.hxx"
35 #include "Basics_Utils.hxx"
42 #include "YacsTrace.hxx"
44 using namespace YACS::ENGINE;
47 SalomeContainer::SalomeContainer():_trueCont(Engines::Container::_nil())
49 /* Init MachinesParameters */
50 _params.container_name = "";
51 _params.hostname = "";
54 _params.cpu_clock = 0;
55 _params.nb_proc_per_node = 0;
57 _params.isMPI = false;
58 _params.parallelLib = "";
59 _params.nb_component_nodes = 0;
62 SalomeContainer::SalomeContainer(const SalomeContainer& other):Container(other),_trueCont(Engines::Container::_nil())
64 _params.container_name = CORBA::string_dup(other._params.container_name);
65 _params.hostname = CORBA::string_dup(other._params.hostname);
66 _params.OS = CORBA::string_dup(other._params.OS);
67 _params.mem_mb = other._params.mem_mb;
68 _params.cpu_clock = other._params.cpu_clock;
69 _params.nb_proc_per_node = other._params.nb_proc_per_node;
70 _params.nb_node = other._params.nb_node;
71 _params.isMPI = other._params.isMPI;
72 _params.parallelLib = CORBA::string_dup(other._params.parallelLib);
73 _params.nb_component_nodes = other._params.nb_component_nodes;
74 _params.workingdir= CORBA::string_dup(other._params.workingdir);
77 SalomeContainer::~SalomeContainer()
81 void SalomeContainer::lock()
86 void SalomeContainer::unLock()
91 bool SalomeContainer::isAlreadyStarted() const
93 if(CORBA::is_nil(_trueCont))
99 void SalomeContainer::start() throw (Exception)
101 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
102 SALOME_NamingService ns;
107 catch(SALOME_Exception& e)
109 throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
111 CORBA::Object_var obj=ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
112 Engines::ContainerManager_var contManager=Engines::ContainerManager::_narrow(obj);
114 std::string str(_params.container_name);
115 DEBTRACE("SalomeContainer::start " << str);
116 //If a container_name is given try to find an already existing container in naming service
117 //If not found start a new container with the given parameters
120 std::string machine(_params.hostname);
121 if(machine == "" || machine == "localhost")
122 //machine=Kernel_Utils::GetHostname();
123 machine=Kernel_Utils::GetHostname();
124 std::string ContainerNameInNS=ns.BuildContainerNameForNS(_params,machine.c_str());
125 obj=ns.Resolve(ContainerNameInNS.c_str());
126 if(!CORBA::is_nil(obj))
128 std::cerr << "Container already exists: " << ContainerNameInNS << std::endl;
129 _trueCont=Engines::Container::_narrow(obj);
136 //give a almost unique name to the container : Pid_Name_Addr
137 std::ostringstream stream;
142 stream << (void *)(this);
143 DEBTRACE("container_name="<<stream.str());
144 _params.container_name=CORBA::string_dup(stream.str().c_str());
146 Engines::CompoList compolist;
147 compolist.length(_componentNames.size());
148 std::vector<std::string>::iterator iter;
149 for(CORBA::ULong i=0; i < _componentNames.size();i++)
151 compolist[i]=CORBA::string_dup(_componentNames[i].c_str());
156 // --- GiveContainer is used in batch mode to retreive launched containers,
157 // and is equivalent to StartContainer when not in batch.
158 std::string policy=getProperty("policy");
160 _trueCont=contManager->GiveContainer(_params,Engines::P_BEST,compolist);
161 else if(policy=="first")
162 _trueCont=contManager->GiveContainer(_params,Engines::P_FIRST,compolist);
164 _trueCont=contManager->GiveContainer(_params,Engines::P_CYCL,compolist);
166 catch(CORBA::COMM_FAILURE&)
168 throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
170 catch(CORBA::Exception&)
172 throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
174 if(CORBA::is_nil(_trueCont))
175 throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
177 CORBA::String_var containerName=_trueCont->name();
178 CORBA::String_var hostName=_trueCont->getHostName();
179 std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << _trueCont->getPID() << std::endl;
182 DEBTRACE(_trueCont->_PR_getobj()->pd_refCount );
186 Container *SalomeContainer::clone() const
188 if(_isAttachedOnCloning)
191 return (Container*) (this);
194 return new SalomeContainer(*this);
197 std::string SalomeContainer::getPlacementId() const
199 if(isAlreadyStarted())
201 const char *what="/";
202 char *corbaStr=_trueCont->name();
203 string ret(corbaStr);
204 CORBA::string_free(corbaStr);
206 std::string::size_type i=ret.find_first_of(what,0);
207 i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
208 if(i!=std::string::npos)
209 return ret.substr(i+1);
213 return "Not placed yet !!!";
216 void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw (Exception)
218 if(inst->getKind()!=SalomeComponent::KIND)
219 throw Exception("SalomeContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
222 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
224 DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
226 if (name == "container_name")
227 _params.container_name = CORBA::string_dup(value.c_str());
228 else if (name == "hostname")
229 _params.hostname = CORBA::string_dup(value.c_str());
230 else if (name == "OS")
231 _params.OS = CORBA::string_dup(value.c_str());
232 else if (name == "parallelLib")
233 _params.parallelLib = CORBA::string_dup(value.c_str());
234 else if (name == "workingdir")
235 _params.workingdir = CORBA::string_dup(value.c_str());
236 else if (name == "isMPI")
239 _params.isMPI = true;
240 else if (value == "false")
241 _params.isMPI = false;
243 throw Exception("SalomeContainer::SetProperty : params.isMPI value not correct : " + value);
245 else if (name == "mem_mb")
247 std::istringstream iss(value);
248 if (!(iss >> _params.mem_mb))
249 throw Exception("salomecontainer::setproperty : params.mem_mb value not correct : " + value);
251 else if (name == "cpu_clock")
253 std::istringstream iss(value);
254 if (!(iss >> _params.cpu_clock))
255 throw Exception("salomecontainer::setproperty : params.cpu_clock value not correct : " + value);
257 else if (name == "nb_proc_per_node")
259 std::istringstream iss(value);
260 if (!(iss >> _params.nb_proc_per_node))
261 throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value);
263 else if (name == "nb_node")
265 std::istringstream iss(value);
266 if (!(iss >> _params.nb_node))
267 throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
269 else if (name == "nb_component_nodes")
271 std::istringstream iss(value);
272 if (!(iss >> _params.nb_component_nodes))
273 throw Exception("salomecontainer::setproperty : params.nb_component_nodes value not correct : " + value);
275 Container::setProperty(name, value);
278 bool SalomeContainer::isAPaCOContainer() const
281 string parallelLib(_params.parallelLib);
282 if (parallelLib != "")
287 void SalomeContainer::addComponentName(std::string name)
289 _componentNames.push_back(name);