Salome HOME
To avoid compilation pb on RedHat 8.0.
[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._serviceName = ''
76         self._ThreadId = 0
77         self._StartUsed = 0
78         self._ThreadCpuUsed = 0
79         self._Executed = 0
80         self._contId = contID
81
82         naming_service = SALOME_NamingServicePy_i(self._orb)
83         myMachine=getShortHostName()
84         Component_path = self._containerName + "/" + self._instanceName
85         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
86         naming_service.Register(self._this(), Component_path)
87
88         # Add componentinstance to registry
89         obj = naming_service.Resolve('/Registry')
90         if obj is None:
91             MESSAGE(  "Registry Reference is invalid" )
92         else:
93             regist = obj._narrow(Registry.Components)
94             if regist is None:
95                 MESSAGE(  "Registry Reference is invalid" )
96             else:
97                 ior = orb.object_to_string(contID)
98                 MESSAGE(  ior )
99
100                 lesInfos = Identity(self._instanceName)
101                 infos = Registry.Infos(lesInfos._name,
102                                        lesInfos._pid,
103                                        lesInfos._machine,
104                                        lesInfos._adip,
105                                        lesInfos._uid,
106                                        lesInfos._pwname,
107                                        int(lesInfos._tc_start),
108                                        0,0,0,
109                                        lesInfos._cdir,
110                                        -1,
111                                        ior)
112
113                 res = regist.add(infos)
114
115         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
116
117     #-------------------------------------------------------------------------
118
119     def _get_instanceName(self):
120         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
121         return self._instanceName
122     
123     #-------------------------------------------------------------------------
124
125     def _get_interfaceName(self):
126         MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
127         return self._interfaceName
128
129     #-------------------------------------------------------------------------
130
131     def ping(self):
132         MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
133         
134     #-------------------------------------------------------------------------
135
136     def setProperties(self, dico):
137         self._fieldsDict = dico
138     
139     #-------------------------------------------------------------------------
140
141     def getProperties(self):
142         return self._fieldsDict
143
144     #-------------------------------------------------------------------------
145
146     def destroy(self):
147         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
148         self._poa.deactivate_object(self)
149         CORBA.release(self._poa)
150         
151     #-------------------------------------------------------------------------
152
153     def GetContainerRef(self):
154         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
155         #corbaObj_ptr = self._poa.id_to_reference(self._contId)
156         #return corbaObj_ptr._narrow(Engines.Container)
157         return self._contId._narrow(Engines.Container)
158                 
159     #-------------------------------------------------------------------------
160
161     def beginService(self , serviceName ):
162         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
163         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
164         self._serviceName = str(serviceName)
165         self._ThreadId = get_ident()
166         self._StartUsed = 0
167         self._StartUsed = self.CpuUsed_impl()
168         self._ThreadCpuUsed = 0
169         self._Executed = 1
170         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
171         
172
173     #-------------------------------------------------------------------------
174
175     def endService(self , serviceName ):
176         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
177         MESSAGE(  "Component instance : " + str(self._instanceName) )
178
179     #-------------------------------------------------------------------------
180
181     def sendMessage(self , event_type , message ):
182         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
183
184     #-------------------------------------------------------------------------
185
186     def Names(self , GraphName , NodeName ):
187         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
188         self._graphName = GraphName
189         self._nodeName = NodeName
190         
191     #-------------------------------------------------------------------------
192
193     def graphName(self):
194         return self._graphName
195
196     #-------------------------------------------------------------------------
197
198     def nodeName(self):
199         return self._nodeName
200
201     #-------------------------------------------------------------------------
202
203     def Killer(self, ThreadId, signum):
204         #if ThreadId > 0:
205             #if signum == 0:
206                 #if pthread_cancel(ThreadId): <- from C++
207                 #   return 0
208                 #else:
209                 #   MESSAGE()
210             #else:
211                 #if pthread_kill(ThreadId): <- from C++
212                 #   return 0
213                 #else:
214                 #   MESSAGE()
215         return 1                 
216     
217     #-------------------------------------------------------------------------
218
219     def Kill_impl(self):
220         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
221         RetVal = 0
222         if self._ThreadId > 0 & self._ThreadId != get_ident():
223             RetVal = Killer(self._ThreadId,0)
224             self._ThreadId = 0
225         return RetVal
226
227     #-------------------------------------------------------------------------
228
229     def Stop_impl(self):
230         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
231         RetVal = 0
232         if self._ThreadId > 0 & self._ThreadId != get_ident():
233             RetVal = Killer(self._ThreadId,0)
234             self._ThreadId = 0
235         return RetVal
236
237     #-------------------------------------------------------------------------
238
239     def Suspend_impl(self):
240         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
241         global _Sleeping
242         RetVal = 0
243         if self._ThreadId > 0 & self._ThreadId != get_ident():
244             if _Sleeping > 0:
245                 return 0
246             else:
247                 RetVal = Killer(self._ThreadId, signal.SIGINT)
248                 if RetVal > 0:
249                     _Sleeping = 1
250         return RetVal
251
252     #-------------------------------------------------------------------------
253
254     def Resume_impl(self):
255         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
256         global _Sleeping
257         RetVal = 0
258         if self._ThreadId > 0 & self._ThreadId != get_ident():
259             if _Sleeping > 0:
260                 _Sleeping = 0
261                 RetVal = 1
262             else:
263                 RetVal = 0
264         return RetVal
265
266     #-------------------------------------------------------------------------
267
268     def CpuUsed_impl(self):
269         if ( self._ThreadId | self._Executed ) :
270             if self._ThreadId == get_ident() :
271                 cpu = time.clock()
272                 self._ThreadCpuUsed = int(cpu) - self._StartUsed
273                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
274                 return self._ThreadCpuUsed
275             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
276             return self._ThreadCpuUsed
277         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
278         return 0
279
280     #-------------------------------------------------------------------------
281    
282     def DumpPython(self, theStudy, isPublished):
283         aBuffer = "def RebuildData(theStudy): pass\n\0"
284         return (aBuffer, 1)
285
286     #-------------------------------------------------------------------------    
287
288     def getStudyId(self):
289         return self._studyId