2 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 # SALOME Container : implementation of container and engine for Kernel
24 # File : SALOME_ContainerPy.py
25 # Author : Paul RASCLE, EDF
33 from omniORB import CORBA, PortableServer
35 import Engines, Engines__POA
36 from SALOME_NamingServicePy import *
37 from SALOME_ComponentPy import *
39 from SALOME_utilities import *
40 from Utils_Identity import getShortHostName
41 from launchConfigureParser import verbose
43 #=============================================================================
45 #define an implementation of the container interface for the container implemented in Python
47 class SALOME_ContainerPy_i (Engines__POA.Container):
51 _listInstances_map = {}
53 #-------------------------------------------------------------------------
55 def __init__(self, orb, poa, containerName):
56 MESSAGE( "SALOME_ContainerPy_i::__init__" )
59 myMachine=getShortHostName()
60 Container_path = "/Containers/" + myMachine + "/" + containerName
61 self._containerName = Container_path
62 if verbose(): print "container name ",self._containerName
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)
69 #-------------------------------------------------------------------------
71 def start_impl(self, ContainerName):
72 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
73 myMachine=getShortHostName()
74 theContainer = "/Containers/" + myMachine + "/" + ContainerName
76 obj = self._naming_service.Resolve(theContainer)
79 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
83 container = obj._narrow(Engines.Container)
85 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
87 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
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
95 # mpv: fix for SAL4731 - allways create new file to write log of server
99 fileName = "/tmp/"+ContainerName+"_%i.log"%num
100 if not os.path.exists(fileName):
110 #shstr += ContainerName
111 #shstr += ".log 2>&1 &"
113 MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
116 while container is None :
119 MESSAGE( str(count) + ". Waiting for " + str(theContainer) )
121 obj = self._naming_service.Resolve(theContainer)
127 container = obj._narrow(Engines.Container)
128 if container is None:
129 MESSAGE( str(containerName) + ".object exists but is not a Container" )
134 #-------------------------------------------------------------------------
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`
141 component=__import__(componentName)
142 factory=getattr(component,componentName)
143 comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
144 instanceName, nameToRegister)
146 MESSAGE( "SALOME_ContainerPy_i::instance : component created")
147 comp_o = comp_i._this()
150 #-------------------------------------------------------------------------
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) )
162 comp_o = comp_i._this()
165 #-------------------------------------------------------------------------
167 def import_component(self, componentName):
168 MESSAGE( "SALOME_Container_i::import_component" )
171 if verbose(): print "try import ",componentName
172 module=__import__(componentName)
173 if verbose(): print "import ",componentName," successful"
177 traceback.print_exc()
178 print "import ",componentName," not possible"
181 #-------------------------------------------------------------------------
183 def load_component_Library(self, componentName):
184 MESSAGE( "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
186 instanceName = componentName + "_inst_" + `self._numInstance`
187 interfaceName = componentName
188 return self.import_component(componentName)
190 #-------------------------------------------------------------------------
192 def create_component_instance(self, componentName, studyId):
193 MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
195 MESSAGE( "Study ID is lower than 0!" )
198 self._numInstance = self._numInstance +1
199 instanceName = componentName + "_inst_" + `self._numInstance`
202 component=__import__(componentName)
203 factory=getattr(component,componentName)
204 comp_i=factory(self._orb,
211 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
212 comp_o = comp_i._this()
213 self._listInstances_map[instanceName] = comp_i
216 traceback.print_exc()
217 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
220 #-------------------------------------------------------------------------
222 def find_component_instance(self, registeredName, studyId):
224 keysList = self._listInstances_map.keys()
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()
233 return anEngine._this()
236 #-------------------------------------------------------------------------
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)
244 self._naming_service.Destroy_Name(str(instanceName))
246 #-------------------------------------------------------------------------
248 def finalize_removal(self):
249 MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
252 #-------------------------------------------------------------------------
255 MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
258 #-------------------------------------------------------------------------
263 #-------------------------------------------------------------------------
266 MESSAGE( "SALOME_ContainerPy_i::_get_name" )
267 return self._containerName
269 #-------------------------------------------------------------------------
271 def getHostName(self):
272 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
273 self._machineName = "localhost"
274 return self._machineName
276 #-------------------------------------------------------------------------
278 def _get_machineName(self):
279 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
280 self._machineName = "localhost"
281 return self._machineName
283 #-------------------------------------------------------------------------
286 self._naming_service.Destroy_Name(self._containerName);
287 self._naming_service.Destroy_FullDirectory(self._containerName);
288 self._orb.shutdown(0)
291 def _get_logfilename(self):
292 return self._logfilename
293 def _set_logfilename(self,logfilename):
294 self._logfilename=logfilename
295 def _get_workingdir(self):
298 #=============================================================================
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"
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
321 poaManager = poa._get_the_POAManager()
322 poaManager.activate()
326 if verbose():print "SALOME_ContainerPy_i shutdown"