Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 7 | #include "server/TThreadedServer.h" |
| 8 | #include "transport/TTransportException.h" |
| 9 | #include "concurrency/PosixThreadFactory.h" |
| 10 | |
| 11 | #include <string> |
| 12 | #include <iostream> |
| 13 | #include <pthread.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | namespace facebook { namespace thrift { namespace server { |
| 17 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 18 | using boost::shared_ptr; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 19 | using namespace std; |
| 20 | using namespace facebook::thrift; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 21 | using namespace facebook::thrift::protocol; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 22 | using namespace facebook::thrift::transport; |
| 23 | using namespace facebook::thrift::concurrency; |
| 24 | |
| 25 | class TThreadedServer::Task: public Runnable { |
| 26 | |
| 27 | public: |
| 28 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 29 | Task(TThreadedServer* server, |
| 30 | shared_ptr<TProcessor> processor, |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 31 | shared_ptr<TProtocol> input, |
| 32 | shared_ptr<TProtocol> output) : |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 33 | server_(server), |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 34 | processor_(processor), |
| 35 | input_(input), |
| 36 | output_(output) { |
| 37 | } |
| 38 | |
| 39 | ~Task() {} |
| 40 | |
| 41 | void run() { |
| 42 | try { |
| 43 | while (processor_->process(input_, output_)) { |
| 44 | if (!input_->getTransport()->peek()) { |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | } catch (TTransportException& ttx) { |
| 49 | cerr << "TThreadedServer client died: " << ttx.what() << endl; |
| 50 | } catch (TException& x) { |
| 51 | cerr << "TThreadedServer exception: " << x.what() << endl; |
| 52 | } catch (...) { |
| 53 | cerr << "TThreadedServer uncaught exception." << endl; |
| 54 | } |
| 55 | input_->getTransport()->close(); |
| 56 | output_->getTransport()->close(); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 57 | |
| 58 | // Remove this task from parent bookkeeping |
| 59 | { |
| 60 | Synchronized s(server_->tasksMonitor_); |
| 61 | server_->tasks_.erase(this); |
| 62 | if (server_->tasks_.empty()) { |
| 63 | server_->tasksMonitor_.notify(); |
| 64 | } |
| 65 | } |
| 66 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 70 | TThreadedServer* server_; |
| 71 | friend class TThreadedServer; |
| 72 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 73 | shared_ptr<TProcessor> processor_; |
| 74 | shared_ptr<TProtocol> input_; |
| 75 | shared_ptr<TProtocol> output_; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | |
| 79 | TThreadedServer::TThreadedServer(shared_ptr<TProcessor> processor, |
| 80 | shared_ptr<TServerTransport> serverTransport, |
| 81 | shared_ptr<TTransportFactory> transportFactory, |
| 82 | shared_ptr<TProtocolFactory> protocolFactory): |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 83 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
| 84 | stop_(false) { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 85 | threadFactory_ = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 86 | } |
| 87 | |
| 88 | TThreadedServer::~TThreadedServer() {} |
| 89 | |
| 90 | void TThreadedServer::serve() { |
| 91 | |
| 92 | shared_ptr<TTransport> client; |
| 93 | shared_ptr<TTransport> inputTransport; |
| 94 | shared_ptr<TTransport> outputTransport; |
| 95 | shared_ptr<TProtocol> inputProtocol; |
| 96 | shared_ptr<TProtocol> outputProtocol; |
| 97 | |
| 98 | try { |
| 99 | // Start the server listening |
| 100 | serverTransport_->listen(); |
| 101 | } catch (TTransportException& ttx) { |
| 102 | cerr << "TThreadedServer::run() listen(): " << ttx.what() << endl; |
| 103 | return; |
| 104 | } |
| 105 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 106 | while (!stop_) { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 107 | try { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 108 | client.reset(); |
| 109 | inputTransport.reset(); |
| 110 | outputTransport.reset(); |
| 111 | inputProtocol.reset(); |
| 112 | outputProtocol.reset(); |
| 113 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 114 | // Fetch client from server |
| 115 | client = serverTransport_->accept(); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 116 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 117 | // Make IO transports |
| 118 | inputTransport = inputTransportFactory_->getTransport(client); |
| 119 | outputTransport = outputTransportFactory_->getTransport(client); |
| 120 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 121 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
| 122 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 123 | TThreadedServer::Task* task = new TThreadedServer::Task(this, |
| 124 | processor_, |
| 125 | inputProtocol, |
| 126 | outputProtocol); |
| 127 | |
| 128 | // Create a task |
| 129 | shared_ptr<Runnable> runnable = |
| 130 | shared_ptr<Runnable>(task); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 131 | |
| 132 | // Create a thread for this task |
| 133 | shared_ptr<Thread> thread = |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 134 | shared_ptr<Thread>(threadFactory_->newThread(runnable)); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 135 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 136 | // Insert thread into the set of threads |
| 137 | { |
| 138 | Synchronized s(tasksMonitor_); |
| 139 | tasks_.insert(task); |
| 140 | } |
| 141 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 142 | // Start the thread! |
| 143 | thread->start(); |
| 144 | |
| 145 | } catch (TTransportException& ttx) { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 146 | if (inputTransport != NULL) { inputTransport->close(); } |
| 147 | if (outputTransport != NULL) { outputTransport->close(); } |
| 148 | if (client != NULL) { client->close(); } |
| 149 | if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) { |
| 150 | cerr << "TThreadedServer: TServerTransport died on accept: " << ttx.what() << endl; |
| 151 | } |
Mark Slee | 907e3d6 | 2007-02-08 22:29:24 +0000 | [diff] [blame] | 152 | continue; |
| 153 | } catch (TException& tx) { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 154 | if (inputTransport != NULL) { inputTransport->close(); } |
| 155 | if (outputTransport != NULL) { outputTransport->close(); } |
| 156 | if (client != NULL) { client->close(); } |
Mark Slee | 907e3d6 | 2007-02-08 22:29:24 +0000 | [diff] [blame] | 157 | cerr << "TThreadedServer: Caught TException: " << tx.what() << endl; |
| 158 | continue; |
| 159 | } catch (string s) { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 160 | if (inputTransport != NULL) { inputTransport->close(); } |
| 161 | if (outputTransport != NULL) { outputTransport->close(); } |
| 162 | if (client != NULL) { client->close(); } |
Mark Slee | 907e3d6 | 2007-02-08 22:29:24 +0000 | [diff] [blame] | 163 | cerr << "TThreadedServer: Unknown exception: " << s << endl; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | } |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 167 | |
| 168 | // If stopped manually, make sure to close server transport |
| 169 | if (stop_) { |
| 170 | try { |
| 171 | serverTransport_->close(); |
| 172 | } catch (TException &tx) { |
| 173 | cerr << "TThreadedServer: Exception shutting down: " << tx.what() << endl; |
| 174 | } |
| 175 | try { |
| 176 | Synchronized s(tasksMonitor_); |
| 177 | while (!tasks_.empty()) { |
| 178 | tasksMonitor_.wait(); |
| 179 | } |
| 180 | } catch (TException &tx) { |
| 181 | cerr << "TThreadedServer: Exception joining workers: " << tx.what() << endl; |
| 182 | } |
| 183 | stop_ = false; |
| 184 | } |
| 185 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | }}} // facebook::thrift::server |