blob: 229261f2f0a7bf1c507d45e2cb4baa357a59104a [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) {
David Reiss25df8e72010-10-06 17:10:54 +000049 string errStr = string("TSimpleServer::run() listen(): ") + ttx.what();
50 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +000051 return;
52 }
53
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000054 // Run the preServe event
55 if (eventHandler_ != NULL) {
56 eventHandler_->preServe();
57 }
58
Mark Sleee8540632006-05-30 09:24:40 +000059 // Fetch client from server
Mark Slee6e3f6372007-03-01 22:05:46 +000060 while (!stop_) {
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000061 try {
Mark Slee8d7e1f62006-06-07 06:48:56 +000062 client = serverTransport_->accept();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000063 inputTransport = inputTransportFactory_->getTransport(client);
64 outputTransport = outputTransportFactory_->getTransport(client);
65 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
66 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000067 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +000068 if (inputTransport != NULL) { inputTransport->close(); }
69 if (outputTransport != NULL) { outputTransport->close(); }
70 if (client != NULL) { client->close(); }
David Reiss25df8e72010-10-06 17:10:54 +000071 string errStr = string("TServerTransport died on accept: ") + ttx.what();
72 GlobalOutput(errStr.c_str());
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000073 continue;
74 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +000075 if (inputTransport != NULL) { inputTransport->close(); }
76 if (outputTransport != NULL) { outputTransport->close(); }
77 if (client != NULL) { client->close(); }
David Reiss25df8e72010-10-06 17:10:54 +000078 string errStr = string("Some kind of accept exception: ") + tx.what();
79 GlobalOutput(errStr.c_str());
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000080 continue;
81 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +000082 if (inputTransport != NULL) { inputTransport->close(); }
83 if (outputTransport != NULL) { outputTransport->close(); }
84 if (client != NULL) { client->close(); }
David Reiss25df8e72010-10-06 17:10:54 +000085 string errStr = string("Some kind of accept exception: ") + s;
86 GlobalOutput(errStr.c_str());
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000087 break;
Mark Sleed788b2e2006-09-07 01:26:35 +000088 }
David Reiss25df8e72010-10-06 17:10:54 +000089
90 void* connectionContext = NULL;
91 if (eventHandler_ != NULL) {
92 connectionContext = eventHandler_->createContext(inputProtocol, outputProtocol);
93 }
94 try {
95 for (;;) {
96 if (eventHandler_ != NULL) {
97 eventHandler_->processContext(connectionContext, client);
98 }
99 if (!processor_->process(inputProtocol, outputProtocol, connectionContext) ||
100 // Peek ahead, is the remote side closed?
101 !inputProtocol->getTransport()->peek()) {
102 break;
103 }
104 }
Bryan Duxbury1e987582011-08-25 17:33:03 +0000105 } catch (const TTransportException& ttx) {
David Reiss25df8e72010-10-06 17:10:54 +0000106 string errStr = string("TSimpleServer client died: ") + ttx.what();
107 GlobalOutput(errStr.c_str());
Bryan Duxbury1e987582011-08-25 17:33:03 +0000108 } catch (const std::exception& x) {
109 GlobalOutput.printf("TSimpleServer exception: %s: %s",
110 typeid(x).name(), x.what());
David Reiss25df8e72010-10-06 17:10:54 +0000111 } catch (...) {
112 GlobalOutput("TSimpleServer uncaught exception.");
113 }
114 if (eventHandler_ != NULL) {
115 eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol);
116 }
117
118 try {
119 inputTransport->close();
Bryan Duxburye04159c2011-08-25 17:43:56 +0000120 } catch (const TTransportException& ttx) {
121 string errStr = string("TSimpleServer input close failed: ")
122 + ttx.what();
David Reiss25df8e72010-10-06 17:10:54 +0000123 GlobalOutput(errStr.c_str());
124 }
125 try {
126 outputTransport->close();
Bryan Duxburye04159c2011-08-25 17:43:56 +0000127 } catch (const TTransportException& ttx) {
128 string errStr = string("TSimpleServer output close failed: ")
129 + ttx.what();
David Reiss25df8e72010-10-06 17:10:54 +0000130 GlobalOutput(errStr.c_str());
131 }
132 try {
133 client->close();
Bryan Duxburye04159c2011-08-25 17:43:56 +0000134 } catch (const TTransportException& ttx) {
135 string errStr = string("TSimpleServer client close failed: ")
136 + ttx.what();
David Reiss25df8e72010-10-06 17:10:54 +0000137 GlobalOutput(errStr.c_str());
138 }
Mark Sleee8540632006-05-30 09:24:40 +0000139 }
140
Mark Slee6e3f6372007-03-01 22:05:46 +0000141 if (stop_) {
142 try {
143 serverTransport_->close();
144 } catch (TTransportException &ttx) {
David Reiss25df8e72010-10-06 17:10:54 +0000145 string errStr = string("TServerTransport failed on close: ") + ttx.what();
146 GlobalOutput(errStr.c_str());
Mark Slee6e3f6372007-03-01 22:05:46 +0000147 }
148 stop_ = false;
149 }
Mark Sleee8540632006-05-30 09:24:40 +0000150}
Marc Slemko6f038a72006-08-03 18:58:09 +0000151
T Jake Lucianib5e62212009-01-31 22:36:20 +0000152}}} // apache::thrift::server