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