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