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