Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/yacs.git] / bin / orbmodule.py
1 # Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
2 #           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either 
6 # version 2.1 of the License.
7
8 # This library is distributed in the hope that it will be useful 
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 # Lesser General Public License for more details.
12
13 # You should have received a copy of the GNU Lesser General Public  
14 # License along with this library; if not, write to the Free Software 
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 import sys,os,time
20 import string
21 from omniORB import CORBA
22
23 # Import the stubs for the Naming service
24 import CosNaming
25
26 # -----------------------------------------------------------------------------
27
28 class Server:
29    XTERM="/usr/bin/X11/xterm -iconic -e "
30    CMD=""
31
32    def run(self):
33        commande=self.XTERM+self.CMD
34        print commande
35        ier=os.system(commande)
36        if ier:print "Commande failed"
37
38 # -----------------------------------------------------------------------------
39
40 class NamingServer(Server):
41    XTERM=""
42    USER=os.getenv('USER')
43    if USER is None:
44       USER='anonymous'
45    os.system("mkdir -m 777 -p /tmp/logs")
46    LOGDIR="/tmp/logs/" + USER
47    os.system("mkdir -m 777 -p " + LOGDIR)
48    CMD="runNS.sh > " + LOGDIR + "/salomeNS.log 2>&1"
49
50 # -----------------------------------------------------------------------------
51
52 class client:
53
54    def __init__(self):
55       # Initialise the ORB
56       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
57       # Initialise the Naming Service
58       self.initNS()
59
60    # --------------------------------------------------------------------------
61
62    def initNS(self):
63       # Obtain a reference to the root naming context
64       obj         = self.orb.resolve_initial_references("NameService")
65       try:
66           self.rootContext = obj._narrow(CosNaming.NamingContext)
67           return
68       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
69           self.rootContext = None
70           print "Lancement du Naming Service",
71           
72       # On lance le Naming Server (doit etre dans le PATH)
73       NamingServer().run()
74       print "Searching Naming Service ",
75       ncount=0
76       delta=0.1
77       while(ncount < 10):
78           ncount += 1
79           try:
80               obj = self.orb.resolve_initial_references("NameService")
81               self.rootContext = obj._narrow(CosNaming.NamingContext)
82               break
83           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
84               self.rootContext = None
85               sys.stdout.write('+')
86               sys.stdout.flush()
87               time.sleep(delta)
88
89       if self.rootContext is None:
90           print "Failed to narrow the root naming context"
91           sys.exit(1)
92       print " found in %s seconds " % ((ncount-1)*delta)
93
94    # --------------------------------------------------------------------------
95
96    def showNScontext(self,context,dec=''):
97       bl,bi=context.list(0)
98       if bi is not None:
99          ok,b=bi.next_one()
100          while(ok):
101             for s in b.binding_name :
102                print "%s%s.%s" %(dec,s.id,s.kind)
103                if s.kind == "dir":
104                   obj=context.resolve([s])
105                   scontext = obj._narrow(CosNaming.NamingContext)
106                   self.showNScontext(scontext,dec=dec+'  ')
107             ok,b=bi.next_one()
108
109    # --------------------------------------------------------------------------
110
111    def showNS(self):
112       """ Show the content of NS"""
113       self.showNScontext(self.rootContext)
114
115    # --------------------------------------------------------------------------
116
117    def Resolve(self, Path):
118       resolve_path=string.split(Path,'/')
119       if resolve_path[0] == '': del resolve_path[0]
120       dir_path=resolve_path[:-1]
121       context_name=[]
122       for e in dir_path:
123          context_name.append(CosNaming.NameComponent(e,"dir"))
124       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
125
126       try:
127           obj = self.rootContext.resolve(context_name)
128       except CosNaming.NamingContext.NotFound, ex:
129           obj = None
130       except CosNaming.NamingContext.InvalidName, ex:
131           obj = None
132       except CosNaming.NamingContext.CannotProceed, ex:
133           obj = None
134       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
135           obj = None
136       return obj
137
138    # --------------------------------------------------------------------------
139
140    def waitNS(self,name,typobj=None,maxcount=60):
141       count=0
142       delta=0.5
143       print "Searching %s in Naming Service " % name,
144       while(1):
145           count += 1
146           if count > maxcount : raise "Impossible de trouver %s" % name
147           obj=self.Resolve(name)
148           if obj : 
149               print " found in %s seconds " % ((count-1)*delta)
150               break
151           else:
152               sys.stdout.write('+')
153               sys.stdout.flush()
154               time.sleep(delta)
155  
156       if typobj is None:return obj
157
158       nobj = obj._narrow(typobj)
159       if nobj is None:
160             print "%s exists but is not a %s" % (name,typobj)
161       return nobj
162    def waitNSPID(self, theName, thePID, theTypObj = None):
163       aCount = 0
164       aDelta = 0.5
165       anObj = None
166       print "Searching %s in Naming Service " % theName,
167       while(1):
168          try:
169             aPid, aStatus = os.waitpid(thePID,os.WNOHANG)
170          except Exception, exc:
171             raise "Impossible de trouver %s" % theName
172          aCount += 1
173          anObj = self.Resolve(theName)
174          if anObj: 
175             print " found in %s seconds " % ((aCount-1)*aDelta)
176             break
177          else:
178             sys.stdout.write('+')
179             sys.stdout.flush()
180             time.sleep(aDelta)
181             pass
182          pass
183       
184       if theTypObj is None:
185          return anObj
186
187       anObject = anObj._narrow(theTypObj)
188       if anObject is None:
189          print "%s exists but is not a %s" % (theName,theTypObj)
190       return anObject
191
192
193    # --------------------------------------------------------------------------
194
195    def ResolveLogger(self, name):
196       context_name=[]
197       context_name.append(CosNaming.NameComponent(name,""))
198
199       try:
200           obj = self.rootContext.resolve(context_name)
201       except CosNaming.NamingContext.NotFound, ex:
202           obj = None
203       except CosNaming.NamingContext.InvalidName, ex:
204           obj = None
205       except CosNaming.NamingContext.CannotProceed, ex:
206           obj = None
207       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
208           obj = None
209       return obj
210    
211    # --------------------------------------------------------------------------
212
213    def waitLogger(self,name,typobj=None,maxcount=40):
214       count=0
215       delta=0.5
216       print "Searching %s in Naming Service " % name,
217       while(1):
218           count += 1
219           if count > maxcount : raise "Impossible de trouver %s" % name
220           obj=self.ResolveLogger(name)
221           if obj : 
222               print " found in %s seconds " % ((count-1)*delta)
223               break
224           else:
225               sys.stdout.write('+')
226               sys.stdout.flush()
227               time.sleep(delta)
228  
229       if typobj is None:return obj
230
231       nobj = obj._narrow(typobj)
232       if nobj is None:
233             print "%s exists but is not a %s" % (name,typobj)
234       return nobj
235