1 """Utility class to start/stop Pyro Name Server.
2
3 __author__ = "http://www.gemalto.com"
4
5 Copyright 2001-2011 gemalto
6 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
7
8 This file is part of pyscard.
9
10 pyscard is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 pyscard is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with pyscard; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 """
24
25 import signal
26 import sys
27 from threading import Thread
28 import time
29
30 import Pyro.naming
31 import Pyro.nsc
32 import Pyro.util
33
34
36 """Thread running the Pyro Name server.
37 """
38
40 """Initialize pyro name server with command line arguments.
41 For a complete list of command line arguments, see pyro documentation
42 for pyro-ns start script."""
43 Thread.__init__(self)
44 self.setDaemon(True)
45 self.setName('smartcard.pyro.server.PyroNameServer')
46 self.args = args
47 self.handler = signal.signal(signal.SIGINT, self)
48
49 _args = []
50 for arg in self.args:
51 _args.append(arg)
52 Args = Pyro.util.ArgParser()
53 Args.parse(_args, 'hkmrvxn:p:b:c:d:s:i:1:2:')
54 self.host = Args.getOpt('n', None)
55 self.bcport = Args.getOpt('b', None)
56 self.bcaddr = Args.getOpt('c', None)
57 self.identification = Args.getOpt('i', None)
58
60 """return command line arguments for shutting down the
61 server; this command line is built from the name server
62 startup arguments."""
63 shutdownArgs = []
64 if self.host:
65 shutdownArgs += ['-h', self.host]
66 if self.bcport:
67 shutdownArgs += ['-p', self.bcport]
68 if self.bcaddr:
69 shutdownArgs += ['-c', self.bcaddr]
70 if self.identification:
71 shutdownArgs += ['-i', self.identification]
72
73 return shutdownArgs
74
76 """List pyro namespace."""
77 args = self.getShutdownArgs() + ['listall']
78 Pyro.nsc.main(args)
79
81 """Ping pyro naming server."""
82 args = self.getShutdownArgs() + ['ping']
83 Pyro.nsc.main(args)
84
86 """Starts Pyro naming server with command line arguments (see pyro documentation)
87 """
88 args = []
89 for arg in self.args:
90 args.append(arg)
91 Pyro.naming.main(args)
92
94 """Shutdown pyro naming server."""
95 args = self.getShutdownArgs() + ['shutdown']
96 Pyro.nsc.main(args)
97 self.join()
98
100 """wait until name server is started."""
101 ns = None
102 while not ns:
103 try:
104 time.sleep(3)
105 ns = Pyro.naming.NameServerLocator(identification=self.identification).getNS()
106 except Pyro.errors.NamingError as er:
107 pass
108
110 """Ctrl+c handler that will gracefully shutdown the server
111 upon Ctrl+C signal"""
112 print 'PyroNameServer Ctrl+C handler'
113 self.stop()
114 time.sleep(1)
115 self.handler(signame, sf)
116
117
118 if __name__ == '__main__':
119 import sys
120 pt = PyroNameServer(sys.argv[1:])
121 pt.start()
122 pt.waitStarted()
123 pt.stop()
124