1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SALOME_ContainerManager.hxx"
21 #include "SALOME_NamingService.hxx"
23 #include <sys/types.h>
28 #include "Utils_CorbaException.hxx"
30 #define TIME_OUT_TO_LAUNCH_CONT 21
34 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
37 //=============================================================================
41 * Define a CORBA single thread policy for the server, which avoid to deal
42 * with non thread-safe usage like Change_Directory in SALOME naming service
44 //=============================================================================
46 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb)
48 MESSAGE("constructor");
49 _NS = new SALOME_NamingService(orb);
50 _ResManager = new SALOME_ResourcesManager(orb);
52 PortableServer::POA_var root_poa = PortableServer::POA::_the_root_poa();
53 PortableServer::POAManager_var pman = root_poa->the_POAManager();
54 PortableServer::POA_var my_poa;
56 CORBA::PolicyList policies;
58 PortableServer::ThreadPolicy_var threadPol =
59 root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
60 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
63 root_poa->create_POA("SThreadPOA",pman,policies);
65 PortableServer::ObjectId_var id = my_poa->activate_object(this);
66 CORBA::Object_var obj = my_poa->id_to_reference(id);
67 Engines::ContainerManager_var refContMan =
68 Engines::ContainerManager::_narrow(obj);
70 _NS->Register(refContMan,_ContainerManagerNameInNS);
71 MESSAGE("constructor end");
74 //=============================================================================
78 //=============================================================================
80 SALOME_ContainerManager::~SALOME_ContainerManager()
82 MESSAGE("destructor");
87 //=============================================================================
89 * shutdown all the containers, then the ContainerManager servant
91 //=============================================================================
93 void SALOME_ContainerManager::Shutdown()
97 PortableServer::ObjectId_var oid = _default_POA()->servant_to_id(this);
98 _default_POA()->deactivate_object(oid);
103 //=============================================================================
105 * Loop on all the containers listed in naming service, ask shutdown on each
107 //=============================================================================
109 void SALOME_ContainerManager::ShutdownContainers()
111 MESSAGE("ShutdownContainers");
112 _NS->Change_Directory("/Containers");
113 vector<string> vec = _NS->list_directory_recurs();
114 for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++)
117 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
118 Engines::Container_var cont=Engines::Container::_narrow(obj);
119 if(!CORBA::is_nil(cont))
121 MESSAGE("ShutdownContainers: " << (*iter));
124 else MESSAGE("ShutdownContainers: no container ref for " << (*iter));
128 //=============================================================================
130 * Find a suitable Container in a list of machines, or start one
131 * \param params Machine Parameters required for the container
132 * \param possibleComputers list of machines usable for find or start
134 //=============================================================================
136 Engines::Container_ptr
137 SALOME_ContainerManager::
138 FindOrStartContainer(const Engines::MachineParameters& params,
139 const Engines::MachineList& possibleComputers)
142 string containerNameInNS;
143 char idc[3*sizeof(long)];
145 Engines::Container_ptr ret = FindContainer(params,possibleComputers);
146 if(!CORBA::is_nil(ret))
148 MESSAGE("Container doesn't exist try to launch it ...");
149 MESSAGE("SALOME_ContainerManager::FindOrStartContainer " <<
150 possibleComputers.length());
151 //vector<string> vector;
152 string theMachine=_ResManager->FindBest(possibleComputers);
153 MESSAGE("try to launch it on " << theMachine);
155 // Get Id for container: a parallel container registers in Naming Service
156 // on the machine where is process 0. ContainerManager does'nt know the name
157 // of this machine before the launch of the parallel container. So to get
158 // the IOR of the parallel container in Naming Service, ContainerManager
159 // gives a unique Id. The parallel container registers his name under
160 // /ContainerManager/Id directory in NamingService
162 id = GetIdForContainer();
167 MESSAGE("SALOME_ContainerManager::FindOrStartContainer : " <<
168 "no possible computer");
169 return Engines::Container::_nil();
171 else if(theMachine==GetHostname())
173 command=_ResManager->BuildCommandToLaunchLocalContainer(params,id);
177 _ResManager->BuildCommandToLaunchRemoteContainer(theMachine,params,id);
179 _ResManager->RmTmpFile();
180 int status=system(command.c_str());
183 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
184 "(system command status -1)");
185 return Engines::Container::_nil();
187 else if (status == 217)
189 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
190 "(system command status 217)");
191 return Engines::Container::_nil();
195 int count=TIME_OUT_TO_LAUNCH_CONT;
196 while ( CORBA::is_nil(ret) && count )
205 MESSAGE( count << ". Waiting for FactoryServer on " << theMachine);
208 containerNameInNS = "/ContainerManager/id";
209 sprintf(idc,"%ld",id);
210 containerNameInNS += idc;
214 _NS->BuildContainerNameForNS(params,theMachine.c_str());
215 SCRUTE(containerNameInNS);
216 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
217 ret=Engines::Container::_narrow(obj);
219 if ( CORBA::is_nil(ret) )
221 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed");
227 //=============================================================================
231 //=============================================================================
233 Engines::MachineList *
234 SALOME_ContainerManager::
235 GetFittingResources(const Engines::MachineParameters& params,
236 const char *componentName)
238 MESSAGE("SALOME_ContainerManager::GetFittingResources");
239 Engines::MachineList *ret=new Engines::MachineList;
243 vec = _ResManager->GetFittingResources(params,componentName);
245 catch(const SALOME_Exception &ex)
247 INFOS("Caught exception.");
248 THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
252 // MESSAGE("Machine list length "<<vec.size());
253 ret->length(vec.size());
254 for(unsigned int i=0;i<vec.size();i++)
256 (*ret)[i]=(vec[i]).c_str();
261 //=============================================================================
265 //=============================================================================
268 SALOME_ContainerManager::
269 FindBest(const Engines::MachineList& possibleComputers)
271 string theMachine=_ResManager->FindBest(possibleComputers);
272 return CORBA::string_dup(theMachine.c_str());
275 //=============================================================================
279 //=============================================================================
281 Engines::Container_ptr
282 SALOME_ContainerManager::
283 FindContainer(const Engines::MachineParameters& params,
284 const char *theMachine)
286 string containerNameInNS(_NS->BuildContainerNameForNS(params,theMachine));
287 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
288 if( !CORBA::is_nil(obj) )
289 return Engines::Container::_narrow(obj);
291 return Engines::Container::_nil();
294 //=============================================================================
298 //=============================================================================
300 Engines::Container_ptr
301 SALOME_ContainerManager::
302 FindContainer(const Engines::MachineParameters& params,
303 const Engines::MachineList& possibleComputers)
305 MESSAGE("FindContainer "<<possibleComputers.length());
306 for(unsigned int i=0;i<possibleComputers.length();i++)
308 MESSAGE("FindContainer possible " << possibleComputers[i]);
309 Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
310 if( !CORBA::is_nil(cont) )
313 MESSAGE("FindContainer: not found");
314 return Engines::Container::_nil();
317 //=============================================================================
319 * Get Id for container: a parallel container registers in Naming Service
320 * on the machine where is process 0. ContainerManager does'nt know the name
321 * of this machine before the launch of the parallel container. So to get
322 * the IOR of the parallel container in Naming Service, ContainerManager
323 * gives a unique Id. The parallel container registers his name under
324 * /ContainerManager/Id directory in NamingService
326 //=============================================================================
329 long SALOME_ContainerManager::GetIdForContainer(void)