1 # SALOME LifeCycleC RBA : 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 : LifeCycleCORBA.py
25 # Author : Paul RASCLE, EDF
33 from omniORB import CORBA
37 import SALOME_ModuleCatalog
39 from SALOME_utilities import *
40 from Utils_Identity import getShortHostName
47 _containerRootContext = None
50 #-------------------------------------------------------------------------
52 def __init__(self, orb):
53 MESSAGE( "LifeCycleCORBA::__init__" )
56 obj = self._orb.resolve_initial_references("NameService")
57 self._rootContext = obj._narrow(CosNaming.NamingContext)
59 if self._rootContext is None:
60 MESSAGE( "Name Service Reference is invalid" )
62 name = [CosNaming.NameComponent("Containers","dir")]
64 self._containerRootContext = self._rootContext.bind_new_context(name)
66 except CosNaming.NamingContext.AlreadyBound, ex:
67 MESSAGE( "/Containers.dir Context already exists" )
68 obj = self._rootContext.resolve(name)
69 self._containerRootContext = obj._narrow(CosNaming.NamingContext)
70 if self._containerRootContext is None:
71 MESSAGE( "Containers.dir exists but it is not a NamingContext" )
73 name = [CosNaming.NameComponent("Kernel","dir"),
74 CosNaming.NameComponent("ModulCatalog","object")]
76 obj = self._rootContext.resolve(name)
77 except CosNaming.NamingContext.NotFound, ex:
78 MESSAGE( "/Kernel.dir/ModulCatalog.object not found in Naming Service" )
80 self._catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
81 if self._catalog is None:
82 MESSAGE( "/Kernel.dir/ModulCatalog.object exists but is not a ModulCatalog" )
84 name = [CosNaming.NameComponent("ContainerManager","object")]
86 obj = self._rootContext.resolve(name)
87 except CosNaming.NamingContext.NotFound, ex:
88 MESSAGE( "ContainerManager.object not found in Naming Service" )
89 self._contManager = obj._narrow(Engines.ContainerManager)
90 if self._contManager is None:
91 MESSAGE( "ContainerManager.object exists but is not a ContainerManager")
93 #-------------------------------------------------------------------------
95 def ContainerName(self, containerName):
98 theComputer , theContainer = containerName.split('/')
101 theContainer = containerName
103 if theComputer in ("","localhost") :
104 theComputer = getShortHostName()
106 MESSAGE( theComputer + theContainer )
107 return theComputer,theContainer
109 #-------------------------------------------------------------------------
111 def ComputerPath(self, ComputerName ):
113 #path = self._catalog.GetPathPrefix( ComputerName )
114 path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
115 except SALOME_ModuleCatalog.NotFound, ex:
119 #-------------------------------------------------------------------------
121 def FindContainer(self, containerName):
122 theComputer,theContainer = self.ContainerName( containerName )
123 name = [CosNaming.NameComponent(theComputer,"dir"),
124 CosNaming.NameComponent(theContainer,"object")]
127 obj = self._containerRootContext.resolve(name)
128 MESSAGE( containerName + ".object found in Naming Service" )
130 except CosNaming.NamingContext.NotFound, ex:
131 MESSAGE( containerName + ".object not found in Naming Service" )
136 container = obj._narrow(Engines.Container)
137 if container is None:
138 MESSAGE( containerName + ".object exists but is not a Container" )
141 #-------------------------------------------------------------------------
143 def FindComponent(self,containerName,componentName,listOfMachines):
144 if containerName!="":
146 for i in range(len(listOfMachines)):
147 currentMachine=listOfMachines[i]
148 componentNameForNS= [CosNaming.NameComponent(currentMachine,"dir"),
149 CosNaming.NameComponent(containerName,"dir"),
150 CosNaming.NameComponent(componentName,"object")]
153 obj = self._containerRootContext.resolve(componentNameForNS)
154 except CosNaming.NamingContext.NotFound, ex:
155 MESSAGE( "component " + componentName + " not found on machine " + currentMachine + " , trying to load" )
158 machinesOK.append(currentMachine)
161 if len(machinesOK)!=0:
162 bestMachine=self._contManager.FindBest(machinesOK)
163 componentNameForNS= [CosNaming.NameComponent(bestMachine,"dir"),
164 CosNaming.NameComponent(containerName,"dir"),
165 CosNaming.NameComponent(componentName,"object")]
168 obj = self._containerRootContext.resolve(componentNameForNS)
172 return obj._narrow(Engines.Component)
174 MESSAGE( "Big problem !!!")
179 bestMachine=self._contManager.FindBest(listOfMachines)
180 MESSAGE("Not implemented yet ...")
184 #-------------------------------------------------------------------------
186 def setLauncher(self,name):
187 """Change default launcher to the launcher identified by name
189 See module Launchers.py
191 Launchers.setLauncher(name)
193 #-------------------------------------------------------------------------
195 def StartContainer(self, theComputer , theContainer ):
196 """Start a container on theComputer machine with theContainer name
198 # Get the Naming Service address
200 addr=self._orb.object_to_string(self._rootContext)
202 # If container name contains "Py" launch a Python Container
204 if theContainer.find('Py') == -1 :
205 CMD=['SALOME_Container',theContainer,'-ORBInitRef','NameService='+addr]
207 CMD=['SALOME_ContainerPy.py',theContainer,'-ORBInitRef','NameService='+addr]
208 if theComputer in ("","localhost"):
209 theComputer=getShortHostName()
211 # Get the appropriate launcher and ask to launch
213 Launchers.getLauncher(theComputer).launch(theComputer,CMD)
215 # Wait until the container is registered in Naming Service
219 while aContainer is None and count > 0:
222 MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
223 aContainer = self.FindContainer( theComputer + "/" + theContainer )
226 #-------------------------------------------------------------------------
228 def FindOrStartContainer(self, theComputer , theContainer ):
229 """Find or Start a container on theComputer machine with theContainer name
231 if theComputer in ("","localhost"):
232 theComputer=getShortHostName()
233 MESSAGE( "FindOrStartContainer: " + theComputer + theContainer )
234 aContainer = self.FindContainer( theComputer + "/" + theContainer )
235 if aContainer is None :
236 aContainer= self.StartContainer(theComputer , theContainer )
239 #-------------------------------------------------------------------------
241 def LoadComponent(self,containerName,componentName,listOfMachine):
242 container=self._contManager.FindOrStartContainer(containerName,listOfMachine)
243 implementation="lib"+componentName+"Engine.so"
245 component = container.load_impl(componentName, implementation)
246 MESSAGE( "component " + component._get_instanceName() + " launched !" )
249 MESSAGE( "component " + componentName + " NOT launched !" )
252 #-------------------------------------------------------------------------
255 def FindOrLoadComponent(self, containerName, componentName):
256 sp=containerName.split("/")
259 listOfMachine.append(getShortHostName())
260 comp=self.FindComponent(containerName,componentName,listOfMachine)
262 return self.LoadComponent(containerName,componentName,listOfMachine)
267 params= Engines.MachineParameters(sp[1],sp[0],"LINUX",0,0,0,0)
268 listOfMachine=self._contManager.GetFittingResources(params,componentName)
269 ret=self.FindComponent(sp[1],componentName,listOfMachine);
271 return self.LoadComponent(sp[1],componentName,listOfMachine)