Salome HOME
PR: merge from branch BR_OCC_For_3_1_0a3 tag mergeto_trunk_16nov05
[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    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    def waitNSPID(self, theName, thePID, theTypObj = None):
145       aCount = 0
146       aDelta = 0.5
147       anObj = None
148       print "Searching %s in Naming Service " % theName,
149       while(1):
150          try:
151             aPid, aStatus = os.waitpid(thePID,os.WNOHANG)
152          except Exception, exc:
153             raise "Impossible de trouver %s" % theName
154          aCount += 1
155          anObj = self.Resolve(theName)
156          if anObj: 
157             print " found in %s seconds " % ((aCount-1)*aDelta)
158             break
159          else:
160             sys.stdout.write('+')
161             sys.stdout.flush()
162             time.sleep(aDelta)
163             pass
164          pass
165       
166       if theTypObj is None:
167          return anObj
168
169       anObject = anObj._narrow(theTypObj)
170       if anObject is None:
171          print "%s exists but is not a %s" % (theName,theTypObj)
172       return anObject
173
174
175    # --------------------------------------------------------------------------
176
177    def ResolveLogger(self, name):
178       context_name=[]
179       context_name.append(CosNaming.NameComponent(name,""))
180
181       try:
182           obj = self.rootContext.resolve(context_name)
183       except CosNaming.NamingContext.NotFound, ex:
184           obj = None
185       except CosNaming.NamingContext.InvalidName, ex:
186           obj = None
187       except CosNaming.NamingContext.CannotProceed, ex:
188           obj = None
189       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
190           obj = None
191       return obj
192    
193    # --------------------------------------------------------------------------
194
195    def waitLogger(self,name,typobj=None,maxcount=40):
196       count=0
197       delta=0.5
198       print "Searching %s in Naming Service " % name,
199       while(1):
200           count += 1
201           if count > maxcount : raise "Impossible de trouver %s" % name
202           obj=self.ResolveLogger(name)
203           if obj : 
204               print " found in %s seconds " % ((count-1)*delta)
205               break
206           else:
207               sys.stdout.write('+')
208               sys.stdout.flush()
209               time.sleep(delta)
210  
211       if typobj is None:return obj
212
213       nobj = obj._narrow(typobj)
214       if nobj is None:
215             print "%s exists but is not a %s" % (name,typobj)
216       return nobj
217