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