]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_ComponentPy.py
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / Container / SALOME_ComponentPy.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  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 #  SALOME Container : implementation of container and engine for Kernel
26 #  File   : SALOME_ComponentPy.py
27 #  Author : Paul RASCLE, EDF
28 #  Module : SALOME
29 #  $Header$
30
31 ## @package SALOME_ComponentPy
32 # \brief python implementation of component interface for Kernel
33 #
34
35 import os
36 import sys
37 import time
38 import string
39 import signal
40 from omniORB import CORBA, PortableServer, any
41 import Engines, Engines__POA
42 import Registry
43 from Utils_Identity import *
44 from SALOME_NamingServicePy import *
45 from libNOTIFICATION import *
46
47 from SALOME_utilities import *
48
49 from thread import *
50
51 #=============================================================================
52
53 _Sleeping = 0
54
55 ## define an implementation of the component interface Engines::Component
56 #
57 #  
58 class SALOME_ComponentPy_i (Engines__POA.EngineComponent):
59     _orb = None
60     _poa = None
61     _fieldsDict = []
62     _studyId = -1
63     
64     #-------------------------------------------------------------------------
65
66     def __init__ (self, orb, poa, contID, containerName,
67                   instanceName, interfaceName, notif=0):
68         # Notif for notification services
69         # NOT YET IMPLEMENTED
70         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
71         self._orb = orb
72         self._poa = poa
73         self._instanceName = instanceName
74         self._interfaceName = interfaceName
75         self._containerName = containerName
76         self._notif = notif
77         self._myConnexionToRegistry = 0
78         self._graphName = ''
79         self._nodeName = ''
80         self._serviceName = ''
81         self._ThreadId = 0
82         self._StartUsed = 0
83         self._ThreadCpuUsed = 0
84         self._Executed = 0
85         self._contId = contID
86
87         naming_service = SALOME_NamingServicePy_i(self._orb)
88         myMachine=getShortHostName()
89         Component_path = self._containerName + "/" + self._instanceName
90         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
91         id_o = poa.activate_object(self)
92         compo_o = poa.id_to_reference(id_o)
93         naming_service.Register(compo_o, Component_path)
94
95         # Add componentinstance to registry
96         obj = naming_service.Resolve('/Registry')
97         if obj is None:
98             MESSAGE(  "Registry Reference is invalid" )
99         else:
100             regist = obj._narrow(Registry.Components)
101             if regist is None:
102                 MESSAGE(  "Registry Reference is invalid" )
103             else:
104                 ior = orb.object_to_string(contID)
105                 MESSAGE(  ior )
106
107                 lesInfos = Identity(self._instanceName)
108                 infos = Registry.Infos(lesInfos._name,
109                                        lesInfos._pid,
110                                        lesInfos._machine,
111                                        lesInfos._adip,
112                                        lesInfos._uid,
113                                        lesInfos._pwname,
114                                        int(lesInfos._tc_start),
115                                        0,0,0,
116                                        lesInfos._cdir,
117                                        -1,
118                                        ior)
119
120                 res = regist.add(infos)
121
122         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
123
124     #-------------------------------------------------------------------------
125
126     def _get_instanceName(self):
127         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
128         return self._instanceName
129     
130     #-------------------------------------------------------------------------
131
132     def _get_interfaceName(self):
133         MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
134         return self._interfaceName
135
136     #-------------------------------------------------------------------------
137
138     def ping(self):
139         MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
140         
141     #-------------------------------------------------------------------------
142
143     def setProperties(self, dico):
144         self._fieldsDict = dico
145     
146     #-------------------------------------------------------------------------
147
148     def getProperties(self):
149         return self._fieldsDict
150
151     #-------------------------------------------------------------------------
152
153     def destroy(self):
154         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
155         id = self._poa.servant_to_id(self)
156         self._poa.deactivate_object(id)
157         return
158         
159     #-------------------------------------------------------------------------
160
161     def GetContainerRef(self):
162         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
163         return self._contId._narrow(Engines.Container)
164                 
165     #-------------------------------------------------------------------------
166
167     def beginService(self , serviceName ):
168         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
169         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
170         self._serviceName = str(serviceName)
171         self._ThreadId = get_ident()
172         self._StartUsed = 0
173         self._StartUsed = self.CpuUsed_impl()
174         self._ThreadCpuUsed = 0
175         self._Executed = 1
176         print "beginService for ",serviceName," Component instance : ",self._instanceName
177         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
178         for e in self._fieldsDict:
179           key=e.key
180           value=any.from_any(e.value)
181           if isinstance(value,str):
182             os.environ[key]=value
183         
184
185     #-------------------------------------------------------------------------
186
187     def endService(self , serviceName ):
188         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
189         MESSAGE(  "Component instance : " + str(self._instanceName) )
190         print "endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) "
191
192
193     #-------------------------------------------------------------------------
194
195     def sendMessage(self , event_type , message ):
196         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
197
198     #-------------------------------------------------------------------------
199
200     def Names(self , GraphName , NodeName ):
201         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
202         self._graphName = GraphName
203         self._nodeName = NodeName
204         
205     #-------------------------------------------------------------------------
206
207     def graphName(self):
208         return self._graphName
209
210     #-------------------------------------------------------------------------
211
212     def nodeName(self):
213         return self._nodeName
214
215     #-------------------------------------------------------------------------
216
217     def Killer(self, ThreadId, signum):
218         #if ThreadId > 0:
219             #if signum == 0:
220                 #if pthread_cancel(ThreadId): <- from C++
221                 #   return 0
222                 #else:
223                 #   MESSAGE()
224             #else:
225                 #if pthread_kill(ThreadId): <- from C++
226                 #   return 0
227                 #else:
228                 #   MESSAGE()
229         return 1                 
230     
231     #-------------------------------------------------------------------------
232
233     def Kill_impl(self):
234         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
235         RetVal = 0
236         if self._ThreadId > 0 & self._ThreadId != get_ident():
237             RetVal = Killer(self._ThreadId,0)
238             self._ThreadId = 0
239         return RetVal
240
241     #-------------------------------------------------------------------------
242
243     def Stop_impl(self):
244         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
245         RetVal = 0
246         if self._ThreadId > 0 & self._ThreadId != get_ident():
247             RetVal = Killer(self._ThreadId,0)
248             self._ThreadId = 0
249         return RetVal
250
251     #-------------------------------------------------------------------------
252
253     def Suspend_impl(self):
254         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
255         global _Sleeping
256         RetVal = 0
257         if self._ThreadId > 0 & self._ThreadId != get_ident():
258             if _Sleeping > 0:
259                 return 0
260             else:
261                 RetVal = Killer(self._ThreadId, signal.SIGINT)
262                 if RetVal > 0:
263                     _Sleeping = 1
264         return RetVal
265
266     #-------------------------------------------------------------------------
267
268     def Resume_impl(self):
269         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
270         global _Sleeping
271         RetVal = 0
272         if self._ThreadId > 0 & self._ThreadId != get_ident():
273             if _Sleeping > 0:
274                 _Sleeping = 0
275                 RetVal = 1
276             else:
277                 RetVal = 0
278         return RetVal
279
280     #-------------------------------------------------------------------------
281
282     def CpuUsed_impl(self):
283         if ( self._ThreadId | self._Executed ) :
284             if self._ThreadId == get_ident() :
285                 cpu = time.clock()
286                 self._ThreadCpuUsed = cpu - self._StartUsed
287                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
288                 return self._ThreadCpuUsed
289             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
290             return self._ThreadCpuUsed
291         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
292         return 0
293
294     #-------------------------------------------------------------------------
295    
296     def DumpPython(self, theStudy, isPublished, isMultiFile):
297         aBuffer = "\0"
298         if isMultiFile :
299             aBuffer = "def RebuildData(theStudy): pass\n\0"
300         return (aBuffer, 1)
301
302     #-------------------------------------------------------------------------    
303
304     def getStudyId(self):
305         return self._studyId
306
307     #-------------------------------------------------------------------------    
308
309     def hasObjectInfo(self):
310         return 0
311
312     #-------------------------------------------------------------------------    
313
314     def getObjectInfo(self, studyId, entry):
315         return ""
316