blob: 65bf3d4a7a4a6cca30094a59305983265d9b6987 [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 Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_TPROCESSOR_H_
21#define _THRIFT_TPROCESSOR_H_ 1
Mark Slee8d7e1f62006-06-07 06:48:56 +000022
23#include <string>
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/protocol/TProtocol.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000025
Konrad Grochowski16a23a62014-11-13 15:33:38 +010026namespace apache {
27namespace thrift {
Marc Slemko6f038a72006-08-03 18:58:09 +000028
Mark Slee8d7e1f62006-06-07 06:48:56 +000029/**
David Reissd7192062010-10-06 17:09:33 +000030 * Virtual interface class that can handle events from the processor. To
31 * use this you should subclass it and implement the methods that you care
32 * about. Your subclass can also store local data that you may care about,
33 * such as additional "arguments" to these methods (stored in the object
34 * instance's state).
35 */
36class TProcessorEventHandler {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010037public:
Sebastian Zenker042580f2019-01-29 15:48:12 +010038 virtual ~TProcessorEventHandler() = default;
David Reissd7192062010-10-06 17:09:33 +000039
40 /**
41 * Called before calling other callback methods.
42 * Expected to return some sort of context object.
43 * The return value is passed to all other callbacks
44 * for that function invocation.
45 */
Roger Meier3b771a12010-11-17 22:11:26 +000046 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010047 (void)fn_name;
48 (void)serverContext;
Sebastian Zenker042580f2019-01-29 15:48:12 +010049 return nullptr;
Roger Meier3b771a12010-11-17 22:11:26 +000050 }
David Reissd7192062010-10-06 17:09:33 +000051
52 /**
53 * Expected to free resources associated with a context.
54 */
Roger Meier3b771a12010-11-17 22:11:26 +000055 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056 (void)ctx;
57 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +000058 }
David Reissd7192062010-10-06 17:09:33 +000059
60 /**
61 * Called before reading arguments.
62 */
Roger Meier3b771a12010-11-17 22:11:26 +000063 virtual void preRead(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 (void)ctx;
65 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +000066 }
David Reissd7192062010-10-06 17:09:33 +000067
68 /**
69 * Called between reading arguments and calling the handler.
70 */
Roger Meier3b771a12010-11-17 22:11:26 +000071 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072 (void)ctx;
73 (void)fn_name;
74 (void)bytes;
Roger Meier3b771a12010-11-17 22:11:26 +000075 }
David Reissd7192062010-10-06 17:09:33 +000076
77 /**
78 * Called between calling the handler and writing the response.
79 */
Roger Meier3b771a12010-11-17 22:11:26 +000080 virtual void preWrite(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081 (void)ctx;
82 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +000083 }
David Reissd7192062010-10-06 17:09:33 +000084
85 /**
86 * Called after writing the response.
87 */
Roger Meier3b771a12010-11-17 22:11:26 +000088 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010089 (void)ctx;
90 (void)fn_name;
91 (void)bytes;
Roger Meier3b771a12010-11-17 22:11:26 +000092 }
David Reissd7192062010-10-06 17:09:33 +000093
94 /**
95 * Called when an async function call completes successfully.
96 */
Roger Meier3b771a12010-11-17 22:11:26 +000097 virtual void asyncComplete(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098 (void)ctx;
99 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +0000100 }
David Reissd7192062010-10-06 17:09:33 +0000101
102 /**
103 * Called if the handler throws an undeclared exception.
104 */
Roger Meier3b771a12010-11-17 22:11:26 +0000105 virtual void handlerError(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100106 (void)ctx;
107 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +0000108 }
David Reissd7192062010-10-06 17:09:33 +0000109
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100110protected:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100111 TProcessorEventHandler() = default;
David Reissd7192062010-10-06 17:09:33 +0000112};
113
114/**
David Reiss18cd0f02010-10-06 17:09:39 +0000115 * A helper class used by the generated code to free each context.
116 */
117class TProcessorContextFreer {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100118public:
119 TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method)
120 : handler_(handler), context_(context), method_(method) {}
121 ~TProcessorContextFreer() {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100122 if (handler_ != nullptr)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100123 handler_->freeContext(context_, method_);
124 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100125 void unregister() { handler_ = nullptr; }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100126
127private:
David Reiss18cd0f02010-10-06 17:09:39 +0000128 apache::thrift::TProcessorEventHandler* handler_;
129 void* context_;
130 const char* method_;
131};
132
133/**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000134 * A processor is a generic object that acts upon two streams of data, one
135 * an input and the other an output. The definition of this object is loose,
136 * though the typical case is for some sort of server that either generates
137 * responses to an input stream or forwards data from one pipe onto another.
138 *
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139 */
140class TProcessor {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100141public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100142 virtual ~TProcessor() = default;
Mark Slee4af6ed72006-10-25 19:02:49 +0000143
cyy316723a2019-01-05 16:35:14 +0800144 virtual bool process(std::shared_ptr<protocol::TProtocol> in,
145 std::shared_ptr<protocol::TProtocol> out,
David Reiss23248712010-10-06 17:10:08 +0000146 void* connectionContext) = 0;
Mark Slee4af6ed72006-10-25 19:02:49 +0000147
cyy316723a2019-01-05 16:35:14 +0800148 bool process(std::shared_ptr<apache::thrift::protocol::TProtocol> io, void* connectionContext) {
David Reiss23248712010-10-06 17:10:08 +0000149 return process(io, io, connectionContext);
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000151
cyy316723a2019-01-05 16:35:14 +0800152 std::shared_ptr<TProcessorEventHandler> getEventHandler() const { return eventHandler_; }
David Reissd7192062010-10-06 17:09:33 +0000153
cyy316723a2019-01-05 16:35:14 +0800154 void setEventHandler(std::shared_ptr<TProcessorEventHandler> eventHandler) {
David Reissd7192062010-10-06 17:09:33 +0000155 eventHandler_ = eventHandler;
156 }
157
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100158protected:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100159 TProcessor() = default;
David Reissd7192062010-10-06 17:09:33 +0000160
cyy316723a2019-01-05 16:35:14 +0800161 std::shared_ptr<TProcessorEventHandler> eventHandler_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000162};
163
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000164/**
cyy316723a2019-01-05 16:35:14 +0800165 * This is a helper class to allow std::shared_ptr to be used with handler
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000166 * pointers returned by the generated handler factories.
167 *
168 * The handler factory classes generated by the thrift compiler return raw
169 * pointers, and factory->releaseHandler() must be called when the handler is
170 * no longer needed.
171 *
172 * A ReleaseHandler object can be instantiated and passed as the second
173 * parameter to a shared_ptr, so that factory->releaseHandler() will be called
174 * when the object is no longer needed, instead of deleting the pointer.
175 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100176template <typename HandlerFactory_>
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000177class ReleaseHandler {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100178public:
cyy316723a2019-01-05 16:35:14 +0800179 ReleaseHandler(const std::shared_ptr<HandlerFactory_>& handlerFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100180 : handlerFactory_(handlerFactory) {}
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000181
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100182 void operator()(typename HandlerFactory_::Handler* handler) {
183 if (handler) {
184 handlerFactory_->releaseHandler(handler);
185 }
186 }
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000187
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100188private:
cyy316723a2019-01-05 16:35:14 +0800189 std::shared_ptr<HandlerFactory_> handlerFactory_;
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000190};
191
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000192struct TConnectionInfo {
193 // The input and output protocols
cyy316723a2019-01-05 16:35:14 +0800194 std::shared_ptr<protocol::TProtocol> input;
195 std::shared_ptr<protocol::TProtocol> output;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000196 // The underlying transport used for the connection
197 // This is the transport that was returned by TServerTransport::accept(),
198 // and it may be different than the transport pointed to by the input and
199 // output protocols.
cyy316723a2019-01-05 16:35:14 +0800200 std::shared_ptr<transport::TTransport> transport;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000201};
202
203class TProcessorFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100204public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100205 virtual ~TProcessorFactory() = default;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000206
207 /**
208 * Get the TProcessor to use for a particular connection.
209 *
210 * This method is always invoked in the same thread that the connection was
211 * accepted on. This generally means that this call does not need to be
212 * thread safe, as it will always be invoked from a single thread.
213 */
cyy316723a2019-01-05 16:35:14 +0800214 virtual std::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) = 0;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000215};
216
217class TSingletonProcessorFactory : public TProcessorFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100218public:
cyy316723a2019-01-05 16:35:14 +0800219 TSingletonProcessorFactory(std::shared_ptr<TProcessor> processor) : processor_(processor) {}
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000220
Sebastian Zenker042580f2019-01-29 15:48:12 +0100221 std::shared_ptr<TProcessor> getProcessor(const TConnectionInfo&) override { return processor_; }
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000222
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100223private:
cyy316723a2019-01-05 16:35:14 +0800224 std::shared_ptr<TProcessor> processor_;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000225};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100226}
227} // apache::thrift
Marc Slemko6f038a72006-08-03 18:58:09 +0000228
David Reiss5ddabb82010-10-06 17:09:37 +0000229#endif // #ifndef _THRIFT_TPROCESSOR_H_