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