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