1 #! /usr/bin/env python3
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2024 CEA, EDF, 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
39 from SALOME_NamingServicePy import *
40 from SALOME_Embedded_NamingService import SALOME_Embedded_NamingService
41 from SALOME_ComponentPy import *
43 from SALOME_utilities import *
44 from Utils_Identity import getShortHostName
45 from launchConfigureParser import verbose
47 #=============================================================================
49 #define an implementation of the container interface for the container implemented in Python
51 class SALOME_ContainerPy_Gen_i(Engines__POA.Container):
53 Implementation without naming_service server
58 _listInstances_map = {}
61 #-------------------------------------------------------------------------
63 def __init__(self, orb, poa, containerName):
64 MESSAGE( "SALOME_ContainerPy_i::__init__" )
67 self._load_script = None
68 myMachine=getShortHostName()
69 Container_path = "/Containers/" + myMachine + "/" + containerName
70 self._containerName = Container_path
71 if verbose(): print("container name ",self._containerName)
73 #-------------------------------------------------------------------------
75 def instance(self, nameToRegister, componentName):
76 MESSAGE( "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
77 self._numInstance = self._numInstance +1
78 instanceName = nameToRegister + "_inst_" + repr(self._numInstance)
80 component=importlib.import_module(componentName)
81 factory=getattr(component,componentName)
82 comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
83 instanceName, nameToRegister)
85 MESSAGE( "SALOME_ContainerPy_i::instance : component created")
86 comp_o = comp_i._this()
89 #-------------------------------------------------------------------------
91 def load_impl(self, nameToRegister, componentName):
92 MESSAGE( "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
93 self._numInstance = self._numInstance +1
94 instanceName = nameToRegister + "_inst_" + repr(self._numInstance)
95 interfaceName = nameToRegister
96 the_command = "import " + nameToRegister + "\n"
97 the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
98 the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
99 MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
101 comp_o = comp_i._this()
104 #-------------------------------------------------------------------------
106 def import_component(self, componentName):
107 MESSAGE( "SALOME_Container_i::import_component" )
110 if verbose(): print("try import %s" % componentName)
111 # try import component
112 module=importlib.import_module(componentName)
113 if verbose(): print("import %s is done successfully" % componentName)
114 # if import successfully, check that component is loadable
115 if not hasattr(module, componentName):
116 reason = "module %s is not loadable" % componentName
122 print("cannot import %s" % componentName)
123 traceback.print_exc()
124 reason = "cannot import %s" % componentName
127 #-------------------------------------------------------------------------
129 def load_component_Library(self, componentName):
130 MESSAGE( "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
132 instanceName = componentName + "_inst_" + repr(self._numInstance)
133 interfaceName = componentName
134 reason = self.import_component(componentName)
135 return reason == "", reason
137 #-------------------------------------------------------------------------
139 def create_component_instance_env(self, componentName, env):
140 return self.create_component_instance(componentName), ""
142 def create_component_instance(self, componentName):
143 MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) )
144 self._numInstance = self._numInstance +1
145 instanceName = componentName + "_inst_" + repr(self._numInstance)
148 component=importlib.import_module(componentName)
149 factory=getattr(component,componentName)
150 comp_i=factory(self._orb,
157 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
158 comp_o = comp_i._this()
159 self._listInstances_map[instanceName] = comp_i
162 traceback.print_exc()
163 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
166 #-------------------------------------------------------------------------
168 def find_component_instance(self, registeredName):
170 for instance in self._listInstances_map:
171 if find(instance,registeredName) == 0:
172 anEngine = self._listInstances_map[instance]
173 return anEngine._this()
177 #-------------------------------------------------------------------------
179 def create_python_service_instance(self, CompName):
180 return self.create_component_instance(CompName)
182 #-------------------------------------------------------------------------
184 def remove_impl(self, component):
185 MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
186 instanceName = component._get_instanceName()
187 MESSAGE( "unload component " + str(instanceName) )
188 self._listInstances_map.remove(instanceName)
190 self._naming_service.Destroy_Name(str(instanceName))
192 #-------------------------------------------------------------------------
194 def finalize_removal(self):
195 MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
198 #-------------------------------------------------------------------------
201 MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
204 #-------------------------------------------------------------------------
209 #-------------------------------------------------------------------------
211 def getNumberOfCPUCores(self):
212 return salome_psutil.getNumberOfCPUCores()
214 #-------------------------------------------------------------------------
216 def loadOfCPUCores(self):
217 return salome_psutil.loadOfCPUCores(self._load_script)
219 #-------------------------------------------------------------------------
221 def setPyScriptForCPULoad(self, script):
222 self._load_script = script
224 #-------------------------------------------------------------------------
226 def resetScriptForCPULoad(self):
227 self._load_script = None
229 #-------------------------------------------------------------------------
231 def getTotalPhysicalMemory(self):
232 return salome_psutil.getTotalPhysicalMemory()
234 #-------------------------------------------------------------------------
236 def getTotalPhysicalMemoryInUse(self):
237 return salome_psutil.getTotalPhysicalMemoryInUse()
239 #-------------------------------------------------------------------------
241 def getTotalPhysicalMemoryInUseByMe(self):
242 return salome_psutil.getTotalPhysicalMemoryInUseByMe()
244 #-------------------------------------------------------------------------
247 MESSAGE( "SALOME_ContainerPy_i::_get_name" )
248 return self._containerName
250 #-------------------------------------------------------------------------
252 def getHostName(self):
253 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
254 self._machineName = "localhost"
255 return self._machineName
257 #-------------------------------------------------------------------------
259 def _get_machineName(self):
260 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
261 self._machineName = "localhost"
262 return self._machineName
264 #-------------------------------------------------------------------------
267 self._naming_service.Destroy_Name(self._containerName);
268 self._naming_service.Destroy_FullDirectory(self._containerName);
269 self._orb.shutdown(0)
272 def _get_logfilename(self):
273 return self._logfilename
274 def _set_logfilename(self,logfilename):
275 self._logfilename=logfilename
276 def _get_workingdir(self):
279 #=============================================================================
281 class SALOME_ContainerPy_i(SALOME_ContainerPy_Gen_i):
283 Implementation with naming_service server
285 def __init__(self, orb, poa, containerName):
286 SALOME_ContainerPy_Gen_i.__init__(self, orb, poa, containerName)
287 naming_service = SALOME_NamingServicePy_i(self._orb)
288 self._naming_service = naming_service
289 MESSAGE( str(Container_path) )
290 naming_service.Register(self._this(), Container_path)
292 #-------------------------------------------------------------------------
294 def start_impl(self, ContainerName):
295 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
296 myMachine=getShortHostName()
297 theContainer = "/Containers/" + myMachine + "/" + ContainerName
299 obj = self._naming_service.Resolve(theContainer)
302 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
306 container = obj._narrow(Engines.Container)
307 if container is None:
308 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
310 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
312 #shstr = os.getenv( "PWD" ) + "/"
313 #shstr += "runSession ./SALOME_ContainerPy.py "
314 shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
315 #shstr = "runSession SALOME_ContainerPy.py "
316 shstr += ContainerName
318 # mpv: fix for SAL4731 - always create new file to write log of server
322 fileName = "/tmp/"+ContainerName+"_%i.log"%num
323 if not os.path.exists(fileName):
333 #shstr += ContainerName
334 #shstr += ".log 2>&1 &"
336 MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
339 while container is None :
342 MESSAGE( str(count) + ". Waiting for " + str(theContainer) )
344 obj = self._naming_service.Resolve(theContainer)
350 container = obj._narrow(Engines.Container)
351 if container is None:
352 MESSAGE( str(containerName) + ".object exists but is not a Container" )
359 class SALOME_ContainerPy_SSL_i(SALOME_ContainerPy_Gen_i):
361 Implementation with naming_service server
363 def __init__(self, orb, poa, containerName):
364 SALOME_ContainerPy_Gen_i.__init__(self, orb, poa, containerName)
365 naming_service = SALOME_Embedded_NamingService()
366 self._naming_service = naming_service._this()
368 def get_embedded_NS_if_ssl(self):
369 return self._naming_service
371 if __name__ == "__main__":
372 # change the stdout buffering to line buffering (same as C++ cout buffering)
373 sys.stdout=os.fdopen(1,"w",1)
374 #initialise the ORB and find the root POA
375 if verbose():print("Starting ",sys.argv[1])
376 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
377 poa = orb.resolve_initial_references("RootPOA")
378 if verbose():print("ORB and POA initialized")
380 #create an instance of SALOME_ContainerPy_i and a Container reference
381 #containerName = "FactoryServerPy"
382 MESSAGE( str(sys.argv) )
383 containerName = sys.argv[1]
384 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
385 if verbose():print("SALOME_ContainerPy_i instance created ",cpy_i)
386 cpy_o = cpy_i._this()
387 if verbose():print("SALOME_ContainerPy_i instance activated ",cpy_o)
392 poaManager = poa._get_the_POAManager()
393 poaManager.activate()
397 if verbose():print("SALOME_ContainerPy_i shutdown")