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