Salome HOME
Integration of 0019971: A patch for cmake compilation.
[modules/kernel.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 from launchConfigureParser import verbose
24
25 # Import the stubs for the Naming service
26 import CosNaming
27 #from runNS import *
28
29 # -----------------------------------------------------------------------------
30
31 class client:
32
33    def __init__(self,args=None):
34       #set GIOP message size for bug 10560: impossible to get field values in TUI mode
35       sys.argv.extend(["-ORBgiopMaxMsgSize", "104857600"]) ## = 100 * 1024 * 1024
36       # Initialise the ORB
37       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
38       # Initialise the Naming Service
39       self.initNS(args or {})
40
41    # --------------------------------------------------------------------------
42
43    def initNS(self,args):
44       # Obtain a reference to the root naming context
45       obj         = self.orb.resolve_initial_references("NameService")
46       try:
47           self.rootContext = obj._narrow(CosNaming.NamingContext)
48           return
49       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
50           self.rootContext = None
51           if verbose(): print "Launch Naming Service++",
52           
53       # On lance le Naming Server (doit etre dans le PATH)
54       NamingServer(args).run()
55       print "Searching Naming Service ",
56       ncount=0
57       delta=0.1
58       while(ncount < 10):
59           ncount += 1
60           try:
61               obj = self.orb.resolve_initial_references("NameService")
62               self.rootContext = obj._narrow(CosNaming.NamingContext)
63               break
64           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
65               self.rootContext = None
66               sys.stdout.write('+')
67               sys.stdout.flush()
68               time.sleep(delta)
69
70       if self.rootContext is None:
71           print "Failed to narrow the root naming context"
72           sys.exit(1)
73       print " found in %s seconds " % ((ncount-1)*delta)
74
75    # --------------------------------------------------------------------------
76
77    def showNScontext(self,context,dec=''):
78       bl,bi=context.list(0)
79       if bi is not None:
80          ok,b=bi.next_one()
81          while(ok):
82             for s in b.binding_name :
83                print "%s%s.%s" %(dec,s.id,s.kind)
84                if s.kind == "dir":
85                   obj=context.resolve([s])
86                   scontext = obj._narrow(CosNaming.NamingContext)
87                   self.showNScontext(scontext,dec=dec+'  ')
88             ok,b=bi.next_one()
89
90    # --------------------------------------------------------------------------
91
92    def showNS(self):
93       """ Show the content of NS"""
94       self.showNScontext(self.rootContext)
95
96    # --------------------------------------------------------------------------
97
98    def Resolve(self, Path):
99       resolve_path=string.split(Path,'/')
100       if resolve_path[0] == '': del resolve_path[0]
101       dir_path=resolve_path[:-1]
102       context_name=[]
103       for e in dir_path:
104          context_name.append(CosNaming.NameComponent(e,"dir"))
105       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
106
107       try:
108           obj = self.rootContext.resolve(context_name)
109       except CosNaming.NamingContext.NotFound, ex:
110           obj = None
111       except CosNaming.NamingContext.InvalidName, ex:
112           obj = None
113       except CosNaming.NamingContext.CannotProceed, ex:
114           obj = None
115       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
116           obj = None
117       return obj
118
119    # --------------------------------------------------------------------------
120
121    def waitNS(self,name,typobj=None,maxcount=60):
122       count=0
123       delta=0.5
124       print "Searching %s in Naming Service " % name,
125       while(1):
126           count += 1
127           if count > maxcount : raise "Impossible de trouver %s" % name
128           obj=self.Resolve(name)
129           if obj : 
130               print " found in %s seconds " % ((count-1)*delta)
131               break
132           else:
133               sys.stdout.write('+')
134               sys.stdout.flush()
135               time.sleep(delta)
136  
137       if typobj is None:return obj
138
139       nobj = obj._narrow(typobj)
140       if nobj is None:
141             print "%s exists but is not a %s" % (name,typobj)
142       return nobj
143
144    if sys.platform != "win32":
145     def waitNSPID(self, theName, thePID, theTypObj = None):
146       aCount = 0
147       aDelta = 0.5
148       anObj = None
149       print "Searching %s in Naming Service " % theName,
150       while(1):
151          try:
152            os.kill(thePID,0)
153          except:
154            raise "Process %d for %s not found" % (thePID,theName)
155          aCount += 1
156          anObj = self.Resolve(theName)
157          if anObj: 
158             print " found in %s seconds " % ((aCount-1)*aDelta)
159             break
160          else:
161             sys.stdout.write('+')
162             sys.stdout.flush()
163             time.sleep(aDelta)
164             pass
165          pass
166       
167       if theTypObj is None:
168          return anObj
169
170       anObject = anObj._narrow(theTypObj)
171       if anObject is None:
172          print "%s exists but is not a %s" % (theName,theTypObj)
173       return anObject
174
175
176    # --------------------------------------------------------------------------
177
178    def ResolveLogger(self, name):
179       context_name=[]
180       context_name.append(CosNaming.NameComponent(name,""))
181
182       try:
183           obj = self.rootContext.resolve(context_name)
184       except CosNaming.NamingContext.NotFound, ex:
185           obj = None
186       except CosNaming.NamingContext.InvalidName, ex:
187           obj = None
188       except CosNaming.NamingContext.CannotProceed, ex:
189           obj = None
190       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
191           obj = None
192       return obj
193    
194    # --------------------------------------------------------------------------
195
196    def waitLogger(self,name,typobj=None,maxcount=40):
197       count=0
198       delta=0.5
199       print "Searching %s in Naming Service " % name,
200       while(1):
201           count += 1
202           if count > maxcount : raise "Impossible de trouver %s" % name
203           obj=self.ResolveLogger(name)
204           if obj : 
205               print " found in %s seconds " % ((count-1)*delta)
206               break
207           else:
208               sys.stdout.write('+')
209               sys.stdout.flush()
210               time.sleep(delta)
211  
212       if typobj is None:return obj
213
214       nobj = obj._narrow(typobj)
215       if nobj is None:
216             print "%s exists but is not a %s" % (name,typobj)
217       return nobj
218