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