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