Salome HOME
CCAR: improve the shutdown process
[modules/kernel.git] / bin / orbmodule.py
1 #  -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 #  This library is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU Lesser General Public
9 #  License as published by the Free Software Foundation; either
10 #  version 2.1 of the License.
11 #
12 #  This library is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public
18 #  License along with this library; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 ## @package orbmodule
24 # \brief Module that provides a client for %SALOME
25 #
26 #
27
28 import sys,os,time
29 import string
30 from nameserver import *
31 from omniORB import CORBA
32 from launchConfigureParser import verbose
33
34 # Import the stubs for the Naming service
35 import CosNaming
36 #from runNS import *
37
38 # -----------------------------------------------------------------------------
39
40 class client:
41    """Client for SALOME"""
42
43    def __init__(self,args=None):
44       #set GIOP message size for bug 10560: impossible to get field values in TUI mode
45       sys.argv.extend(["-ORBgiopMaxMsgSize", "104857600"]) ## = 100 * 1024 * 1024
46       # Initialise the ORB
47       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
48       # Initialise the Naming Service
49       self.initNS(args or {})
50
51    # --------------------------------------------------------------------------
52
53    def initNS(self,args):
54       # Obtain a reference to the root naming context
55       obj         = self.orb.resolve_initial_references("NameService")
56       try:
57           self.rootContext = obj._narrow(CosNaming.NamingContext)
58           return
59       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
60           self.rootContext = None
61           if verbose(): print "Launch Naming Service++",
62           
63       # On lance le Naming Server (doit etre dans le PATH)
64       NamingServer(args).run()
65       print "Searching Naming Service ",
66       ncount=0
67       delta=0.1
68       while(ncount < 10):
69           ncount += 1
70           try:
71               obj = self.orb.resolve_initial_references("NameService")
72               self.rootContext = obj._narrow(CosNaming.NamingContext)
73               break
74           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
75               self.rootContext = None
76               sys.stdout.write('+')
77               sys.stdout.flush()
78               time.sleep(delta)
79
80       if self.rootContext is None:
81           print "Failed to narrow the root naming context"
82           sys.exit(1)
83       print " found in %s seconds " % ((ncount-1)*delta)
84
85    # --------------------------------------------------------------------------
86
87    def showNScontext(self,context,dec=''):
88       bl,bi=context.list(0)
89       if bi is not None:
90          ok,b=bi.next_one()
91          while(ok):
92             for s in b.binding_name :
93                print "%s%s.%s" %(dec,s.id,s.kind)
94                if s.kind == "dir":
95                   obj=context.resolve([s])
96                   scontext = obj._narrow(CosNaming.NamingContext)
97                   self.showNScontext(scontext,dec=dec+'  ')
98             ok,b=bi.next_one()
99
100    # --------------------------------------------------------------------------
101
102    def showNS(self):
103       """ Show the content of SALOME naming service """
104       self.showNScontext(self.rootContext)
105
106    # --------------------------------------------------------------------------
107
108    def Resolve(self, Path):
109       resolve_path=string.split(Path,'/')
110       if resolve_path[0] == '': del resolve_path[0]
111       dir_path=resolve_path[:-1]
112       context_name=[]
113       for e in dir_path:
114          context_name.append(CosNaming.NameComponent(e,"dir"))
115       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
116
117       try:
118           obj = self.rootContext.resolve(context_name)
119       except CosNaming.NamingContext.NotFound, ex:
120           obj = None
121       except CosNaming.NamingContext.InvalidName, ex:
122           obj = None
123       except CosNaming.NamingContext.CannotProceed, ex:
124           obj = None
125       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
126           obj = None
127       return obj
128
129    # --------------------------------------------------------------------------
130
131    def waitNS(self,name,typobj=None,maxcount=240):
132       count=0
133       delta=0.5
134       print "Searching %s in Naming Service " % name,
135       while(1):
136           count += 1
137           if count > maxcount : raise RuntimeError, "Impossible de trouver %s" % name
138           obj=self.Resolve(name)
139           if obj : 
140               print " found in %s seconds " % ((count-1)*delta)
141               break
142           else:
143               sys.stdout.write('+')
144               sys.stdout.flush()
145               time.sleep(delta)
146  
147       if typobj is None:return obj
148
149       nobj = obj._narrow(typobj)
150       if nobj is None:
151             print "%s exists but is not a %s" % (name,typobj)
152       return nobj
153
154    if sys.platform != "win32":
155     def waitNSPID(self, theName, thePID, theTypObj = None):
156       aCount = 0
157       aDelta = 0.5
158       anObj = None
159       print "Searching %s in Naming Service " % theName,
160       while(1):
161          try:
162            os.kill(thePID,0)
163          except:
164            raise RuntimeError, "Process %d for %s not found" % (thePID,theName)
165          aCount += 1
166          anObj = self.Resolve(theName)
167          if anObj: 
168             print " found in %s seconds " % ((aCount-1)*aDelta)
169             break
170          else:
171             sys.stdout.write('+')
172             sys.stdout.flush()
173             time.sleep(aDelta)
174             pass
175          pass
176       
177       if theTypObj is None:
178          return anObj
179
180       anObject = anObj._narrow(theTypObj)
181       if anObject is None:
182          print "%s exists but is not a %s" % (theName,theTypObj)
183       return anObject
184
185
186    # --------------------------------------------------------------------------
187
188    def ResolveLogger(self, name):
189       context_name=[]
190       context_name.append(CosNaming.NameComponent(name,""))
191
192       try:
193           obj = self.rootContext.resolve(context_name)
194       except CosNaming.NamingContext.NotFound, ex:
195           obj = None
196       except CosNaming.NamingContext.InvalidName, ex:
197           obj = None
198       except CosNaming.NamingContext.CannotProceed, ex:
199           obj = None
200       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
201           obj = None
202       return obj
203    
204    # --------------------------------------------------------------------------
205
206    def waitLogger(self,name,typobj=None,maxcount=40):
207       count=0
208       delta=0.5
209       print "Searching %s in Naming Service " % name,
210       while(1):
211           count += 1
212           if count > maxcount : raise RuntimeError, "Impossible de trouver %s" % name
213           obj=self.ResolveLogger(name)
214           if obj : 
215               print " found in %s seconds " % ((count-1)*delta)
216               break
217           else:
218               sys.stdout.write('+')
219               sys.stdout.flush()
220               time.sleep(delta)
221  
222       if typobj is None:return obj
223
224       nobj = obj._narrow(typobj)
225       if nobj is None:
226             print "%s exists but is not a %s" % (name,typobj)
227       return nobj
228