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; |
| 9 | import com.facebook.thrift.transport.TTransportException; |
| 10 | |
| 11 | /** |
| 12 | * Simple singlethreaded server for testing. |
| 13 | * |
| 14 | * @author Mark Slee <mcslee@facebook.com> |
| 15 | */ |
| 16 | public class TSimpleServer extends TServer { |
| 17 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 18 | public TSimpleServer(TProcessor processor, |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 19 | TServerTransport serverTransport) { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 20 | super(processor, serverTransport); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 23 | public void serve() { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 24 | try { |
| 25 | serverTransport_.listen(); |
| 26 | } catch (TTransportException ttx) { |
| 27 | ttx.printStackTrace(); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | while (true) { |
| 32 | TTransport client = null; |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame^] | 33 | TTransport[] iot = null; |
| 34 | TProtocol[] iop = null; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 35 | try { |
| 36 | client = serverTransport_.accept(); |
| 37 | if (client != null) { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame^] | 38 | iot = transportFactory_.getIOTransports(client); |
| 39 | iop = protocolFactory_.getIOProtocols(iot[0], iot[1]); |
| 40 | while (processor_.process(iop[0], iop[1])); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 41 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 42 | } catch (TTransportException ttx) { |
| 43 | // Client died, just move on |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 44 | } catch (TException tx) { |
| 45 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 46 | } catch (Exception x) { |
| 47 | x.printStackTrace(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame^] | 50 | if (iot != null) { |
| 51 | if (iot[0] != null) { |
| 52 | iot[0].close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 53 | } |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame^] | 54 | if (iot[1] != null) { |
| 55 | iot[1].close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 56 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |