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