]> SALOME platform Git repositories - modules/kernel.git/blob - src/LifeCycleCORBA/LifeCycleCORBA.py
Salome HOME
82c3649368b57350d234e7dee3cbe816f6850800
[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         name = [CosNaming.NameComponent("ContainerManager","object")]
82         try:
83             obj = self._rootContext.resolve(name)
84         except CosNaming.NamingContext.NotFound, ex:
85             MESSAGE( "ContainerManager.object not found in Naming Service" )
86         self._contManager = obj._narrow(Engines.ContainerManager)
87         if self._contManager is None:
88             MESSAGE( "ContainerManager.object exists but is not a ContainerManager")
89
90     #-------------------------------------------------------------------------
91
92     def ContainerName(self, containerName):
93         theComputer = ""
94         try:
95             theComputer , theContainer = containerName.split('/')
96         except:
97             theComputer = ""
98             theContainer = containerName
99         if theComputer == "" :
100             theComputer = getShortHostName()
101         if theComputer == "localhost" :
102             theComputer = getShortHostName()
103         computerSplitName = theComputer.split('.')
104         theComputer = computerSplitName[0]
105         MESSAGE( theComputer + theContainer )
106         return theComputer,theContainer
107
108     #-------------------------------------------------------------------------
109
110     def ComputerPath(self, ComputerName ):
111         try:
112             #path = self._catalog.GetPathPrefix( ComputerName )
113             path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
114         except SALOME_ModuleCatalog.NotFound, ex:
115             path = ""
116         return path
117
118     #-------------------------------------------------------------------------
119
120     def FindContainer(self, containerName):
121         theComputer,theContainer = self.ContainerName( containerName )
122         name = [CosNaming.NameComponent(theComputer,"dir"),
123                 CosNaming.NameComponent(theContainer,"object")]
124         obj = None
125         try:
126             obj = self._containerRootContext.resolve(name)
127             MESSAGE( containerName + ".object found in Naming Service" )
128
129         except CosNaming.NamingContext.NotFound, ex:
130             MESSAGE( containerName + ".object not found in Naming Service" )
131
132         if obj is None:
133             container = None
134         else:
135             container = obj._narrow(Engines.Container)
136             if container is None:
137                 MESSAGE( containerName + ".object exists but is not a Container" )
138         return container
139     
140     #-------------------------------------------------------------------------
141
142     def FindComponent(self,containerName,componentName,listOfMachines):
143         if containerName!="":
144             machinesOK=[]
145             for i in range(len(listOfMachines)):
146                 currentMachine=listOfMachines[i]
147                 componentNameForNS= [CosNaming.NameComponent(currentMachine,"dir"),
148                                      CosNaming.NameComponent(containerName,"dir"),
149                                      CosNaming.NameComponent(componentName,"object")]
150                 obj=None
151                 try:
152                     obj = self._containerRootContext.resolve(componentNameForNS)
153                 except CosNaming.NamingContext.NotFound, ex:
154                     MESSAGE( "component " + componentName + " not found on machine " + currentMachine + " , trying to load" )
155                     pass
156                 if obj is not None:
157                     machinesOK.append(currentMachine)
158                     pass
159                 pass
160             if len(machinesOK)!=0:
161                 bestMachine=self._contManager.FindBest(machinesOK)
162                 componentNameForNS= [CosNaming.NameComponent(bestMachine,"dir"),
163                                      CosNaming.NameComponent(containerName,"dir"),
164                                      CosNaming.NameComponent(componentName,"object")]
165                 obj=None
166                 try:
167                     obj = self._containerRootContext.resolve(componentNameForNS)
168                 except:
169                     pass
170                 if obj is not None:
171                     return obj._narrow(Engines.Component)
172                 else:
173                     MESSAGE( "Big problem !!!")
174                     return None
175             else:
176                 return None
177         else:
178             bestMachine=self._contManager.FindBest(listOfMachines)
179             MESSAGE("Not implemented yet ...")
180             return None
181         pass
182
183     #-------------------------------------------------------------------------
184
185     def LoadComponent(self,containerName,componentName,listOfMachine):
186         container=self._contManager.FindOrStartContainer(containerName,listOfMachine)
187         implementation="lib"+componentName+"Engine.so"
188         try:
189             component = container.load_impl(componentName, implementation)
190             MESSAGE( "component " + component._get_instanceName() + " launched !" )
191             return component
192         except:
193             MESSAGE( "component " + componentName + " NOT launched !" )
194             return None
195
196     #-------------------------------------------------------------------------
197     
198
199     def FindOrLoadComponent(self, containerName, componentName):
200         sp=containerName.split("/")
201         if len(sp)==1:
202             listOfMachine=[]
203             listOfMachine.append(getShortHostName())
204             comp=self.FindComponent(containerName,componentName,listOfMachine)
205             if comp is None:
206                 return self.LoadComponent(containerName,componentName,listOfMachine)
207             else:
208                 return comp
209             pass
210         else:
211             params= Engines.MachineParameters(sp[1],sp[0],"LINUX",0,0,0,0)
212             listOfMachine=self._contManager.GetFittingResources(params,componentName)
213             ret=self.FindComponent(sp[1],componentName,listOfMachine);
214             if ret is None:
215                 return self.LoadComponent(sp[1],componentName,listOfMachine)
216             else:
217                 return ret
218             pass
219