2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2012 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.
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_ComponentPy.py
27 # Author : Paul RASCLE, EDF
31 ## @package SALOME_ComponentPy
32 # \brief python implementation of component interface for Kernel
40 from omniORB import CORBA, PortableServer, any
41 import Engines, Engines__POA
43 from Utils_Identity import *
44 from SALOME_NamingServicePy import *
45 from libNOTIFICATION import *
47 from SALOME_utilities import *
51 #=============================================================================
55 ## define an implementation of the component interface Engines::Component
58 class SALOME_ComponentPy_i (Engines__POA.EngineComponent):
64 #-------------------------------------------------------------------------
66 def __init__ (self, orb, poa, contID, containerName,
67 instanceName, interfaceName, notif=0):
68 # Notif for notification services
70 MESSAGE( "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
73 self._instanceName = instanceName
74 self._interfaceName = interfaceName
75 self._containerName = containerName
77 self._myConnexionToRegistry = 0
80 self._serviceName = ''
83 self._ThreadCpuUsed = 0
87 naming_service = SALOME_NamingServicePy_i(self._orb)
88 myMachine=getShortHostName()
89 Component_path = self._containerName + "/" + self._instanceName
90 MESSAGE( 'SALOME_ComponentPy_i Register' + str( Component_path ) )
91 id_o = poa.activate_object(self)
92 compo_o = poa.id_to_reference(id_o)
93 naming_service.Register(compo_o, Component_path)
95 # Add componentinstance to registry
96 obj = naming_service.Resolve('/Registry')
98 MESSAGE( "Registry Reference is invalid" )
100 regist = obj._narrow(Registry.Components)
102 MESSAGE( "Registry Reference is invalid" )
104 ior = orb.object_to_string(contID)
107 lesInfos = Identity(self._instanceName)
108 infos = Registry.Infos(lesInfos._name,
114 int(lesInfos._tc_start),
120 res = regist.add(infos)
122 self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
124 #-------------------------------------------------------------------------
126 def _get_instanceName(self):
127 MESSAGE( "SALOME_ComponentPy_i::_get_instanceName" )
128 return self._instanceName
130 #-------------------------------------------------------------------------
132 def _get_interfaceName(self):
133 MESSAGE( "SALOME_ComponentPy_i::_get_interfaceName" )
134 return self._interfaceName
136 #-------------------------------------------------------------------------
139 MESSAGE( "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
141 #-------------------------------------------------------------------------
143 def setProperties(self, dico):
144 self._fieldsDict = dico
146 #-------------------------------------------------------------------------
148 def getProperties(self):
149 return self._fieldsDict
151 #-------------------------------------------------------------------------
154 MESSAGE( "SALOME_ComponentPy_i::destroy" )
155 id = self._poa.servant_to_id(self)
156 self._poa.deactivate_object(id)
159 #-------------------------------------------------------------------------
161 def GetContainerRef(self):
162 MESSAGE( "SALOME_ComponentPy_i::GetContainerRef" )
163 return self._contId._narrow(Engines.Container)
165 #-------------------------------------------------------------------------
167 def beginService(self , serviceName ):
168 MESSAGE( "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
169 MESSAGE( "Component instance : " + str ( self._instanceName ) )
170 self._serviceName = str(serviceName)
171 self._ThreadId = get_ident()
173 self._StartUsed = self.CpuUsed_impl()
174 self._ThreadCpuUsed = 0
176 print "beginService for ",serviceName," Component instance : ",self._instanceName
177 MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
178 for e in self._fieldsDict:
180 value=any.from_any(e.value)
181 if isinstance(value,str):
182 os.environ[key]=value
185 #-------------------------------------------------------------------------
187 def endService(self , serviceName ):
188 MESSAGE( "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
189 MESSAGE( "Component instance : " + str(self._instanceName) )
190 print "endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) "
193 #-------------------------------------------------------------------------
195 def sendMessage(self , event_type , message ):
196 self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
198 #-------------------------------------------------------------------------
200 def Names(self , GraphName , NodeName ):
201 MESSAGE( "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
202 self._graphName = GraphName
203 self._nodeName = NodeName
205 #-------------------------------------------------------------------------
208 return self._graphName
210 #-------------------------------------------------------------------------
213 return self._nodeName
215 #-------------------------------------------------------------------------
217 def Killer(self, ThreadId, signum):
220 #if pthread_cancel(ThreadId): <- from C++
225 #if pthread_kill(ThreadId): <- from C++
231 #-------------------------------------------------------------------------
234 MESSAGE( "SALOME_ComponentPy_i::Kill_impl" )
236 if self._ThreadId > 0 & self._ThreadId != get_ident():
237 RetVal = Killer(self._ThreadId,0)
241 #-------------------------------------------------------------------------
244 MESSAGE( "SALOME_ComponentPy_i::Stop_impl" )
246 if self._ThreadId > 0 & self._ThreadId != get_ident():
247 RetVal = Killer(self._ThreadId,0)
251 #-------------------------------------------------------------------------
253 def Suspend_impl(self):
254 MESSAGE( "SALOME_ComponentPy_i::Suspend_impl" )
257 if self._ThreadId > 0 & self._ThreadId != get_ident():
261 RetVal = Killer(self._ThreadId, signal.SIGINT)
266 #-------------------------------------------------------------------------
268 def Resume_impl(self):
269 MESSAGE( "SALOME_ComponentPy_i::Resume_impl" )
272 if self._ThreadId > 0 & self._ThreadId != get_ident():
280 #-------------------------------------------------------------------------
282 def CpuUsed_impl(self):
283 if ( self._ThreadId | self._Executed ) :
284 if self._ThreadId == get_ident() :
286 self._ThreadCpuUsed = cpu - self._StartUsed
287 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
288 return self._ThreadCpuUsed
289 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
290 return self._ThreadCpuUsed
291 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
294 #-------------------------------------------------------------------------
296 def DumpPython(self, theStudy, isPublished, isMultiFile):
299 aBuffer = "def RebuildData(theStudy): pass\n\0"
302 #-------------------------------------------------------------------------
304 def getStudyId(self):
307 #-------------------------------------------------------------------------
309 def hasObjectInfo(self):
312 #-------------------------------------------------------------------------
314 def getObjectInfo(self, studyId, entry):