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 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 45 | try { |
| 46 | // Start the server listening |
| 47 | serverTransport_->listen(); |
| 48 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 49 | cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 53 | // Run the preServe event |
| 54 | if (eventHandler_ != NULL) { |
| 55 | eventHandler_->preServe(); |
| 56 | } |
| 57 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 58 | // Fetch client from server |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 59 | while (!stop_) { |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 60 | try { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 61 | client = serverTransport_->accept(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 62 | inputTransport = inputTransportFactory_->getTransport(client); |
| 63 | outputTransport = outputTransportFactory_->getTransport(client); |
| 64 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 65 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 66 | void* connectionContext = NULL; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 67 | if (eventHandler_ != NULL) { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 68 | connectionContext = eventHandler_->createContext(inputProtocol, outputProtocol); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 69 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 70 | try { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 71 | 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 Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 81 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 82 | cerr << "TSimpleServer client died: " << ttx.what() << endl; |
Mark Slee | 3860c9a | 2006-12-06 00:13:42 +0000 | [diff] [blame] | 83 | } catch (TException& tx) { |
| 84 | cerr << "TSimpleServer exception: " << tx.what() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 86 | if (eventHandler_ != NULL) { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 87 | eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 88 | } |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 89 | inputTransport->close(); |
| 90 | outputTransport->close(); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 91 | client->close(); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 92 | } catch (TTransportException& ttx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 93 | if (inputTransport != NULL) { inputTransport->close(); } |
| 94 | if (outputTransport != NULL) { outputTransport->close(); } |
| 95 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 96 | cerr << "TServerTransport died on accept: " << ttx.what() << endl; |
| 97 | continue; |
| 98 | } catch (TException& tx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 99 | if (inputTransport != NULL) { inputTransport->close(); } |
| 100 | if (outputTransport != NULL) { outputTransport->close(); } |
| 101 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 102 | cerr << "Some kind of accept exception: " << tx.what() << endl; |
| 103 | continue; |
| 104 | } catch (string s) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 105 | if (inputTransport != NULL) { inputTransport->close(); } |
| 106 | if (outputTransport != NULL) { outputTransport->close(); } |
| 107 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 108 | cerr << "TThreadPoolServer: Unknown exception: " << s << endl; |
| 109 | break; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 110 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 113 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 121 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 122 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 123 | }}} // apache::thrift::server |