Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / runtime / SalomeContainer.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //#define REFCNT
20 //
21 #ifdef REFCNT
22 #define private public
23 #define protected public
24 #include <omniORB4/CORBA.h>
25 #include <omniORB4/internal/typecode.h>
26 #endif
27
28 #include "RuntimeSALOME.hxx"
29 #include "SalomeContainer.hxx"
30 #include "SalomeComponent.hxx"
31
32 #include "SALOME_NamingService.hxx"
33 #include "SALOME_LifeCycleCORBA.hxx"
34 #include "SALOME_ContainerManager.hxx"
35 #include "Basics_Utils.hxx"
36 #include "OpUtil.hxx"
37
38 #include <sstream>
39 #include <iostream>
40
41 //#define _DEVDEBUG_
42 #include "YacsTrace.hxx"
43
44 using namespace YACS::ENGINE;
45 using namespace std;
46
47 SalomeContainer::SalomeContainer():_trueCont(Engines::Container::_nil())
48 {
49   /* Init MachinesParameters */
50   _params.container_name = "";
51   _params.hostname = "";
52   _params.OS = "";
53   _params.mem_mb = 0;
54   _params.cpu_clock = 0;
55   _params.nb_proc_per_node = 0;
56   _params.nb_node = 0;
57   _params.isMPI = false;
58   _params.parallelLib = "";
59   _params.nb_component_nodes = 0;
60 }
61
62 SalomeContainer::SalomeContainer(const SalomeContainer& other):Container(other),_trueCont(Engines::Container::_nil())
63 {
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);
75 }
76
77 SalomeContainer::~SalomeContainer()
78 {
79 }
80
81 void SalomeContainer::lock()
82 {
83   _mutex.lock();
84 }
85
86 void SalomeContainer::unLock()
87 {
88   _mutex.unlock();
89 }
90
91 bool SalomeContainer::isAlreadyStarted() const
92 {
93   if(CORBA::is_nil(_trueCont))
94     return false;
95   else
96     return true;
97 }
98
99 void SalomeContainer::start() throw (Exception)
100 {
101   CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
102   SALOME_NamingService ns;
103   try
104     {
105       ns.init_orb(orb);
106     }
107   catch(SALOME_Exception& e)
108     {
109       throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
110     }
111   CORBA::Object_var obj=ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
112   Engines::ContainerManager_var contManager=Engines::ContainerManager::_narrow(obj);
113
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
118   if (str != "")
119     {
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))
127         {
128           std::cerr << "Container already exists: " << ContainerNameInNS << std::endl;
129           _trueCont=Engines::Container::_narrow(obj);
130           return;
131         }
132     }
133
134   if (str == "") 
135     {
136       //give a almost unique name to the container : Pid_Name_Addr
137       std::ostringstream stream;
138       stream << getpid();
139       stream << "_";
140       stream << _name;
141       stream << "_";
142       stream << (void *)(this);
143       DEBTRACE("container_name="<<stream.str());
144       _params.container_name=CORBA::string_dup(stream.str().c_str());
145     }
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++)
150     {
151       compolist[i]=CORBA::string_dup(_componentNames[i].c_str());
152     }
153
154   try
155     { 
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");
159       if(policy=="best")
160         _trueCont=contManager->GiveContainer(_params,Engines::P_BEST,compolist);
161       else if(policy=="first")
162         _trueCont=contManager->GiveContainer(_params,Engines::P_FIRST,compolist);
163       else
164         _trueCont=contManager->GiveContainer(_params,Engines::P_CYCL,compolist);
165     }
166   catch(CORBA::COMM_FAILURE&)
167     {
168       throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
169     }
170   catch(CORBA::Exception&)
171     {
172       throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
173     }
174   if(CORBA::is_nil(_trueCont))
175     throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
176
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;
180
181 #ifdef REFCNT
182     DEBTRACE(_trueCont->_PR_getobj()->pd_refCount );
183 #endif
184 }
185
186 Container *SalomeContainer::clone() const
187 {
188   if(_isAttachedOnCloning)
189     {
190       incrRef();
191       return (Container*) (this);
192     }
193   else
194     return new SalomeContainer(*this);
195 }
196
197 std::string SalomeContainer::getPlacementId() const
198 {
199   if(isAlreadyStarted())
200     {
201       const char *what="/";
202       char *corbaStr=_trueCont->name();
203       string ret(corbaStr);
204       CORBA::string_free(corbaStr);
205       //Salome FOREVER ...
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);
210       return ret;
211     }
212   else
213     return "Not placed yet !!!";
214 }
215
216 void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw (Exception)
217 {
218   if(inst->getKind()!=SalomeComponent::KIND)
219     throw Exception("SalomeContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
220 }
221
222 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
223 {
224   DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
225   
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")
237     {
238       if (value == "true")
239         _params.isMPI = true;
240       else if (value == "false")
241         _params.isMPI = false;
242       else 
243         throw Exception("SalomeContainer::SetProperty : params.isMPI value not correct : " + value);
244     }
245   else if (name == "mem_mb")
246     {
247       std::istringstream iss(value);
248       if (!(iss >> _params.mem_mb))
249         throw Exception("salomecontainer::setproperty : params.mem_mb value not correct : " + value);
250     }
251   else if (name == "cpu_clock")
252     {
253       std::istringstream iss(value);
254       if (!(iss >> _params.cpu_clock))
255         throw Exception("salomecontainer::setproperty : params.cpu_clock value not correct : " + value);
256     }
257   else if (name == "nb_proc_per_node")
258     {
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);
262     }
263   else if (name == "nb_node")
264     {
265       std::istringstream iss(value);
266       if (!(iss >> _params.nb_node))
267         throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
268     }
269   else if (name == "nb_component_nodes")
270     {
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);
274     }
275   Container::setProperty(name, value);
276 }
277
278 bool SalomeContainer::isAPaCOContainer() const
279 {
280   bool result = false;
281   string parallelLib(_params.parallelLib);
282   if (parallelLib != "")
283     result = true;
284   return result;
285 }
286
287 void SalomeContainer::addComponentName(std::string name)
288 {
289   _componentNames.push_back(name);
290 }