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 | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame^] | 26 | private boolean stopped_ = false; |
| 27 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 28 | public TSimpleServer(TProcessor processor, |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 29 | TServerTransport serverTransport) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 30 | super(new TProcessorFactory(processor), serverTransport); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 33 | public TSimpleServer(TProcessor processor, |
| 34 | TServerTransport serverTransport, |
| 35 | TTransportFactory transportFactory, |
| 36 | TProtocolFactory protocolFactory) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 37 | super(new TProcessorFactory(processor), serverTransport, transportFactory, protocolFactory); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | public TSimpleServer(TProcessor processor, |
| 41 | TServerTransport serverTransport, |
| 42 | TTransportFactory inputTransportFactory, |
| 43 | TTransportFactory outputTransportFactory, |
| 44 | TProtocolFactory inputProtocolFactory, |
| 45 | TProtocolFactory outputProtocolFactory) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 46 | super(new TProcessorFactory(processor), serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 47 | inputTransportFactory, outputTransportFactory, |
| 48 | inputProtocolFactory, outputProtocolFactory); |
| 49 | } |
| 50 | |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 51 | public TSimpleServer(TProcessorFactory processorFactory, |
| 52 | TServerTransport serverTransport) { |
| 53 | super(processorFactory, serverTransport); |
| 54 | } |
| 55 | |
| 56 | public TSimpleServer(TProcessorFactory processorFactory, |
| 57 | TServerTransport serverTransport, |
| 58 | TTransportFactory transportFactory, |
| 59 | TProtocolFactory protocolFactory) { |
| 60 | super(processorFactory, serverTransport, transportFactory, protocolFactory); |
| 61 | } |
| 62 | |
| 63 | public TSimpleServer(TProcessorFactory processorFactory, |
| 64 | TServerTransport serverTransport, |
| 65 | TTransportFactory inputTransportFactory, |
| 66 | TTransportFactory outputTransportFactory, |
| 67 | TProtocolFactory inputProtocolFactory, |
| 68 | TProtocolFactory outputProtocolFactory) { |
| 69 | super(processorFactory, serverTransport, |
| 70 | inputTransportFactory, outputTransportFactory, |
| 71 | inputProtocolFactory, outputProtocolFactory); |
| 72 | } |
| 73 | |
| 74 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 75 | public void serve() { |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame^] | 76 | stopped_ = false; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 77 | try { |
| 78 | serverTransport_.listen(); |
| 79 | } catch (TTransportException ttx) { |
| 80 | ttx.printStackTrace(); |
| 81 | return; |
| 82 | } |
| 83 | |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame^] | 84 | while (!stopped_) { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 85 | TTransport client = null; |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 86 | TProcessor processor = null; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 87 | TTransport inputTransport = null; |
| 88 | TTransport outputTransport = null; |
| 89 | TProtocol inputProtocol = null; |
| 90 | TProtocol outputProtocol = null; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 91 | try { |
| 92 | client = serverTransport_.accept(); |
| 93 | if (client != null) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 94 | processor = processorFactory_.getProcessor(client); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 95 | inputTransport = inputTransportFactory_.getTransport(client); |
| 96 | outputTransport = outputTransportFactory_.getTransport(client); |
| 97 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 98 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 99 | while (processor.process(inputProtocol, outputProtocol)) {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 100 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 101 | } catch (TTransportException ttx) { |
| 102 | // Client died, just move on |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 103 | } catch (TException tx) { |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame^] | 104 | if (!stopped_) { |
| 105 | tx.printStackTrace(); |
| 106 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 107 | } catch (Exception x) { |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame^] | 108 | if (!stopped_) { |
| 109 | x.printStackTrace(); |
| 110 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 113 | if (inputTransport != null) { |
| 114 | inputTransport.close(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 115 | } |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 116 | |
| 117 | if (outputTransport != null) { |
| 118 | outputTransport.close(); |
| 119 | } |
| 120 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame^] | 123 | |
| 124 | public void stop() { |
| 125 | stopped_ = true; |
| 126 | serverTransport_.interrupt(); |
| 127 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 128 | } |