Salome HOME
Fix for bug 0020152 (New tooltip functionality on GUI object browser induces a crash...
[modules/kernel.git] / src / Container / SALOME_ComponentPy.py
1 #! /usr/bin/env python
2 #  Copyright (C) 2007-2008  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.
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 #  SALOME Container : implementation of container and engine for Kernel
24 #  File   : SALOME_ComponentPy.py
25 #  Author : Paul RASCLE, EDF
26 #  Module : SALOME
27 #  $Header$
28 #
29 import os
30 import sys
31 import time
32 import string
33 import signal
34 from omniORB import CORBA, PortableServer
35 import Engines, Engines__POA
36 import Registry
37 from Utils_Identity import *
38 from SALOME_NamingServicePy import *
39 from libNOTIFICATION import *
40
41 from SALOME_utilities import *
42
43 from thread import *
44
45 #=============================================================================
46
47 _Sleeping = 0
48
49 #define an implementation of the component interface
50
51 class SALOME_ComponentPy_i (Engines__POA.Component):
52     _orb = None
53     _poa = None
54     _fieldsDict = []
55     _studyId = -1
56     
57     #-------------------------------------------------------------------------
58
59     def __init__ (self, orb, poa, contID, containerName,
60                   instanceName, interfaceName, notif=0):
61         # Notif for notification services
62         # NOT YET IMPLEMENTED
63         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
64         self._orb = orb
65         self._poa = poa
66         self._instanceName = instanceName
67         self._interfaceName = interfaceName
68         self._containerName = containerName
69         self._notif = notif
70         self._myConnexionToRegistry = 0
71         self._graphName = ''
72         self._nodeName = ''
73         self._serviceName = ''
74         self._ThreadId = 0
75         self._StartUsed = 0
76         self._ThreadCpuUsed = 0
77         self._Executed = 0
78         self._contId = contID
79
80         naming_service = SALOME_NamingServicePy_i(self._orb)
81         myMachine=getShortHostName()
82         Component_path = self._containerName + "/" + self._instanceName
83         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
84         id_o = poa.activate_object(self)
85         compo_o = poa.id_to_reference(id_o)
86         naming_service.Register(compo_o, 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         #id = self._poa.servant_to_id(self)
149         #self._poa.deactivate_object(id)
150         return
151         
152     #-------------------------------------------------------------------------
153
154     def GetContainerRef(self):
155         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
156         #corbaObj_ptr = self._poa.id_to_reference(self._contId)
157         #return corbaObj_ptr._narrow(Engines.Container)
158         return self._contId._narrow(Engines.Container)
159                 
160     #-------------------------------------------------------------------------
161
162     def beginService(self , serviceName ):
163         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
164         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
165         self._serviceName = str(serviceName)
166         self._ThreadId = get_ident()
167         self._StartUsed = 0
168         self._StartUsed = self.CpuUsed_impl()
169         self._ThreadCpuUsed = 0
170         self._Executed = 1
171         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
172         
173
174     #-------------------------------------------------------------------------
175
176     def endService(self , serviceName ):
177         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
178         MESSAGE(  "Component instance : " + str(self._instanceName) )
179
180     #-------------------------------------------------------------------------
181
182     def sendMessage(self , event_type , message ):
183         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
184
185     #-------------------------------------------------------------------------
186
187     def Names(self , GraphName , NodeName ):
188         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
189         self._graphName = GraphName
190         self._nodeName = NodeName
191         
192     #-------------------------------------------------------------------------
193
194     def graphName(self):
195         return self._graphName
196
197     #-------------------------------------------------------------------------
198
199     def nodeName(self):
200         return self._nodeName
201
202     #-------------------------------------------------------------------------
203
204     def Killer(self, ThreadId, signum):
205         #if ThreadId > 0:
206             #if signum == 0:
207                 #if pthread_cancel(ThreadId): <- from C++
208                 #   return 0
209                 #else:
210                 #   MESSAGE()
211             #else:
212                 #if pthread_kill(ThreadId): <- from C++
213                 #   return 0
214                 #else:
215                 #   MESSAGE()
216         return 1                 
217     
218     #-------------------------------------------------------------------------
219
220     def Kill_impl(self):
221         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
222         RetVal = 0
223         if self._ThreadId > 0 & self._ThreadId != get_ident():
224             RetVal = Killer(self._ThreadId,0)
225             self._ThreadId = 0
226         return RetVal
227
228     #-------------------------------------------------------------------------
229
230     def Stop_impl(self):
231         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
232         RetVal = 0
233         if self._ThreadId > 0 & self._ThreadId != get_ident():
234             RetVal = Killer(self._ThreadId,0)
235             self._ThreadId = 0
236         return RetVal
237
238     #-------------------------------------------------------------------------
239
240     def Suspend_impl(self):
241         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
242         global _Sleeping
243         RetVal = 0
244         if self._ThreadId > 0 & self._ThreadId != get_ident():
245             if _Sleeping > 0:
246                 return 0
247             else:
248                 RetVal = Killer(self._ThreadId, signal.SIGINT)
249                 if RetVal > 0:
250                     _Sleeping = 1
251         return RetVal
252
253     #-------------------------------------------------------------------------
254
255     def Resume_impl(self):
256         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
257         global _Sleeping
258         RetVal = 0
259         if self._ThreadId > 0 & self._ThreadId != get_ident():
260             if _Sleeping > 0:
261                 _Sleeping = 0
262                 RetVal = 1
263             else:
264                 RetVal = 0
265         return RetVal
266
267     #-------------------------------------------------------------------------
268
269     def CpuUsed_impl(self):
270         if ( self._ThreadId | self._Executed ) :
271             if self._ThreadId == get_ident() :
272                 cpu = time.clock()
273                 self._ThreadCpuUsed = int(cpu) - self._StartUsed
274                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
275                 return self._ThreadCpuUsed
276             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
277             return self._ThreadCpuUsed
278         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
279         return 0
280
281     #-------------------------------------------------------------------------
282    
283     def DumpPython(self, theStudy, isPublished):
284         aBuffer = "def RebuildData(theStudy): pass\n\0"
285         return (aBuffer, 1)
286
287     #-------------------------------------------------------------------------    
288
289     def getStudyId(self):
290         return self._studyId
291
292     #-------------------------------------------------------------------------    
293
294     def hasObjectInfo(self):
295         return 0
296
297     #-------------------------------------------------------------------------    
298
299     def getObjectInfo(self, studyId, entry):
300         return ""
301