blob: 902c596733fcb0586ef3b9ecc3e847811e896765 [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 Slee456b7a82006-10-25 20:53:37 +000011import com.facebook.thrift.protocol.TProtocol;
12import com.facebook.thrift.protocol.TProtocolFactory;
Mark Slee83c52a82006-06-07 06:51:18 +000013import com.facebook.thrift.transport.TServerTransport;
14import com.facebook.thrift.transport.TTransport;
Aditya Agarwal5a429582007-02-06 02:51:15 +000015import com.facebook.thrift.transport.TTransportFactory;
Mark Slee83c52a82006-06-07 06:51:18 +000016import com.facebook.thrift.transport.TTransportException;
17
18/**
19 * Simple singlethreaded server for testing.
20 *
21 * @author Mark Slee <mcslee@facebook.com>
22 */
23public class TSimpleServer extends TServer {
24
Mark Slee83c52a82006-06-07 06:51:18 +000025 public TSimpleServer(TProcessor processor,
Mark Sleeffcddd62006-09-06 20:37:03 +000026 TServerTransport serverTransport) {
Mark Sleed788b2e2006-09-07 01:26:35 +000027 super(processor, serverTransport);
Mark Slee83c52a82006-06-07 06:51:18 +000028 }
29
Aditya Agarwal5a429582007-02-06 02:51:15 +000030 public TSimpleServer(TProcessor processor,
31 TServerTransport serverTransport,
32 TTransportFactory transportFactory,
33 TProtocolFactory protocolFactory) {
34 super(processor, serverTransport, transportFactory, protocolFactory);
35 }
36
37 public TSimpleServer(TProcessor processor,
38 TServerTransport serverTransport,
39 TTransportFactory inputTransportFactory,
40 TTransportFactory outputTransportFactory,
41 TProtocolFactory inputProtocolFactory,
42 TProtocolFactory outputProtocolFactory) {
43 super(processor, serverTransport,
44 inputTransportFactory, outputTransportFactory,
45 inputProtocolFactory, outputProtocolFactory);
46 }
47
Mark Slee4e755ca2006-09-12 00:46:08 +000048 public void serve() {
Mark Slee83c52a82006-06-07 06:51:18 +000049 try {
50 serverTransport_.listen();
51 } catch (TTransportException ttx) {
52 ttx.printStackTrace();
53 return;
54 }
55
56 while (true) {
57 TTransport client = null;
Aditya Agarwal5a429582007-02-06 02:51:15 +000058 TTransport inputTransport = null;
59 TTransport outputTransport = null;
60 TProtocol inputProtocol = null;
61 TProtocol outputProtocol = null;
Mark Slee83c52a82006-06-07 06:51:18 +000062 try {
63 client = serverTransport_.accept();
64 if (client != null) {
Aditya Agarwal5a429582007-02-06 02:51:15 +000065 inputTransport = inputTransportFactory_.getTransport(client);
66 outputTransport = outputTransportFactory_.getTransport(client);
67 inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
68 outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
69 while (processor_.process(inputProtocol, outputProtocol)) {}
Mark Slee83c52a82006-06-07 06:51:18 +000070 }
Mark Sleeade2c832006-09-08 03:41:50 +000071 } catch (TTransportException ttx) {
72 // Client died, just move on
Mark Slee83c52a82006-06-07 06:51:18 +000073 } catch (TException tx) {
74 tx.printStackTrace();
Mark Sleeade2c832006-09-08 03:41:50 +000075 } catch (Exception x) {
76 x.printStackTrace();
Mark Slee83c52a82006-06-07 06:51:18 +000077 }
78
Aditya Agarwal5a429582007-02-06 02:51:15 +000079 if (inputTransport != null) {
80 inputTransport.close();
Mark Slee83c52a82006-06-07 06:51:18 +000081 }
Aditya Agarwal5a429582007-02-06 02:51:15 +000082
83 if (outputTransport != null) {
84 outputTransport.close();
85 }
86
Mark Slee83c52a82006-06-07 06:51:18 +000087 }
88 }
89}