Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / Container / SALOME_ContainerPy.py
1 #! /usr/bin/env python
2 #
3 #  SALOME Container : implementation of container and engine for Kernel
4 #
5 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
7
8 #  This library is free software; you can redistribute it and/or 
9 #  modify it under the terms of the GNU Lesser General Public 
10 #  License as published by the Free Software Foundation; either 
11 #  version 2.1 of the License. 
12
13 #  This library is distributed in the hope that it will be useful, 
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
16 #  Lesser General Public License for more details. 
17
18 #  You should have received a copy of the GNU Lesser General Public 
19 #  License along with this library; if not, write to the Free Software 
20 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
21
22 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
23 #
24 #
25 #
26 #  File   : SALOME_ContainerPy.py
27 #  Author : Paul RASCLE, EDF
28 #  Module : SALOME
29 #  $Header$
30
31 import os
32 import sys
33 import string
34 from omniORB import CORBA, PortableServer
35 # import SALOMEDS before other SALOME modules
36 # (if not, incomplete import done by SALOME module: no load of SALOMEDS_attributes)
37 import SALOMEDS 
38 import Engines, Engines__POA
39 reload(Engines)
40 reload(Engines__POA)
41 from SALOME_NamingServicePy import *
42 from SALOME_ComponentPy import *
43
44 from SALOME_utilities import *
45 from Utils_Identity import getShortHostName
46
47 #=============================================================================
48
49 #define an implementation of the container interface
50
51 class SALOME_ContainerPy_i (Engines__POA.Container):
52     _orb = None
53     _poa = None
54     _numInstance = 0
55     _listInstances_map = {}
56
57     #-------------------------------------------------------------------------
58
59     def __init__(self, orb, poa, containerName):
60         MESSAGE( "SALOME_ContainerPy_i::__init__" )
61         self._orb = orb
62         self._poa = poa
63         myMachine=getShortHostName()
64         Container_path = "/Containers/" + myMachine + "/" + containerName
65         #self._containerName = containerName
66         self._containerName = Container_path
67         print "container name ",self._containerName
68
69         naming_service = SALOME_NamingServicePy_i(self._orb)
70         self._naming_service = naming_service
71         MESSAGE( str(Container_path) )
72         naming_service.Register(self._this(), Container_path)
73             
74     #-------------------------------------------------------------------------
75
76     def start_impl(self, ContainerName):
77         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
78         myMachine=getShortHostName()
79         theContainer = "/Containers/" + myMachine + "/" + ContainerName
80         try:
81             obj = self._naming_service.Resolve(theContainer)
82         except :
83             obj = None
84             MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
85         if obj is None:
86             container = None
87         else:
88             container = obj._narrow(Engines.Container)
89             if container is None:
90                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
91             else :
92                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
93             return container
94         #shstr = os.getenv( "PWD" ) + "/"
95         #shstr += "runSession ./SALOME_ContainerPy.py "
96         shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
97         #shstr = "runSession SALOME_ContainerPy.py "
98         shstr += ContainerName
99
100         # mpv: fix for SAL4731 - allways create new file to write log of server
101         num = 1
102         fileName = ""
103         while 1:
104             fileName = "/tmp/"+ContainerName+"_%i.log"%num
105             if not os.path.exists(fileName):
106                 break
107             num += 1
108             pass
109         
110         shstr += " > "
111         shstr += fileName
112         shstr += " 2>&1 &"
113         
114         #shstr += " > /tmp/"
115         #shstr += ContainerName
116         #shstr += ".log 2>&1 &"
117         
118         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
119         os.system( shstr )
120         count = 21
121         while container is None :
122             time.sleep(1)
123             count = count - 1
124             MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
125             try :
126                 obj = self._naming_service.Resolve(theContainer)
127             except :
128                 obj = None
129             if obj is None:
130                 container = None
131             else:
132                 container = obj._narrow(Engines.Container)
133                 if container is None:
134                     MESSAGE(  str(containerName) + ".object exists but is not a Container" )
135                 return container
136             if count == 0 :
137                 return container
138
139     #-------------------------------------------------------------------------
140
141     def instance(self, nameToRegister, componentName):
142         MESSAGE(  "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
143         self._numInstance = self._numInstance +1
144         instanceName = nameToRegister + "_inst_" + `self._numInstance`
145
146         component=__import__(componentName)
147         factory=getattr(component,componentName)
148         comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
149                        instanceName, nameToRegister)
150
151         MESSAGE( "SALOME_ContainerPy_i::instance : component created")
152         comp_o = comp_i._this()
153         return comp_o
154
155     #-------------------------------------------------------------------------
156
157     def load_impl(self, nameToRegister, componentName):
158         MESSAGE(  "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
159         self._numInstance = self._numInstance +1
160         instanceName = nameToRegister + "_inst_" + `self._numInstance`
161         interfaceName = nameToRegister
162         the_command = "import " + nameToRegister + "\n"
163         the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
164         the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
165         MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
166         exec the_command
167         comp_o = comp_i._this()
168         return comp_o
169     
170     #-------------------------------------------------------------------------
171     
172     def import_component(self, componentName):
173         MESSAGE( "SALOME_Container_i::import_component" )
174         ret=0
175         try:
176             print "try import ",componentName
177             __import__(componentName)
178             print "import ",componentName," successful"
179             ret=1
180         except:
181             import traceback
182             traceback.print_exc()
183             print "import ",componentName," not possible"
184         return ret
185
186     #-------------------------------------------------------------------------
187
188     def load_component_Library(self, componentName):
189         MESSAGE(  "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
190         ret = 0
191         instanceName = componentName + "_inst_" + `self._numInstance`
192         interfaceName = componentName
193         #the_command = "import " + componentName + "\n"
194         #the_command = the_command + "comp_i = " + componentName + "." + componentName
195         #the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
196         #MESSAGE( "SALOME_ContainerPy_i::load_component_Library :" + str (the_command) )
197         #exec the_command
198         #comp_o = comp_i._this()
199         #if comp_o is not None:
200         #    ret = 1
201         #else:
202             # --- try to import Python component
203         #    retImpl = self.import_component(componentName)
204         #    if retImpl == 1:
205                 #import is possible
206         #        ret = 1
207         #    else:
208                 #import isn't possible
209         #        ret = 0
210         #return ret
211         return self.import_component(componentName)
212     
213     #-------------------------------------------------------------------------
214
215     def create_component_instance(self, componentName, studyId):
216         MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
217         if studyId < 0:
218             MESSAGE( "Study ID is lower than 0!" )
219             return None
220         else:
221             self._numInstance = self._numInstance +1
222             instanceName = componentName + "_inst_" + `self._numInstance`
223             comp_iors=""
224             try:
225                 component=__import__(componentName)
226                 factory=getattr(component,componentName)
227                 comp_i=factory(self._orb,
228                                self._poa,
229                                self._this(),
230                                self._containerName,
231                                instanceName,
232                                componentName)
233                 
234                 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
235                 comp_o = comp_i._this()
236                 self._listInstances_map[instanceName] = comp_i
237             except:
238                 import traceback
239                 traceback.print_exc()
240                 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
241             return comp_o
242
243     #-------------------------------------------------------------------------
244
245     def find_component_instance(self, registeredName, studyId):
246         anEngine = None
247         keysList = self._listInstances_map.keys()
248         i = 0
249         while i < len(keysList):
250             instance = keysList[i]
251             if find(instance,registeredName) == 0:
252                 anEngine = self._listInstances_map[instance]
253                 if studyId == anEngine.getStudyId():
254                     return anEngine._this()
255             i = i + 1
256         return anEngine._this()
257         
258         
259     #-------------------------------------------------------------------------
260
261     def remove_impl(self, component):
262         MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
263         instanceName = component._get_instanceName()
264         MESSAGE( "unload component " + str(instanceName) )
265         self._listInstances_map.remove(instanceName)
266         component.destroy()
267         self._naming_service.Destroy_Name(str(instanceName))
268
269     #-------------------------------------------------------------------------
270
271     def finalize_removal(self):
272         MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
273         return None
274
275     #-------------------------------------------------------------------------
276
277     def ping(self):
278         MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
279         return None
280
281     #-------------------------------------------------------------------------
282
283     def getPID(self):
284         return os.getpid()
285
286     #-------------------------------------------------------------------------
287
288     def _get_name(self):
289         MESSAGE( "SALOME_ContainerPy_i::_get_name" )
290         return self._containerName
291
292     #-------------------------------------------------------------------------
293
294     def getHostName(self):
295         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
296         self._machineName = "localhost"
297         return self._machineName
298
299     #-------------------------------------------------------------------------
300     
301     def _get_machineName(self):
302         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
303         self._machineName = "localhost"
304         return self._machineName
305
306     #-------------------------------------------------------------------------
307
308     def Shutdown(self):
309         self._orb.shutdown(0)
310         pass
311
312 #=============================================================================
313
314 #initialise the ORB and find the root POA
315 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
316 poa = orb.resolve_initial_references("RootPOA")
317
318 #create an instance of SALOME_ContainerPy_i and a Container reference
319 #containerName = "FactoryServerPy"
320 MESSAGE( str(sys.argv) )
321 containerName = sys.argv[1]
322 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
323 cpy_o = cpy_i._this()
324
325 #activate the POA
326 poaManager = poa._get_the_POAManager()
327 poaManager.activate()
328
329 #Block for ever
330 orb.run()
331
332
333         
334             
335
336