]> SALOME platform Git repositories - modules/yacs.git/blob - src/LifeCycleCORBA/LifeCycleCORBA.py
Salome HOME
448491864e96b1ee4a1ca18ac4ba94b9a90cfc7f
[modules/yacs.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
40 class LifeCycleCORBA:
41     _orb = None
42     _rootcontext = None
43     _containerRootContext = None
44     _catalog = None
45     
46     #-------------------------------------------------------------------------
47
48     def __init__(self, orb):
49         MESSAGE( "LifeCycleCORBA::__init__" )
50         self._orb = orb
51
52         obj = self._orb.resolve_initial_references("NameService")
53         self._rootContext = obj._narrow(CosNaming.NamingContext)
54
55         if self._rootContext is None:
56             MESSAGE( "Name Service Reference is invalid" )
57
58         name = [CosNaming.NameComponent("Containers","dir")]
59         try:
60             self._containerRootContext = self._rootContext.bind_new_context(name)
61
62         except CosNaming.NamingContext.AlreadyBound, ex:
63             MESSAGE( "/Containers.dir Context already exists" )
64             obj = self._rootContext.resolve(name)
65             self._containerRootContext = obj._narrow(CosNaming.NamingContext)
66             if self._containerRootContext is None:
67                 MESSAGE( "Containers.dir exists but it is not a NamingContext" )
68
69         name = [CosNaming.NameComponent("Kernel","dir"),
70                 CosNaming.NameComponent("ModulCatalog","object")]
71         try:
72             obj = self._rootContext.resolve(name)
73         except CosNaming.NamingContext.NotFound, ex:
74             MESSAGE( "/Kernel.dir/ModulCatalog.object not found in Naming Service" )
75
76         self._catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
77         if self._catalog is None:
78             MESSAGE( "/Kernel.dir/ModulCatalog.object exists but is not a ModulCatalog" )
79
80     #-------------------------------------------------------------------------
81
82     def ContainerName(self, containerName):
83         theComputer = ""
84         try:
85             theComputer , theContainer = containerName.split('/')
86         except:
87             theComputer = ""
88             theContainer = containerName
89         if theComputer == "" :
90             theComputer = os.getenv("HOSTNAME")
91         if theComputer == "localhost" :
92             theComputer = os.getenv("HOSTNAME")
93         computerSplitName = theComputer.split('.')
94         theComputer = computerSplitName[0]
95         MESSAGE( theComputer + theContainer )
96         return theComputer,theContainer
97
98     #-------------------------------------------------------------------------
99
100     def ComputerPath(self, ComputerName ):
101         try:
102             #path = self._catalog.GetPathPrefix( ComputerName )
103             path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
104         except SALOME_ModuleCatalog.NotFound, ex:
105             path = ""
106         return path
107
108     #-------------------------------------------------------------------------
109
110     def FindContainer(self, containerName):
111         theComputer,theContainer = self.ContainerName( containerName )
112         name = [CosNaming.NameComponent(theComputer,"dir"),
113                 CosNaming.NameComponent(theContainer,"object")]
114         obj = None
115         try:
116             obj = self._containerRootContext.resolve(name)
117             MESSAGE( containerName + ".object found in Naming Service" )
118
119         except CosNaming.NamingContext.NotFound, ex:
120             MESSAGE( containerName + ".object not found in Naming Service" )
121
122         if obj is None:
123             container = None
124         else:
125             container = obj._narrow(Engines.Container)
126             if container is None:
127                 MESSAGE( containerName + ".object exists but is not a Container" )
128         return container
129     
130     #-------------------------------------------------------------------------
131
132     def FindOrStartContainer(self, theComputer , theContainer ):
133         MESSAGE( "FindOrStartContainer" + theComputer + theContainer )
134         aContainer = self.FindContainer( theComputer + "/" + theContainer )
135         if aContainer is None :
136             if (theContainer == "FactoryServer") | (theContainer == "FactoryServerPy") :
137                 myMachine=string.split(os.getenv( "HOSTNAME" ),'.')
138                 if theComputer == myMachine[0] :
139                     rshstr = ""
140                 else :
141                     rshstr = "rsh -n " + theComputer + " "
142                 path = self.ComputerPath( theComputer )
143 ##                if path != "" :
144 ##                    rshstr = rshstr + path + "/../bin/"
145 ##                else :
146 ##                    rshstr = rshstr + os.getenv( "KERNEL_ROOT_DIR" ) + "/bin/"
147                 if theContainer == "FactoryServer" :
148                     rshstr = rshstr + path + "SALOME_Container "
149                 else :
150                     rshstr = rshstr + path + "SALOME_ContainerPy.py '"
151                 rshstr = rshstr + theContainer + " -"
152                 omniORBcfg = os.getenv( "OMNIORB_CONFIG" )
153                 file = os.open( omniORBcfg , os.O_RDONLY )
154                 ORBInitRef = os.read(file,132)
155                 if ORBInitRef[len(ORBInitRef)-1] == '\n' :
156                     ORBInitRef,bsn = ORBInitRef.split('\n')
157                 os.close( file )
158                 rshstr = rshstr + ORBInitRef
159                 if theContainer == "FactoryServerPy" :
160                     rshstr = rshstr + "'"
161                 rshstr = rshstr + " > /tmp/" + theContainer + "_"
162                 rshstr = rshstr + theComputer
163                 rshstr = rshstr + ".log 2>&1 &"
164                 os.system( rshstr )
165                 MESSAGE( "FindOrStartContainer" + rshstr + " done" )
166             else :
167                 if theContainer.find('Py') == -1 :
168                     aContainer = self.FindContainer( theComputer + "/" + "FactoryServer" )
169                 else :
170                     aContainer = self.FindContainer( theComputer + "/" + "FactoryServerPy" )
171                 aContainer = aContainer.start_impl( theContainer )
172
173             count = 21
174             while aContainer is None :
175                 time.sleep(1)
176                 count = count - 1
177                 MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
178                 aContainer = self.FindContainer( theComputer + "/" + theContainer )
179                 if count == 0 :
180                     return aContainer
181             
182         return  aContainer       
183         #os.system("rsh -n dm2s0017 /export/home/KERNEL_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515")
184
185     #-------------------------------------------------------------------------
186
187     def FindOrLoadComponent(self, containerName, componentName):
188
189         theComputer,theContainer = self.ContainerName( containerName )
190         name = [CosNaming.NameComponent(theComputer,"dir"),
191                 CosNaming.NameComponent(theContainer,"dir"),
192                 CosNaming.NameComponent(componentName,"object")]
193         try:
194             obj = self._containerRootContext.resolve(name)
195         except CosNaming.NamingContext.NotFound, ex:
196             MESSAGE( "component " + componentName + " not found, trying to load" )
197             container = self.FindContainer(theComputer + "/" + theContainer)
198             if container is None:
199                 MESSAGE( "container " + theComputer + "/" + theContainer + " not found in Naming Service, trying to start" )
200                 if (theContainer != "FactoryServer") & (theContainer != "FactoryServerPy") :
201                     if theContainer.find('Py') == -1 :
202                         theFactorycontainer = "FactoryServer"
203                     else :
204                         theFactorycontainer = "FactoryServerPy"
205                     Factorycontainer = self.FindContainer(theComputer + "/" + theFactorycontainer)
206                     if Factorycontainer is None:
207                         MESSAGE( "container " + theComputer + "/" + theFactorycontainer + " not found in Naming Service, trying to start" )
208                         Factorycontainer = self.FindOrStartContainer(theComputer,theFactorycontainer)
209                 else:
210                     Factorycontainer = self.FindOrStartContainer(theComputer,theContainer)
211                 if Factorycontainer != None :
212                     container = self.FindOrStartContainer(theComputer,theContainer)
213
214             if container != None:
215                 compoinfo = self._catalog.GetComponent(componentName)
216                 if compoinfo is None:
217                     MESSAGE( "component " + componentName + " not found in Module Catalog" )
218                 else:
219                     try:
220                         machineName = theComputer
221                         path = compoinfo.GetPathPrefix(machineName) + "/"
222                     except SALOME_ModuleCatalog.NotFound, ex:
223                         MESSAGE( "machine " + machineName + " not found in Module Catalog" )
224                         MESSAGE( "trying localhost" )
225                         try:
226                             path = compoinfo.GetPathPrefix("localhost") + "/"
227                         except SALOME_ModuleCatalog.NotFound, ex:
228                             path = ""
229                     implementation = path + "lib" + componentName + "Engine.so"
230                     MESSAGE( "Trying to load " + implementation )
231                     try:
232                         component = container.load_impl(componentName, implementation)
233                         MESSAGE( "component " + component._get_instanceName() + " launched !" )
234                         return component
235                     except:
236                         MESSAGE( "component " + componentName + " NOT launched !" )
237
238         else:
239             try:
240                 component = obj._narrow(Engines.Component)
241                 if component is None:
242                     MESSAGE( componentName + " is not a component !" )
243                 else:
244                     MESSAGE( "component " + component._get_instanceName() + " found !" )
245                 return component
246             except:
247                 MESSAGE( componentName + " failure" )
248                 return None