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 _NS->Change_Directory("/"); // mpv 250105: current directory may be not root (in SALOMEDS for an example)
51 CORBA::Object_var obj=_NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
52 ASSERT( !CORBA::is_nil(obj));
53 _ContManager=Engines::ContainerManager::_narrow(obj);
56 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
60 string SALOME_LifeCycleCORBA::ContainerName(
61 const char * aComputerContainer ,
62 string * theComputer ,
63 string * theContainer ) {
64 char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
65 strcpy( ContainerName , aComputerContainer ) ;
66 string theComputerContainer("/Containers/");
67 char * slash = strchr( ContainerName , '/' ) ;
69 *theComputer = GetHostname() ;
70 theComputerContainer += *theComputer ;
71 theComputerContainer += "/" ;
72 *theContainer = ContainerName ;
73 theComputerContainer += *theContainer ;
78 *theContainer = slash ;
79 if ( !strcmp( ContainerName , "localhost" ) ) {
80 *theComputer = GetHostname() ;
83 *theComputer = ContainerName ;
85 theComputerContainer += *theComputer ;
86 theComputerContainer += "/" ;
87 theComputerContainer += *theContainer ;
89 delete [] ContainerName;
90 return theComputerContainer ;
93 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
98 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
99 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
100 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
101 SALOME_ModuleCatalog::Acomponent_ptr compoInfo =
102 Catalog->GetComponent(componentName);
103 if (CORBA::is_nil (compoInfo))
105 INFOS("Catalog Error : Component not found in the catalog");
110 catch (ServiceUnreachable&)
112 INFOS("Caught exception: Naming Service Unreachable");
116 INFOS("Caught unknown exception.");
121 string SALOME_LifeCycleCORBA::ComputerPath(
122 const char * theComputer ) {
123 CORBA::String_var path;
124 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
125 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
126 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
128 path = Catalog->GetPathPrefix( theComputer );
130 catch (SALOME_ModuleCatalog::NotFound&) {
131 INFOS("GetPathPrefix(" << theComputer << ") not found!");
135 return CORBA::string_dup( path ) ;
138 Engines::Container_ptr SALOME_LifeCycleCORBA::FindContainer(const char *containerName)
142 if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
144 string theContainer ;
145 cont = ContainerName( containerName , &theComputer , &theContainer ) ;
148 cont = containerName ;
154 CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
155 if( !CORBA::is_nil( obj ) ) {
156 return Engines::Container::_narrow( obj ) ;
159 catch (ServiceUnreachable&) {
160 INFOS("Caught exception: Naming Service Unreachable");
163 INFOS("Caught unknown exception.");
165 return Engines::Container::_nil();
168 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component
169 (const char *containerName,
170 const char *componentName)
172 if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
173 char *stContainer=strdup(containerName);
174 string st2Container(stContainer);
175 int rg=st2Container.find("/");
178 stContainer[rg]='\0';
179 if(strcmp(stContainer,"localhost")==0)
181 Engines::Component_ptr ret=FindOrLoad_Component(stContainer+rg+1,componentName);
187 //containerName doesn't contain "/" => Local container
189 Engines::MachineList_var listOfMachine=new Engines::MachineList;
190 listOfMachine->length(1);
191 listOfMachine[0]=CORBA::string_dup(GetHostname().c_str());
192 Engines::Component_ptr ret=FindComponent(containerName,componentName,listOfMachine.in());
193 if(CORBA::is_nil(ret))
194 return LoadComponent(containerName,componentName,listOfMachine);
199 //containerName contains "/" => Remote container
200 stContainer[rg]='\0';
201 Engines::MachineParameters_var params=new Engines::MachineParameters;
202 params->container_name=CORBA::string_dup(stContainer+rg+1);
203 params->hostname=CORBA::string_dup(stContainer);
204 params->OS=CORBA::string_dup("LINUX");
206 return FindOrLoad_Component(params,componentName);
210 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component(const Engines::MachineParameters& params,
211 const char *componentName)
213 if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
214 Engines::MachineList_var listOfMachine=_ContManager->GetFittingResources(params,componentName);
215 Engines::Component_ptr ret=FindComponent(params.container_name,componentName,listOfMachine);
216 if(CORBA::is_nil(ret))
217 return LoadComponent(params.container_name,componentName,listOfMachine);
222 Engines::Component_ptr SALOME_LifeCycleCORBA::FindComponent(const char *containerName,
223 const char *componentName,
224 const Engines::MachineList& listOfMachines)
226 if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
227 if(containerName[0]!='\0')
229 Engines::MachineList_var machinesOK=new Engines::MachineList;
230 unsigned int lghtOfmachinesOK=0;
231 machinesOK->length(listOfMachines.length());
232 for(unsigned int i=0;i<listOfMachines.length();i++)
234 const char *currentMachine=listOfMachines[i];
235 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,currentMachine);
236 CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
237 if(!CORBA::is_nil(obj))
239 machinesOK[lghtOfmachinesOK++]=CORBA::string_dup(currentMachine);
242 if(lghtOfmachinesOK!=0)
244 machinesOK->length(lghtOfmachinesOK);
245 CORBA::String_var bestMachine=_ContManager->FindBest(machinesOK);
246 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
247 CORBA::Object_var obj=_NS->Resolve(componentNameForNS.c_str());
248 return Engines::Component::_narrow(obj);
251 return Engines::Component::_nil();
255 //user specified no container name so trying to find a component in the best machine among listOfMachines
256 CORBA::String_var bestMachine=_ContManager->FindBest(listOfMachines);
257 //Normally look at all containers launched on bestMachine to see if componentName is already launched on one of them. To do..
258 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
259 CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
260 return Engines::Component::_narrow(obj);
264 Engines::Component_ptr SALOME_LifeCycleCORBA::LoadComponent(const char *containerName, const char *componentName, const Engines::MachineList& listOfMachines)
266 Engines::Container_var cont=_ContManager->FindOrStartContainer(containerName,listOfMachines);
267 string implementation=Engines_Component_i::GetDynLibraryName(componentName);
268 return cont->load_impl(componentName, implementation.c_str());