blob: 705829594fbe55954b9fbedcfb8b6ba3d5a6cd5f [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;
Aditya Agarwal5a429582007-02-06 02:51:15 +00009import com.facebook.thrift.transport.TTransportFactory;
Mark Slee83c52a82006-06-07 06:51:18 +000010import com.facebook.thrift.transport.TTransportException;
11
12/**
13 * Simple singlethreaded server for testing.
14 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
17public class TSimpleServer extends TServer {
18
Mark Slee83c52a82006-06-07 06:51:18 +000019 public TSimpleServer(TProcessor processor,
Mark Sleeffcddd62006-09-06 20:37:03 +000020 TServerTransport serverTransport) {
Mark Sleed788b2e2006-09-07 01:26:35 +000021 super(processor, serverTransport);
Mark Slee83c52a82006-06-07 06:51:18 +000022 }
23
Aditya Agarwal5a429582007-02-06 02:51:15 +000024 public TSimpleServer(TProcessor processor,
25 TServerTransport serverTransport,
26 TTransportFactory transportFactory,
27 TProtocolFactory protocolFactory) {
28 super(processor, serverTransport, transportFactory, protocolFactory);
29 }
30
31 public TSimpleServer(TProcessor processor,
32 TServerTransport serverTransport,
33 TTransportFactory inputTransportFactory,
34 TTransportFactory outputTransportFactory,
35 TProtocolFactory inputProtocolFactory,
36 TProtocolFactory outputProtocolFactory) {
37 super(processor, serverTransport,
38 inputTransportFactory, outputTransportFactory,
39 inputProtocolFactory, outputProtocolFactory);
40 }
41
Mark Slee4e755ca2006-09-12 00:46:08 +000042 public void serve() {
Mark Slee83c52a82006-06-07 06:51:18 +000043 try {
44 serverTransport_.listen();
45 } catch (TTransportException ttx) {
46 ttx.printStackTrace();
47 return;
48 }
49
50 while (true) {
51 TTransport client = null;
Aditya Agarwal5a429582007-02-06 02:51:15 +000052 TTransport inputTransport = null;
53 TTransport outputTransport = null;
54 TProtocol inputProtocol = null;
55 TProtocol outputProtocol = null;
Mark Slee83c52a82006-06-07 06:51:18 +000056 try {
57 client = serverTransport_.accept();
58 if (client != null) {
Aditya Agarwal5a429582007-02-06 02:51:15 +000059 inputTransport = inputTransportFactory_.getTransport(client);
60 outputTransport = outputTransportFactory_.getTransport(client);
61 inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
62 outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
63 while (processor_.process(inputProtocol, outputProtocol)) {}
Mark Slee83c52a82006-06-07 06:51:18 +000064 }
Mark Sleeade2c832006-09-08 03:41:50 +000065 } catch (TTransportException ttx) {
66 // Client died, just move on
Mark Slee83c52a82006-06-07 06:51:18 +000067 } catch (TException tx) {
68 tx.printStackTrace();
Mark Sleeade2c832006-09-08 03:41:50 +000069 } catch (Exception x) {
70 x.printStackTrace();
Mark Slee83c52a82006-06-07 06:51:18 +000071 }
72
Aditya Agarwal5a429582007-02-06 02:51:15 +000073 if (inputTransport != null) {
74 inputTransport.close();
Mark Slee83c52a82006-06-07 06:51:18 +000075 }
Aditya Agarwal5a429582007-02-06 02:51:15 +000076
77 if (outputTransport != null) {
78 outputTransport.close();
79 }
80
Mark Slee83c52a82006-06-07 06:51:18 +000081 }
82 }
83}