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