Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/kernel.git] / src / LifeCycleCORBA / LifeCycleCORBA.py
1 #==============================================================================
2 #  File      : LifeCycleCORBA.py
3 #  Created   : mer oct 17 08:42:01 CEST 2001
4 #  Author    : Paul RASCLE, EDF
5 #  Project   : SALOME
6 #  Copyright : EDF 2001
7 #  $Header$
8 #==============================================================================
9
10 import os
11 import sys
12 import time
13 from omniORB import CORBA
14 import CosNaming
15 import Engines
16 import SALOME_ModuleCatalog
17
18 from SALOME_utilities import *
19
20 class LifeCycleCORBA:
21     _orb = None
22     _rootcontext = None
23     _containerRootContext = None
24     _catalog = None
25     
26     #-------------------------------------------------------------------------
27
28     def __init__(self, orb):
29         MESSAGE( "LifeCycleCORBA::__init__" )
30         self._orb = orb
31
32         obj = self._orb.resolve_initial_references("NameService")
33         self._rootContext = obj._narrow(CosNaming.NamingContext)
34
35         if self._rootContext is None:
36             MESSAGE( "Name Service Reference is invalid" )
37
38         name = [CosNaming.NameComponent("Containers","dir")]
39         try:
40             self._containerRootContext = self._rootContext.bind_new_context(name)
41
42         except CosNaming.NamingContext.AlreadyBound, ex:
43             MESSAGE( "/Containers.dir Context already exists" )
44             obj = self._rootContext.resolve(name)
45             self._containerRootContext = obj._narrow(CosNaming.NamingContext)
46             if self._containerRootContext is None:
47                 MESSAGE( "Containers.dir exists but it is not a NamingContext" )
48
49         name = [CosNaming.NameComponent("Kernel","dir"),
50                 CosNaming.NameComponent("ModulCatalog","object")]
51         try:
52             obj = self._rootContext.resolve(name)
53         except CosNaming.NamingContext.NotFound, ex:
54             MESSAGE( "/Kernel.dir/ModulCatalog.object not found in Naming Service" )
55
56         self._catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
57         if self._catalog is None:
58             MESSAGE( "/Kernel.dir/ModulCatalog.object exists but is not a ModulCatalog" )
59
60     #-------------------------------------------------------------------------
61
62     def ContainerName(self, containerName):
63         try:
64             theComputer , theContainer = containerName.split('/')
65         except:
66             theComputer = ""
67             theContainer = containerName
68         if theComputer == "" :
69             theComputer = os.getenv("HOSTNAME")
70         if theComputer == "localhost" :
71             theComputer = os.getenv("HOSTNAME")
72         computerSplitName = theComputer.split('.')
73         theComputer = computerSplitName[0]
74         MESSAGE( theComputer + theContainer )
75         return theComputer,theContainer
76
77     #-------------------------------------------------------------------------
78
79     def ComputerPath(self, ComputerName ):
80         try:
81             path = self._catalog.GetPathPrefix( ComputerName )
82         except SALOME_ModuleCatalog.NotFound, ex:
83             path = ""
84         return path
85
86     #-------------------------------------------------------------------------
87
88     def FindContainer(self, containerName):
89         theComputer,theContainer = self.ContainerName( containerName )
90         name = [CosNaming.NameComponent(theComputer,"dir"),
91                 CosNaming.NameComponent(theContainer,"object")]
92         obj = None
93         try:
94             obj = self._containerRootContext.resolve(name)
95             MESSAGE( containerName + ".object found in Naming Service" )
96
97         except CosNaming.NamingContext.NotFound, ex:
98             MESSAGE( containerName + ".object not found in Naming Service" )
99
100         if obj is None:
101             container = None
102         else:
103             container = obj._narrow(Engines.Container)
104             if container is None:
105                 MESSAGE( containerName + ".object exists but is not a Container" )
106         return container
107     
108     #-------------------------------------------------------------------------
109
110     def FindOrStartContainer(self, theComputer , theContainer ):
111         MESSAGE( "FindOrStartContainer" + theComputer + theContainer )
112         aContainer = self.FindContainer( theComputer + "/" + theContainer )
113         if aContainer is None :
114             if (theContainer == "FactoryServer") | (theContainer == "FactoryServerPy") :
115                 if theComputer == os.getenv("HOSTNAME") :
116                     rshstr = ""
117                 else :
118                     rshstr = "rsh -n " + theComputer + " "
119                 path = self.ComputerPath( theComputer )
120                 if path != "" :
121                     rshstr = rshstr + path + "/../bin/"
122                 else :
123                     rshstr = rshstr + os.getenv( "PWD" ) + "/"
124                 if theContainer == "FactoryServer" :
125                     rshstr = rshstr + "./runSession ./SALOME_Container "
126                 else :
127                     rshstr = rshstr + "./runSession ./SALOME_ContainerPy.py '"
128                 rshstr = rshstr + theContainer + " -"
129                 omniORBcfg = os.getenv( "HOME" ) + "/omniORB.cfg"
130 #                omniORBcfg = os.getenv( "HOME" ) + "/.omniORB.cfg"
131                 file = os.open( omniORBcfg , os.O_RDONLY )
132                 ORBInitRef = os.read(file,132)
133                 if ORBInitRef[len(ORBInitRef)-1] == '\n' :
134                     ORBInitRef,bsn = ORBInitRef.split('\n')
135                 os.close( file )
136                 rshstr = rshstr + ORBInitRef
137                 if theContainer == "FactoryServerPy" :
138                     rshstr = rshstr + "'"
139                 rshstr = rshstr + " > /tmp/" + theContainer + "_"
140                 rshstr = rshstr + theComputer
141                 rshstr = rshstr + ".log 2>&1 &"
142                 os.system( rshstr )
143                 MESSAGE( "FindOrStartContainer" + rshstr + " done" )
144             else :
145                 if theContainer.find('Py') == -1 :
146                     aContainer = self.FindContainer( theComputer + "/" + "FactoryServer" )
147                 else :
148                     aContainer = self.FindContainer( theComputer + "/" + "FactoryServerPy" )
149                 aContainer = aContainer.start_impl( theContainer )
150
151             count = 21
152             while aContainer is None :
153                 time.sleep(1)
154                 count = count - 1
155                 MESSAGE( count + ". Waiting for " + theComputer + "/" + theContainer )
156                 aContainer = self.FindContainer( theComputer + "/" + theContainer )
157                 if count == 0 :
158                     return aContainer
159             
160         return  aContainer       
161         #os.system("rsh -n dm2s0017 /export/home/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515")
162
163     #-------------------------------------------------------------------------
164
165     def FindOrLoadComponent(self, containerName, componentName):
166
167         theComputer,theContainer = self.ContainerName( containerName )
168         name = [CosNaming.NameComponent(theComputer,"dir"),
169                 CosNaming.NameComponent(theContainer,"dir"),
170                 CosNaming.NameComponent(componentName,"object")]
171         try:
172             obj = self._containerRootContext.resolve(name)
173         except CosNaming.NamingContext.NotFound, ex:
174             MESSAGE( "component " + componentName + " not found, trying to load" )
175             container = self.FindContainer(theComputer + "/" + theContainer)
176             if container is None:
177                 MESSAGE( "container " + theComputer + "/" + theContainer + " not found in Naming Service, trying to start" )
178                 if (theContainer != "FactoryServer") & (theContainer != "FactoryServerPy") :
179                     if theContainer.find('Py') == -1 :
180                         theFactorycontainer = "FactoryServer"
181                     else :
182                         theFactorycontainer = "FactoryServerPy"
183                     Factorycontainer = self.FindContainer(theComputer + "/" + theFactorycontainer)
184                     if Factorycontainer is None:
185                         MESSAGE( "container " + theComputer + "/" + theFactorycontainer + " not found in Naming Service, trying to start" )
186                         Factorycontainer = self.FindOrStartContainer(theComputer,theFactorycontainer)
187                 else:
188                     Factorycontainer = self.FindOrStartContainer(theComputer,theContainer)
189                 if Factorycontainer != None :
190                     container = self.FindOrStartContainer(theComputer,theContainer)
191
192             if container != None:
193                 compoinfo = self._catalog.GetComponent(componentName)
194                 if compoinfo is None:
195                     MESSAGE( "component " + componentName + " not found in Module Catalog" )
196                 else:
197                     try:
198                         machineName = theComputer
199                         path = compoinfo.GetPathPrefix(machineName) + "/"
200                     except SALOME_ModuleCatalog.NotFound, ex:
201                         MESSAGE( "machine " + machineName + " not found in Module Catalog" )
202                         MESSAGE( "trying localhost" )
203                         try:
204                             path = compoinfo.GetPathPrefix("localhost") + "/"
205                         except SALOME_ModuleCatalog.NotFound, ex:
206                             path = ""
207                     implementation = path + "lib" + componentName + "Engine.so"
208                     MESSAGE( "Trying to load " + implementation )
209                     try:
210                         component = container.load_impl(componentName, implementation)
211                         MESSAGE( "component " + component._get_instanceName() + " launched !" )
212                         return component
213                     except:
214                         MESSAGE( "component " + componentName + " NOT launched !" )
215
216         else:
217             try:
218                 component = obj._narrow(Engines.Component)
219                 if component is None:
220                     MESSAGE( componentName + " is not a component !" )
221                 else:
222                     MESSAGE( "component " + component._get_instanceName() + " found !" )
223                 return component
224             except:
225                 MESSAGE( componentName + " failure" )
226                 return None