Salome HOME
merged from mergeto_V2_2_0_maintainance_18mar05
[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.TRANSIENT,CORBA.OBJECT_NOT_EXIST,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.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
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       if bi is not None:
81          ok,b=bi.next_one()
82          while(ok):
83             for s in b.binding_name :
84                print "%s%s.%s" %(dec,s.id,s.kind)
85                if s.kind == "dir":
86                   obj=context.resolve([s])
87                   scontext = obj._narrow(CosNaming.NamingContext)
88                   self.showNScontext(scontext,dec=dec+'  ')
89             ok,b=bi.next_one()
90
91    # --------------------------------------------------------------------------
92
93    def showNS(self):
94       """ Show the content of NS"""
95       self.showNScontext(self.rootContext)
96
97    # --------------------------------------------------------------------------
98
99    def Resolve(self, Path):
100       resolve_path=string.split(Path,'/')
101       if resolve_path[0] == '': del resolve_path[0]
102       dir_path=resolve_path[:-1]
103       context_name=[]
104       for e in dir_path:
105          context_name.append(CosNaming.NameComponent(e,"dir"))
106       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
107
108       try:
109           obj = self.rootContext.resolve(context_name)
110       except CosNaming.NamingContext.NotFound, ex:
111           obj = None
112       except CosNaming.NamingContext.InvalidName, ex:
113           obj = None
114       except CosNaming.NamingContext.CannotProceed, ex:
115           obj = None
116       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
117           obj = None
118       return obj
119
120    # --------------------------------------------------------------------------
121
122    def waitNS(self,name,typobj=None,maxcount=60):
123       count=0
124       delta=0.5
125       print "Searching %s in Naming Service " % name,
126       while(1):
127           count += 1
128           if count > maxcount : raise "Impossible de trouver %s" % name
129           obj=self.Resolve(name)
130           if obj : 
131               print " found in %s seconds " % ((count-1)*delta)
132               break
133           else:
134               sys.stdout.write('+')
135               sys.stdout.flush()
136               time.sleep(delta)
137  
138       if typobj is None:return obj
139
140       nobj = obj._narrow(typobj)
141       if nobj is None:
142             print "%s exists but is not a %s" % (name,typobj)
143       return nobj
144
145    # --------------------------------------------------------------------------
146
147    def ResolveLogger(self, name):
148       context_name=[]
149       context_name.append(CosNaming.NameComponent(name,""))
150
151       try:
152           obj = self.rootContext.resolve(context_name)
153       except CosNaming.NamingContext.NotFound, ex:
154           obj = None
155       except CosNaming.NamingContext.InvalidName, ex:
156           obj = None
157       except CosNaming.NamingContext.CannotProceed, ex:
158           obj = None
159       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
160           obj = None
161       return obj
162    
163    # --------------------------------------------------------------------------
164
165    def waitLogger(self,name,typobj=None,maxcount=40):
166       count=0
167       delta=0.5
168       print "Searching %s in Naming Service " % name,
169       while(1):
170           count += 1
171           if count > maxcount : raise "Impossible de trouver %s" % name
172           obj=self.ResolveLogger(name)
173           if obj : 
174               print " found in %s seconds " % ((count-1)*delta)
175               break
176           else:
177               sys.stdout.write('+')
178               sys.stdout.flush()
179               time.sleep(delta)
180  
181       if typobj is None:return obj
182
183       nobj = obj._narrow(typobj)
184       if nobj is None:
185             print "%s exists but is not a %s" % (name,typobj)
186       return nobj
187