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 | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_TPROCESSOR_H_ |
| 21 | #define _THRIFT_TPROCESSOR_H_ 1 |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 22 | |
| 23 | #include <string> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 24 | #include <thrift/protocol/TProtocol.h> |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 25 | #include <thrift/stdcxx.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 26 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 27 | namespace apache { |
| 28 | namespace thrift { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 29 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 30 | /** |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 31 | * Virtual interface class that can handle events from the processor. To |
| 32 | * use this you should subclass it and implement the methods that you care |
| 33 | * about. Your subclass can also store local data that you may care about, |
| 34 | * such as additional "arguments" to these methods (stored in the object |
| 35 | * instance's state). |
| 36 | */ |
| 37 | class TProcessorEventHandler { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | public: |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 39 | virtual ~TProcessorEventHandler() {} |
| 40 | |
| 41 | /** |
| 42 | * Called before calling other callback methods. |
| 43 | * Expected to return some sort of context object. |
| 44 | * The return value is passed to all other callbacks |
| 45 | * for that function invocation. |
| 46 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 47 | virtual void* getContext(const char* fn_name, void* serverContext) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 48 | (void)fn_name; |
| 49 | (void)serverContext; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Expected to free resources associated with a context. |
| 55 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 56 | virtual void freeContext(void* ctx, const char* fn_name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 57 | (void)ctx; |
| 58 | (void)fn_name; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 59 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Called before reading arguments. |
| 63 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 64 | virtual void preRead(void* ctx, const char* fn_name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 65 | (void)ctx; |
| 66 | (void)fn_name; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 67 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Called between reading arguments and calling the handler. |
| 71 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 72 | virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 73 | (void)ctx; |
| 74 | (void)fn_name; |
| 75 | (void)bytes; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 76 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Called between calling the handler and writing the response. |
| 80 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 81 | virtual void preWrite(void* ctx, const char* fn_name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 82 | (void)ctx; |
| 83 | (void)fn_name; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 84 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Called after writing the response. |
| 88 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 89 | virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 90 | (void)ctx; |
| 91 | (void)fn_name; |
| 92 | (void)bytes; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 93 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Called when an async function call completes successfully. |
| 97 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 98 | virtual void asyncComplete(void* ctx, const char* fn_name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 99 | (void)ctx; |
| 100 | (void)fn_name; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 101 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Called if the handler throws an undeclared exception. |
| 105 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 106 | virtual void handlerError(void* ctx, const char* fn_name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 107 | (void)ctx; |
| 108 | (void)fn_name; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 109 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 110 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 111 | protected: |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 112 | TProcessorEventHandler() {} |
| 113 | }; |
| 114 | |
| 115 | /** |
David Reiss | 18cd0f0 | 2010-10-06 17:09:39 +0000 | [diff] [blame] | 116 | * A helper class used by the generated code to free each context. |
| 117 | */ |
| 118 | class TProcessorContextFreer { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 119 | public: |
| 120 | TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method) |
| 121 | : handler_(handler), context_(context), method_(method) {} |
| 122 | ~TProcessorContextFreer() { |
| 123 | if (handler_ != NULL) |
| 124 | handler_->freeContext(context_, method_); |
| 125 | } |
David Reiss | 18cd0f0 | 2010-10-06 17:09:39 +0000 | [diff] [blame] | 126 | void unregister() { handler_ = NULL; } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 127 | |
| 128 | private: |
David Reiss | 18cd0f0 | 2010-10-06 17:09:39 +0000 | [diff] [blame] | 129 | apache::thrift::TProcessorEventHandler* handler_; |
| 130 | void* context_; |
| 131 | const char* method_; |
| 132 | }; |
| 133 | |
| 134 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 135 | * A processor is a generic object that acts upon two streams of data, one |
| 136 | * an input and the other an output. The definition of this object is loose, |
| 137 | * though the typical case is for some sort of server that either generates |
| 138 | * responses to an input stream or forwards data from one pipe onto another. |
| 139 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 140 | */ |
| 141 | class TProcessor { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 142 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 143 | virtual ~TProcessor() {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 144 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 145 | virtual bool process(stdcxx::shared_ptr<protocol::TProtocol> in, |
| 146 | stdcxx::shared_ptr<protocol::TProtocol> out, |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 147 | void* connectionContext) = 0; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 148 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 149 | bool process(stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> io, void* connectionContext) { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 150 | return process(io, io, connectionContext); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 151 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 152 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 153 | stdcxx::shared_ptr<TProcessorEventHandler> getEventHandler() const { return eventHandler_; } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 154 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 155 | void setEventHandler(stdcxx::shared_ptr<TProcessorEventHandler> eventHandler) { |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 156 | eventHandler_ = eventHandler; |
| 157 | } |
| 158 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 159 | protected: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 160 | TProcessor() {} |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 161 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 162 | stdcxx::shared_ptr<TProcessorEventHandler> eventHandler_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 165 | /** |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 166 | * This is a helper class to allow stdcxx::shared_ptr to be used with handler |
Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 167 | * pointers returned by the generated handler factories. |
| 168 | * |
| 169 | * The handler factory classes generated by the thrift compiler return raw |
| 170 | * pointers, and factory->releaseHandler() must be called when the handler is |
| 171 | * no longer needed. |
| 172 | * |
| 173 | * A ReleaseHandler object can be instantiated and passed as the second |
| 174 | * parameter to a shared_ptr, so that factory->releaseHandler() will be called |
| 175 | * when the object is no longer needed, instead of deleting the pointer. |
| 176 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 177 | template <typename HandlerFactory_> |
Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 178 | class ReleaseHandler { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 179 | public: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 180 | ReleaseHandler(const stdcxx::shared_ptr<HandlerFactory_>& handlerFactory) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 181 | : handlerFactory_(handlerFactory) {} |
Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 182 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 183 | void operator()(typename HandlerFactory_::Handler* handler) { |
| 184 | if (handler) { |
| 185 | handlerFactory_->releaseHandler(handler); |
| 186 | } |
| 187 | } |
Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 188 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 189 | private: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 190 | stdcxx::shared_ptr<HandlerFactory_> handlerFactory_; |
Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 193 | struct TConnectionInfo { |
| 194 | // The input and output protocols |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 195 | stdcxx::shared_ptr<protocol::TProtocol> input; |
| 196 | stdcxx::shared_ptr<protocol::TProtocol> output; |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 197 | // The underlying transport used for the connection |
| 198 | // This is the transport that was returned by TServerTransport::accept(), |
| 199 | // and it may be different than the transport pointed to by the input and |
| 200 | // output protocols. |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 201 | stdcxx::shared_ptr<transport::TTransport> transport; |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | class TProcessorFactory { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 205 | public: |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 206 | virtual ~TProcessorFactory() {} |
| 207 | |
| 208 | /** |
| 209 | * Get the TProcessor to use for a particular connection. |
| 210 | * |
| 211 | * This method is always invoked in the same thread that the connection was |
| 212 | * accepted on. This generally means that this call does not need to be |
| 213 | * thread safe, as it will always be invoked from a single thread. |
| 214 | */ |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 215 | virtual stdcxx::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) = 0; |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | class TSingletonProcessorFactory : public TProcessorFactory { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 219 | public: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 220 | TSingletonProcessorFactory(stdcxx::shared_ptr<TProcessor> processor) : processor_(processor) {} |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 221 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 222 | stdcxx::shared_ptr<TProcessor> getProcessor(const TConnectionInfo&) { return processor_; } |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 223 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 224 | private: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 225 | stdcxx::shared_ptr<TProcessor> processor_; |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 226 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 227 | } |
| 228 | } // apache::thrift |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 229 | |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 230 | #endif // #ifndef _THRIFT_TPROCESSOR_H_ |