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