blob: 05156c3f1da74c42e96c0515faa18eae459f33cf [file] [log] [blame]
Mark Slee83c52a82006-06-07 06:51:18 +00001package com.facebook.thrift.server;
2
3import com.facebook.thrift.TException;
4import com.facebook.thrift.TProcessor;
5import com.facebook.thrift.transport.TServerTransport;
6import com.facebook.thrift.transport.TTransport;
7import com.facebook.thrift.transport.TTransportException;
8
9/**
10 * Simple singlethreaded server for testing.
11 *
12 * @author Mark Slee <mcslee@facebook.com>
13 */
14public class TSimpleServer extends TServer {
15
Mark Slee83c52a82006-06-07 06:51:18 +000016 public TSimpleServer(TProcessor processor,
Mark Sleeffcddd62006-09-06 20:37:03 +000017 TServerTransport serverTransport) {
Mark Sleed788b2e2006-09-07 01:26:35 +000018 super(processor, serverTransport);
Mark Slee83c52a82006-06-07 06:51:18 +000019 }
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 Sleed788b2e2006-09-07 01:26:35 +000031 TTransport[] io = null;
Mark Slee83c52a82006-06-07 06:51:18 +000032 try {
33 client = serverTransport_.accept();
34 if (client != null) {
Mark Sleed788b2e2006-09-07 01:26:35 +000035 io = transportFactory_.getIOTransports(client);
36 while (processor_.process(io[0], io[1]));
Mark Slee83c52a82006-06-07 06:51:18 +000037 }
Mark Sleeade2c832006-09-08 03:41:50 +000038 } catch (TTransportException ttx) {
39 // Client died, just move on
Mark Slee83c52a82006-06-07 06:51:18 +000040 } catch (TException tx) {
41 tx.printStackTrace();
Mark Sleeade2c832006-09-08 03:41:50 +000042 } catch (Exception x) {
43 x.printStackTrace();
Mark Slee83c52a82006-06-07 06:51:18 +000044 }
45
Mark Sleed788b2e2006-09-07 01:26:35 +000046 if (io != null) {
47 if (io[0] != null) {
48 io[0].close();
49 }
50 if (io[1] != null) {
51 io[1].close();
52 }
Mark Slee83c52a82006-06-07 06:51:18 +000053 }
54 }
55 }
56}