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