Salome HOME
507c68c3950c41fb90aa762e82ddff7d6283a073
[modules/kernel.git] / bin / orbmodule.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2012  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       # Initialise the ORB
45       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
46
47       # Initialise the Naming Service
48       self.initNS(args or {})
49
50    # --------------------------------------------------------------------------
51
52    def initNS(self,args):
53       # Obtain a reference to the root naming context
54       obj         = self.orb.resolve_initial_references("NameService")
55       try:
56           self.rootContext = obj._narrow(CosNaming.NamingContext)
57           return
58       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
59           self.rootContext = None
60           if verbose(): print "Launch Naming Service++",
61
62       # On lance le Naming Server (doit etre dans le PATH)
63       test = True
64       if args['wake_up_session']:
65          test = False
66          pass
67       if test:
68          NamingServer(args).run()
69          pass
70       print "Searching Naming Service ",
71       ncount=0
72       delta=0.1
73       while(ncount < 100):
74           ncount += 1
75           try:
76               obj = self.orb.resolve_initial_references("NameService")
77               self.rootContext = obj._narrow(CosNaming.NamingContext)
78               break
79           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
80               self.rootContext = None
81               sys.stdout.write('+')
82               sys.stdout.flush()
83               time.sleep(delta)
84
85       if self.rootContext is None:
86           print "Failed to narrow the root naming context"
87           sys.exit(1)
88       print " found in %s seconds " % ((ncount-1)*delta)
89
90    # --------------------------------------------------------------------------
91
92    def showNScontext(self,context,dec=''):
93       bl,bi=context.list(0)
94       if bi is not None:
95          ok,b=bi.next_one()
96          while(ok):
97             for s in b.binding_name :
98                print "%s%s.%s" %(dec,s.id,s.kind)
99                if s.kind == "dir":
100                   obj=context.resolve([s])
101                   scontext = obj._narrow(CosNaming.NamingContext)
102                   self.showNScontext(scontext,dec=dec+'  ')
103             ok,b=bi.next_one()
104
105    # --------------------------------------------------------------------------
106
107    def showNS(self):
108       """ Show the content of SALOME naming service """
109       self.showNScontext(self.rootContext)
110
111    # --------------------------------------------------------------------------
112
113    def Resolve(self, Path):
114       resolve_path=string.split(Path,'/')
115       if resolve_path[0] == '': del resolve_path[0]
116       dir_path=resolve_path[:-1]
117       context_name=[]
118       for e in dir_path:
119          context_name.append(CosNaming.NameComponent(e,"dir"))
120       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
121
122       try:
123           obj = self.rootContext.resolve(context_name)
124       except CosNaming.NamingContext.NotFound, ex:
125           obj = None
126       except CosNaming.NamingContext.InvalidName, ex:
127           obj = None
128       except CosNaming.NamingContext.CannotProceed, ex:
129           obj = None
130       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
131           obj = None
132       return obj
133
134    # --------------------------------------------------------------------------
135
136    def waitNS(self,name,typobj=None,maxcount=240):
137       count=0
138       delta=0.5
139       print "Searching %s in Naming Service " % name,
140       while(1):
141           count += 1
142           if count > maxcount : raise RuntimeError, "Impossible de trouver %s" % name
143           obj=self.Resolve(name)
144           if obj :
145               print " found in %s seconds " % ((count-1)*delta)
146               break
147           else:
148               sys.stdout.write('+')
149               sys.stdout.flush()
150               time.sleep(delta)
151
152       if typobj is None:return obj
153
154       nobj = obj._narrow(typobj)
155       if nobj is None:
156             print "%s exists but is not a %s" % (name,typobj)
157       return nobj
158
159    if sys.platform != "win32":
160     def waitNSPID(self, theName, thePID, theTypObj = None):
161       aCount = 0
162       aDelta = 0.5
163       anObj = None
164       print "Searching %s in Naming Service " % theName,
165       while(1):
166          try:
167            os.kill(thePID,0)
168          except:
169            raise RuntimeError, "Process %d for %s not found" % (thePID,theName)
170          aCount += 1
171          anObj = self.Resolve(theName)
172          if anObj:
173             print " found in %s seconds " % ((aCount-1)*aDelta)
174             break
175          else:
176             sys.stdout.write('+')
177             sys.stdout.flush()
178             time.sleep(aDelta)
179             pass
180          pass
181
182       if theTypObj is None:
183          return anObj
184
185       anObject = anObj._narrow(theTypObj)
186       if anObject is None:
187          print "%s exists but is not a %s" % (theName,theTypObj)
188       return anObject
189
190
191    # --------------------------------------------------------------------------
192
193    def ResolveLogger(self, name):
194       context_name=[]
195       context_name.append(CosNaming.NameComponent(name,""))
196
197       try:
198           obj = self.rootContext.resolve(context_name)
199       except CosNaming.NamingContext.NotFound, ex:
200           obj = None
201       except CosNaming.NamingContext.InvalidName, ex:
202           obj = None
203       except CosNaming.NamingContext.CannotProceed, ex:
204           obj = None
205       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
206           obj = None
207       return obj
208
209    # --------------------------------------------------------------------------
210
211    def waitLogger(self,name,typobj=None,maxcount=40):
212       count=0
213       delta=0.5
214       print "Searching %s in Naming Service " % name,
215       while(1):
216           count += 1
217           if count > maxcount : raise RuntimeError, "Impossible de trouver %s" % name
218           obj=self.ResolveLogger(name)
219           if obj :
220               print " found in %s seconds " % ((count-1)*delta)
221               break
222           else:
223               sys.stdout.write('+')
224               sys.stdout.flush()
225               time.sleep(delta)
226
227       if typobj is None:return obj
228
229       nobj = obj._narrow(typobj)
230       if nobj is None:
231             print "%s exists but is not a %s" % (name,typobj)
232       return nobj