blob: 181d8e4f207a5679dc0e4bdec7818709215b74cd [file] [log] [blame]
Mark Slee7eb0d632007-03-01 00:00:27 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee83c52a82006-06-07 06:51:18 +00007package com.facebook.thrift.server;
8
9import com.facebook.thrift.TException;
10import com.facebook.thrift.TProcessor;
Mark Slee448849d2007-05-31 01:30:22 +000011import com.facebook.thrift.TProcessorFactory;
Mark Slee456b7a82006-10-25 20:53:37 +000012import com.facebook.thrift.protocol.TProtocol;
13import com.facebook.thrift.protocol.TProtocolFactory;
Mark Slee83c52a82006-06-07 06:51:18 +000014import com.facebook.thrift.transport.TServerTransport;
15import com.facebook.thrift.transport.TTransport;
Aditya Agarwal5a429582007-02-06 02:51:15 +000016import com.facebook.thrift.transport.TTransportFactory;
Mark Slee83c52a82006-06-07 06:51:18 +000017import com.facebook.thrift.transport.TTransportException;
18
19/**
20 * Simple singlethreaded server for testing.
21 *
22 * @author Mark Slee <mcslee@facebook.com>
23 */
24public class TSimpleServer extends TServer {
25
Mark Slee83c52a82006-06-07 06:51:18 +000026 public TSimpleServer(TProcessor processor,
Mark Sleeffcddd62006-09-06 20:37:03 +000027 TServerTransport serverTransport) {
Mark Slee448849d2007-05-31 01:30:22 +000028 super(new TProcessorFactory(processor), serverTransport);
Mark Slee83c52a82006-06-07 06:51:18 +000029 }
30
Aditya Agarwal5a429582007-02-06 02:51:15 +000031 public TSimpleServer(TProcessor processor,
32 TServerTransport serverTransport,
33 TTransportFactory transportFactory,
34 TProtocolFactory protocolFactory) {
Mark Slee448849d2007-05-31 01:30:22 +000035 super(new TProcessorFactory(processor), serverTransport, transportFactory, protocolFactory);
Aditya Agarwal5a429582007-02-06 02:51:15 +000036 }
37
38 public TSimpleServer(TProcessor processor,
39 TServerTransport serverTransport,
40 TTransportFactory inputTransportFactory,
41 TTransportFactory outputTransportFactory,
42 TProtocolFactory inputProtocolFactory,
43 TProtocolFactory outputProtocolFactory) {
Mark Slee448849d2007-05-31 01:30:22 +000044 super(new TProcessorFactory(processor), serverTransport,
Aditya Agarwal5a429582007-02-06 02:51:15 +000045 inputTransportFactory, outputTransportFactory,
46 inputProtocolFactory, outputProtocolFactory);
47 }
48
Mark Slee448849d2007-05-31 01:30:22 +000049 public TSimpleServer(TProcessorFactory processorFactory,
50 TServerTransport serverTransport) {
51 super(processorFactory, serverTransport);
52 }
53
54 public TSimpleServer(TProcessorFactory processorFactory,
55 TServerTransport serverTransport,
56 TTransportFactory transportFactory,
57 TProtocolFactory protocolFactory) {
58 super(processorFactory, serverTransport, transportFactory, protocolFactory);
59 }
60
61 public TSimpleServer(TProcessorFactory processorFactory,
62 TServerTransport serverTransport,
63 TTransportFactory inputTransportFactory,
64 TTransportFactory outputTransportFactory,
65 TProtocolFactory inputProtocolFactory,
66 TProtocolFactory outputProtocolFactory) {
67 super(processorFactory, serverTransport,
68 inputTransportFactory, outputTransportFactory,
69 inputProtocolFactory, outputProtocolFactory);
70 }
71
72
Mark Slee4e755ca2006-09-12 00:46:08 +000073 public void serve() {
Mark Slee83c52a82006-06-07 06:51:18 +000074 try {
75 serverTransport_.listen();
76 } catch (TTransportException ttx) {
77 ttx.printStackTrace();
78 return;
79 }
80
81 while (true) {
82 TTransport client = null;
Mark Slee448849d2007-05-31 01:30:22 +000083 TProcessor processor = null;
Aditya Agarwal5a429582007-02-06 02:51:15 +000084 TTransport inputTransport = null;
85 TTransport outputTransport = null;
86 TProtocol inputProtocol = null;
87 TProtocol outputProtocol = null;
Mark Slee83c52a82006-06-07 06:51:18 +000088 try {
89 client = serverTransport_.accept();
90 if (client != null) {
Mark Slee448849d2007-05-31 01:30:22 +000091 processor = processorFactory_.getProcessor(client);
Aditya Agarwal5a429582007-02-06 02:51:15 +000092 inputTransport = inputTransportFactory_.getTransport(client);
93 outputTransport = outputTransportFactory_.getTransport(client);
94 inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
95 outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
Mark Slee448849d2007-05-31 01:30:22 +000096 while (processor.process(inputProtocol, outputProtocol)) {}
Mark Slee83c52a82006-06-07 06:51:18 +000097 }
Mark Sleeade2c832006-09-08 03:41:50 +000098 } catch (TTransportException ttx) {
99 // Client died, just move on
Mark Slee83c52a82006-06-07 06:51:18 +0000100 } catch (TException tx) {
101 tx.printStackTrace();
Mark Sleeade2c832006-09-08 03:41:50 +0000102 } catch (Exception x) {
103 x.printStackTrace();
Mark Slee83c52a82006-06-07 06:51:18 +0000104 }
105
Aditya Agarwal5a429582007-02-06 02:51:15 +0000106 if (inputTransport != null) {
107 inputTransport.close();
Mark Slee83c52a82006-06-07 06:51:18 +0000108 }
Aditya Agarwal5a429582007-02-06 02:51:15 +0000109
110 if (outputTransport != null) {
111 outputTransport.close();
112 }
113
Mark Slee83c52a82006-06-07 06:51:18 +0000114 }
115 }
116}