Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.server; |
| 8 | |
| 9 | import com.facebook.thrift.TException; |
| 10 | import com.facebook.thrift.TProcessor; |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 11 | import com.facebook.thrift.TProcessorFactory; |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 12 | import com.facebook.thrift.protocol.TProtocol; |
| 13 | import com.facebook.thrift.protocol.TProtocolFactory; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 14 | import com.facebook.thrift.transport.TServerTransport; |
| 15 | import com.facebook.thrift.transport.TTransport; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 16 | import com.facebook.thrift.transport.TTransportFactory; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 17 | import com.facebook.thrift.transport.TTransportException; |
| 18 | |
| 19 | /** |
| 20 | * Simple singlethreaded server for testing. |
| 21 | * |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
| 24 | public class TSimpleServer extends TServer { |
| 25 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 26 | public TSimpleServer(TProcessor processor, |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 27 | TServerTransport serverTransport) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 28 | super(new TProcessorFactory(processor), serverTransport); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 31 | public TSimpleServer(TProcessor processor, |
| 32 | TServerTransport serverTransport, |
| 33 | TTransportFactory transportFactory, |
| 34 | TProtocolFactory protocolFactory) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 35 | super(new TProcessorFactory(processor), serverTransport, transportFactory, protocolFactory); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public TSimpleServer(TProcessor processor, |
| 39 | TServerTransport serverTransport, |
| 40 | TTransportFactory inputTransportFactory, |
| 41 | TTransportFactory outputTransportFactory, |
| 42 | TProtocolFactory inputProtocolFactory, |
| 43 | TProtocolFactory outputProtocolFactory) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 44 | super(new TProcessorFactory(processor), serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 45 | inputTransportFactory, outputTransportFactory, |
| 46 | inputProtocolFactory, outputProtocolFactory); |
| 47 | } |
| 48 | |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 49 | public TSimpleServer(TProcessorFactory processorFactory, |
| 50 | TServerTransport serverTransport) { |
| 51 | super(processorFactory, serverTransport); |
| 52 | } |
| 53 | |
| 54 | public TSimpleServer(TProcessorFactory processorFactory, |
| 55 | TServerTransport serverTransport, |
| 56 | TTransportFactory transportFactory, |
| 57 | TProtocolFactory protocolFactory) { |
| 58 | super(processorFactory, serverTransport, transportFactory, protocolFactory); |
| 59 | } |
| 60 | |
| 61 | public TSimpleServer(TProcessorFactory processorFactory, |
| 62 | TServerTransport serverTransport, |
| 63 | TTransportFactory inputTransportFactory, |
| 64 | TTransportFactory outputTransportFactory, |
| 65 | TProtocolFactory inputProtocolFactory, |
| 66 | TProtocolFactory outputProtocolFactory) { |
| 67 | super(processorFactory, serverTransport, |
| 68 | inputTransportFactory, outputTransportFactory, |
| 69 | inputProtocolFactory, outputProtocolFactory); |
| 70 | } |
| 71 | |
| 72 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 73 | public void serve() { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 74 | try { |
| 75 | serverTransport_.listen(); |
| 76 | } catch (TTransportException ttx) { |
| 77 | ttx.printStackTrace(); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | while (true) { |
| 82 | TTransport client = null; |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 83 | TProcessor processor = null; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 84 | TTransport inputTransport = null; |
| 85 | TTransport outputTransport = null; |
| 86 | TProtocol inputProtocol = null; |
| 87 | TProtocol outputProtocol = null; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 88 | try { |
| 89 | client = serverTransport_.accept(); |
| 90 | if (client != null) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 91 | processor = processorFactory_.getProcessor(client); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 92 | inputTransport = inputTransportFactory_.getTransport(client); |
| 93 | outputTransport = outputTransportFactory_.getTransport(client); |
| 94 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 95 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame^] | 96 | while (processor.process(inputProtocol, outputProtocol)) {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 97 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 98 | } catch (TTransportException ttx) { |
| 99 | // Client died, just move on |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 100 | } catch (TException tx) { |
| 101 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 102 | } catch (Exception x) { |
| 103 | x.printStackTrace(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 106 | if (inputTransport != null) { |
| 107 | inputTransport.close(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 108 | } |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 109 | |
| 110 | if (outputTransport != null) { |
| 111 | outputTransport.close(); |
| 112 | } |
| 113 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |