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