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
36 import SALOME_ModuleCatalog
38 from SALOME_utilities import *
39 from Utils_Identity import getShortHostName
44 _containerRootContext = None
47 #-------------------------------------------------------------------------
49 def __init__(self, orb):
50 MESSAGE( "LifeCycleCORBA::__init__" )
53 obj = self._orb.resolve_initial_references("NameService")
54 self._rootContext = obj._narrow(CosNaming.NamingContext)
56 if self._rootContext is None:
57 MESSAGE( "Name Service Reference is invalid" )
59 name = [CosNaming.NameComponent("Containers","dir")]
61 self._containerRootContext = self._rootContext.bind_new_context(name)
63 except CosNaming.NamingContext.AlreadyBound, ex:
64 MESSAGE( "/Containers.dir Context already exists" )
65 obj = self._rootContext.resolve(name)
66 self._containerRootContext = obj._narrow(CosNaming.NamingContext)
67 if self._containerRootContext is None:
68 MESSAGE( "Containers.dir exists but it is not a NamingContext" )
70 name = [CosNaming.NameComponent("Kernel","dir"),
71 CosNaming.NameComponent("ModulCatalog","object")]
73 obj = self._rootContext.resolve(name)
74 except CosNaming.NamingContext.NotFound, ex:
75 MESSAGE( "/Kernel.dir/ModulCatalog.object not found in Naming Service" )
77 self._catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
78 if self._catalog is None:
79 MESSAGE( "/Kernel.dir/ModulCatalog.object exists but is not a ModulCatalog" )
81 name = [CosNaming.NameComponent("ContainerManager","object")]
83 obj = self._rootContext.resolve(name)
84 except CosNaming.NamingContext.NotFound, ex:
85 MESSAGE( "ContainerManager.object not found in Naming Service" )
86 self._contManager = obj._narrow(Engines.ContainerManager)
87 if self._contManager is None:
88 MESSAGE( "ContainerManager.object exists but is not a ContainerManager")
90 #-------------------------------------------------------------------------
92 def ContainerName(self, containerName):
95 theComputer , theContainer = containerName.split('/')
98 theContainer = containerName
99 if theComputer == "" :
100 theComputer = getShortHostName()
101 if theComputer == "localhost" :
102 theComputer = getShortHostName()
103 computerSplitName = theComputer.split('.')
104 theComputer = computerSplitName[0]
105 MESSAGE( theComputer + theContainer )
106 return theComputer,theContainer
108 #-------------------------------------------------------------------------
110 def ComputerPath(self, ComputerName ):
112 #path = self._catalog.GetPathPrefix( ComputerName )
113 path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
114 except SALOME_ModuleCatalog.NotFound, ex:
118 #-------------------------------------------------------------------------
120 def FindContainer(self, containerName):
121 theComputer,theContainer = self.ContainerName( containerName )
122 name = [CosNaming.NameComponent(theComputer,"dir"),
123 CosNaming.NameComponent(theContainer,"object")]
126 obj = self._containerRootContext.resolve(name)
127 MESSAGE( containerName + ".object found in Naming Service" )
129 except CosNaming.NamingContext.NotFound, ex:
130 MESSAGE( containerName + ".object not found in Naming Service" )
135 container = obj._narrow(Engines.Container)
136 if container is None:
137 MESSAGE( containerName + ".object exists but is not a Container" )
140 #-------------------------------------------------------------------------
142 def FindComponent(self,containerName,componentName,listOfMachines):
143 if containerName!="":
145 for i in range(len(listOfMachines)):
146 currentMachine=listOfMachines[i]
147 componentNameForNS= [CosNaming.NameComponent(currentMachine,"dir"),
148 CosNaming.NameComponent(containerName,"dir"),
149 CosNaming.NameComponent(componentName,"object")]
152 obj = self._containerRootContext.resolve(componentNameForNS)
153 except CosNaming.NamingContext.NotFound, ex:
154 MESSAGE( "component " + componentName + " not found on machine " + currentMachine + " , trying to load" )
157 machinesOK.append(currentMachine)
160 print "Finding best ..........",len(machinesOK)
161 if len(machinesOK)!=0:
162 print "Finding best .........."
163 bestMachine=self._contManager.FindBest(machinesOK)
164 print "Finding best done .........."
165 componentNameForNS= [CosNaming.NameComponent(bestMachine,"dir"),
166 CosNaming.NameComponent(containerName,"dir"),
167 CosNaming.NameComponent(componentName,"object")]
170 obj = self._containerRootContext.resolve(componentNameForNS)
174 return obj._narrow(Engines.Component)
176 MESSAGE( "Big problem !!!")
181 bestMachine=self._contManager.FindBest(listOfMachines)
182 MESSAGE("Not implemented yet ...")
186 #-------------------------------------------------------------------------
188 def LoadComponent(self,containerName,componentName,listOfMachine):
189 container=self._contManager.FindOrStartContainer(containerName,listOfMachine)
190 implementation="lib"+componentName+"Engine.so"
192 component = container.load_impl(componentName, implementation)
193 MESSAGE( "component " + component._get_instanceName() + " launched !" )
196 MESSAGE( "component " + componentName + " NOT launched !" )
199 #-------------------------------------------------------------------------
202 def FindOrLoadComponent(self, containerName, componentName):
203 sp=containerName.split("/")
206 listOfMachine.append(getShortHostName())
207 comp=self.FindComponent(containerName,componentName,listOfMachine)
209 return self.LoadComponent(containerName,componentName,listOfMachine)
214 params= Engines.MachineParameters(sp[1],sp[0],"LINUX",0,0,0,0)
215 listOfMachine=self._contManager.GetFittingResources(params,componentName)
216 ret=self.FindComponent(sp[1],componentName,listOfMachine);
218 return self.LoadComponent(sp[1],componentName,listOfMachine)
225 ## def FindOrStartContainer(self, theComputer , theContainer ):
226 ## MESSAGE( "FindOrStartContainer" + theComputer + theContainer )
227 ## aContainer = self.FindContainer( theComputer + "/" + theContainer )
228 ## if aContainer is None :
229 ## if (theContainer == "FactoryServer") | (theContainer == "FactoryServerPy") :
230 ## myMachine=getShortHostName()
231 ## if theComputer == myMachine :
234 ## rshstr = "rsh -n " + theComputer + " "
235 ## path = self.ComputerPath( theComputer )
237 #### rshstr = rshstr + path + "/../bin/"
239 #### rshstr = rshstr + os.getenv( "KERNEL_ROOT_DIR" ) + "/bin/"
240 ## if theContainer == "FactoryServer" :
241 ## rshstr = rshstr + path + "SALOME_Container "
243 ## rshstr = rshstr + path + "SALOME_ContainerPy.py '"
244 ## rshstr = rshstr + theContainer + " -"
245 ## omniORBcfg = os.getenv( "OMNIORB_CONFIG" )
246 ## file = os.open( omniORBcfg , os.O_RDONLY )
247 ## ORBInitRef = os.read(file,132)
248 ## if ORBInitRef[len(ORBInitRef)-1] == '\n' :
249 ## ORBInitRef,bsn = ORBInitRef.split('\n')
251 ## rshstr = rshstr + ORBInitRef
252 ## if theContainer == "FactoryServerPy" :
253 ## rshstr = rshstr + "'"
254 ## rshstr = rshstr + " > /tmp/" + theContainer + "_"
255 ## rshstr = rshstr + theComputer
256 ## rshstr = rshstr + ".log 2>&1 &"
257 ## os.system( rshstr )
258 ## MESSAGE( "FindOrStartContainer" + rshstr + " done" )
260 ## if theContainer.find('Py') == -1 :
261 ## aContainer = self.FindContainer( theComputer + "/" + "FactoryServer" )
263 ## aContainer = self.FindContainer( theComputer + "/" + "FactoryServerPy" )
264 ## aContainer = aContainer.start_impl( theContainer )
267 ## while aContainer is None :
270 ## MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
271 ## aContainer = self.FindContainer( theComputer + "/" + theContainer )
276 ## #os.system("rsh -n dm2s0017 /export/home/KERNEL_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515")
278 ## #-------------------------------------------------------------------------
280 ## def FindOrLoadComponent(self, containerName, componentName):
282 ## theComputer,theContainer = self.ContainerName( containerName )
283 ## name = [CosNaming.NameComponent(theComputer,"dir"),
284 ## CosNaming.NameComponent(theContainer,"dir"),
285 ## CosNaming.NameComponent(componentName,"object")]
287 ## obj = self._containerRootContext.resolve(name)
288 ## except CosNaming.NamingContext.NotFound, ex:
289 ## MESSAGE( "component " + componentName + " not found, trying to load" )
290 ## container = self.FindContainer(theComputer + "/" + theContainer)
291 ## if container is None:
292 ## MESSAGE( "container " + theComputer + "/" + theContainer + " not found in Naming Service, trying to start" )
293 ## if (theContainer != "FactoryServer") & (theContainer != "FactoryServerPy") :
294 ## if theContainer.find('Py') == -1 :
295 ## theFactorycontainer = "FactoryServer"
297 ## theFactorycontainer = "FactoryServerPy"
298 ## Factorycontainer = self.FindContainer(theComputer + "/" + theFactorycontainer)
299 ## if Factorycontainer is None:
300 ## MESSAGE( "container " + theComputer + "/" + theFactorycontainer + " not found in Naming Service, trying to start" )
301 ## Factorycontainer = self.FindOrStartContainer(theComputer,theFactorycontainer)
303 ## Factorycontainer = self.FindOrStartContainer(theComputer,theContainer)
304 ## if Factorycontainer != None :
305 ## container = self.FindOrStartContainer(theComputer,theContainer)
307 ## if container != None:
308 ## compoinfo = self._catalog.GetComponent(componentName)
309 ## if compoinfo is None:
310 ## MESSAGE( "component " + componentName + " not found in Module Catalog" )
313 ## machineName = theComputer
314 ## path = compoinfo.GetPathPrefix(machineName) + "/"
315 ## except SALOME_ModuleCatalog.NotFound, ex:
316 ## MESSAGE( "machine " + machineName + " not found in Module Catalog" )
317 ## MESSAGE( "trying localhost" )
319 ## path = compoinfo.GetPathPrefix("localhost") + "/"
320 ## except SALOME_ModuleCatalog.NotFound, ex:
322 ## implementation = path + "lib" + componentName + "Engine.so"
323 ## MESSAGE( "Trying to load " + implementation )
325 ## component = container.load_impl(componentName, implementation)
326 ## MESSAGE( "component " + component._get_instanceName() + " launched !" )
329 ## MESSAGE( "component " + componentName + " NOT launched !" )
333 ## component = obj._narrow(Engines.Component)
334 ## if component is None:
335 ## MESSAGE( componentName + " is not a component !" )
337 ## MESSAGE( "component " + component._get_instanceName() + " found !" )
340 ## MESSAGE( componentName + " failure" )