]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_ComponentPy.py
Salome HOME
DCQ : Merge with Ecole_ete_a6.
[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 from omniORB import CORBA, PortableServer
35 import Engines, Engines__POA
36 import Registry
37 from Utils_Identity import *
38 from SALOME_NamingServicePy import *
39 from libNOTIFICATION import *
40
41 from SALOME_utilities import *
42
43 from thread import *
44
45 #=============================================================================
46
47 #define an implementation of the component interface
48
49 class SALOME_ComponentPy_i (Engines__POA.Component):
50     _orb = None
51     _poa = None
52     _fieldsDict = []
53     
54     #-------------------------------------------------------------------------
55
56     def __init__ (self, orb, poa, contID, containerName,
57                   instanceName, interfaceName, notif):
58         # Notif for notification services
59         # NOT YET IMPLEMENTED
60         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
61         self._orb = orb
62         self._poa = poa
63         self._instanceName = instanceName
64         self._interfaceName = interfaceName
65         self._containerName = containerName
66         self._notif = notif
67         self._myConnexionToRegistry = 0
68         self._graphName = ''
69         self._nodeName = ''
70         self._ThreadId = 0
71         self._StartUsed = 0
72         self._ThreadCpuUsed = 0
73         self._Executed = 0
74
75         naming_service = SALOME_NamingServicePy_i(self._orb)
76         Component_path = "/Containers/" +  os.getenv( "HOSTNAME" ) + "/" + self._containerName + "/" + self._interfaceName
77         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
78         naming_service.Register(self._this(), Component_path)
79
80         # Add componentinstance to registry
81         obj = naming_service.Resolve('/Registry')
82         if obj is None:
83             MESSAGE(  "Registry Reference is invalid" )
84         else:
85             regist = obj._narrow(Registry.Components)
86             if regist is None:
87                 MESSAGE(  "Registry Reference is invalid" )
88             else:
89                 ior = orb.object_to_string(contID)
90                 MESSAGE(  ior )
91
92                 lesInfos = Identity(self._instanceName)
93                 infos = Registry.Infos(lesInfos._name,
94                                        lesInfos._pid,
95                                        lesInfos._machine,
96                                        lesInfos._adip,
97                                        lesInfos._uid,
98                                        lesInfos._pwname,
99                                        int(lesInfos._tc_start),
100                                        0,0,0,
101                                        lesInfos._cdir,
102                                        -1,
103                                        ior)
104
105                 res = regist.add(infos)
106
107         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
108
109     #-------------------------------------------------------------------------
110
111     def _get_instanceName(self):
112         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
113         return self._instanceName
114     
115     #-------------------------------------------------------------------------
116
117     def ping(self):
118         MESSAGE(  "SALOME_ComponentPy_i::ping" )
119         
120     #-------------------------------------------------------------------------
121
122     def setProperties(self, dico):
123         self._fieldsDict = dico
124     
125     #-------------------------------------------------------------------------
126
127     def getProperties(self):
128         return self._fieldsDict
129
130     #-------------------------------------------------------------------------
131
132     def destroy(self):
133         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
134         poa.deactivate_object(self)
135         CORBA.release(_poa)
136         
137     #-------------------------------------------------------------------------
138
139     def GetContainerRef(self):
140         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
141         
142     #-------------------------------------------------------------------------
143
144     def beginService(self , serviceName ):
145         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
146         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
147         self._serviceName = str(serviceName)
148         self._ThreadId = get_ident()
149         self._StartUsed = 0
150         self._StartUsed = self.CpuUsed_impl()
151         self._ThreadCpuUsed = 0
152         self._Executed = 1
153         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
154         
155
156     #-------------------------------------------------------------------------
157
158     def endService(self , serviceName ):
159         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
160         MESSAGE(  "Component instance : " + str(self._instanceName) )
161
162     #-------------------------------------------------------------------------
163
164     def sendMessage(self , event_type , message ):
165         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
166
167     #-------------------------------------------------------------------------
168
169     def Names(self , GraphName , NodeName ):
170         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
171         self._graphName = GraphName
172         self._nodeName = NodeName
173         
174     #-------------------------------------------------------------------------
175
176     def graphName(self):
177         return self._graphName
178
179     #-------------------------------------------------------------------------
180
181     def nodeName(self):
182         return self._nodeName
183
184     #-------------------------------------------------------------------------
185
186     def Kill(self):
187         MESSAGE(  "SALOME_ComponentPy_i::Kill not yet implemented" )
188
189     #-------------------------------------------------------------------------
190
191     def Stop(self):
192         MESSAGE(  "SALOME_ComponentPy_i::Stop not yet implemented" )
193
194     #-------------------------------------------------------------------------
195
196     def Suspend(self):
197         MESSAGE(  "SALOME_ComponentPy_i::Suspend not yet implemented" )
198
199     #-------------------------------------------------------------------------
200
201     def Resume(self):
202         MESSAGE(  "SALOME_ComponentPy_i::Resume not yet implemented" )
203
204     #-------------------------------------------------------------------------
205
206     def CpuUsed_impl(self):
207         if ( self._ThreadId | self._Executed ) :
208             if self._ThreadId == get_ident() :
209                 cpu = time.clock()
210                 self._ThreadCpuUsed = int(cpu) - self._StartUsed
211                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
212                 return self._ThreadCpuUsed
213             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
214             return self._ThreadCpuUsed
215         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
216         return 0
217
218     #-------------------------------------------------------------------------
219