1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024 CEA, EDF, 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, or (at your option) any later version.
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
24 # SALOME Container : implementation of container and engine for Kernel
25 # File : SALOME_ComponentPy.py
26 # Author : Paul RASCLE, EDF
30 ## @package SALOME_ComponentPy
31 # \brief python implementation of component interface for Kernel
39 from omniORB import CORBA, PortableServer, any
40 import Engines, Engines__POA
42 from Utils_Identity import *
43 from SALOME_NamingServicePy import *
44 from SALOME_Embedded_NamingService_ClientPy import SALOME_Embedded_NamingService_ClientPy
45 from libNOTIFICATION import *
47 from SALOME_utilities import *
51 #=============================================================================
55 ## define an implementation of the component interface Engines::Component
58 class SALOME_ComponentPy_Gen_i (Engines__POA.EngineComponent):
60 Implementation Without naming_service server
66 #-------------------------------------------------------------------------
68 def __init__ (self, orb, poa, contID, containerName,
69 instanceName, interfaceName, notif=False):
70 # Notif for notification services
72 MESSAGE( "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
75 self._instanceName = instanceName
76 self._interfaceName = interfaceName
77 self._containerName = containerName
79 self._myConnexionToRegistry = 0
82 self._serviceName = ''
85 self._ThreadCpuUsed = 0
89 myMachine=getShortHostName()
90 id_o = poa.activate_object(self)
91 self._compo_o = poa.id_to_reference(id_o)
92 self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, self._notif)
94 def getCorbaRef(self):
97 #-------------------------------------------------------------------------
99 def _get_instanceName(self):
100 MESSAGE( "SALOME_ComponentPy_i::_get_instanceName" )
101 return self._instanceName
103 #-------------------------------------------------------------------------
105 def _get_interfaceName(self):
106 MESSAGE( "SALOME_ComponentPy_i::_get_interfaceName" )
107 return self._interfaceName
109 #-------------------------------------------------------------------------
112 MESSAGE( "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
114 #-------------------------------------------------------------------------
116 def setProperties(self, dico):
117 self._fieldsDict = dico
119 #-------------------------------------------------------------------------
121 def getProperties(self):
122 return self._fieldsDict
124 #-------------------------------------------------------------------------
127 MESSAGE( "SALOME_ComponentPy_i::destroy" )
128 id = self._poa.servant_to_id(self)
129 self._poa.deactivate_object(id)
132 #-------------------------------------------------------------------------
134 def GetContainerRef(self):
135 MESSAGE( "SALOME_ComponentPy_i::GetContainerRef" )
136 return self._contId._narrow(Engines.Container)
138 #-------------------------------------------------------------------------
140 def beginService(self , serviceName ):
141 MESSAGE( "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
142 MESSAGE( "Component instance : " + str ( self._instanceName ) )
143 self._serviceName = str(serviceName)
144 self._ThreadId = get_ident()
146 self._StartUsed = self.CpuUsed_impl()
147 self._ThreadCpuUsed = 0
149 print("beginService for ",serviceName," Component instance : ",self._instanceName)
150 MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
151 for e in self._fieldsDict:
153 value=any.from_any(e.value)
154 if isinstance(value,str):
155 os.environ[key]=value
158 #-------------------------------------------------------------------------
160 def endService(self , serviceName ):
161 MESSAGE( "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
162 MESSAGE( "Component instance : " + str(self._instanceName) )
163 print("endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) ")
166 #-------------------------------------------------------------------------
168 def sendMessage(self , event_type , message ):
169 self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
171 #-------------------------------------------------------------------------
173 def Names(self , GraphName , NodeName ):
174 MESSAGE( "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
175 self._graphName = GraphName
176 self._nodeName = NodeName
178 #-------------------------------------------------------------------------
181 return self._graphName
183 #-------------------------------------------------------------------------
186 return self._nodeName
188 #-------------------------------------------------------------------------
190 def Killer(self, ThreadId, signum):
193 #if pthread_cancel(ThreadId): <- from C++
198 #if pthread_kill(ThreadId): <- from C++
204 #-------------------------------------------------------------------------
207 MESSAGE( "SALOME_ComponentPy_i::Kill_impl" )
209 if self._ThreadId > 0 & self._ThreadId != get_ident():
210 RetVal = Killer(self._ThreadId,0)
214 #-------------------------------------------------------------------------
217 MESSAGE( "SALOME_ComponentPy_i::Stop_impl" )
219 if self._ThreadId > 0 & self._ThreadId != get_ident():
220 RetVal = Killer(self._ThreadId,0)
224 #-------------------------------------------------------------------------
226 def Suspend_impl(self):
227 MESSAGE( "SALOME_ComponentPy_i::Suspend_impl" )
230 if self._ThreadId > 0 & self._ThreadId != get_ident():
234 RetVal = Killer(self._ThreadId, signal.SIGINT)
239 #-------------------------------------------------------------------------
241 def Resume_impl(self):
242 MESSAGE( "SALOME_ComponentPy_i::Resume_impl" )
245 if self._ThreadId > 0 & self._ThreadId != get_ident():
253 #-------------------------------------------------------------------------
255 def CpuUsed_impl(self):
256 if ( self._ThreadId | self._Executed ) :
257 if self._ThreadId == get_ident() :
258 cpu = time.process_time()
259 self._ThreadCpuUsed = cpu - self._StartUsed
260 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
261 return self._ThreadCpuUsed
262 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
263 return self._ThreadCpuUsed
264 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
267 #-------------------------------------------------------------------------
269 def DumpPython(self, isPublished, isMultiFile):
272 aBuffer = "def RebuildData(): pass\n\0"
273 return (aBuffer.encode(), 1)
275 #-------------------------------------------------------------------------
277 def hasObjectInfo(self):
280 #-------------------------------------------------------------------------
282 def getObjectInfo(self, entry):
285 #-------------------------------------------------------------------------
287 def getVersion(self):
288 return "" # empty string means "unknown" version
290 #-------------------------------------------------------------------------
292 pass # end of SALOME_ComponentPy_i
294 class SALOME_ComponentPy_i(SALOME_ComponentPy_Gen_i):
296 Implementation with naming_service server
298 def __init__ (self, orb, poa, contID, containerName, instanceName, interfaceName, notif=False):
299 SALOME_ComponentPy_Gen_i.__init__(self, orb, poa, contID, containerName, instanceName, interfaceName, notif)
300 emb_ns = self._contId.get_embedded_NS_if_ssl()
301 if CORBA.is_nil(emb_ns):
302 self._naming_service = SALOME_NamingServicePy_i(self._orb)
304 self._naming_service = SALOME_Embedded_NamingService_ClientPy(emb_ns)
305 Component_path = contID.name + "/" + self._instanceName#self._containerName
306 MESSAGE( 'SALOME_ComponentPy_i Register' + str( Component_path ) )
307 self._naming_service.Register(self._compo_o, Component_path)
308 # Add componentinstance to registry
309 obj = self._naming_service.Resolve('/Registry')
311 MESSAGE( "Registry Reference is invalid" )
313 regist = obj._narrow(Registry.Components)
315 MESSAGE( "Registry Reference is invalid" )
317 ior = orb.object_to_string(contID)
320 lesInfos = Identity(self._instanceName)
321 infos = Registry.Infos(lesInfos._name,
327 int(lesInfos._tc_start),
333 res = regist.add(infos)