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