Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import traceback |
| 3 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 4 | from thrift.Thrift import TProcessor |
| 5 | from thrift.transport import TTransport |
| 6 | |
| 7 | class TServer: |
| 8 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 9 | """Base interface for a server, which must have a serve method.""" |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 10 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 11 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 12 | self.processor = processor |
| 13 | self.serverTransport = serverTransport |
| 14 | if transportFactory == None: |
| 15 | self.transportFactory = TTransport.TTransportFactoryBase() |
| 16 | else: |
| 17 | self.transportFactory = transportFactory |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 18 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 19 | def serve(self): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 20 | pass |
| 21 | |
| 22 | class TSimpleServer(TServer): |
| 23 | |
| 24 | """Simple single-threaded server that just pumps around one transport.""" |
| 25 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 26 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 27 | TServer.__init__(self, processor, serverTransport, transportFactory) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 28 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 29 | def serve(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 30 | self.serverTransport.listen() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 31 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 32 | client = self.serverTransport.accept() |
| 33 | (input, output) = self.transportFactory.getIOTransports(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 34 | try: |
| 35 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 36 | self.processor.process(input, output) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame^] | 37 | except TTransport.TTransportException, tx: |
| 38 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 39 | except Exception, x: |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 40 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 41 | |
| 42 | input.close() |
| 43 | output.close() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame^] | 44 | |
| 45 | class TThreadedServer(TServer): |
| 46 | |
| 47 | """Threaded server that spawns a new thread per each connection.""" |
| 48 | |
| 49 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 50 | TServer.__init__(self, processor, serverTransport, transportFactory) |
| 51 | |
| 52 | def serve(self): |
| 53 | self.serverTransport.listen() |
| 54 | while True: |
| 55 | try: |
| 56 | client = self.serverTransport.accept() |
| 57 | t = threading.Thread(target = self.handle, args=(client,)) |
| 58 | t.start() |
| 59 | except Exception, x: |
| 60 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
| 61 | |
| 62 | def handle(self, client): |
| 63 | (input, output) = self.transportFactory.getIOTransports(client) |
| 64 | try: |
| 65 | while True: |
| 66 | self.processor.process(input, output) |
| 67 | except TTransport.TTransportException, tx: |
| 68 | pass |
| 69 | except Exception, x: |
| 70 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |