Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import traceback |
Mark Slee | 3c4d7fd | 2006-10-02 17:53:20 +0000 | [diff] [blame] | 3 | import threading |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 4 | import Queue |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 5 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 6 | from thrift.Thrift import TProcessor |
| 7 | from thrift.transport import TTransport |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 8 | from thrift.protocol import TBinaryProtocol |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 9 | |
| 10 | class TServer: |
| 11 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 12 | """Base interface for a server, which must have a serve method.""" |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 13 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 14 | """ 3 constructors for all servers: |
| 15 | 1) (processor, serverTransport) |
| 16 | 2) (processor, serverTransport, transportFactory, protocolFactory) |
| 17 | 3) (processor, serverTransport, |
| 18 | inputTransportFactory, outputTransportFactory, |
| 19 | inputProtocolFactory, outputProtocolFactory)""" |
| 20 | def __init__(self, *args): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 21 | if (len(args) == 2): |
| 22 | self.__initArgs__(args[0], args[1], |
| 23 | TTransport.TTransportFactoryBase(), |
| 24 | TTransport.TTransportFactoryBase(), |
| 25 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 26 | TBinaryProtocol.TBinaryProtocolFactory()) |
| 27 | elif (len(args) == 4): |
| 28 | self.__initArgs__(args[0], args[1], args[2], args[2], args[3], args[3]) |
| 29 | elif (len(args) == 6): |
| 30 | self.__initArgs__(args[0], args[1], args[2], args[3], args[4], args[5]) |
| 31 | |
| 32 | def __initArgs__(self, processor, serverTransport, |
| 33 | inputTransportFactory, outputTransportFactory, |
| 34 | inputProtocolFactory, outputProtocolFactory): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 35 | self.processor = processor |
| 36 | self.serverTransport = serverTransport |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 37 | self.inputTransportFactory = inputTransportFactory |
| 38 | self.outputTransportFactory = outputTransportFactory |
| 39 | self.inputProtocolFactory = inputProtocolFactory |
| 40 | self.outputProtocolFactory = outputProtocolFactory |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 41 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 42 | def serve(self): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 43 | pass |
| 44 | |
| 45 | class TSimpleServer(TServer): |
| 46 | |
| 47 | """Simple single-threaded server that just pumps around one transport.""" |
| 48 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 49 | def __init__(self, *args): |
| 50 | TServer.__init__(self, *args) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 51 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 52 | def serve(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 53 | self.serverTransport.listen() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 54 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 55 | client = self.serverTransport.accept() |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 56 | itrans = self.inputTransportFactory.getTransport(client) |
| 57 | otrans = self.outputTransportFactory.getTransport(client) |
| 58 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 59 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 60 | try: |
| 61 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 62 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 63 | except TTransport.TTransportException, tx: |
| 64 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 65 | except Exception, x: |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 66 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 67 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 68 | itrans.close() |
| 69 | otrans.close() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 70 | |
| 71 | class TThreadedServer(TServer): |
| 72 | |
| 73 | """Threaded server that spawns a new thread per each connection.""" |
| 74 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 75 | def __init__(self, *args): |
| 76 | TServer.__init__(self, *args) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 77 | |
| 78 | def serve(self): |
| 79 | self.serverTransport.listen() |
| 80 | while True: |
| 81 | try: |
| 82 | client = self.serverTransport.accept() |
| 83 | t = threading.Thread(target = self.handle, args=(client,)) |
| 84 | t.start() |
| 85 | except Exception, x: |
| 86 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
| 87 | |
| 88 | def handle(self, client): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 89 | itrans = self.inputTransportFactory.getTransport(client) |
| 90 | otrans = self.outputTransportFactory.getTransport(client) |
| 91 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 92 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 93 | try: |
| 94 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 95 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 96 | except TTransport.TTransportException, tx: |
| 97 | pass |
| 98 | except Exception, x: |
| 99 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 100 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 101 | itrans.close() |
| 102 | otrans.close() |
| 103 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 104 | class TThreadPoolServer(TServer): |
| 105 | |
| 106 | """Server with a fixed size pool of threads which service requests.""" |
| 107 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 108 | def __init__(self, *args): |
| 109 | TServer.__init__(self, *args) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 110 | self.clients = Queue.Queue() |
| 111 | self.threads = 10 |
| 112 | |
Mark Slee | 4ce787f | 2006-10-24 18:54:06 +0000 | [diff] [blame] | 113 | def setNumThreads(self, num): |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 114 | """Set the number of worker threads that should be created""" |
| 115 | self.threads = num |
| 116 | |
| 117 | def serveThread(self): |
| 118 | """Loop around getting clients from the shared queue and process them.""" |
| 119 | while True: |
| 120 | try: |
Mark Slee | 9a695ba | 2006-10-24 18:55:36 +0000 | [diff] [blame] | 121 | client = self.clients.get() |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 122 | self.serveClient(client) |
| 123 | except Exception, x: |
| 124 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
| 125 | |
| 126 | def serveClient(self, client): |
| 127 | """Process input/output from a client for as long as possible""" |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 128 | itrans = self.inputTransportFactory.getTransport(client) |
| 129 | otrans = self.outputTransportFactory.getTransport(client) |
| 130 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | 04342d8 | 2007-02-20 03:41:35 +0000 | [diff] [blame] | 131 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 132 | try: |
| 133 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 134 | self.processor.process(iprot, oprot) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 135 | except TTransport.TTransportException, tx: |
| 136 | pass |
| 137 | except Exception, x: |
| 138 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
| 139 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 140 | itrans.close() |
| 141 | otrans.close() |
| 142 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 143 | def serve(self): |
| 144 | """Start a fixed number of worker threads and put client into a queue""" |
| 145 | for i in range(self.threads): |
| 146 | try: |
| 147 | t = threading.Thread(target = self.serveThread) |
| 148 | t.start() |
| 149 | except Exception, x: |
| 150 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
| 151 | |
| 152 | # Pump the socket for clients |
| 153 | self.serverTransport.listen() |
| 154 | while True: |
| 155 | try: |
| 156 | client = self.serverTransport.accept() |
| 157 | self.clients.put(client) |
| 158 | except Exception, x: |
| 159 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |