Salome HOME
Copyright update 2020
[modules/kernel.git] / src / Container / SALOME_ComponentPy.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  SALOME Container : implementation of container and engine for Kernel
25 #  File   : SALOME_ComponentPy.py
26 #  Author : Paul RASCLE, EDF
27 #  Module : SALOME
28 #  $Header$
29
30 ## @package SALOME_ComponentPy
31 # \brief python implementation of component interface for Kernel
32 #
33
34 import os
35 import sys
36 import time
37 import string
38 import signal
39 from omniORB import CORBA, PortableServer, any
40 import Engines, Engines__POA
41 import Registry
42 from Utils_Identity import *
43 from SALOME_NamingServicePy import *
44 from libNOTIFICATION import *
45
46 from SALOME_utilities import *
47
48 from _thread import *
49
50 #=============================================================================
51
52 _Sleeping = 0
53
54 ## define an implementation of the component interface Engines::Component
55 #
56 #
57 class SALOME_ComponentPy_i (Engines__POA.EngineComponent):
58     _orb = None
59     _poa = None
60     _fieldsDict = []
61
62     #-------------------------------------------------------------------------
63
64     def __init__ (self, orb, poa, contID, containerName,
65                   instanceName, interfaceName, notif=False):
66         # Notif for notification services
67         # NOT YET IMPLEMENTED
68         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
69         self._orb = orb
70         self._poa = poa
71         self._instanceName = instanceName
72         self._interfaceName = interfaceName
73         self._containerName = containerName
74         self._notif = notif
75         self._myConnexionToRegistry = 0
76         self._graphName = ''
77         self._nodeName = ''
78         self._serviceName = ''
79         self._ThreadId = 0
80         self._StartUsed = 0
81         self._ThreadCpuUsed = 0
82         self._Executed = 0
83         self._contId = contID
84
85         naming_service = SALOME_NamingServicePy_i(self._orb)
86         myMachine=getShortHostName()
87         Component_path = self._containerName + "/" + self._instanceName
88         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
89         id_o = poa.activate_object(self)
90         compo_o = poa.id_to_reference(id_o)
91         naming_service.Register(compo_o, Component_path)
92
93         # Add componentinstance to registry
94         obj = naming_service.Resolve('/Registry')
95         if obj is None:
96             MESSAGE(  "Registry Reference is invalid" )
97         else:
98             regist = obj._narrow(Registry.Components)
99             if regist is None:
100                 MESSAGE(  "Registry Reference is invalid" )
101             else:
102                 ior = orb.object_to_string(contID)
103                 MESSAGE(  ior )
104
105                 lesInfos = Identity(self._instanceName)
106                 infos = Registry.Infos(lesInfos._name,
107                                        lesInfos._pid,
108                                        lesInfos._machine,
109                                        lesInfos._adip,
110                                        lesInfos._uid,
111                                        lesInfos._pwname,
112                                        int(lesInfos._tc_start),
113                                        0,0,0,
114                                        lesInfos._cdir,
115                                        -1,
116                                        ior)
117
118                 res = regist.add(infos)
119
120         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
121
122     #-------------------------------------------------------------------------
123
124     def _get_instanceName(self):
125         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
126         return self._instanceName
127
128     #-------------------------------------------------------------------------
129
130     def _get_interfaceName(self):
131         MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
132         return self._interfaceName
133
134     #-------------------------------------------------------------------------
135
136     def ping(self):
137         MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
138
139     #-------------------------------------------------------------------------
140
141     def setProperties(self, dico):
142         self._fieldsDict = dico
143
144     #-------------------------------------------------------------------------
145
146     def getProperties(self):
147         return self._fieldsDict
148
149     #-------------------------------------------------------------------------
150
151     def destroy(self):
152         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
153         id = self._poa.servant_to_id(self)
154         self._poa.deactivate_object(id)
155         return
156
157     #-------------------------------------------------------------------------
158
159     def GetContainerRef(self):
160         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
161         return self._contId._narrow(Engines.Container)
162
163     #-------------------------------------------------------------------------
164
165     def beginService(self , serviceName ):
166         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
167         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
168         self._serviceName = str(serviceName)
169         self._ThreadId = get_ident()
170         self._StartUsed = 0
171         self._StartUsed = self.CpuUsed_impl()
172         self._ThreadCpuUsed = 0
173         self._Executed = 1
174         print("beginService for ",serviceName," Component instance : ",self._instanceName)
175         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
176         for e in self._fieldsDict:
177           key=e.key
178           value=any.from_any(e.value)
179           if isinstance(value,str):
180             os.environ[key]=value
181
182
183     #-------------------------------------------------------------------------
184
185     def endService(self , serviceName ):
186         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
187         MESSAGE(  "Component instance : " + str(self._instanceName) )
188         print("endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) ")
189
190
191     #-------------------------------------------------------------------------
192
193     def sendMessage(self , event_type , message ):
194         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
195
196     #-------------------------------------------------------------------------
197
198     def Names(self , GraphName , NodeName ):
199         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
200         self._graphName = GraphName
201         self._nodeName = NodeName
202
203     #-------------------------------------------------------------------------
204
205     def graphName(self):
206         return self._graphName
207
208     #-------------------------------------------------------------------------
209
210     def nodeName(self):
211         return self._nodeName
212
213     #-------------------------------------------------------------------------
214
215     def Killer(self, ThreadId, signum):
216         #if ThreadId > 0:
217             #if signum == 0:
218                 #if pthread_cancel(ThreadId): <- from C++
219                 #   return 0
220                 #else:
221                 #   MESSAGE()
222             #else:
223                 #if pthread_kill(ThreadId): <- from C++
224                 #   return 0
225                 #else:
226                 #   MESSAGE()
227         return 1
228
229     #-------------------------------------------------------------------------
230
231     def Kill_impl(self):
232         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
233         RetVal = 0
234         if self._ThreadId > 0 & self._ThreadId != get_ident():
235             RetVal = Killer(self._ThreadId,0)
236             self._ThreadId = 0
237         return RetVal
238
239     #-------------------------------------------------------------------------
240
241     def Stop_impl(self):
242         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
243         RetVal = 0
244         if self._ThreadId > 0 & self._ThreadId != get_ident():
245             RetVal = Killer(self._ThreadId,0)
246             self._ThreadId = 0
247         return RetVal
248
249     #-------------------------------------------------------------------------
250
251     def Suspend_impl(self):
252         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
253         global _Sleeping
254         RetVal = 0
255         if self._ThreadId > 0 & self._ThreadId != get_ident():
256             if _Sleeping > 0:
257                 return 0
258             else:
259                 RetVal = Killer(self._ThreadId, signal.SIGINT)
260                 if RetVal > 0:
261                     _Sleeping = 1
262         return RetVal
263
264     #-------------------------------------------------------------------------
265
266     def Resume_impl(self):
267         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
268         global _Sleeping
269         RetVal = 0
270         if self._ThreadId > 0 & self._ThreadId != get_ident():
271             if _Sleeping > 0:
272                 _Sleeping = 0
273                 RetVal = 1
274             else:
275                 RetVal = 0
276         return RetVal
277
278     #-------------------------------------------------------------------------
279
280     def CpuUsed_impl(self):
281         if ( self._ThreadId | self._Executed ) :
282             if self._ThreadId == get_ident() :
283                 cpu = time.clock()
284                 self._ThreadCpuUsed = cpu - self._StartUsed
285                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
286                 return self._ThreadCpuUsed
287             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
288             return self._ThreadCpuUsed
289         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
290         return 0
291
292     #-------------------------------------------------------------------------
293
294     def DumpPython(self, isPublished, isMultiFile):
295         aBuffer = "\0"
296         if isMultiFile :
297             aBuffer = "def RebuildData(): pass\n\0"
298         return (aBuffer.encode(), 1)
299
300     #-------------------------------------------------------------------------
301
302     def hasObjectInfo(self):
303         return 0
304
305     #-------------------------------------------------------------------------
306
307     def getObjectInfo(self, entry):
308         return ""
309
310     #-------------------------------------------------------------------------
311
312     def getVersion(self):
313         return "" # empty string means "unknown" version
314
315     #-------------------------------------------------------------------------
316
317     pass # end of SALOME_ComponentPy_i