Back to index

salome-kernel  6.5.0
SALOME_ComponentPy.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 #  SALOME Container : implementation of container and engine for Kernel
00026 #  File   : SALOME_ComponentPy.py
00027 #  Author : Paul RASCLE, EDF
00028 #  Module : SALOME
00029 #  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/SALOME_ComponentPy.py,v 1.11.2.5.14.4.12.1 2012-04-12 14:05:03 vsr Exp $
00030 
00031 ## @package SALOME_ComponentPy
00032 # \brief python implementation of component interface for Kernel
00033 #
00034 
00035 import os
00036 import sys
00037 import time
00038 import string
00039 import signal
00040 from omniORB import CORBA, PortableServer, any
00041 import Engines, Engines__POA
00042 import Registry
00043 from Utils_Identity import *
00044 from SALOME_NamingServicePy import *
00045 from libNOTIFICATION import *
00046 
00047 from SALOME_utilities import *
00048 
00049 from thread import *
00050 
00051 #=============================================================================
00052 
00053 _Sleeping = 0
00054 
00055 ## define an implementation of the component interface Engines::Component
00056 #
00057 #  
00058 class SALOME_ComponentPy_i (Engines__POA.EngineComponent):
00059     _orb = None
00060     _poa = None
00061     _fieldsDict = []
00062     _studyId = -1
00063     
00064     #-------------------------------------------------------------------------
00065 
00066     def __init__ (self, orb, poa, contID, containerName,
00067                   instanceName, interfaceName, notif=0):
00068         # Notif for notification services
00069         # NOT YET IMPLEMENTED
00070         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
00071         self._orb = orb
00072         self._poa = poa
00073         self._instanceName = instanceName
00074         self._interfaceName = interfaceName
00075         self._containerName = containerName
00076         self._notif = notif
00077         self._myConnexionToRegistry = 0
00078         self._graphName = ''
00079         self._nodeName = ''
00080         self._serviceName = ''
00081         self._ThreadId = 0
00082         self._StartUsed = 0
00083         self._ThreadCpuUsed = 0
00084         self._Executed = 0
00085         self._contId = contID
00086 
00087         naming_service = SALOME_NamingServicePy_i(self._orb)
00088         myMachine=getShortHostName()
00089         Component_path = self._containerName + "/" + self._instanceName
00090         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
00091         id_o = poa.activate_object(self)
00092         compo_o = poa.id_to_reference(id_o)
00093         naming_service.Register(compo_o, Component_path)
00094 
00095         # Add componentinstance to registry
00096         obj = naming_service.Resolve('/Registry')
00097         if obj is None:
00098             MESSAGE(  "Registry Reference is invalid" )
00099         else:
00100             regist = obj._narrow(Registry.Components)
00101             if regist is None:
00102                 MESSAGE(  "Registry Reference is invalid" )
00103             else:
00104                 ior = orb.object_to_string(contID)
00105                 MESSAGE(  ior )
00106 
00107                 lesInfos = Identity(self._instanceName)
00108                 infos = Registry.Infos(lesInfos._name,
00109                                        lesInfos._pid,
00110                                        lesInfos._machine,
00111                                        lesInfos._adip,
00112                                        lesInfos._uid,
00113                                        lesInfos._pwname,
00114                                        int(lesInfos._tc_start),
00115                                        0,0,0,
00116                                        lesInfos._cdir,
00117                                        -1,
00118                                        ior)
00119 
00120                 res = regist.add(infos)
00121 
00122         self._notifSupplier = NOTIFICATION_Supplier_Swig(instanceName, notif)
00123 
00124     #-------------------------------------------------------------------------
00125 
00126     def _get_instanceName(self):
00127         MESSAGE(  "SALOME_ComponentPy_i::_get_instanceName" )
00128         return self._instanceName
00129     
00130     #-------------------------------------------------------------------------
00131 
00132     def _get_interfaceName(self):
00133         MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
00134         return self._interfaceName
00135 
00136     #-------------------------------------------------------------------------
00137 
00138     def ping(self):
00139         MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
00140         
00141     #-------------------------------------------------------------------------
00142 
00143     def setProperties(self, dico):
00144         self._fieldsDict = dico
00145     
00146     #-------------------------------------------------------------------------
00147 
00148     def getProperties(self):
00149         return self._fieldsDict
00150 
00151     #-------------------------------------------------------------------------
00152 
00153     def destroy(self):
00154         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
00155         id = self._poa.servant_to_id(self)
00156         self._poa.deactivate_object(id)
00157         return
00158         
00159     #-------------------------------------------------------------------------
00160 
00161     def GetContainerRef(self):
00162         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
00163         return self._contId._narrow(Engines.Container)
00164                 
00165     #-------------------------------------------------------------------------
00166 
00167     def beginService(self , serviceName ):
00168         MESSAGE(  "Send BeginService notification for " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) )
00169         MESSAGE(  "Component instance : " + str ( self._instanceName ) )
00170         self._serviceName = str(serviceName)
00171         self._ThreadId = get_ident()
00172         self._StartUsed = 0
00173         self._StartUsed = self.CpuUsed_impl()
00174         self._ThreadCpuUsed = 0
00175         self._Executed = 1
00176         print "beginService for ",serviceName," Component instance : ",self._instanceName
00177         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
00178         for e in self._fieldsDict:
00179           key=e.key
00180           value=any.from_any(e.value)
00181           if isinstance(value,str):
00182             os.environ[key]=value
00183         
00184 
00185     #-------------------------------------------------------------------------
00186 
00187     def endService(self , serviceName ):
00188         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
00189         MESSAGE(  "Component instance : " + str(self._instanceName) )
00190         print "endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) "
00191 
00192 
00193     #-------------------------------------------------------------------------
00194 
00195     def sendMessage(self , event_type , message ):
00196         self._notifSupplier.Send(self.graphName(), self.nodeName(), event_type, message)
00197 
00198     #-------------------------------------------------------------------------
00199 
00200     def Names(self , GraphName , NodeName ):
00201         MESSAGE(  "SALOME_ComponentPy_i::Names" + str(GraphName) + str(NodeName) )
00202         self._graphName = GraphName
00203         self._nodeName = NodeName
00204         
00205     #-------------------------------------------------------------------------
00206 
00207     def graphName(self):
00208         return self._graphName
00209 
00210     #-------------------------------------------------------------------------
00211 
00212     def nodeName(self):
00213         return self._nodeName
00214 
00215     #-------------------------------------------------------------------------
00216 
00217     def Killer(self, ThreadId, signum):
00218         #if ThreadId > 0:
00219             #if signum == 0:
00220                 #if pthread_cancel(ThreadId): <- from C++
00221                 #   return 0
00222                 #else:
00223                 #   MESSAGE()
00224             #else:
00225                 #if pthread_kill(ThreadId): <- from C++
00226                 #   return 0
00227                 #else:
00228                 #   MESSAGE()
00229         return 1                 
00230     
00231     #-------------------------------------------------------------------------
00232 
00233     def Kill_impl(self):
00234         MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
00235         RetVal = 0
00236         if self._ThreadId > 0 & self._ThreadId != get_ident():
00237             RetVal = Killer(self._ThreadId,0)
00238             self._ThreadId = 0
00239         return RetVal
00240 
00241     #-------------------------------------------------------------------------
00242 
00243     def Stop_impl(self):
00244         MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
00245         RetVal = 0
00246         if self._ThreadId > 0 & self._ThreadId != get_ident():
00247             RetVal = Killer(self._ThreadId,0)
00248             self._ThreadId = 0
00249         return RetVal
00250 
00251     #-------------------------------------------------------------------------
00252 
00253     def Suspend_impl(self):
00254         MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
00255         global _Sleeping
00256         RetVal = 0
00257         if self._ThreadId > 0 & self._ThreadId != get_ident():
00258             if _Sleeping > 0:
00259                 return 0
00260             else:
00261                 RetVal = Killer(self._ThreadId, signal.SIGINT)
00262                 if RetVal > 0:
00263                     _Sleeping = 1
00264         return RetVal
00265 
00266     #-------------------------------------------------------------------------
00267 
00268     def Resume_impl(self):
00269         MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
00270         global _Sleeping
00271         RetVal = 0
00272         if self._ThreadId > 0 & self._ThreadId != get_ident():
00273             if _Sleeping > 0:
00274                 _Sleeping = 0
00275                 RetVal = 1
00276             else:
00277                 RetVal = 0
00278         return RetVal
00279 
00280     #-------------------------------------------------------------------------
00281 
00282     def CpuUsed_impl(self):
00283         if ( self._ThreadId | self._Executed ) :
00284             if self._ThreadId == get_ident() :
00285                 cpu = time.clock()
00286                 self._ThreadCpuUsed = cpu - self._StartUsed
00287                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
00288                 return self._ThreadCpuUsed
00289             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
00290             return self._ThreadCpuUsed
00291         MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl self._StartUsed " + self._serviceName + " " + str(self._StartUsed) )
00292         return 0
00293 
00294     #-------------------------------------------------------------------------
00295    
00296     def DumpPython(self, theStudy, isPublished, isMultiFile):
00297         aBuffer = "\0"
00298         if isMultiFile :
00299             aBuffer = "def RebuildData(theStudy): pass\n\0"
00300         return (aBuffer, 1)
00301 
00302     #-------------------------------------------------------------------------    
00303 
00304     def getStudyId(self):
00305         return self._studyId
00306 
00307     #-------------------------------------------------------------------------    
00308 
00309     def hasObjectInfo(self):
00310         return 0
00311 
00312     #-------------------------------------------------------------------------    
00313 
00314     def getObjectInfo(self, studyId, entry):
00315         return ""
00316