David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 20 | #include "server/TSimpleServer.h" |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 21 | #include "transport/TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | #include <string> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 23 | #include <iostream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 25 | namespace apache { namespace thrift { namespace server { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 26 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 27 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 28 | using namespace apache::thrift; |
| 29 | using namespace apache::thrift::protocol; |
| 30 | using namespace apache::thrift::transport; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 31 | using boost::shared_ptr; |
| 32 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 33 | /** |
| 34 | * A simple single-threaded application server. Perfect for unit tests! |
| 35 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 36 | */ |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 37 | void TSimpleServer::serve() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 38 | |
| 39 | shared_ptr<TTransport> client; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 40 | shared_ptr<TTransport> inputTransport; |
| 41 | shared_ptr<TTransport> outputTransport; |
| 42 | shared_ptr<TProtocol> inputProtocol; |
| 43 | shared_ptr<TProtocol> outputProtocol; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 44 | |
Roger Meier | d8f50f3 | 2012-04-11 21:48:56 +0000 | [diff] [blame] | 45 | // Start the server listening |
| 46 | serverTransport_->listen(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 47 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 48 | // Run the preServe event |
| 49 | if (eventHandler_ != NULL) { |
| 50 | eventHandler_->preServe(); |
| 51 | } |
| 52 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 53 | // Fetch client from server |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 54 | while (!stop_) { |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 55 | try { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 56 | client = serverTransport_->accept(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 57 | inputTransport = inputTransportFactory_->getTransport(client); |
| 58 | outputTransport = outputTransportFactory_->getTransport(client); |
| 59 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 60 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 61 | } catch (TTransportException& ttx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 62 | if (inputTransport != NULL) { inputTransport->close(); } |
| 63 | if (outputTransport != NULL) { outputTransport->close(); } |
| 64 | if (client != NULL) { client->close(); } |
Jake Farrell | 6ce2704 | 2012-01-27 04:32:17 +0000 | [diff] [blame] | 65 | if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) { |
| 66 | string errStr = string("TServerTransport died on accept: ") + ttx.what(); |
| 67 | GlobalOutput(errStr.c_str()); |
| 68 | } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 69 | continue; |
| 70 | } catch (TException& tx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 71 | if (inputTransport != NULL) { inputTransport->close(); } |
| 72 | if (outputTransport != NULL) { outputTransport->close(); } |
| 73 | if (client != NULL) { client->close(); } |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 74 | string errStr = string("Some kind of accept exception: ") + tx.what(); |
| 75 | GlobalOutput(errStr.c_str()); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 76 | continue; |
| 77 | } catch (string s) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 78 | if (inputTransport != NULL) { inputTransport->close(); } |
| 79 | if (outputTransport != NULL) { outputTransport->close(); } |
| 80 | if (client != NULL) { client->close(); } |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 81 | string errStr = string("Some kind of accept exception: ") + s; |
| 82 | GlobalOutput(errStr.c_str()); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 83 | break; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 84 | } |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 85 | |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 86 | // Get the processor |
| 87 | shared_ptr<TProcessor> processor = getProcessor(inputProtocol, |
| 88 | outputProtocol, client); |
| 89 | |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 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 | } |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 99 | if (!processor->process(inputProtocol, outputProtocol, |
| 100 | connectionContext) || |
| 101 | // Peek ahead, is the remote side closed? |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 102 | !inputProtocol->getTransport()->peek()) { |
| 103 | break; |
| 104 | } |
| 105 | } |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 106 | } catch (const TTransportException& ttx) { |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 107 | string errStr = string("TSimpleServer client died: ") + ttx.what(); |
| 108 | GlobalOutput(errStr.c_str()); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 109 | } catch (const std::exception& x) { |
| 110 | GlobalOutput.printf("TSimpleServer exception: %s: %s", |
| 111 | typeid(x).name(), x.what()); |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 112 | } catch (...) { |
| 113 | GlobalOutput("TSimpleServer uncaught exception."); |
| 114 | } |
| 115 | if (eventHandler_ != NULL) { |
| 116 | eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol); |
| 117 | } |
| 118 | |
| 119 | try { |
| 120 | inputTransport->close(); |
Bryan Duxbury | e04159c | 2011-08-25 17:43:56 +0000 | [diff] [blame] | 121 | } catch (const TTransportException& ttx) { |
| 122 | string errStr = string("TSimpleServer input close failed: ") |
| 123 | + ttx.what(); |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 124 | GlobalOutput(errStr.c_str()); |
| 125 | } |
| 126 | try { |
| 127 | outputTransport->close(); |
Bryan Duxbury | e04159c | 2011-08-25 17:43:56 +0000 | [diff] [blame] | 128 | } catch (const TTransportException& ttx) { |
| 129 | string errStr = string("TSimpleServer output close failed: ") |
| 130 | + ttx.what(); |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 131 | GlobalOutput(errStr.c_str()); |
| 132 | } |
| 133 | try { |
| 134 | client->close(); |
Bryan Duxbury | e04159c | 2011-08-25 17:43:56 +0000 | [diff] [blame] | 135 | } catch (const TTransportException& ttx) { |
| 136 | string errStr = string("TSimpleServer client close failed: ") |
| 137 | + ttx.what(); |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 138 | GlobalOutput(errStr.c_str()); |
| 139 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 142 | if (stop_) { |
| 143 | try { |
| 144 | serverTransport_->close(); |
| 145 | } catch (TTransportException &ttx) { |
David Reiss | 25df8e7 | 2010-10-06 17:10:54 +0000 | [diff] [blame] | 146 | string errStr = string("TServerTransport failed on close: ") + ttx.what(); |
| 147 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 148 | } |
| 149 | stop_ = false; |
| 150 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 151 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 152 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 153 | }}} // apache::thrift::server |