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