Back to index

salome-kernel  6.5.0
SALOME_ContainerPy.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_ContainerPy.py
00027 #  Author : Paul RASCLE, EDF
00028 #  Module : SALOME
00029 #  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/SALOME_ContainerPy.py,v 1.12.2.2.14.3.2.1 2012-04-12 14:05:03 vsr Exp $
00030 #
00031 import os
00032 import sys
00033 import string
00034 
00035 from omniORB import CORBA, PortableServer
00036 import SALOMEDS 
00037 import Engines, Engines__POA
00038 from SALOME_NamingServicePy import *
00039 from SALOME_ComponentPy import *
00040 
00041 from SALOME_utilities import *
00042 from Utils_Identity import getShortHostName
00043 from launchConfigureParser import verbose
00044 
00045 #=============================================================================
00046 
00047 #define an implementation of the container interface for the container implemented in Python
00048 
00049 class SALOME_ContainerPy_i (Engines__POA.Container):
00050     _orb = None
00051     _poa = None
00052     _numInstance = 0
00053     _listInstances_map = {}
00054 
00055     #-------------------------------------------------------------------------
00056 
00057     def __init__(self, orb, poa, containerName):
00058         MESSAGE( "SALOME_ContainerPy_i::__init__" )
00059         self._orb = orb
00060         self._poa = poa
00061         myMachine=getShortHostName()
00062         Container_path = "/Containers/" + myMachine + "/" + containerName
00063         self._containerName = Container_path
00064         if verbose(): print "container name ",self._containerName
00065 
00066         naming_service = SALOME_NamingServicePy_i(self._orb)
00067         self._naming_service = naming_service
00068         MESSAGE( str(Container_path) )
00069         naming_service.Register(self._this(), Container_path)
00070             
00071     #-------------------------------------------------------------------------
00072 
00073     def start_impl(self, ContainerName):
00074         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
00075         myMachine=getShortHostName()
00076         theContainer = "/Containers/" + myMachine + "/" + ContainerName
00077         try:
00078             obj = self._naming_service.Resolve(theContainer)
00079         except :
00080             obj = None
00081             MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
00082         if obj is None:
00083             container = None
00084         else:
00085             container = obj._narrow(Engines.Container)
00086             if container is None:
00087                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
00088             else :
00089                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
00090             return container
00091         #shstr = os.getenv( "PWD" ) + "/"
00092         #shstr += "runSession ./SALOME_ContainerPy.py "
00093         shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
00094         #shstr = "runSession SALOME_ContainerPy.py "
00095         shstr += ContainerName
00096 
00097         # mpv: fix for SAL4731 - allways create new file to write log of server
00098         num = 1
00099         fileName = ""
00100         while 1:
00101             fileName = "/tmp/"+ContainerName+"_%i.log"%num
00102             if not os.path.exists(fileName):
00103                 break
00104             num += 1
00105             pass
00106         
00107         shstr += " > "
00108         shstr += fileName
00109         shstr += " 2>&1 &"
00110         
00111         #shstr += " > /tmp/"
00112         #shstr += ContainerName
00113         #shstr += ".log 2>&1 &"
00114         
00115         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
00116         os.system( shstr )
00117         count = 21
00118         while container is None :
00119             time.sleep(1)
00120             count = count - 1
00121             MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
00122             try :
00123                 obj = self._naming_service.Resolve(theContainer)
00124             except :
00125                 obj = None
00126             if obj is None:
00127                 container = None
00128             else:
00129                 container = obj._narrow(Engines.Container)
00130                 if container is None:
00131                     MESSAGE(  str(containerName) + ".object exists but is not a Container" )
00132                 return container
00133             if count == 0 :
00134                 return container
00135 
00136     #-------------------------------------------------------------------------
00137 
00138     def instance(self, nameToRegister, componentName):
00139         MESSAGE(  "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
00140         self._numInstance = self._numInstance +1
00141         instanceName = nameToRegister + "_inst_" + `self._numInstance`
00142 
00143         component=__import__(componentName)
00144         factory=getattr(component,componentName)
00145         comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
00146                        instanceName, nameToRegister)
00147 
00148         MESSAGE( "SALOME_ContainerPy_i::instance : component created")
00149         comp_o = comp_i._this()
00150         return comp_o
00151 
00152     #-------------------------------------------------------------------------
00153 
00154     def load_impl(self, nameToRegister, componentName):
00155         MESSAGE(  "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
00156         self._numInstance = self._numInstance +1
00157         instanceName = nameToRegister + "_inst_" + `self._numInstance`
00158         interfaceName = nameToRegister
00159         the_command = "import " + nameToRegister + "\n"
00160         the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
00161         the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
00162         MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
00163         exec the_command
00164         comp_o = comp_i._this()
00165         return comp_o
00166     
00167     #-------------------------------------------------------------------------
00168     
00169     def import_component(self, componentName):
00170         MESSAGE( "SALOME_Container_i::import_component" )
00171         reason = ""
00172         try:
00173             if verbose(): print "try import %s" % componentName
00174             # try import component
00175             module=__import__(componentName)
00176             if verbose(): print "import %s is done successfully" % componentName
00177             # if import successfully, check that component is loadable
00178             if not hasattr(module, componentName):
00179                 reason = "module %s is not loadable" % componentName
00180                 print reason
00181                 pass
00182             pass
00183         except:
00184             import traceback
00185             print "cannot import %s" % componentName
00186             traceback.print_exc()
00187             reason = "cannot import %s" % componentName
00188         return reason
00189 
00190     #-------------------------------------------------------------------------
00191 
00192     def load_component_Library(self, componentName):
00193         MESSAGE(  "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
00194         ret = 0
00195         instanceName = componentName + "_inst_" + `self._numInstance`
00196         interfaceName = componentName
00197         reason = self.import_component(componentName)
00198         return reason == "", reason
00199     
00200     #-------------------------------------------------------------------------
00201 
00202     def create_component_instance_env(self, componentName, studyId, env):
00203       return self.create_component_instance(componentName, studyId), ""
00204 
00205     def create_component_instance(self, componentName, studyId):
00206         MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
00207         if studyId < 0:
00208             MESSAGE( "Study ID is lower than 0!" )
00209             return None
00210         else:
00211             self._numInstance = self._numInstance +1
00212             instanceName = componentName + "_inst_" + `self._numInstance`
00213             comp_iors=""
00214             try:
00215                 component=__import__(componentName)
00216                 factory=getattr(component,componentName)
00217                 comp_i=factory(self._orb,
00218                                self._poa,
00219                                self._this(),
00220                                self._containerName,
00221                                instanceName,
00222                                componentName)
00223                 
00224                 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
00225                 comp_o = comp_i._this()
00226                 self._listInstances_map[instanceName] = comp_i
00227             except:
00228                 import traceback
00229                 traceback.print_exc()
00230                 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
00231             return comp_o
00232 
00233     #-------------------------------------------------------------------------
00234 
00235     def find_component_instance(self, registeredName, studyId):
00236         anEngine = None
00237         keysList = self._listInstances_map.keys()
00238         i = 0
00239         while i < len(keysList):
00240             instance = keysList[i]
00241             if find(instance,registeredName) == 0:
00242                 anEngine = self._listInstances_map[instance]
00243                 if studyId == anEngine.getStudyId():
00244                     return anEngine._this()
00245             i = i + 1
00246         return anEngine._this()
00247         
00248         
00249     #-------------------------------------------------------------------------
00250 
00251     def remove_impl(self, component):
00252         MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
00253         instanceName = component._get_instanceName()
00254         MESSAGE( "unload component " + str(instanceName) )
00255         self._listInstances_map.remove(instanceName)
00256         component.destroy()
00257         self._naming_service.Destroy_Name(str(instanceName))
00258 
00259     #-------------------------------------------------------------------------
00260 
00261     def finalize_removal(self):
00262         MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
00263         return None
00264 
00265     #-------------------------------------------------------------------------
00266 
00267     def ping(self):
00268         MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
00269         return None
00270 
00271     #-------------------------------------------------------------------------
00272 
00273     def getPID(self):
00274         return os.getpid()
00275 
00276     #-------------------------------------------------------------------------
00277 
00278     def _get_name(self):
00279         MESSAGE( "SALOME_ContainerPy_i::_get_name" )
00280         return self._containerName
00281 
00282     #-------------------------------------------------------------------------
00283 
00284     def getHostName(self):
00285         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
00286         self._machineName = "localhost"
00287         return self._machineName
00288 
00289     #-------------------------------------------------------------------------
00290     
00291     def _get_machineName(self):
00292         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
00293         self._machineName = "localhost"
00294         return self._machineName
00295 
00296     #-------------------------------------------------------------------------
00297 
00298     def Shutdown(self):
00299         self._naming_service.Destroy_Name(self._containerName);
00300         self._naming_service.Destroy_FullDirectory(self._containerName);
00301         self._orb.shutdown(0)
00302         pass
00303 
00304     def _get_logfilename(self):
00305       return self._logfilename
00306     def _set_logfilename(self,logfilename):
00307       self._logfilename=logfilename
00308     def _get_workingdir(self):
00309       return os.getcwd()
00310 
00311 #=============================================================================
00312 
00313 if __name__ == "__main__":
00314   # change the stdout buffering to line buffering (same as C++ cout buffering)
00315   sys.stdout=os.fdopen(1,"w",1)
00316   #initialise the ORB and find the root POA
00317   if verbose():print "Starting ",sys.argv[1]
00318   orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
00319   poa = orb.resolve_initial_references("RootPOA")
00320   if verbose():print "ORB and POA initialized"
00321 
00322   #create an instance of SALOME_ContainerPy_i and a Container reference
00323   #containerName = "FactoryServerPy"
00324   MESSAGE( str(sys.argv) )
00325   containerName = sys.argv[1]
00326   cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
00327   if verbose():print "SALOME_ContainerPy_i instance created ",cpy_i 
00328   cpy_o = cpy_i._this()
00329   if verbose():print "SALOME_ContainerPy_i instance activated ",cpy_o
00330   sys.stdout.flush()
00331   sys.stderr.flush()
00332 
00333   #activate the POA
00334   poaManager = poa._get_the_POAManager()
00335   poaManager.activate()
00336 
00337   #Block for ever
00338   orb.run()
00339   if verbose():print "SALOME_ContainerPy_i shutdown"