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