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