blob: 394ce21e2060bb48aaa08fc8ba7eed920bf2e8a5 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleee8540632006-05-30 09:24:40 +000020#include "server/TSimpleServer.h"
Mark Slee8d7e1f62006-06-07 06:48:56 +000021#include "transport/TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000022#include <string>
Mark Slee8d7e1f62006-06-07 06:48:56 +000023#include <iostream>
Mark Sleee8540632006-05-30 09:24:40 +000024
T Jake Lucianib5e62212009-01-31 22:36:20 +000025namespace apache { namespace thrift { namespace server {
Marc Slemko6f038a72006-08-03 18:58:09 +000026
Mark Slee5ea15f92007-03-05 22:55:59 +000027using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000028using namespace apache::thrift;
29using namespace apache::thrift::protocol;
30using namespace apache::thrift::transport;
Mark Slee5ea15f92007-03-05 22:55:59 +000031using boost::shared_ptr;
32
Mark Slee8d7e1f62006-06-07 06:48:56 +000033/**
34 * A simple single-threaded application server. Perfect for unit tests!
35 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000036 */
Mark Slee794993d2006-09-20 01:56:10 +000037void TSimpleServer::serve() {
Marc Slemko16698852006-08-04 03:16:10 +000038
39 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000040 shared_ptr<TTransport> inputTransport;
41 shared_ptr<TTransport> outputTransport;
42 shared_ptr<TProtocol> inputProtocol;
43 shared_ptr<TProtocol> outputProtocol;
Mark Sleee8540632006-05-30 09:24:40 +000044
Mark Slee8d7e1f62006-06-07 06:48:56 +000045 try {
46 // Start the server listening
47 serverTransport_->listen();
48 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000049 cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000050 return;
51 }
52
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000053 // Run the preServe event
54 if (eventHandler_ != NULL) {
55 eventHandler_->preServe();
56 }
57
Mark Sleee8540632006-05-30 09:24:40 +000058 // Fetch client from server
Mark Slee6e3f6372007-03-01 22:05:46 +000059 while (!stop_) {
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000060 try {
Mark Slee8d7e1f62006-06-07 06:48:56 +000061 client = serverTransport_->accept();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000062 inputTransport = inputTransportFactory_->getTransport(client);
63 outputTransport = outputTransportFactory_->getTransport(client);
64 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
65 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000066 if (eventHandler_ != NULL) {
67 eventHandler_->clientBegin(inputProtocol, outputProtocol);
68 }
Mark Sleed788b2e2006-09-07 01:26:35 +000069 try {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000070 while (processor_->process(inputProtocol, outputProtocol)) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000071 // Peek ahead, is the remote side closed?
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000072 if (!inputTransport->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000073 break;
74 }
75 }
Mark Sleed788b2e2006-09-07 01:26:35 +000076 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000077 cerr << "TSimpleServer client died: " << ttx.what() << endl;
Mark Slee3860c9a2006-12-06 00:13:42 +000078 } catch (TException& tx) {
79 cerr << "TSimpleServer exception: " << tx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000080 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000081 if (eventHandler_ != NULL) {
82 eventHandler_->clientEnd(inputProtocol, outputProtocol);
83 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000084 inputTransport->close();
85 outputTransport->close();
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000086 client->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000087 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +000088 if (inputTransport != NULL) { inputTransport->close(); }
89 if (outputTransport != NULL) { outputTransport->close(); }
90 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000091 cerr << "TServerTransport died on accept: " << ttx.what() << endl;
92 continue;
93 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +000094 if (inputTransport != NULL) { inputTransport->close(); }
95 if (outputTransport != NULL) { outputTransport->close(); }
96 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000097 cerr << "Some kind of accept exception: " << tx.what() << endl;
98 continue;
99 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +0000100 if (inputTransport != NULL) { inputTransport->close(); }
101 if (outputTransport != NULL) { outputTransport->close(); }
102 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000103 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
104 break;
Mark Sleed788b2e2006-09-07 01:26:35 +0000105 }
Mark Sleee8540632006-05-30 09:24:40 +0000106 }
107
Mark Slee6e3f6372007-03-01 22:05:46 +0000108 if (stop_) {
109 try {
110 serverTransport_->close();
111 } catch (TTransportException &ttx) {
112 cerr << "TServerTransport failed on close: " << ttx.what() << endl;
113 }
114 stop_ = false;
115 }
Mark Sleee8540632006-05-30 09:24:40 +0000116}
Marc Slemko6f038a72006-08-03 18:58:09 +0000117
T Jake Lucianib5e62212009-01-31 22:36:20 +0000118}}} // apache::thrift::server