Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.server; |
| 2 | |
| 3 | import com.facebook.thrift.TException; |
| 4 | import com.facebook.thrift.TProcessor; |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 5 | import com.facebook.thrift.protocol.TProtocol; |
| 6 | import com.facebook.thrift.protocol.TProtocolFactory; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 7 | import com.facebook.thrift.transport.TServerTransport; |
| 8 | import com.facebook.thrift.transport.TTransport; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 9 | import com.facebook.thrift.transport.TTransportFactory; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 10 | import com.facebook.thrift.transport.TTransportException; |
| 11 | |
| 12 | /** |
| 13 | * Simple singlethreaded server for testing. |
| 14 | * |
| 15 | * @author Mark Slee <mcslee@facebook.com> |
| 16 | */ |
| 17 | public class TSimpleServer extends TServer { |
| 18 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 19 | public TSimpleServer(TProcessor processor, |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 20 | TServerTransport serverTransport) { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 21 | super(processor, serverTransport); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 24 | public TSimpleServer(TProcessor processor, |
| 25 | TServerTransport serverTransport, |
| 26 | TTransportFactory transportFactory, |
| 27 | TProtocolFactory protocolFactory) { |
| 28 | super(processor, serverTransport, transportFactory, protocolFactory); |
| 29 | } |
| 30 | |
| 31 | public TSimpleServer(TProcessor processor, |
| 32 | TServerTransport serverTransport, |
| 33 | TTransportFactory inputTransportFactory, |
| 34 | TTransportFactory outputTransportFactory, |
| 35 | TProtocolFactory inputProtocolFactory, |
| 36 | TProtocolFactory outputProtocolFactory) { |
| 37 | super(processor, serverTransport, |
| 38 | inputTransportFactory, outputTransportFactory, |
| 39 | inputProtocolFactory, outputProtocolFactory); |
| 40 | } |
| 41 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 42 | public void serve() { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 43 | try { |
| 44 | serverTransport_.listen(); |
| 45 | } catch (TTransportException ttx) { |
| 46 | ttx.printStackTrace(); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | while (true) { |
| 51 | TTransport client = null; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 52 | TTransport inputTransport = null; |
| 53 | TTransport outputTransport = null; |
| 54 | TProtocol inputProtocol = null; |
| 55 | TProtocol outputProtocol = null; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 56 | try { |
| 57 | client = serverTransport_.accept(); |
| 58 | if (client != null) { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 59 | inputTransport = inputTransportFactory_.getTransport(client); |
| 60 | outputTransport = outputTransportFactory_.getTransport(client); |
| 61 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 62 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
| 63 | while (processor_.process(inputProtocol, outputProtocol)) {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 64 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 65 | } catch (TTransportException ttx) { |
| 66 | // Client died, just move on |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 67 | } catch (TException tx) { |
| 68 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 69 | } catch (Exception x) { |
| 70 | x.printStackTrace(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 73 | if (inputTransport != null) { |
| 74 | inputTransport.close(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 75 | } |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 76 | |
| 77 | if (outputTransport != null) { |
| 78 | outputTransport.close(); |
| 79 | } |
| 80 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |