Salome HOME
97e8155aeedfa5c03bb2fd54c8af70279e8e86de
[modules/kernel.git] / src / LifeCycleCORBA / LifeCycleCORBA.py
1 #  SALOME LifeCycleC RBA : implementation of containers and engines life cycle both in Python and C++
2 #
3 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5
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. 
10
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. 
15
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 
19
20 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 #
22 #
23 #
24 #  File   : LifeCycleCORBA.py
25 #  Author : Paul RASCLE, EDF
26 #  Module : SALOME
27 #  $Header$
28
29 import os
30 import sys
31 import time
32 import string
33 from omniORB import CORBA
34 import CosNaming
35 import Engines
36 import SALOME_ModuleCatalog
37
38 from SALOME_utilities import *
39 from Utils_Identity import getShortHostName
40
41 class LifeCycleCORBA:
42     _orb = None
43     _rootcontext = None
44     _containerRootContext = None
45     _catalog = None
46     
47     #-------------------------------------------------------------------------
48
49     def __init__(self, orb):
50         MESSAGE( "LifeCycleCORBA::__init__" )
51         self._orb = orb
52
53         obj = self._orb.resolve_initial_references("NameService")
54         self._rootContext = obj._narrow(CosNaming.NamingContext)
55
56         if self._rootContext is None:
57             MESSAGE( "Name Service Reference is invalid" )
58
59         name = [CosNaming.NameComponent("Containers","dir")]
60         try:
61             self._containerRootContext = self._rootContext.bind_new_context(name)
62
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" )
69
70         name = [CosNaming.NameComponent("Kernel","dir"),
71                 CosNaming.NameComponent("ModulCatalog","object")]
72         try:
73             obj = self._rootContext.resolve(name)
74         except CosNaming.NamingContext.NotFound, ex:
75             MESSAGE( "/Kernel.dir/ModulCatalog.object not found in Naming Service" )
76
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" )
80
81         name = [CosNaming.NameComponent("ContainerManager","object")]
82         try:
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")
89
90     #-------------------------------------------------------------------------
91
92     def ContainerName(self, containerName):
93         theComputer = ""
94         try:
95             theComputer , theContainer = containerName.split('/')
96         except:
97             theComputer = ""
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
107
108     #-------------------------------------------------------------------------
109
110     def ComputerPath(self, ComputerName ):
111         try:
112             #path = self._catalog.GetPathPrefix( ComputerName )
113             path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
114         except SALOME_ModuleCatalog.NotFound, ex:
115             path = ""
116         return path
117
118     #-------------------------------------------------------------------------
119
120     def FindContainer(self, containerName):
121         theComputer,theContainer = self.ContainerName( containerName )
122         name = [CosNaming.NameComponent(theComputer,"dir"),
123                 CosNaming.NameComponent(theContainer,"object")]
124         obj = None
125         try:
126             obj = self._containerRootContext.resolve(name)
127             MESSAGE( containerName + ".object found in Naming Service" )
128
129         except CosNaming.NamingContext.NotFound, ex:
130             MESSAGE( containerName + ".object not found in Naming Service" )
131
132         if obj is None:
133             container = None
134         else:
135             container = obj._narrow(Engines.Container)
136             if container is None:
137                 MESSAGE( containerName + ".object exists but is not a Container" )
138         return container
139     
140     #-------------------------------------------------------------------------
141
142     def FindComponent(self,containerName,componentName,listOfMachines):
143         if containerName!="":
144             machinesOK=[]
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")]
150                 obj=None
151                 try:
152                     obj = self._containerRootContext.resolve(componentNameForNS)
153                 except CosNaming.NamingContext.NotFound, ex:
154                     MESSAGE( "component " + componentName + " not found on machine " + currentMachine + " , trying to load" )
155                     pass
156                 if obj is not None:
157                     machinesOK.append(currentMachine)
158                     pass
159                 pass
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")]
168                 obj=None
169                 try:
170                     obj = self._containerRootContext.resolve(componentNameForNS)
171                 except:
172                     pass
173                 if obj is not None:
174                     return obj._narrow(Engines.Component)
175                 else:
176                     MESSAGE( "Big problem !!!")
177                     return None
178             else:
179                 return None
180         else:
181             bestMachine=self._contManager.FindBest(listOfMachines)
182             MESSAGE("Not implemented yet ...")
183             return None
184         pass
185
186     #-------------------------------------------------------------------------
187
188     def LoadComponent(self,containerName,componentName,listOfMachine):
189         container=self._contManager.FindOrStartContainer(containerName,listOfMachine)
190         implementation="lib"+componentName+"Engine.so"
191         try:
192             component = container.load_impl(componentName, implementation)
193             MESSAGE( "component " + component._get_instanceName() + " launched !" )
194             return component
195         except:
196             MESSAGE( "component " + componentName + " NOT launched !" )
197             return None
198
199     #-------------------------------------------------------------------------
200     
201
202     def FindOrLoadComponent(self, containerName, componentName):
203         sp=containerName.split("/")
204         if len(sp)==1:
205             listOfMachine=[]
206             listOfMachine.append(getShortHostName())
207             comp=self.FindComponent(containerName,componentName,listOfMachine)
208             if comp is None:
209                 return self.LoadComponent(containerName,componentName,listOfMachine)
210             else:
211                 return comp
212             pass
213         else:
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);
217             if ret is None:
218                 return self.LoadComponent(sp[1],componentName,listOfMachine)
219             else:
220                 return ret
221             pass
222         
223     
224
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 :
232 ##                    rshstr = ""
233 ##                else :
234 ##                    rshstr = "rsh -n " + theComputer + " "
235 ##                path = self.ComputerPath( theComputer )
236 ####                if path != "" :
237 ####                    rshstr = rshstr + path + "/../bin/"
238 ####                else :
239 ####                    rshstr = rshstr + os.getenv( "KERNEL_ROOT_DIR" ) + "/bin/"
240 ##                if theContainer == "FactoryServer" :
241 ##                    rshstr = rshstr + path + "SALOME_Container "
242 ##                else :
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')
250 ##                os.close( file )
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" )
259 ##            else :
260 ##                if theContainer.find('Py') == -1 :
261 ##                    aContainer = self.FindContainer( theComputer + "/" + "FactoryServer" )
262 ##                else :
263 ##                    aContainer = self.FindContainer( theComputer + "/" + "FactoryServerPy" )
264 ##                aContainer = aContainer.start_impl( theContainer )
265
266 ##            count = 21
267 ##            while aContainer is None :
268 ##                time.sleep(1)
269 ##                count = count - 1
270 ##                MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
271 ##                aContainer = self.FindContainer( theComputer + "/" + theContainer )
272 ##                if count == 0 :
273 ##                    return aContainer
274             
275 ##        return  aContainer       
276 ##        #os.system("rsh -n dm2s0017 /export/home/KERNEL_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515")
277
278 ##    #-------------------------------------------------------------------------
279
280 ##    def FindOrLoadComponent(self, containerName, componentName):
281
282 ##        theComputer,theContainer = self.ContainerName( containerName )
283 ##        name = [CosNaming.NameComponent(theComputer,"dir"),
284 ##                CosNaming.NameComponent(theContainer,"dir"),
285 ##                CosNaming.NameComponent(componentName,"object")]
286 ##        try:
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"
296 ##                    else :
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)
302 ##                else:
303 ##                    Factorycontainer = self.FindOrStartContainer(theComputer,theContainer)
304 ##                if Factorycontainer != None :
305 ##                    container = self.FindOrStartContainer(theComputer,theContainer)
306
307 ##            if container != None:
308 ##                compoinfo = self._catalog.GetComponent(componentName)
309 ##                if compoinfo is None:
310 ##                    MESSAGE( "component " + componentName + " not found in Module Catalog" )
311 ##                else:
312 ##                    try:
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" )
318 ##                        try:
319 ##                            path = compoinfo.GetPathPrefix("localhost") + "/"
320 ##                        except SALOME_ModuleCatalog.NotFound, ex:
321 ##                            path = ""
322 ##                    implementation = path + "lib" + componentName + "Engine.so"
323 ##                    MESSAGE( "Trying to load " + implementation )
324 ##                    try:
325 ##                        component = container.load_impl(componentName, implementation)
326 ##                        MESSAGE( "component " + component._get_instanceName() + " launched !" )
327 ##                        return component
328 ##                    except:
329 ##                        MESSAGE( "component " + componentName + " NOT launched !" )
330
331 ##        else:
332 ##            try:
333 ##                component = obj._narrow(Engines.Component)
334 ##                if component is None:
335 ##                    MESSAGE( componentName + " is not a component !" )
336 ##                else:
337 ##                    MESSAGE( "component " + component._get_instanceName() + " found !" )
338 ##                return component
339 ##            except:
340 ##                MESSAGE( componentName + " failure" )
341 ##                return None