]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_ComponentPy.py
Salome HOME
KERNEL Services to deal with embedded Study/ModulCatalog.
[modules/kernel.git] / src / Container / SALOME_ComponentPy.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  SALOME Container : implementation of container and engine for Kernel
25 #  File   : SALOME_ComponentPy.py
26 #  Author : Paul RASCLE, EDF
27 #  Module : SALOME
28 #  $Header$
29
30 ## @package SALOME_ComponentPy
31 # \brief python implementation of component interface for Kernel
32 #
33
34 import os
35 import sys
36 import time
37 import string
38 import signal
39 from omniORB import CORBA, PortableServer, any
40 import Engines, Engines__POA
41 import Registry
42 from Utils_Identity import *
43 from SALOME_NamingServicePy import *
44 from libNOTIFICATION import *
45
46 from SALOME_utilities import *
47
48 from _thread import *
49
50 #=============================================================================
51
52 _Sleeping = 0
53
54 ## define an implementation of the component interface Engines::Component
55 #
56 #
57 class SALOME_ComponentPy_Gen_i (Engines__POA.EngineComponent):
58     """
59     Implementation Without naming_service server
60     """
61     _orb = None
62     _poa = None
63     _fieldsDict = []
64
65     #-------------------------------------------------------------------------
66
67     def __init__ (self, orb, poa, contID, containerName,
68                   instanceName, interfaceName, notif=False):
69         # Notif for notification services
70         # NOT YET IMPLEMENTED
71         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
72         self._orb = orb
73         self._poa = poa
74         self._instanceName = instanceName
75         self._interfaceName = interfaceName
76         self._containerName = containerName
77         self._notif = notif
78         self._myConnexionToRegistry = 0
79         self._graphName = ''
80         self._nodeName = ''
81         self._serviceName = ''
82         self._ThreadId = 0
83         self._StartUsed = 0
84         self._ThreadCpuUsed = 0
85         self._Executed = 0
86         self._contId = contID
87
88         myMachine=getShortHostName()
89         id_o = poa.activate_object(self)
90         self._compo_o = poa.id_to_reference(id_o)
91         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, self._notif)
92
93     def getCorbaRef(self):
94         return self._compo_o
95
96     #-------------------------------------------------------------------------
97
98     def _get_instanceName(self):
99         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
100         return self._instanceName
101
102     #-------------------------------------------------------------------------
103
104     def _get_interfaceName(self):
105         MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
106         return self._interfaceName
107
108     #-------------------------------------------------------------------------
109
110     def ping(self):
111         MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
112
113     #-------------------------------------------------------------------------
114
115     def setProperties(self, dico):
116         self._fieldsDict = dico
117
118     #-------------------------------------------------------------------------
119
120     def getProperties(self):
121         return self._fieldsDict
122
123     #-------------------------------------------------------------------------
124
125     def destroy(self):
126         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
127         id = self._poa.servant_to_id(self)
128         self._poa.deactivate_object(id)
129         return
130
131     #-------------------------------------------------------------------------
132
133     def GetContainerRef(self):
134         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
135         return self._contId._narrow(Engines.Container)
136
137     #-------------------------------------------------------------------------
138
139     def beginService(self , serviceName ):
140         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
141         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
142         self._serviceName = str(serviceName)
143         self._ThreadId = get_ident()
144         self._StartUsed = 0
145         self._StartUsed = self.CpuUsed_impl()
146         self._ThreadCpuUsed = 0
147         self._Executed = 1
148         print("beginService for ",serviceName," Component instance : ",self._instanceName)
149         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
150         for e in self._fieldsDict:
151           key=e.key
152           value=any.from_any(e.value)
153           if isinstance(value,str):
154             os.environ[key]=value
155
156
157     #-------------------------------------------------------------------------
158
159     def endService(self , serviceName ):
160         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
161         MESSAGE(  "Component instance : " + str(self._instanceName) )
162         print("endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) ")
163
164
165     #-------------------------------------------------------------------------
166
167     def sendMessage(self , event_type , message ):
168         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
169
170     #-------------------------------------------------------------------------
171
172     def Names(self , GraphName , NodeName ):
173         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
174         self._graphName = GraphName
175         self._nodeName = NodeName
176
177     #-------------------------------------------------------------------------
178
179     def graphName(self):
180         return self._graphName
181
182     #-------------------------------------------------------------------------
183
184     def nodeName(self):
185         return self._nodeName
186
187     #-------------------------------------------------------------------------
188
189     def Killer(self, ThreadId, signum):
190         #if ThreadId > 0:
191             #if signum == 0:
192                 #if pthread_cancel(ThreadId): <- from C++
193                 #   return 0
194                 #else:
195                 #   MESSAGE()
196             #else:
197                 #if pthread_kill(ThreadId): <- from C++
198                 #   return 0
199                 #else:
200                 #   MESSAGE()
201         return 1
202
203     #-------------------------------------------------------------------------
204
205     def Kill_impl(self):
206         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
207         RetVal = 0
208         if self._ThreadId > 0 & self._ThreadId != get_ident():
209             RetVal = Killer(self._ThreadId,0)
210             self._ThreadId = 0
211         return RetVal
212
213     #-------------------------------------------------------------------------
214
215     def Stop_impl(self):
216         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
217         RetVal = 0
218         if self._ThreadId > 0 & self._ThreadId != get_ident():
219             RetVal = Killer(self._ThreadId,0)
220             self._ThreadId = 0
221         return RetVal
222
223     #-------------------------------------------------------------------------
224
225     def Suspend_impl(self):
226         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
227         global _Sleeping
228         RetVal = 0
229         if self._ThreadId > 0 & self._ThreadId != get_ident():
230             if _Sleeping > 0:
231                 return 0
232             else:
233                 RetVal = Killer(self._ThreadId, signal.SIGINT)
234                 if RetVal > 0:
235                     _Sleeping = 1
236         return RetVal
237
238     #-------------------------------------------------------------------------
239
240     def Resume_impl(self):
241         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
242         global _Sleeping
243         RetVal = 0
244         if self._ThreadId > 0 & self._ThreadId != get_ident():
245             if _Sleeping > 0:
246                 _Sleeping = 0
247                 RetVal = 1
248             else:
249                 RetVal = 0
250         return RetVal
251
252     #-------------------------------------------------------------------------
253
254     def CpuUsed_impl(self):
255         if ( self._ThreadId | self._Executed ) :
256             if self._ThreadId == get_ident() :
257                 cpu = time.clock()
258                 self._ThreadCpuUsed = cpu - self._StartUsed
259                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
260                 return self._ThreadCpuUsed
261             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
262             return self._ThreadCpuUsed
263         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
264         return 0
265
266     #-------------------------------------------------------------------------
267
268     def DumpPython(self, isPublished, isMultiFile):
269         aBuffer = "\0"
270         if isMultiFile :
271             aBuffer = "def RebuildData(): pass\n\0"
272         return (aBuffer.encode(), 1)
273
274     #-------------------------------------------------------------------------
275
276     def hasObjectInfo(self):
277         return 0
278
279     #-------------------------------------------------------------------------
280
281     def getObjectInfo(self, entry):
282         return ""
283
284     #-------------------------------------------------------------------------
285
286     def getVersion(self):
287         return "" # empty string means "unknown" version
288
289     #-------------------------------------------------------------------------
290
291     pass # end of SALOME_ComponentPy_i
292
293 class SALOME_ComponentPy_i(SALOME_ComponentPy_Gen_i):
294     """
295     Implementation with naming_service server
296     """
297     def __init__ (self, orb, poa, contID, containerName, instanceName, interfaceName, notif=False):
298         SALOME_ComponentPy_Gen_i.__init__(self, orb, poa, contID, containerName, instanceName, interfaceName, notif)
299         naming_service = SALOME_NamingServicePy_i(self._orb)
300         Component_path = self._containerName + "/" + self._instanceName
301         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
302         naming_service.Register(self._compo_o, Component_path)
303         # Add componentinstance to registry
304         obj = naming_service.Resolve('/Registry')
305         if obj is None:
306             MESSAGE(  "Registry Reference is invalid" )
307         else:
308             regist = obj._narrow(Registry.Components)
309             if regist is None:
310                 MESSAGE(  "Registry Reference is invalid" )
311             else:
312                 ior = orb.object_to_string(contID)
313                 MESSAGE(  ior )
314
315                 lesInfos = Identity(self._instanceName)
316                 infos = Registry.Infos(lesInfos._name,
317                                        lesInfos._pid,
318                                        lesInfos._machine,
319                                        lesInfos._adip,
320                                        lesInfos._uid,
321                                        lesInfos._pwname,
322                                        int(lesInfos._tc_start),
323                                        0,0,0,
324                                        lesInfos._cdir,
325                                        -1,
326                                        ior)
327
328                 res = regist.add(infos)
329
330     pass