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