Salome HOME
Merging from V3_2_6pre4
[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                 
158     #-------------------------------------------------------------------------
159
160     def beginService(self , serviceName ):
161         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
162         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
163         self._serviceName = str(serviceName)
164         self._ThreadId = get_ident()
165         self._StartUsed = 0
166         self._StartUsed = self.CpuUsed_impl()
167         self._ThreadCpuUsed = 0
168         self._Executed = 1
169         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
170         
171
172     #-------------------------------------------------------------------------
173
174     def endService(self , serviceName ):
175         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
176         MESSAGE(  "Component instance : " + str(self._instanceName) )
177
178     #-------------------------------------------------------------------------
179
180     def sendMessage(self , event_type , message ):
181         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
182
183     #-------------------------------------------------------------------------
184
185     def Names(self , GraphName , NodeName ):
186         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
187         self._graphName = GraphName
188         self._nodeName = NodeName
189         
190     #-------------------------------------------------------------------------
191
192     def graphName(self):
193         return self._graphName
194
195     #-------------------------------------------------------------------------
196
197     def nodeName(self):
198         return self._nodeName
199
200     #-------------------------------------------------------------------------
201
202     def Killer(self, ThreadId, signum):
203         #if ThreadId > 0:
204             #if signum == 0:
205                 #if pthread_cancel(ThreadId): <- from C++
206                 #   return 0
207                 #else:
208                 #   MESSAGE()
209             #else:
210                 #if pthread_kill(ThreadId): <- from C++
211                 #   return 0
212                 #else:
213                 #   MESSAGE()
214         return 1                 
215     
216     #-------------------------------------------------------------------------
217
218     def Kill_impl(self):
219         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
220         RetVal = 0
221         if self._ThreadId > 0 & self._ThreadId != get_ident():
222             RetVal = Killer(self._ThreadId,0)
223             self._ThreadId = 0
224         return RetVal
225
226     #-------------------------------------------------------------------------
227
228     def Stop_impl(self):
229         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
230         RetVal = 0
231         if self._ThreadId > 0 & self._ThreadId != get_ident():
232             RetVal = Killer(self._ThreadId,0)
233             self._ThreadId = 0
234         return RetVal
235
236     #-------------------------------------------------------------------------
237
238     def Suspend_impl(self):
239         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
240         global _Sleeping
241         RetVal = 0
242         if self._ThreadId > 0 & self._ThreadId != get_ident():
243             if _Sleeping > 0:
244                 return 0
245             else:
246                 RetVal = Killer(self._ThreadId, signal.SIGINT)
247                 if RetVal > 0:
248                     _Sleeping = 1
249         return RetVal
250
251     #-------------------------------------------------------------------------
252
253     def Resume_impl(self):
254         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
255         global _Sleeping
256         RetVal = 0
257         if self._ThreadId > 0 & self._ThreadId != get_ident():
258             if _Sleeping > 0:
259                 _Sleeping = 0
260                 RetVal = 1
261             else:
262                 RetVal = 0
263         return RetVal
264
265     #-------------------------------------------------------------------------
266
267     def CpuUsed_impl(self):
268         if ( self._ThreadId | self._Executed ) :
269             if self._ThreadId == get_ident() :
270                 cpu = time.clock()
271                 self._ThreadCpuUsed = int(cpu) - self._StartUsed
272                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
273                 return self._ThreadCpuUsed
274             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
275             return self._ThreadCpuUsed
276         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
277         return 0
278
279     #-------------------------------------------------------------------------
280    
281     def DumpPython(self, theStudy, isPublished):
282         aBuffer = "def RebuildData(theStudy): pass\n\0"
283         return (aBuffer, 1)
284
285     #-------------------------------------------------------------------------    
286
287     def getStudyId(self):
288         return self._studyId