blob: 438a587e296512ea8d56a30636e5a8ba446b1efa [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);
David Reiss23248712010-10-06 17:10:08 +000066 void* connectionContext = NULL;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000067 if (eventHandler_ != NULL) {
David Reiss23248712010-10-06 17:10:08 +000068 connectionContext = eventHandler_->createContext(inputProtocol, outputProtocol);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000069 }
Mark Sleed788b2e2006-09-07 01:26:35 +000070 try {
David Reiss23248712010-10-06 17:10:08 +000071 for (;;) {
72 if (eventHandler_ != NULL) {
73 eventHandler_->processContext(connectionContext, client);
74 }
75 if (!processor_->process(inputProtocol, outputProtocol, connectionContext) ||
76 // Peek ahead, is the remote side closed?
77 !inputProtocol->getTransport()->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000078 break;
79 }
80 }
Mark Sleed788b2e2006-09-07 01:26:35 +000081 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000082 cerr << "TSimpleServer client died: " << ttx.what() << endl;
Mark Slee3860c9a2006-12-06 00:13:42 +000083 } catch (TException& tx) {
84 cerr << "TSimpleServer exception: " << tx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000085 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000086 if (eventHandler_ != NULL) {
David Reiss23248712010-10-06 17:10:08 +000087 eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000088 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000089 inputTransport->close();
90 outputTransport->close();
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000091 client->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000092 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +000093 if (inputTransport != NULL) { inputTransport->close(); }
94 if (outputTransport != NULL) { outputTransport->close(); }
95 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000096 cerr << "TServerTransport died on accept: " << ttx.what() << endl;
97 continue;
98 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +000099 if (inputTransport != NULL) { inputTransport->close(); }
100 if (outputTransport != NULL) { outputTransport->close(); }
101 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000102 cerr << "Some kind of accept exception: " << tx.what() << endl;
103 continue;
104 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +0000105 if (inputTransport != NULL) { inputTransport->close(); }
106 if (outputTransport != NULL) { outputTransport->close(); }
107 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000108 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
109 break;
Mark Sleed788b2e2006-09-07 01:26:35 +0000110 }
Mark Sleee8540632006-05-30 09:24:40 +0000111 }
112
Mark Slee6e3f6372007-03-01 22:05:46 +0000113 if (stop_) {
114 try {
115 serverTransport_->close();
116 } catch (TTransportException &ttx) {
117 cerr << "TServerTransport failed on close: " << ttx.what() << endl;
118 }
119 stop_ = false;
120 }
Mark Sleee8540632006-05-30 09:24:40 +0000121}
Marc Slemko6f038a72006-08-03 18:58:09 +0000122
T Jake Lucianib5e62212009-01-31 22:36:20 +0000123}}} // apache::thrift::server