Back to index

salome-kernel  6.5.0
orbmodule.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 ## @package orbmodule
00025 # \brief Module that provides a client for %SALOME
00026 #
00027 
00028 import sys,os,time
00029 import string
00030 from nameserver import *
00031 from omniORB import CORBA
00032 from launchConfigureParser import verbose
00033 
00034 # Import the stubs for the Naming service
00035 import CosNaming
00036 #from runNS import *
00037 
00038 # -----------------------------------------------------------------------------
00039 
00040 class client:
00041    """Client for SALOME"""
00042 
00043    def __init__(self,args=None):
00044       #set GIOP message size for bug 10560: impossible to get field values in TUI mode
00045       sys.argv.extend(["-ORBgiopMaxMsgSize", "104857600"]) ## = 100 * 1024 * 1024
00046       # Initialise the ORB
00047       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
00048       # Initialise the Naming Service
00049       self.initNS(args or {})
00050 
00051    # --------------------------------------------------------------------------
00052 
00053    def initNS(self,args):
00054       # Obtain a reference to the root naming context
00055       obj         = self.orb.resolve_initial_references("NameService")
00056       try:
00057           self.rootContext = obj._narrow(CosNaming.NamingContext)
00058           return
00059       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00060           self.rootContext = None
00061           if verbose(): print "Launch Naming Service++",
00062           
00063       # On lance le Naming Server (doit etre dans le PATH)
00064       test = True
00065       if args['wake_up_session']:
00066          test = False
00067          pass
00068       if test:
00069          NamingServer(args).run()
00070          pass
00071       print "Searching Naming Service ",
00072       ncount=0
00073       delta=0.1
00074       while(ncount < 100):
00075           ncount += 1
00076           try:
00077               obj = self.orb.resolve_initial_references("NameService")
00078               self.rootContext = obj._narrow(CosNaming.NamingContext)
00079               break
00080           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00081               self.rootContext = None
00082               sys.stdout.write('+')
00083               sys.stdout.flush()
00084               time.sleep(delta)
00085 
00086       if self.rootContext is None:
00087           print "Failed to narrow the root naming context"
00088           sys.exit(1)
00089       print " found in %s seconds " % ((ncount-1)*delta)
00090 
00091    # --------------------------------------------------------------------------
00092 
00093    def showNScontext(self,context,dec=''):
00094       bl,bi=context.list(0)
00095       if bi is not None:
00096          ok,b=bi.next_one()
00097          while(ok):
00098             for s in b.binding_name :
00099                print "%s%s.%s" %(dec,s.id,s.kind)
00100                if s.kind == "dir":
00101                   obj=context.resolve([s])
00102                   scontext = obj._narrow(CosNaming.NamingContext)
00103                   self.showNScontext(scontext,dec=dec+'  ')
00104             ok,b=bi.next_one()
00105 
00106    # --------------------------------------------------------------------------
00107 
00108    def showNS(self):
00109       """ Show the content of SALOME naming service """
00110       self.showNScontext(self.rootContext)
00111 
00112    # --------------------------------------------------------------------------
00113 
00114    def Resolve(self, Path):
00115       resolve_path=string.split(Path,'/')
00116       if resolve_path[0] == '': del resolve_path[0]
00117       dir_path=resolve_path[:-1]
00118       context_name=[]
00119       for e in dir_path:
00120          context_name.append(CosNaming.NameComponent(e,"dir"))
00121       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
00122 
00123       try:
00124           obj = self.rootContext.resolve(context_name)
00125       except CosNaming.NamingContext.NotFound, ex:
00126           obj = None
00127       except CosNaming.NamingContext.InvalidName, ex:
00128           obj = None
00129       except CosNaming.NamingContext.CannotProceed, ex:
00130           obj = None
00131       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00132           obj = None
00133       return obj
00134 
00135    # --------------------------------------------------------------------------
00136 
00137    def waitNS(self,name,typobj=None,maxcount=240):
00138       count=0
00139       delta=0.5
00140       print "Searching %s in Naming Service " % name,
00141       while(1):
00142           count += 1
00143           if count > maxcount : raise RuntimeError, "Impossible de trouver %s" % name
00144           obj=self.Resolve(name)
00145           if obj : 
00146               print " found in %s seconds " % ((count-1)*delta)
00147               break
00148           else:
00149               sys.stdout.write('+')
00150               sys.stdout.flush()
00151               time.sleep(delta)
00152  
00153       if typobj is None:return obj
00154 
00155       nobj = obj._narrow(typobj)
00156       if nobj is None:
00157             print "%s exists but is not a %s" % (name,typobj)
00158       return nobj
00159 
00160    if sys.platform != "win32":
00161     def waitNSPID(self, theName, thePID, theTypObj = None):
00162       aCount = 0
00163       aDelta = 0.5
00164       anObj = None
00165       print "Searching %s in Naming Service " % theName,
00166       while(1):
00167          try:
00168            os.kill(thePID,0)
00169          except:
00170            raise RuntimeError, "Process %d for %s not found" % (thePID,theName)
00171          aCount += 1
00172          anObj = self.Resolve(theName)
00173          if anObj: 
00174             print " found in %s seconds " % ((aCount-1)*aDelta)
00175             break
00176          else:
00177             sys.stdout.write('+')
00178             sys.stdout.flush()
00179             time.sleep(aDelta)
00180             pass
00181          pass
00182       
00183       if theTypObj is None:
00184          return anObj
00185 
00186       anObject = anObj._narrow(theTypObj)
00187       if anObject is None:
00188          print "%s exists but is not a %s" % (theName,theTypObj)
00189       return anObject
00190 
00191 
00192    # --------------------------------------------------------------------------
00193 
00194    def ResolveLogger(self, name):
00195       context_name=[]
00196       context_name.append(CosNaming.NameComponent(name,""))
00197 
00198       try:
00199           obj = self.rootContext.resolve(context_name)
00200       except CosNaming.NamingContext.NotFound, ex:
00201           obj = None
00202       except CosNaming.NamingContext.InvalidName, ex:
00203           obj = None
00204       except CosNaming.NamingContext.CannotProceed, ex:
00205           obj = None
00206       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00207           obj = None
00208       return obj
00209    
00210    # --------------------------------------------------------------------------
00211 
00212    def waitLogger(self,name,typobj=None,maxcount=40):
00213       count=0
00214       delta=0.5
00215       print "Searching %s in Naming Service " % name,
00216       while(1):
00217           count += 1
00218           if count > maxcount : raise RuntimeError, "Impossible de trouver %s" % name
00219           obj=self.ResolveLogger(name)
00220           if obj : 
00221               print " found in %s seconds " % ((count-1)*delta)
00222               break
00223           else:
00224               sys.stdout.write('+')
00225               sys.stdout.flush()
00226               time.sleep(delta)
00227  
00228       if typobj is None:return obj
00229 
00230       nobj = obj._narrow(typobj)
00231       if nobj is None:
00232             print "%s exists but is not a %s" % (name,typobj)
00233       return nobj
00234