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