Salome HOME
CCAR:
[modules/kernel.git] / src / LifeCycleCORBA / LifeCycleCORBA.py
index 550a88ff9d988bbfeb9716312f0c6e5e41679d68..371affd1cef2010b1ef94424c38a9de83a33c302 100644 (file)
@@ -1,40 +1,44 @@
-#  SALOME LifeCycleC RBA : implementation of containers and engines life cycle both in Python and C++
+#  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+#
+#  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 LifeCycleC RBA : implementation of containers and engines life cycle both in Python and C++
 #  File   : LifeCycleCORBA.py
 #  Author : Paul RASCLE, EDF
 #  Module : SALOME
 #  $Header$
-
+#
 import os
 import sys
 import time
+import string
 from omniORB import CORBA
 import CosNaming
 import Engines
+reload(Engines)
 import SALOME_ModuleCatalog
 
 from SALOME_utilities import *
+from Utils_Identity import getShortHostName
+import Utils_Identity 
+import Launchers
 
 class LifeCycleCORBA:
     _orb = None
@@ -76,6 +80,15 @@ class LifeCycleCORBA:
         if self._catalog is None:
             MESSAGE( "/Kernel.dir/ModulCatalog.object exists but is not a ModulCatalog" )
 
+        name = [CosNaming.NameComponent("ContainerManager","object")]
+        try:
+            obj = self._rootContext.resolve(name)
+        except CosNaming.NamingContext.NotFound, ex:
+            MESSAGE( "ContainerManager.object not found in Naming Service" )
+        self._contManager = obj._narrow(Engines.ContainerManager)
+        if self._contManager is None:
+            MESSAGE( "ContainerManager.object exists but is not a ContainerManager")
+
     #-------------------------------------------------------------------------
 
     def ContainerName(self, containerName):
@@ -85,22 +98,16 @@ class LifeCycleCORBA:
         except:
             theComputer = ""
             theContainer = containerName
-        if theComputer == "" :
-            theComputer = os.getenv("HOSTNAME")
-        if theComputer == "localhost" :
-            theComputer = os.getenv("HOSTNAME")
-        computerSplitName = theComputer.split('.')
-        theComputer = computerSplitName[0]
+
+        if theComputer in ("","localhost") :
+            theComputer = getShortHostName()
+
         MESSAGE( theComputer + theContainer )
         return theComputer,theContainer
 
     #-------------------------------------------------------------------------
 
     def ComputerPath(self, ComputerName ):
-        # Modification provisoire B. Secher en attendant
-        # le gestionnaire de ressources 21/10/2003
-        # Le KERNEL_ROOT_DIR sera a lire dans le catalogue de machines
-        # en attendant on suppose qu'il est identique au KERNEL_ROOT_DIR local
         try:
             #path = self._catalog.GetPathPrefix( ComputerName )
             path = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/"
@@ -132,119 +139,136 @@ class LifeCycleCORBA:
     
     #-------------------------------------------------------------------------
 
+    def FindComponent(self,containerName,componentName,listOfMachines):
+        if containerName!="":
+            machinesOK=[]
+            for i in range(len(listOfMachines)):
+                currentMachine=listOfMachines[i]
+                componentNameForNS= [CosNaming.NameComponent(currentMachine,"dir"),
+                                     CosNaming.NameComponent(containerName,"dir"),
+                                     CosNaming.NameComponent(componentName,"object")]
+                obj=None
+                try:
+                    obj = self._containerRootContext.resolve(componentNameForNS)
+                except CosNaming.NamingContext.NotFound, ex:
+                    MESSAGE( "component " + componentName + " not found on machine " + currentMachine + " , trying to load" )
+                    pass
+                if obj is not None:
+                    machinesOK.append(currentMachine)
+                    pass
+                pass
+            if len(machinesOK)!=0:
+                bestMachine=self._contManager.FindFirst(machinesOK)
+                componentNameForNS= [CosNaming.NameComponent(bestMachine,"dir"),
+                                     CosNaming.NameComponent(containerName,"dir"),
+                                     CosNaming.NameComponent(componentName,"object")]
+                obj=None
+                try:
+                    obj = self._containerRootContext.resolve(componentNameForNS)
+                except:
+                    pass
+                if obj is not None:
+                    return obj._narrow(Engines.Component)
+                else:
+                    MESSAGE( "Big problem !!!")
+                    return None
+            else:
+                return None
+        else:
+            bestMachine=self._contManager.FindFirst(listOfMachines)
+            MESSAGE("Not implemented yet ...")
+            return None
+        pass
+
+    #-------------------------------------------------------------------------
+
+    def setLauncher(self,name):
+        """Change default launcher to the launcher identified by name
+
+           See module Launchers.py
+        """
+        Launchers.setLauncher(name)
+
+    #-------------------------------------------------------------------------
+
+    def StartContainer(self, theComputer , theContainer ):
+        """Start a container on theComputer machine with theContainer name
+       """
+       # Get the Naming Service address
+       #
+        addr=self._orb.object_to_string(self._rootContext)
+       #
+       # If container name contains "Py" launch a Python Container
+       #
+        if theContainer.find('Py') == -1 :
+           CMD=['SALOME_Container',theContainer,'-ORBInitRef','NameService='+addr]
+        else:
+           CMD=['SALOME_ContainerPy.py',theContainer,'-ORBInitRef','NameService='+addr]
+        if theComputer in ("","localhost"):
+           theComputer=getShortHostName()
+       #
+       # Get the appropriate launcher and ask to launch
+       #
+        Launchers.getLauncher(theComputer).launch(theComputer,CMD)
+       #
+       # Wait until the container is registered in Naming Service
+       #
+        count =5 
+       aContainer=None
+        while aContainer is None and count > 0:
+            time.sleep(1)
+            count = count - 1
+            MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
+            aContainer = self.FindContainer( theComputer + "/" + theContainer )
+       return aContainer
+
+    #-------------------------------------------------------------------------
+
     def FindOrStartContainer(self, theComputer , theContainer ):
-        MESSAGE( "FindOrStartContainer" + theComputer + theContainer )
+        """Find or Start a container on theComputer machine with theContainer name
+       """
+        if theComputer in ("","localhost"):
+           theComputer=getShortHostName()
+        MESSAGE( "FindOrStartContainer: " + theComputer + theContainer )
         aContainer = self.FindContainer( theComputer + "/" + theContainer )
         if aContainer is None :
-            if (theContainer == "FactoryServer") | (theContainer == "FactoryServerPy") :
-                if theComputer == os.getenv("HOSTNAME") :
-                    rshstr = ""
-                else :
-                    rshstr = "rsh -n " + theComputer + " "
-                path = self.ComputerPath( theComputer )
-##                if path != "" :
-##                    rshstr = rshstr + path + "/../bin/"
-##                else :
-##                    rshstr = rshstr + os.getenv( "KERNEL_ROOT_DIR" ) + "/bin/"
-                if theContainer == "FactoryServer" :
-                    rshstr = rshstr + path + "SALOME_Container "
-                else :
-                    rshstr = rshstr + path + "SALOME_ContainerPy.py '"
-                rshstr = rshstr + theContainer + " -"
-               omniORBcfg = os.getenv( "OMNIORB_CONFIG" )
-                file = os.open( omniORBcfg , os.O_RDONLY )
-                ORBInitRef = os.read(file,132)
-                if ORBInitRef[len(ORBInitRef)-1] == '\n' :
-                    ORBInitRef,bsn = ORBInitRef.split('\n')
-                os.close( file )
-                rshstr = rshstr + ORBInitRef
-                if theContainer == "FactoryServerPy" :
-                    rshstr = rshstr + "'"
-                rshstr = rshstr + " > /tmp/" + theContainer + "_"
-                rshstr = rshstr + theComputer
-                rshstr = rshstr + ".log 2>&1 &"
-                os.system( rshstr )
-                MESSAGE( "FindOrStartContainer" + rshstr + " done" )
-            else :
-                if theContainer.find('Py') == -1 :
-                    aContainer = self.FindContainer( theComputer + "/" + "FactoryServer" )
-                else :
-                    aContainer = self.FindContainer( theComputer + "/" + "FactoryServerPy" )
-                aContainer = aContainer.start_impl( theContainer )
-
-            count = 21
-            while aContainer is None :
-                time.sleep(1)
-                count = count - 1
-                MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theContainer )
-                aContainer = self.FindContainer( theComputer + "/" + theContainer )
-                if count == 0 :
-                    return aContainer
-            
-        return  aContainer       
-        #os.system("rsh -n dm2s0017 /export/home/KERNEL_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515")
-
+            aContainer= self.StartContainer(theComputer , theContainer )
+       return aContainer
+           
     #-------------------------------------------------------------------------
 
-    def FindOrLoadComponent(self, containerName, componentName):
-
-        theComputer,theContainer = self.ContainerName( containerName )
-        name = [CosNaming.NameComponent(theComputer,"dir"),
-                CosNaming.NameComponent(theContainer,"dir"),
-                CosNaming.NameComponent(componentName,"object")]
+    def LoadComponent(self,containerName,componentName,listOfMachine):
+        container=self._contManager.FindOrStartContainer(containerName,listOfMachine)
+        implementation="lib"+componentName+"Engine.so"
         try:
-            obj = self._containerRootContext.resolve(name)
-        except CosNaming.NamingContext.NotFound, ex:
-            MESSAGE( "component " + componentName + " not found, trying to load" )
-            container = self.FindContainer(theComputer + "/" + theContainer)
-            if container is None:
-                MESSAGE( "container " + theComputer + "/" + theContainer + " not found in Naming Service, trying to start" )
-                if (theContainer != "FactoryServer") & (theContainer != "FactoryServerPy") :
-                    if theContainer.find('Py') == -1 :
-                        theFactorycontainer = "FactoryServer"
-                    else :
-                        theFactorycontainer = "FactoryServerPy"
-                    Factorycontainer = self.FindContainer(theComputer + "/" + theFactorycontainer)
-                    if Factorycontainer is None:
-                        MESSAGE( "container " + theComputer + "/" + theFactorycontainer + " not found in Naming Service, trying to start" )
-                        Factorycontainer = self.FindOrStartContainer(theComputer,theFactorycontainer)
-                else:
-                    Factorycontainer = self.FindOrStartContainer(theComputer,theContainer)
-                if Factorycontainer != None :
-                    container = self.FindOrStartContainer(theComputer,theContainer)
-
-            if container != None:
-                compoinfo = self._catalog.GetComponent(componentName)
-                if compoinfo is None:
-                    MESSAGE( "component " + componentName + " not found in Module Catalog" )
-                else:
-                    try:
-                        machineName = theComputer
-                        path = compoinfo.GetPathPrefix(machineName) + "/"
-                    except SALOME_ModuleCatalog.NotFound, ex:
-                        MESSAGE( "machine " + machineName + " not found in Module Catalog" )
-                        MESSAGE( "trying localhost" )
-                        try:
-                            path = compoinfo.GetPathPrefix("localhost") + "/"
-                        except SALOME_ModuleCatalog.NotFound, ex:
-                            path = ""
-                    implementation = path + "lib" + componentName + "Engine.so"
-                    MESSAGE( "Trying to load " + implementation )
-                    try:
-                        component = container.load_impl(componentName, implementation)
-                        MESSAGE( "component " + component._get_instanceName() + " launched !" )
-                        return component
-                    except:
-                        MESSAGE( "component " + componentName + " NOT launched !" )
+            component = container.load_impl(componentName, implementation)
+            MESSAGE( "component " + component._get_instanceName() + " launched !" )
+            return component
+        except:
+            MESSAGE( "component " + componentName + " NOT launched !" )
+            return None
+
+    #-------------------------------------------------------------------------
+    
 
+    def FindOrLoadComponent(self, containerName, componentName):
+        sp=containerName.split("/")
+        if len(sp)==1:
+            listOfMachine=[]
+            listOfMachine.append(getShortHostName())
+            comp=self.FindComponent(containerName,componentName,listOfMachine)
+            if comp is None:
+                return self.LoadComponent(containerName,componentName,listOfMachine)
+            else:
+                return comp
+            pass
         else:
-            try:
-                component = obj._narrow(Engines.Component)
-                if component is None:
-                    MESSAGE( componentName + " is not a component !" )
-                else:
-                    MESSAGE( "component " + component._get_instanceName() + " found !" )
-                return component
-            except:
-                MESSAGE( componentName + " failure" )
-                return None
+            params= Engines.MachineParameters(sp[1],sp[0],"LINUX",0,0,0,0)
+            listOfMachine=self._contManager.GetFittingResources(params,componentName)
+            ret=self.FindComponent(sp[1],componentName,listOfMachine);
+            if ret is None:
+                return self.LoadComponent(sp[1],componentName,listOfMachine)
+            else:
+                return ret
+            pass
+