Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 nameserver import *
22 from omniORB import CORBA
23
24 # Import the stubs for the Naming service
25 import CosNaming
26 #from runNS import *
27
28 # -----------------------------------------------------------------------------
29
30 class client:
31
32    def __init__(self,args=None):
33       #set GIOP message size for bug 10560: impossible to get field values in TUI mode
34       sys.argv.extend(["-ORBgiopMaxMsgSize", "104857600"]) ## = 100 * 1024 * 1024
35       # Initialise the ORB
36       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
37       # Initialise the Naming Service
38       self.initNS(args or {})
39
40    # --------------------------------------------------------------------------
41
42    def initNS(self,args):
43       # Obtain a reference to the root naming context
44       obj         = self.orb.resolve_initial_references("NameService")
45       try:
46           self.rootContext = obj._narrow(CosNaming.NamingContext)
47           return
48       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
49           self.rootContext = None
50           print "Launch Naming Service++",
51           
52       # On lance le Naming Server (doit etre dans le PATH)
53       NamingServer(args).run()
54       print "Searching Naming Service ",
55       ncount=0
56       delta=0.1
57       while(ncount < 10):
58           ncount += 1
59           try:
60               obj = self.orb.resolve_initial_references("NameService")
61               self.rootContext = obj._narrow(CosNaming.NamingContext)
62               break
63           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
64               self.rootContext = None
65               sys.stdout.write('+')
66               sys.stdout.flush()
67               time.sleep(delta)
68
69       if self.rootContext is None:
70           print "Failed to narrow the root naming context"
71           sys.exit(1)
72       print " found in %s seconds " % ((ncount-1)*delta)
73
74    # --------------------------------------------------------------------------
75
76    def showNScontext(self,context,dec=''):
77       bl,bi=context.list(0)
78       if bi is not None:
79          ok,b=bi.next_one()
80          while(ok):
81             for s in b.binding_name :
82                print "%s%s.%s" %(dec,s.id,s.kind)
83                if s.kind == "dir":
84                   obj=context.resolve([s])
85                   scontext = obj._narrow(CosNaming.NamingContext)
86                   self.showNScontext(scontext,dec=dec+'  ')
87             ok,b=bi.next_one()
88
89    # --------------------------------------------------------------------------
90
91    def showNS(self):
92       """ Show the content of NS"""
93       self.showNScontext(self.rootContext)
94
95    # --------------------------------------------------------------------------
96
97    def Resolve(self, Path):
98       resolve_path=string.split(Path,'/')
99       if resolve_path[0] == '': del resolve_path[0]
100       dir_path=resolve_path[:-1]
101       context_name=[]
102       for e in dir_path:
103          context_name.append(CosNaming.NameComponent(e,"dir"))
104       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
105
106       try:
107           obj = self.rootContext.resolve(context_name)
108       except CosNaming.NamingContext.NotFound, ex:
109           obj = None
110       except CosNaming.NamingContext.InvalidName, ex:
111           obj = None
112       except CosNaming.NamingContext.CannotProceed, ex:
113           obj = None
114       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
115           obj = None
116       return obj
117
118    # --------------------------------------------------------------------------
119
120    def waitNS(self,name,typobj=None,maxcount=60):
121       count=0
122       delta=0.5
123       print "Searching %s in Naming Service " % name,
124       while(1):
125           count += 1
126           if count > maxcount : raise "Impossible de trouver %s" % name
127           obj=self.Resolve(name)
128           if obj : 
129               print " found in %s seconds " % ((count-1)*delta)
130               break
131           else:
132               sys.stdout.write('+')
133               sys.stdout.flush()
134               time.sleep(delta)
135  
136       if typobj is None:return obj
137
138       nobj = obj._narrow(typobj)
139       if nobj is None:
140             print "%s exists but is not a %s" % (name,typobj)
141       return nobj
142
143    if sys.platform != "win32":
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            os.kill(thePID,0)
152          except:
153            raise "Process %d for %s not found" % (thePID,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