Salome HOME
Merge multi-study removal branch.
[modules/kernel.git] / src / Container / SALOME_ComponentPy.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016  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, or (at your option) any later version.
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
63     #-------------------------------------------------------------------------
64
65     def __init__ (self, orb, poa, contID, containerName,
66                   instanceName, interfaceName, notif=False):
67         # Notif for notification services
68         # NOT YET IMPLEMENTED
69         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
70         self._orb = orb
71         self._poa = poa
72         self._instanceName = instanceName
73         self._interfaceName = interfaceName
74         self._containerName = containerName
75         self._notif = notif
76         self._myConnexionToRegistry = 0
77         self._graphName = ''
78         self._nodeName = ''
79         self._serviceName = ''
80         self._ThreadId = 0
81         self._StartUsed = 0
82         self._ThreadCpuUsed = 0
83         self._Executed = 0
84         self._contId = contID
85
86         naming_service = SALOME_NamingServicePy_i(self._orb)
87         myMachine=getShortHostName()
88         Component_path = self._containerName + "/" + self._instanceName
89         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
90         id_o = poa.activate_object(self)
91         compo_o = poa.id_to_reference(id_o)
92         naming_service.Register(compo_o, Component_path)
93
94         # Add componentinstance to registry
95         obj = naming_service.Resolve('/Registry')
96         if obj is None:
97             MESSAGE(  "Registry Reference is invalid" )
98         else:
99             regist = obj._narrow(Registry.Components)
100             if regist is None:
101                 MESSAGE(  "Registry Reference is invalid" )
102             else:
103                 ior = orb.object_to_string(contID)
104                 MESSAGE(  ior )
105
106                 lesInfos = Identity(self._instanceName)
107                 infos = Registry.Infos(lesInfos._name,
108                                        lesInfos._pid,
109                                        lesInfos._machine,
110                                        lesInfos._adip,
111                                        lesInfos._uid,
112                                        lesInfos._pwname,
113                                        int(lesInfos._tc_start),
114                                        0,0,0,
115                                        lesInfos._cdir,
116                                        -1,
117                                        ior)
118
119                 res = regist.add(infos)
120
121         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
122
123     #-------------------------------------------------------------------------
124
125     def _get_instanceName(self):
126         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
127         return self._instanceName
128
129     #-------------------------------------------------------------------------
130
131     def _get_interfaceName(self):
132         MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
133         return self._interfaceName
134
135     #-------------------------------------------------------------------------
136
137     def ping(self):
138         MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
139
140     #-------------------------------------------------------------------------
141
142     def setProperties(self, dico):
143         self._fieldsDict = dico
144
145     #-------------------------------------------------------------------------
146
147     def getProperties(self):
148         return self._fieldsDict
149
150     #-------------------------------------------------------------------------
151
152     def destroy(self):
153         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
154         id = self._poa.servant_to_id(self)
155         self._poa.deactivate_object(id)
156         return
157
158     #-------------------------------------------------------------------------
159
160     def GetContainerRef(self):
161         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
162         return self._contId._narrow(Engines.Container)
163
164     #-------------------------------------------------------------------------
165
166     def beginService(self , serviceName ):
167         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
168         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
169         self._serviceName = str(serviceName)
170         self._ThreadId = get_ident()
171         self._StartUsed = 0
172         self._StartUsed = self.CpuUsed_impl()
173         self._ThreadCpuUsed = 0
174         self._Executed = 1
175         print "beginService for ",serviceName," Component instance : ",self._instanceName
176         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
177         for e in self._fieldsDict:
178           key=e.key
179           value=any.from_any(e.value)
180           if isinstance(value,str):
181             os.environ[key]=value
182
183
184     #-------------------------------------------------------------------------
185
186     def endService(self , serviceName ):
187         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
188         MESSAGE(  "Component instance : " + str(self._instanceName) )
189         print "endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) "
190
191
192     #-------------------------------------------------------------------------
193
194     def sendMessage(self , event_type , message ):
195         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
196
197     #-------------------------------------------------------------------------
198
199     def Names(self , GraphName , NodeName ):
200         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
201         self._graphName = GraphName
202         self._nodeName = NodeName
203
204     #-------------------------------------------------------------------------
205
206     def graphName(self):
207         return self._graphName
208
209     #-------------------------------------------------------------------------
210
211     def nodeName(self):
212         return self._nodeName
213
214     #-------------------------------------------------------------------------
215
216     def Killer(self, ThreadId, signum):
217         #if ThreadId > 0:
218             #if signum == 0:
219                 #if pthread_cancel(ThreadId): <- from C++
220                 #   return 0
221                 #else:
222                 #   MESSAGE()
223             #else:
224                 #if pthread_kill(ThreadId): <- from C++
225                 #   return 0
226                 #else:
227                 #   MESSAGE()
228         return 1
229
230     #-------------------------------------------------------------------------
231
232     def Kill_impl(self):
233         MESSAGE(  "SALOME_ComponentPy_i::Kill_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 Stop_impl(self):
243         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
244         RetVal = 0
245         if self._ThreadId > 0 & self._ThreadId != get_ident():
246             RetVal = Killer(self._ThreadId,0)
247             self._ThreadId = 0
248         return RetVal
249
250     #-------------------------------------------------------------------------
251
252     def Suspend_impl(self):
253         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
254         global _Sleeping
255         RetVal = 0
256         if self._ThreadId > 0 & self._ThreadId != get_ident():
257             if _Sleeping > 0:
258                 return 0
259             else:
260                 RetVal = Killer(self._ThreadId, signal.SIGINT)
261                 if RetVal > 0:
262                     _Sleeping = 1
263         return RetVal
264
265     #-------------------------------------------------------------------------
266
267     def Resume_impl(self):
268         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
269         global _Sleeping
270         RetVal = 0
271         if self._ThreadId > 0 & self._ThreadId != get_ident():
272             if _Sleeping > 0:
273                 _Sleeping = 0
274                 RetVal = 1
275             else:
276                 RetVal = 0
277         return RetVal
278
279     #-------------------------------------------------------------------------
280
281     def CpuUsed_impl(self):
282         if ( self._ThreadId | self._Executed ) :
283             if self._ThreadId == get_ident() :
284                 cpu = time.clock()
285                 self._ThreadCpuUsed = cpu - self._StartUsed
286                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
287                 return self._ThreadCpuUsed
288             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
289             return self._ThreadCpuUsed
290         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
291         return 0
292
293     #-------------------------------------------------------------------------
294
295     def DumpPython(self, isPublished, isMultiFile):
296         aBuffer = "\0"
297         if isMultiFile :
298             aBuffer = "def RebuildData(): pass\n\0"
299         return (aBuffer, 1)
300
301     #-------------------------------------------------------------------------
302
303     def hasObjectInfo(self):
304         return 0
305
306     #-------------------------------------------------------------------------
307
308     def getObjectInfo(self, entry):
309         return ""
310
311     #-------------------------------------------------------------------------
312
313     def getVersion(self):
314         return "" # empty string means "unknown" version
315
316     #-------------------------------------------------------------------------
317
318     pass # end of SALOME_ComponentPy_i