Salome HOME
CCAR: import_hook.py was too strict in ensure_list (ImportError raised)
[modules/kernel.git] / src / Container / SALOME_ComponentPy.py
index 10a47c9781447c770817d9f1e470bdaa38fff828..86ec00e6d8e6d58c4ac4de2648e41c2fe2eef7d6 100755 (executable)
@@ -1,38 +1,43 @@
 #! /usr/bin/env python
+#  -*- coding: iso-8859-1 -*-
+#  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
 #
-#  SALOME Container : implementation of container and engine for Kernel
+#  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+#  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License.
 #
-#  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-#  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-# 
-#  This library is free software; you can redistribute it and/or 
-#  modify it under the terms of the GNU Lesser General Public 
-#  License as published by the Free Software Foundation; either 
-#  version 2.1 of the License. 
-# 
-#  This library is distributed in the hope that it will be useful, 
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-#  Lesser General Public License for more details. 
-# 
-#  You should have received a copy of the GNU Lesser General Public 
-#  License along with this library; if not, write to the Free Software 
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-# 
-#  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
 #
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
+#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
+
+#  SALOME Container : implementation of container and engine for Kernel
 #  File   : SALOME_ComponentPy.py
 #  Author : Paul RASCLE, EDF
 #  Module : SALOME
 #  $Header$
 
+## @package SALOME_ComponentPy
+# \brief python implementation of component interface for Kernel
+#
+
 import os
 import sys
 import time
 import string
-from omniORB import CORBA, PortableServer
+import signal
+from omniORB import CORBA, PortableServer, any
 import Engines, Engines__POA
 import Registry
 from Utils_Identity import *
@@ -45,17 +50,21 @@ from thread import *
 
 #=============================================================================
 
-#define an implementation of the component interface
+_Sleeping = 0
 
+## define an implementation of the component interface Engines::Component
+#
+#  
 class SALOME_ComponentPy_i (Engines__POA.Component):
     _orb = None
     _poa = None
     _fieldsDict = []
+    _studyId = -1
     
     #-------------------------------------------------------------------------
 
     def __init__ (self, orb, poa, contID, containerName,
-                  instanceName, interfaceName, notif):
+                  instanceName, interfaceName, notif=0):
         # Notif for notification services
         # NOT YET IMPLEMENTED
         MESSAGE(  "SALOME_ComponentPy_i::__init__" + " " + str (containerName) + " " + str(instanceName) + " " + str(interfaceName) )
@@ -68,16 +77,20 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
         self._myConnexionToRegistry = 0
         self._graphName = ''
         self._nodeName = ''
+        self._serviceName = ''
         self._ThreadId = 0
         self._StartUsed = 0
         self._ThreadCpuUsed = 0
         self._Executed = 0
+        self._contId = contID
 
         naming_service = SALOME_NamingServicePy_i(self._orb)
         myMachine=getShortHostName()
-        Component_path = "/Containers/" + myMachine + "/" + self._containerName + "/" + self._interfaceName
+        Component_path = self._containerName + "/" + self._instanceName
         MESSAGE(  'SALOME_ComponentPy_i Register' + str( Component_path ) )
-        naming_service.Register(self._this(), Component_path)
+        id_o = poa.activate_object(self)
+        compo_o = poa.id_to_reference(id_o)
+        naming_service.Register(compo_o, Component_path)
 
         # Add componentinstance to registry
         obj = naming_service.Resolve('/Registry')
@@ -116,8 +129,14 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
     
     #-------------------------------------------------------------------------
 
+    def _get_interfaceName(self):
+        MESSAGE(  "SALOME_ComponentPy_i::_get_interfaceName" )
+        return self._interfaceName
+
+    #-------------------------------------------------------------------------
+
     def ping(self):
-        MESSAGE(  "SALOME_ComponentPy_i::ping" )
+        MESSAGE(  "SALOME_ComponentPy_i::ping() pid " + str(os.getpid()) )
         
     #-------------------------------------------------------------------------
 
@@ -133,14 +152,16 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
 
     def destroy(self):
         MESSAGE(  "SALOME_ComponentPy_i::destroy" )
-        poa.deactivate_object(self)
-        CORBA.release(_poa)
+        id = self._poa.servant_to_id(self)
+        self._poa.deactivate_object(id)
+        return
         
     #-------------------------------------------------------------------------
 
     def GetContainerRef(self):
         MESSAGE(  "SALOME_ComponentPy_i::GetContainerRef" )
-        
+        return self._contId._narrow(Engines.Container)
+                
     #-------------------------------------------------------------------------
 
     def beginService(self , serviceName ):
@@ -152,7 +173,13 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
         self._StartUsed = self.CpuUsed_impl()
         self._ThreadCpuUsed = 0
         self._Executed = 1
+        print "beginService for ",serviceName," Component instance : ",self._instanceName
         MESSAGE( "SALOME_ComponentPy_i::beginService _StartUsed " + str( self._ThreadId ) + " " + str( self._StartUsed ) )
+        for e in self._fieldsDict:
+          key=e.key
+          value=any.from_any(e.value)
+          if isinstance(value,str):
+            os.environ[key]=value
         
 
     #-------------------------------------------------------------------------
@@ -160,6 +187,8 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
     def endService(self , serviceName ):
         MESSAGE(  "Send EndService notification for " + str( self._ThreadId ) + " " + str(serviceName) + " for graph/node " + str(self._graphName) + " " + str(self._nodeName) + " CpuUsed " + str( self.CpuUsed_impl() ) )
         MESSAGE(  "Component instance : " + str(self._instanceName) )
+        print "endService for",serviceName,"Component instance :",self._instanceName,"Cpu Used:",self.CpuUsed_impl()," (s) "
+
 
     #-------------------------------------------------------------------------
 
@@ -185,23 +214,68 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
 
     #-------------------------------------------------------------------------
 
-    def Kill(self):
-        MESSAGE(  "SALOME_ComponentPy_i::Kill not yet implemented" )
+    def Killer(self, ThreadId, signum):
+        #if ThreadId > 0:
+            #if signum == 0:
+                #if pthread_cancel(ThreadId): <- from C++
+                #   return 0
+                #else:
+                #   MESSAGE()
+            #else:
+                #if pthread_kill(ThreadId): <- from C++
+                #   return 0
+                #else:
+                #   MESSAGE()
+        return 1                 
+    
+    #-------------------------------------------------------------------------
+
+    def Kill_impl(self):
+        MESSAGE(  "SALOME_ComponentPy_i::Kill_impl" )
+        RetVal = 0
+        if self._ThreadId > 0 & self._ThreadId != get_ident():
+            RetVal = Killer(self._ThreadId,0)
+            self._ThreadId = 0
+        return RetVal
 
     #-------------------------------------------------------------------------
 
-    def Stop(self):
-        MESSAGE(  "SALOME_ComponentPy_i::Stop not yet implemented" )
+    def Stop_impl(self):
+        MESSAGE(  "SALOME_ComponentPy_i::Stop_impl" )
+        RetVal = 0
+        if self._ThreadId > 0 & self._ThreadId != get_ident():
+            RetVal = Killer(self._ThreadId,0)
+            self._ThreadId = 0
+        return RetVal
 
     #-------------------------------------------------------------------------
 
-    def Suspend(self):
-        MESSAGE(  "SALOME_ComponentPy_i::Suspend not yet implemented" )
+    def Suspend_impl(self):
+        MESSAGE(  "SALOME_ComponentPy_i::Suspend_impl" )
+        global _Sleeping
+        RetVal = 0
+        if self._ThreadId > 0 & self._ThreadId != get_ident():
+            if _Sleeping > 0:
+                return 0
+            else:
+                RetVal = Killer(self._ThreadId, signal.SIGINT)
+                if RetVal > 0:
+                    _Sleeping = 1
+        return RetVal
 
     #-------------------------------------------------------------------------
 
-    def Resume(self):
-        MESSAGE(  "SALOME_ComponentPy_i::Resume not yet implemented" )
+    def Resume_impl(self):
+        MESSAGE(  "SALOME_ComponentPy_i::Resume_impl" )
+        global _Sleeping
+        RetVal = 0
+        if self._ThreadId > 0 & self._ThreadId != get_ident():
+            if _Sleeping > 0:
+                _Sleeping = 0
+                RetVal = 1
+            else:
+                RetVal = 0
+        return RetVal
 
     #-------------------------------------------------------------------------
 
@@ -209,7 +283,7 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
         if ( self._ThreadId | self._Executed ) :
             if self._ThreadId == get_ident() :
                 cpu = time.clock()
-                self._ThreadCpuUsed = int(cpu) - self._StartUsed
+                self._ThreadCpuUsed = cpu - self._StartUsed
                 MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( int(cpu) ) + " - " + str( self._StartUsed ) + " = " + str( self._ThreadCpuUsed ) )
                 return self._ThreadCpuUsed
             MESSAGE( "SALOME_ComponentPy_i::CpuUsed_impl " + self._serviceName + " " + str( self._ThreadCpuUsed ) )
@@ -218,4 +292,23 @@ class SALOME_ComponentPy_i (Engines__POA.Component):
         return 0
 
     #-------------------------------------------------------------------------
+   
+    def DumpPython(self, theStudy, isPublished):
+        aBuffer = "def RebuildData(theStudy): pass\n\0"
+        return (aBuffer, 1)
+
+    #-------------------------------------------------------------------------    
+
+    def getStudyId(self):
+        return self._studyId
+
+    #-------------------------------------------------------------------------    
+
+    def hasObjectInfo(self):
+        return 0
+
+    #-------------------------------------------------------------------------    
+
+    def getObjectInfo(self, studyId, entry):
+        return ""