]> SALOME platform Git repositories - modules/kernel.git/blob - bin/orbmodule.py
Salome HOME
Updare for path with space
[modules/kernel.git] / bin / orbmodule.py
1 import sys,os,time
2 import string
3 from nameserver import *
4
5 from omniORB import CORBA
6
7 # Import the stubs for the Naming service
8 import CosNaming
9
10
11 # -----------------------------------------------------------------------------
12
13 class client:
14
15    def __init__(self,args):
16       # Initialise the ORB
17       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
18       # Initialise the Naming Service
19       self.initNS(args)
20
21    # --------------------------------------------------------------------------
22
23    def initNS(self,args):
24       # Obtain a reference to the root naming context
25       obj         = self.orb.resolve_initial_references("NameService")
26       try:
27           self.rootContext = obj._narrow(CosNaming.NamingContext)
28           return
29       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
30           self.rootContext = None
31           print "Lancement du Naming Service",
32           
33       # On lance le Naming Server (doit etre dans le PATH)
34       ns = NamingServer(args).run()
35       print "Searching Naming Service ",
36       ncount=0
37       delta=0.1
38       while(ncount < 10):
39           ncount += 1
40           try:
41               obj = self.orb.resolve_initial_references("NameService")
42               self.rootContext = obj._narrow(CosNaming.NamingContext)
43               break
44           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
45               self.rootContext = None
46               sys.stdout.write('+')
47               sys.stdout.flush()
48               time.sleep(delta)
49
50       if self.rootContext is None:
51           print "Failed to narrow the root naming context"
52           sys.exit(1)
53       print " found in %s seconds " % ((ncount-1)*delta)
54
55    # --------------------------------------------------------------------------
56
57    def showNScontext(self,context,dec=''):
58       bl,bi=context.list(0)
59       if bi is not None:
60          ok,b=bi.next_one()
61          while(ok):
62             for s in b.binding_name :
63                print "%s%s.%s" %(dec,s.id,s.kind)
64                if s.kind == "dir":
65                   obj=context.resolve([s])
66                   scontext = obj._narrow(CosNaming.NamingContext)
67                   self.showNScontext(scontext,dec=dec+'  ')
68             ok,b=bi.next_one()
69
70    # --------------------------------------------------------------------------
71
72    def showNS(self):
73       """ Show the content of NS"""
74       self.showNScontext(self.rootContext)
75
76    # --------------------------------------------------------------------------
77
78    def Resolve(self, Path):
79       resolve_path=string.split(Path,'/')
80       if resolve_path[0] == '': del resolve_path[0]
81       dir_path=resolve_path[:-1]
82       context_name=[]
83       for e in dir_path:
84          context_name.append(CosNaming.NameComponent(e,"dir"))
85       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
86
87       try:
88           obj = self.rootContext.resolve(context_name)
89       except CosNaming.NamingContext.NotFound, ex:
90           obj = None
91       except CosNaming.NamingContext.InvalidName, ex:
92           obj = None
93       except CosNaming.NamingContext.CannotProceed, ex:
94           obj = None
95       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
96           obj = None
97       return obj
98
99    # --------------------------------------------------------------------------
100
101    def waitNS(self,name,typobj=None,maxcount=1000):
102       count=0
103       delta=0.5
104       print "Searching %s in Naming Service " % name,
105       while(1):
106           count += 1
107           if count > maxcount : raise "Impossible de trouver %s" % name
108           obj=self.Resolve(name)
109           if obj : 
110               print " found in %s seconds " % ((count-1)*delta)
111               break
112           else:
113               sys.stdout.write('+')
114               sys.stdout.flush()
115               time.sleep(delta)
116  
117       if typobj is None:return obj
118
119       nobj = obj._narrow(typobj)
120       if nobj is None:
121             print "%s exists but is not a %s" % (name,typobj)
122       return nobj
123
124    # --------------------------------------------------------------------------
125
126    if sys.platform != "win32":
127      def waitNSPID(self, theName, thePID, theTypObj = None):
128         aCount = 0
129         aDelta = 0.5
130         anObj = None
131         print "Searching %s in Naming Service " % theName,
132         while(1):
133            try:
134               aPid, aStatus = os.waitpid(thePID,os.WNOHANG)
135            except Exception, exc:
136               raise "Impossible de trouver %s" % theName
137            aCount += 1
138            anObj = self.Resolve(theName)
139            if anObj: 
140               print " found in %s seconds " % ((aCount-1)*aDelta)
141               break
142            else:
143               sys.stdout.write('+')
144               sys.stdout.flush()
145               time.sleep(aDelta)
146               pass
147            pass
148       
149         if theTypObj is None:
150            return anObj
151
152         anObject = anObj._narrow(theTypObj)
153         if anObject is None:
154            print "%s exists but is not a %s" % (theName,theTypObj)
155         return anObject
156
157
158    # --------------------------------------------------------------------------
159
160    def ResolveLogger(self, name):
161       context_name=[]
162       context_name.append(CosNaming.NameComponent(name,""))
163
164       try:
165           obj = self.rootContext.resolve(context_name)
166       except CosNaming.NamingContext.NotFound, ex:
167           obj = None
168       except CosNaming.NamingContext.InvalidName, ex:
169           obj = None
170       except CosNaming.NamingContext.CannotProceed, ex:
171           obj = None
172       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
173           obj = None
174       return obj
175    
176    # --------------------------------------------------------------------------
177
178    def waitLogger(self,name,typobj=None,maxcount=40):
179       count=0
180       delta=0.5
181       print "Searching %s in Naming Service " % name,
182       while(1):
183           count += 1
184           if count > maxcount : raise "Impossible de trouver %s" % name
185           obj=self.ResolveLogger(name)
186           if obj : 
187               print " found in %s seconds " % ((count-1)*delta)
188               break
189           else:
190               sys.stdout.write('+')
191               sys.stdout.flush()
192               time.sleep(delta)
193  
194       if typobj is None:return obj
195
196       nobj = obj._narrow(typobj)
197       if nobj is None:
198             print "%s exists but is not a %s" % (name,typobj)
199       return nobj
200