1 #! /usr/bin/env python3
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 # SALOME Container : implementation of container and engine for Kernel
26 # File : SALOME_ContainerPy.py
27 # Author : Paul RASCLE, EDF
35 from omniORB import CORBA, PortableServer
37 import Engines, Engines__POA
38 from SALOME_NamingServicePy import *
39 from SALOME_ComponentPy import *
41 from SALOME_utilities import *
42 from Utils_Identity import getShortHostName
43 from launchConfigureParser import verbose
45 #=============================================================================
47 #define an implementation of the container interface for the container implemented in Python
49 class SALOME_ContainerPy_Gen_i(Engines__POA.Container):
51 Implementation without naming_service server
56 _listInstances_map = {}
58 #-------------------------------------------------------------------------
60 def __init__(self, orb, poa, containerName):
61 MESSAGE( "SALOME_ContainerPy_i::__init__" )
64 myMachine=getShortHostName()
65 Container_path = "/Containers/" + myMachine + "/" + containerName
66 self._containerName = Container_path
67 if verbose(): print("container name ",self._containerName)
69 #-------------------------------------------------------------------------
71 def instance(self, nameToRegister, componentName):
72 MESSAGE( "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
73 self._numInstance = self._numInstance +1
74 instanceName = nameToRegister + "_inst_" + repr(self._numInstance)
76 component=__import__(componentName)
77 factory=getattr(component,componentName)
78 comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
79 instanceName, nameToRegister)
81 MESSAGE( "SALOME_ContainerPy_i::instance : component created")
82 comp_o = comp_i._this()
85 #-------------------------------------------------------------------------
87 def load_impl(self, nameToRegister, componentName):
88 MESSAGE( "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
89 self._numInstance = self._numInstance +1
90 instanceName = nameToRegister + "_inst_" + repr(self._numInstance)
91 interfaceName = nameToRegister
92 the_command = "import " + nameToRegister + "\n"
93 the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
94 the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
95 MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
97 comp_o = comp_i._this()
100 #-------------------------------------------------------------------------
102 def import_component(self, componentName):
103 MESSAGE( "SALOME_Container_i::import_component" )
106 if verbose(): print("try import %s" % componentName)
107 # try import component
108 module=__import__(componentName)
109 if verbose(): print("import %s is done successfully" % componentName)
110 # if import successfully, check that component is loadable
111 if not hasattr(module, componentName):
112 reason = "module %s is not loadable" % componentName
118 print("cannot import %s" % componentName)
119 traceback.print_exc()
120 reason = "cannot import %s" % componentName
123 #-------------------------------------------------------------------------
125 def load_component_Library(self, componentName):
126 MESSAGE( "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
128 instanceName = componentName + "_inst_" + repr(self._numInstance)
129 interfaceName = componentName
130 reason = self.import_component(componentName)
131 return reason == "", reason
133 #-------------------------------------------------------------------------
135 def create_component_instance_env(self, componentName, env):
136 return self.create_component_instance(componentName), ""
138 def create_component_instance(self, componentName):
139 MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) )
140 self._numInstance = self._numInstance +1
141 instanceName = componentName + "_inst_" + repr(self._numInstance)
144 component=__import__(componentName)
145 factory=getattr(component,componentName)
146 comp_i=factory(self._orb,
153 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
154 comp_o = comp_i._this()
155 self._listInstances_map[instanceName] = comp_i
158 traceback.print_exc()
159 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
162 #-------------------------------------------------------------------------
164 def find_component_instance(self, registeredName):
166 for instance in self._listInstances_map:
167 if find(instance,registeredName) == 0:
168 anEngine = self._listInstances_map[instance]
169 return anEngine._this()
173 #-------------------------------------------------------------------------
175 def create_python_service_instance(self, CompName):
176 return self.create_component_instance(CompName)
178 #-------------------------------------------------------------------------
180 def remove_impl(self, component):
181 MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
182 instanceName = component._get_instanceName()
183 MESSAGE( "unload component " + str(instanceName) )
184 self._listInstances_map.remove(instanceName)
186 self._naming_service.Destroy_Name(str(instanceName))
188 #-------------------------------------------------------------------------
190 def finalize_removal(self):
191 MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
194 #-------------------------------------------------------------------------
197 MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
200 #-------------------------------------------------------------------------
205 #-------------------------------------------------------------------------
208 MESSAGE( "SALOME_ContainerPy_i::_get_name" )
209 return self._containerName
211 #-------------------------------------------------------------------------
213 def getHostName(self):
214 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
215 self._machineName = "localhost"
216 return self._machineName
218 #-------------------------------------------------------------------------
220 def _get_machineName(self):
221 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
222 self._machineName = "localhost"
223 return self._machineName
225 #-------------------------------------------------------------------------
228 self._naming_service.Destroy_Name(self._containerName);
229 self._naming_service.Destroy_FullDirectory(self._containerName);
230 self._orb.shutdown(0)
233 def _get_logfilename(self):
234 return self._logfilename
235 def _set_logfilename(self,logfilename):
236 self._logfilename=logfilename
237 def _get_workingdir(self):
240 #=============================================================================
242 class SALOME_ContainerPy_i(SALOME_ContainerPy_Gen_i):
244 Implementation with naming_service server
246 def __init__(self, orb, poa, containerName):
247 SALOME_ContainerPy_Gen_i.__init__(self, orb, poa, containerName)
248 naming_service = SALOME_NamingServicePy_i(self._orb)
249 self._naming_service = naming_service
250 MESSAGE( str(Container_path) )
251 naming_service.Register(self._this(), Container_path)
253 #-------------------------------------------------------------------------
255 def start_impl(self, ContainerName):
256 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
257 myMachine=getShortHostName()
258 theContainer = "/Containers/" + myMachine + "/" + ContainerName
260 obj = self._naming_service.Resolve(theContainer)
263 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
267 container = obj._narrow(Engines.Container)
268 if container is None:
269 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
271 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
273 #shstr = os.getenv( "PWD" ) + "/"
274 #shstr += "runSession ./SALOME_ContainerPy.py "
275 shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
276 #shstr = "runSession SALOME_ContainerPy.py "
277 shstr += ContainerName
279 # mpv: fix for SAL4731 - always create new file to write log of server
283 fileName = "/tmp/"+ContainerName+"_%i.log"%num
284 if not os.path.exists(fileName):
294 #shstr += ContainerName
295 #shstr += ".log 2>&1 &"
297 MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
300 while container is None :
303 MESSAGE( str(count) + ". Waiting for " + str(theContainer) )
305 obj = self._naming_service.Resolve(theContainer)
311 container = obj._narrow(Engines.Container)
312 if container is None:
313 MESSAGE( str(containerName) + ".object exists but is not a Container" )
320 if __name__ == "__main__":
321 # change the stdout buffering to line buffering (same as C++ cout buffering)
322 sys.stdout=os.fdopen(1,"w",1)
323 #initialise the ORB and find the root POA
324 if verbose():print("Starting ",sys.argv[1])
325 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
326 poa = orb.resolve_initial_references("RootPOA")
327 if verbose():print("ORB and POA initialized")
329 #create an instance of SALOME_ContainerPy_i and a Container reference
330 #containerName = "FactoryServerPy"
331 MESSAGE( str(sys.argv) )
332 containerName = sys.argv[1]
333 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
334 if verbose():print("SALOME_ContainerPy_i instance created ",cpy_i)
335 cpy_o = cpy_i._this()
336 if verbose():print("SALOME_ContainerPy_i instance activated ",cpy_o)
341 poaManager = poa._get_the_POAManager()
342 poaManager.activate()
346 if verbose():print("SALOME_ContainerPy_i shutdown")