blob: 7ecd34733a87102276a226edc28e53c5e0db50f2 [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;
Mark Slee456b7a82006-10-25 20:53:37 +00005import com.facebook.thrift.protocol.TProtocol;
6import com.facebook.thrift.protocol.TProtocolFactory;
Mark Slee83c52a82006-06-07 06:51:18 +00007import com.facebook.thrift.transport.TServerTransport;
8import com.facebook.thrift.transport.TTransport;
9import com.facebook.thrift.transport.TTransportException;
10
11/**
12 * Simple singlethreaded server for testing.
13 *
14 * @author Mark Slee <mcslee@facebook.com>
15 */
16public class TSimpleServer extends TServer {
17
Mark Slee83c52a82006-06-07 06:51:18 +000018 public TSimpleServer(TProcessor processor,
Mark Sleeffcddd62006-09-06 20:37:03 +000019 TServerTransport serverTransport) {
Mark Sleed788b2e2006-09-07 01:26:35 +000020 super(processor, serverTransport);
Mark Slee83c52a82006-06-07 06:51:18 +000021 }
22
Mark Slee4e755ca2006-09-12 00:46:08 +000023 public void serve() {
Mark Slee83c52a82006-06-07 06:51:18 +000024 try {
25 serverTransport_.listen();
26 } catch (TTransportException ttx) {
27 ttx.printStackTrace();
28 return;
29 }
30
31 while (true) {
32 TTransport client = null;
Mark Slee456b7a82006-10-25 20:53:37 +000033 TTransport[] iot = null;
34 TProtocol[] iop = null;
Mark Slee83c52a82006-06-07 06:51:18 +000035 try {
36 client = serverTransport_.accept();
37 if (client != null) {
Mark Slee456b7a82006-10-25 20:53:37 +000038 iot = transportFactory_.getIOTransports(client);
39 iop = protocolFactory_.getIOProtocols(iot[0], iot[1]);
40 while (processor_.process(iop[0], iop[1]));
Mark Slee83c52a82006-06-07 06:51:18 +000041 }
Mark Sleeade2c832006-09-08 03:41:50 +000042 } catch (TTransportException ttx) {
43 // Client died, just move on
Mark Slee83c52a82006-06-07 06:51:18 +000044 } catch (TException tx) {
45 tx.printStackTrace();
Mark Sleeade2c832006-09-08 03:41:50 +000046 } catch (Exception x) {
47 x.printStackTrace();
Mark Slee83c52a82006-06-07 06:51:18 +000048 }
49
Mark Slee456b7a82006-10-25 20:53:37 +000050 if (iot != null) {
51 if (iot[0] != null) {
52 iot[0].close();
Mark Sleed788b2e2006-09-07 01:26:35 +000053 }
Mark Slee456b7a82006-10-25 20:53:37 +000054 if (iot[1] != null) {
55 iot[1].close();
Mark Sleed788b2e2006-09-07 01:26:35 +000056 }
Mark Slee83c52a82006-06-07 06:51:18 +000057 }
58 }
59 }
60}