1 // SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_LifeCycleCORBA.cxx
25 // Author : Paul RASCLE, EDF
35 #include "utilities.h"
37 #include <ServiceUnreachable.hxx>
39 #include "SALOME_LifeCycleCORBA.hxx"
40 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
41 #include "SALOME_ContainerManager.hxx"
42 #include "SALOME_Component_i.hxx"
43 #include "SALOME_NamingService.hxx"
46 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
50 CORBA::Object_var obj=_NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
51 ASSERT( !CORBA::is_nil(obj));
52 _ContManager=Engines::ContainerManager::_narrow(obj);
55 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
59 string SALOME_LifeCycleCORBA::ContainerName(
60 const char * aComputerContainer ,
61 string * theComputer ,
62 string * theContainer ) {
63 char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
64 strcpy( ContainerName , aComputerContainer ) ;
65 string theComputerContainer("/Containers/");
66 char * slash = strchr( ContainerName , '/' ) ;
68 *theComputer = GetHostname() ;
69 theComputerContainer += *theComputer ;
70 theComputerContainer += "/" ;
71 *theContainer = ContainerName ;
72 theComputerContainer += *theContainer ;
77 *theContainer = slash ;
78 if ( !strcmp( ContainerName , "localhost" ) ) {
79 *theComputer = GetHostname() ;
82 *theComputer = ContainerName ;
84 theComputerContainer += *theComputer ;
85 theComputerContainer += "/" ;
86 theComputerContainer += *theContainer ;
88 delete [] ContainerName;
89 return theComputerContainer ;
92 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
97 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
98 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
99 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
100 SALOME_ModuleCatalog::Acomponent_ptr compoInfo =
101 Catalog->GetComponent(componentName);
102 if (CORBA::is_nil (compoInfo))
104 INFOS("Catalog Error : Component not found in the catalog");
109 catch (ServiceUnreachable&)
111 INFOS("Caught exception: Naming Service Unreachable");
115 INFOS("Caught unknown exception.");
120 string SALOME_LifeCycleCORBA::ComputerPath(
121 const char * theComputer ) {
122 CORBA::String_var path;
123 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
124 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
125 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
127 path = Catalog->GetPathPrefix( theComputer );
129 catch (SALOME_ModuleCatalog::NotFound&) {
130 INFOS("GetPathPrefix(" << theComputer << ") not found!");
134 return CORBA::string_dup( path ) ;
137 Engines::Container_ptr SALOME_LifeCycleCORBA::FindContainer(const char *containerName)
141 if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
143 string theContainer ;
144 cont = ContainerName( containerName , &theComputer , &theContainer ) ;
147 cont = containerName ;
153 CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
154 if( !CORBA::is_nil( obj ) ) {
155 return Engines::Container::_narrow( obj ) ;
158 catch (ServiceUnreachable&) {
159 INFOS("Caught exception: Naming Service Unreachable");
162 INFOS("Caught unknown exception.");
164 return Engines::Container::_nil();
167 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component
168 (const char *containerName,
169 const char *componentName)
171 if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
172 char *stContainer=strdup(containerName);
173 string st2Container(stContainer);
174 int rg=st2Container.find("/");
176 //containerName doesn't contain "/" => Local container
178 Engines::MachineList_var listOfMachine=new Engines::MachineList;
179 listOfMachine->length(1);
180 listOfMachine[0]=CORBA::string_dup(GetHostname().c_str());
181 Engines::Component_ptr ret=FindComponent(containerName,componentName,listOfMachine.in());
182 if(CORBA::is_nil(ret))
183 return LoadComponent(containerName,componentName,listOfMachine);
188 //containerName contains "/" => Remote container
189 stContainer[rg]='\0';
190 Engines::MachineParameters_var params=new Engines::MachineParameters;
191 params->container_name=CORBA::string_dup(stContainer+rg+1);
192 params->hostname=CORBA::string_dup(stContainer);
193 params->OS=CORBA::string_dup("LINUX");
195 return FindOrLoad_Component(params,componentName);
199 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component(const Engines::MachineParameters& params,
200 const char *componentName)
202 if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
203 Engines::MachineList_var listOfMachine=_ContManager->GetFittingResources(params,componentName);
204 Engines::Component_ptr ret=FindComponent(params.container_name,componentName,listOfMachine);
205 if(CORBA::is_nil(ret))
206 return LoadComponent(params.container_name,componentName,listOfMachine);
211 Engines::Component_ptr SALOME_LifeCycleCORBA::FindComponent(const char *containerName,
212 const char *componentName,
213 const Engines::MachineList& listOfMachines)
215 if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
216 if(containerName[0]!='\0')
218 Engines::MachineList_var machinesOK=new Engines::MachineList;
219 unsigned int lghtOfmachinesOK=0;
220 machinesOK->length(listOfMachines.length());
221 for(unsigned int i=0;i<listOfMachines.length();i++)
223 const char *currentMachine=listOfMachines[i];
224 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,currentMachine);
225 CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
226 if(!CORBA::is_nil(obj))
228 machinesOK[lghtOfmachinesOK++]=CORBA::string_dup(currentMachine);
231 if(lghtOfmachinesOK!=0)
233 machinesOK->length(lghtOfmachinesOK);
234 CORBA::String_var bestMachine=_ContManager->FindBest(machinesOK);
235 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
236 CORBA::Object_var obj=_NS->Resolve(componentNameForNS.c_str());
237 return Engines::Component::_narrow(obj);
240 return Engines::Component::_nil();
244 //user specified no container name so trying to find a component in the best machine among listOfMachines
245 CORBA::String_var bestMachine=_ContManager->FindBest(listOfMachines);
246 //Normally look at all containers launched on bestMachine to see if componentName is already launched on one of them. To do..
247 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
248 CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
249 return Engines::Component::_narrow(obj);
253 Engines::Component_ptr SALOME_LifeCycleCORBA::LoadComponent(const char *containerName, const char *componentName, const Engines::MachineList& listOfMachines)
255 Engines::Container_var cont=_ContManager->FindOrStartContainer(containerName,listOfMachines);
256 string implementation=Engines_Component_i::GetDynLibraryName(componentName);
257 return cont->load_impl(componentName, implementation.c_str());