Salome HOME
Integrate missing files
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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
35 import omnipatch                     # PAL10310
36     
37 from omniORB import CORBA, PortableServer
38 import SALOMEDS 
39 import Engines, Engines__POA
40 from SALOME_NamingServicePy import *
41 from SALOME_ComponentPy import *
42
43 from SALOME_utilities import *
44 from Utils_Identity import getShortHostName
45
46 #=============================================================================
47
48 #define an implementation of the container interface
49
50 class SALOME_ContainerPy_i (Engines__POA.Container):
51     _orb = None
52     _poa = None
53     _numInstance = 0
54     _listInstances_map = {}
55
56     #-------------------------------------------------------------------------
57
58     def __init__(self, orb, poa, containerName):
59         MESSAGE( "SALOME_ContainerPy_i::__init__" )
60         self._orb = orb
61         self._poa = poa
62         myMachine=getShortHostName()
63         Container_path = "/Containers/" + myMachine + "/" + containerName
64         #self._containerName = containerName
65         self._containerName = Container_path
66         print "container name ",self._containerName
67
68         naming_service = SALOME_NamingServicePy_i(self._orb)
69         self._naming_service = naming_service
70         MESSAGE( str(Container_path) )
71         naming_service.Register(self._this(), Container_path)
72             
73     #-------------------------------------------------------------------------
74
75     def start_impl(self, ContainerName):
76         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
77         myMachine=getShortHostName()
78         theContainer = "/Containers/" + myMachine + "/" + ContainerName
79         try:
80             obj = self._naming_service.Resolve(theContainer)
81         except :
82             obj = None
83             MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
84         if obj is None:
85             container = None
86         else:
87             container = obj._narrow(Engines.Container)
88             if container is None:
89                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
90             else :
91                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
92             return container
93         #shstr = os.getenv( "PWD" ) + "/"
94         #shstr += "runSession ./SALOME_ContainerPy.py "
95         shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
96         #shstr = "runSession SALOME_ContainerPy.py "
97         shstr += ContainerName
98
99         # mpv: fix for SAL4731 - allways create new file to write log of server
100         num = 1
101         fileName = ""
102         while 1:
103             fileName = "/tmp/"+ContainerName+"_%i.log"%num
104             if not os.path.exists(fileName):
105                 break
106             num += 1
107             pass
108         
109         shstr += " > "
110         shstr += fileName
111         shstr += " 2>&1 &"
112         
113         #shstr += " > /tmp/"
114         #shstr += ContainerName
115         #shstr += ".log 2>&1 &"
116         
117         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
118         os.system( shstr )
119         count = 21
120         while container is None :
121             time.sleep(1)
122             count = count - 1
123             MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
124             try :
125                 obj = self._naming_service.Resolve(theContainer)
126             except :
127                 obj = None
128             if obj is None:
129                 container = None
130             else:
131                 container = obj._narrow(Engines.Container)
132                 if container is None:
133                     MESSAGE(  str(containerName) + ".object exists but is not a Container" )
134                 return container
135             if count == 0 :
136                 return container
137
138     #-------------------------------------------------------------------------
139
140     def instance(self, nameToRegister, componentName):
141         MESSAGE(  "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
142         self._numInstance = self._numInstance +1
143         instanceName = nameToRegister + "_inst_" + `self._numInstance`
144
145         component=__import__(componentName)
146         factory=getattr(component,componentName)
147         comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
148                        instanceName, nameToRegister)
149
150         MESSAGE( "SALOME_ContainerPy_i::instance : component created")
151         comp_o = comp_i._this()
152         return comp_o
153
154     #-------------------------------------------------------------------------
155
156     def load_impl(self, nameToRegister, componentName):
157         MESSAGE(  "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
158         self._numInstance = self._numInstance +1
159         instanceName = nameToRegister + "_inst_" + `self._numInstance`
160         interfaceName = nameToRegister
161         the_command = "import " + nameToRegister + "\n"
162         the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
163         the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
164         MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
165         exec the_command
166         comp_o = comp_i._this()
167         return comp_o
168     
169     #-------------------------------------------------------------------------
170     
171     def import_component(self, componentName):
172         MESSAGE( "SALOME_Container_i::import_component" )
173         ret=0
174         try:
175             print "try import ",componentName
176             __import__(componentName)
177             print "import ",componentName," successful"
178             ret=1
179         except:
180             import traceback
181             traceback.print_exc()
182             print "import ",componentName," not possible"
183         return ret
184
185     #-------------------------------------------------------------------------
186
187     def load_component_Library(self, componentName):
188         MESSAGE(  "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
189         ret = 0
190         instanceName = componentName + "_inst_" + `self._numInstance`
191         interfaceName = componentName
192         #the_command = "import " + componentName + "\n"
193         #the_command = the_command + "comp_i = " + componentName + "." + componentName
194         #the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
195         #MESSAGE( "SALOME_ContainerPy_i::load_component_Library :" + str (the_command) )
196         #exec the_command
197         #comp_o = comp_i._this()
198         #if comp_o is not None:
199         #    ret = 1
200         #else:
201             # --- try to import Python component
202         #    retImpl = self.import_component(componentName)
203         #    if retImpl == 1:
204                 #import is possible
205         #        ret = 1
206         #    else:
207                 #import isn't possible
208         #        ret = 0
209         #return ret
210         return self.import_component(componentName)
211     
212     #-------------------------------------------------------------------------
213
214     def create_component_instance(self, componentName, studyId):
215         MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
216         if studyId < 0:
217             MESSAGE( "Study ID is lower than 0!" )
218             return None
219         else:
220             self._numInstance = self._numInstance +1
221             instanceName = componentName + "_inst_" + `self._numInstance`
222             comp_iors=""
223             try:
224                 component=__import__(componentName)
225                 factory=getattr(component,componentName)
226                 comp_i=factory(self._orb,
227                                self._poa,
228                                self._this(),
229                                self._containerName,
230                                instanceName,
231                                componentName)
232                 
233                 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
234                 comp_o = comp_i._this()
235                 self._listInstances_map[instanceName] = comp_i
236             except:
237                 import traceback
238                 traceback.print_exc()
239                 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
240             return comp_o
241
242     #-------------------------------------------------------------------------
243
244     def find_component_instance(self, registeredName, studyId):
245         anEngine = None
246         keysList = self._listInstances_map.keys()
247         i = 0
248         while i < len(keysList):
249             instance = keysList[i]
250             if find(instance,registeredName) == 0:
251                 anEngine = self._listInstances_map[instance]
252                 if studyId == anEngine.getStudyId():
253                     return anEngine._this()
254             i = i + 1
255         return anEngine._this()
256         
257         
258     #-------------------------------------------------------------------------
259
260     def remove_impl(self, component):
261         MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
262         instanceName = component._get_instanceName()
263         MESSAGE( "unload component " + str(instanceName) )
264         self._listInstances_map.remove(instanceName)
265         component.destroy()
266         self._naming_service.Destroy_Name(str(instanceName))
267
268     #-------------------------------------------------------------------------
269
270     def finalize_removal(self):
271         MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
272         return None
273
274     #-------------------------------------------------------------------------
275
276     def ping(self):
277         MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
278         return None
279
280     #-------------------------------------------------------------------------
281
282     def getPID(self):
283         return os.getpid()
284
285     #-------------------------------------------------------------------------
286
287     def _get_name(self):
288         MESSAGE( "SALOME_ContainerPy_i::_get_name" )
289         return self._containerName
290
291     #-------------------------------------------------------------------------
292
293     def getHostName(self):
294         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
295         self._machineName = "localhost"
296         return self._machineName
297
298     #-------------------------------------------------------------------------
299     
300     def _get_machineName(self):
301         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
302         self._machineName = "localhost"
303         return self._machineName
304
305     #-------------------------------------------------------------------------
306
307     def Shutdown(self):
308         self._orb.shutdown(0)
309         pass
310
311 #=============================================================================
312
313 #initialise the ORB and find the root POA
314 print "Starting ",sys.argv[1]
315 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
316 poa = orb.resolve_initial_references("RootPOA")
317 print "ORB and POA initialized"
318
319 #create an instance of SALOME_ContainerPy_i and a Container reference
320 #containerName = "FactoryServerPy"
321 MESSAGE( str(sys.argv) )
322 containerName = sys.argv[1]
323 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
324 print "SALOME_ContainerPy_i instance created ",cpy_i 
325 cpy_o = cpy_i._this()
326 print "SALOME_ContainerPy_i instance activated ",cpy_o
327
328 #activate the POA
329 poaManager = poa._get_the_POAManager()
330 poaManager.activate()
331
332 #Block for ever
333 orb.run()
334
335
336         
337             
338
339