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