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 _ContManager=Engines::ContainerManager::_narrow(obj);
54 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
58 string SALOME_LifeCycleCORBA::ContainerName(
59 const char * aComputerContainer ,
60 string * theComputer ,
61 string * theContainer ) {
62 char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
63 strcpy( ContainerName , aComputerContainer ) ;
64 string theComputerContainer("/Containers/");
65 char * slash = strchr( ContainerName , '/' ) ;
67 *theComputer = GetHostname() ;
68 theComputerContainer += *theComputer ;
69 theComputerContainer += "/" ;
70 *theContainer = ContainerName ;
71 theComputerContainer += *theContainer ;
76 *theContainer = slash ;
77 if ( !strcmp( ContainerName , "localhost" ) ) {
78 *theComputer = GetHostname() ;
81 *theComputer = ContainerName ;
83 theComputerContainer += *theComputer ;
84 theComputerContainer += "/" ;
85 theComputerContainer += *theContainer ;
87 delete [] ContainerName;
88 return theComputerContainer ;
91 string SALOME_LifeCycleCORBA::ComputerPath(
92 const char * theComputer ) {
93 CORBA::String_var path;
94 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
95 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
96 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
98 path = Catalog->GetPathPrefix( theComputer );
100 catch (SALOME_ModuleCatalog::NotFound&) {
101 INFOS("GetPathPrefix(" << theComputer << ") not found!");
105 return CORBA::string_dup( path ) ;
108 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component
109 (const char *containerName,
110 const char *componentName)
112 char *stContainer=strdup(containerName);
113 string st2Container(stContainer);
114 int rg=st2Container.find("/");
116 //containerName doesn't contain "/" => Local container
118 Engines::MachineList_var listOfMachine=new Engines::MachineList;
119 listOfMachine->length(1);
120 listOfMachine[0]=CORBA::string_dup(GetHostname().c_str());
121 Engines::Component_ptr ret=FindComponent(containerName,componentName,listOfMachine.in());
122 if(CORBA::is_nil(ret))
123 return LoadComponent(containerName,componentName,listOfMachine);
128 //containerName contains "/" => Remote container
129 stContainer[rg]='\0';
130 Engines::MachineParameters_var params=new Engines::MachineParameters;
131 params->container_name=CORBA::string_dup(stContainer+rg+1);
132 params->hostname=CORBA::string_dup(stContainer);
133 params->OS=CORBA::string_dup("LINUX");
135 return FindOrLoad_Component(params,componentName);
139 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component(const Engines::MachineParameters& params,
140 const char *componentName)
142 Engines::MachineList_var listOfMachine=_ContManager->GetFittingResources(params,componentName);
143 Engines::Component_ptr ret=FindComponent(params.container_name,componentName,listOfMachine);
144 if(CORBA::is_nil(ret))
145 return LoadComponent(params.container_name,componentName,listOfMachine);
150 Engines::Component_ptr SALOME_LifeCycleCORBA::FindComponent(const char *containerName,
151 const char *componentName,
152 const Engines::MachineList& listOfMachines)
154 if(containerName[0]!='\0')
156 Engines::MachineList_var machinesOK=new Engines::MachineList;
157 unsigned int lghtOfmachinesOK=0;
158 machinesOK->length(listOfMachines.length());
159 for(unsigned int i=0;i<listOfMachines.length();i++)
161 const char *currentMachine=listOfMachines[i];
162 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,currentMachine);
163 CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
164 if(!CORBA::is_nil(obj))
166 machinesOK[lghtOfmachinesOK++]=CORBA::string_dup(currentMachine);
169 if(lghtOfmachinesOK!=0)
171 machinesOK->length(lghtOfmachinesOK);
172 CORBA::String_var bestMachine=_ContManager->FindBest(machinesOK);
173 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
174 CORBA::Object_var obj=_NS->Resolve(componentNameForNS.c_str());
175 return Engines::Component::_narrow(obj);
178 return Engines::Component::_nil();
182 //user specified no container name so trying to find a component in the best machine among listOfMachines
183 CORBA::String_var bestMachine=_ContManager->FindBest(listOfMachines);
184 //Normally look at all containers launched on bestMachine to see if componentName is already launched on one of them. To do..
185 string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
186 CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
187 return Engines::Component::_narrow(obj);
191 Engines::Component_ptr SALOME_LifeCycleCORBA::LoadComponent(const char *containerName, const char *componentName, const Engines::MachineList& listOfMachines)
193 Engines::Container_var cont=_ContManager->FindOrStartContainer(containerName,listOfMachines);
194 string implementation=Engines_Component_i::GetDynLibraryName(componentName);
195 return cont->load_impl(componentName, implementation.c_str());