Salome HOME
Copyrights update
[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 reload(Engines)
37 import SALOME_ModuleCatalog
38
39 from SALOME_utilities import *
40 from Utils_Identity import getShortHostName
41 import Utils_Identity 
42 import Launchers
43
44 class LifeCycleCORBA:
45     _orb = None
46     _rootcontext = None
47     _containerRootContext = None
48     _catalog = None
49     
50     #-------------------------------------------------------------------------
51
52     def __init__(self, orb):
53         MESSAGE( "LifeCycleCORBA::__init__" )
54         self._orb = orb
55
56         obj = self._orb.resolve_initial_references("NameService")
57         self._rootContext = obj._narrow(CosNaming.NamingContext)
58
59         if self._rootContext is None:
60             MESSAGE( "Name Service Reference is invalid" )
61
62         name = [CosNaming.NameComponent("Containers","dir")]
63         try:
64             self._containerRootContext = self._rootContext.bind_new_context(name)
65
66         except CosNaming.NamingContext.AlreadyBound, ex:
67             MESSAGE( "/Containers.dir Context already exists" )
68             obj = self._rootContext.resolve(name)
69             self._containerRootContext = obj._narrow(CosNaming.NamingContext)
70             if self._containerRootContext is None:
71                 MESSAGE( "Containers.dir exists but it is not a NamingContext" )
72
73         name = [CosNaming.NameComponent("Kernel","dir"),
74                 CosNaming.NameComponent("ModulCatalog","object")]
75         try:
76             obj = self._rootContext.resolve(name)
77         except CosNaming.NamingContext.NotFound, ex:
78             MESSAGE( "/Kernel.dir/ModulCatalog.object not found in Naming Service" )
79
80         self._catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
81         if self._catalog is None:
82             MESSAGE( "/Kernel.dir/ModulCatalog.object exists but is not a ModulCatalog" )
83
84         name = [CosNaming.NameComponent("ContainerManager","object")]
85         try:
86             obj = self._rootContext.resolve(name)
87         except CosNaming.NamingContext.NotFound, ex:
88             MESSAGE( "ContainerManager.object not found in Naming Service" )
89         self._contManager = obj._narrow(Engines.ContainerManager)
90         if self._contManager is None:
91             MESSAGE( "ContainerManager.object exists but is not a ContainerManager")
92
93     #-------------------------------------------------------------------------
94
95     def ContainerName(self, containerName):
96         theComputer = ""
97         try:
98             theComputer , theContainer = containerName.split('/')
99         except:
100             theComputer = ""
101             theContainer = containerName
102
103         if theComputer in ("","localhost") :
104             theComputer = getShortHostName()
105
106         MESSAGE( theComputer + theContainer )
107         return theComputer,theContainer
108
109     #-------------------------------------------------------------------------
110
111     def ComputerPath(self, ComputerName ):
112         try:
113             #path = self._catalog.GetPathPrefix( ComputerName )
114             path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
115         except SALOME_ModuleCatalog.NotFound, ex:
116             path = ""
117         return path
118
119     #-------------------------------------------------------------------------
120
121     def FindContainer(self, containerName):
122         theComputer,theContainer = self.ContainerName( containerName )
123         name = [CosNaming.NameComponent(theComputer,"dir"),
124                 CosNaming.NameComponent(theContainer,"object")]
125         obj = None
126         try:
127             obj = self._containerRootContext.resolve(name)
128             MESSAGE( containerName + ".object found in Naming Service" )
129
130         except CosNaming.NamingContext.NotFound, ex:
131             MESSAGE( containerName + ".object not found in Naming Service" )
132
133         if obj is None:
134             container = None
135         else:
136             container = obj._narrow(Engines.Container)
137             if container is None:
138                 MESSAGE( containerName + ".object exists but is not a Container" )
139         return container
140     
141     #-------------------------------------------------------------------------
142
143     def FindComponent(self,containerName,componentName,listOfMachines):
144         if containerName!="":
145             machinesOK=[]
146             for i in range(len(listOfMachines)):
147                 currentMachine=listOfMachines[i]
148                 componentNameForNS= [CosNaming.NameComponent(currentMachine,"dir"),
149                                      CosNaming.NameComponent(containerName,"dir"),
150                                      CosNaming.NameComponent(componentName,"object")]
151                 obj=None
152                 try:
153                     obj = self._containerRootContext.resolve(componentNameForNS)
154                 except CosNaming.NamingContext.NotFound, ex:
155                     MESSAGE( "component " + componentName + " not found on machine " + currentMachine + " , trying to load" )
156                     pass
157                 if obj is not None:
158                     machinesOK.append(currentMachine)
159                     pass
160                 pass
161             if len(machinesOK)!=0:
162                 bestMachine=self._contManager.FindBest(machinesOK)
163                 componentNameForNS= [CosNaming.NameComponent(bestMachine,"dir"),
164                                      CosNaming.NameComponent(containerName,"dir"),
165                                      CosNaming.NameComponent(componentName,"object")]
166                 obj=None
167                 try:
168                     obj = self._containerRootContext.resolve(componentNameForNS)
169                 except:
170                     pass
171                 if obj is not None:
172                     return obj._narrow(Engines.Component)
173                 else:
174                     MESSAGE( "Big problem !!!")
175                     return None
176             else:
177                 return None
178         else:
179             bestMachine=self._contManager.FindBest(listOfMachines)
180             MESSAGE("Not implemented yet ...")
181             return None
182         pass
183
184     #-------------------------------------------------------------------------
185
186     def setLauncher(self,name):
187         """Change default launcher to the launcher identified by name
188
189            See module Launchers.py
190         """
191         Launchers.setLauncher(name)
192
193     #-------------------------------------------------------------------------
194
195     def StartContainer(self, theComputer , theContainer ):
196         """Start a container on theComputer machine with theContainer name
197         """
198         # Get the Naming Service address
199         #
200         addr=self._orb.object_to_string(self._rootContext)
201         #
202         # If container name contains "Py" launch a Python Container
203         #
204         if theContainer.find('Py') == -1 :
205            CMD=['SALOME_Container',theContainer,'-ORBInitRef','NameService='+addr]
206         else:
207            CMD=['SALOME_ContainerPy.py',theContainer,'-ORBInitRef','NameService='+addr]
208         if theComputer in ("","localhost"):
209            theComputer=getShortHostName()
210         #
211         # Get the appropriate launcher and ask to launch
212         #
213         Launchers.getLauncher(theComputer).launch(theComputer,CMD)
214         #
215         # Wait until the container is registered in Naming Service
216         #
217         count =5 
218         aContainer=None
219         while aContainer is None and count > 0:
220             time.sleep(1)
221             count = count - 1
222             MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
223             aContainer = self.FindContainer( theComputer + "/" + theContainer )
224         return aContainer
225
226     #-------------------------------------------------------------------------
227
228     def FindOrStartContainer(self, theComputer , theContainer ):
229         """Find or Start a container on theComputer machine with theContainer name
230         """
231         if theComputer in ("","localhost"):
232            theComputer=getShortHostName()
233         MESSAGE( "FindOrStartContainer: " + theComputer + theContainer )
234         aContainer = self.FindContainer( theComputer + "/" + theContainer )
235         if aContainer is None :
236             aContainer= self.StartContainer(theComputer , theContainer )
237         return aContainer
238             
239     #-------------------------------------------------------------------------
240
241     def LoadComponent(self,containerName,componentName,listOfMachine):
242         container=self._contManager.FindOrStartContainer(containerName,listOfMachine)
243         implementation="lib"+componentName+"Engine.so"
244         try:
245             component = container.load_impl(componentName, implementation)
246             MESSAGE( "component " + component._get_instanceName() + " launched !" )
247             return component
248         except:
249             MESSAGE( "component " + componentName + " NOT launched !" )
250             return None
251
252     #-------------------------------------------------------------------------
253     
254
255     def FindOrLoadComponent(self, containerName, componentName):
256         sp=containerName.split("/")
257         if len(sp)==1:
258             listOfMachine=[]
259             listOfMachine.append(getShortHostName())
260             comp=self.FindComponent(containerName,componentName,listOfMachine)
261             if comp is None:
262                 return self.LoadComponent(containerName,componentName,listOfMachine)
263             else:
264                 return comp
265             pass
266         else:
267             params= Engines.MachineParameters(sp[1],sp[0],"LINUX",0,0,0,0)
268             listOfMachine=self._contManager.GetFittingResources(params,componentName)
269             ret=self.FindComponent(sp[1],componentName,listOfMachine);
270             if ret is None:
271                 return self.LoadComponent(sp[1],componentName,listOfMachine)
272             else:
273                 return ret
274             pass
275