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