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