blob: 27294d3ec0c65fae7affe87d148dd44c0a859a0d [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>
James E. King, III82ae9572017-08-05 12:23:54 -040025#include <thrift/stdcxx.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000026
Konrad Grochowski16a23a62014-11-13 15:33:38 +010027namespace apache {
28namespace thrift {
Marc Slemko6f038a72006-08-03 18:58:09 +000029
Mark Slee8d7e1f62006-06-07 06:48:56 +000030/**
David Reissd7192062010-10-06 17:09:33 +000031 * 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 */
37class TProcessorEventHandler {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038public:
David Reissd7192062010-10-06 17:09:33 +000039 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 Meier3b771a12010-11-17 22:11:26 +000047 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010048 (void)fn_name;
49 (void)serverContext;
Roger Meier3b771a12010-11-17 22:11:26 +000050 return NULL;
51 }
David Reissd7192062010-10-06 17:09:33 +000052
53 /**
54 * Expected to free resources associated with a context.
55 */
Roger Meier3b771a12010-11-17 22:11:26 +000056 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057 (void)ctx;
58 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +000059 }
David Reissd7192062010-10-06 17:09:33 +000060
61 /**
62 * Called before reading arguments.
63 */
Roger Meier3b771a12010-11-17 22:11:26 +000064 virtual void preRead(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 (void)ctx;
66 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +000067 }
David Reissd7192062010-10-06 17:09:33 +000068
69 /**
70 * Called between reading arguments and calling the handler.
71 */
Roger Meier3b771a12010-11-17 22:11:26 +000072 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073 (void)ctx;
74 (void)fn_name;
75 (void)bytes;
Roger Meier3b771a12010-11-17 22:11:26 +000076 }
David Reissd7192062010-10-06 17:09:33 +000077
78 /**
79 * Called between calling the handler and writing the response.
80 */
Roger Meier3b771a12010-11-17 22:11:26 +000081 virtual void preWrite(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082 (void)ctx;
83 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +000084 }
David Reissd7192062010-10-06 17:09:33 +000085
86 /**
87 * Called after writing the response.
88 */
Roger Meier3b771a12010-11-17 22:11:26 +000089 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010090 (void)ctx;
91 (void)fn_name;
92 (void)bytes;
Roger Meier3b771a12010-11-17 22:11:26 +000093 }
David Reissd7192062010-10-06 17:09:33 +000094
95 /**
96 * Called when an async function call completes successfully.
97 */
Roger Meier3b771a12010-11-17 22:11:26 +000098 virtual void asyncComplete(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010099 (void)ctx;
100 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +0000101 }
David Reissd7192062010-10-06 17:09:33 +0000102
103 /**
104 * Called if the handler throws an undeclared exception.
105 */
Roger Meier3b771a12010-11-17 22:11:26 +0000106 virtual void handlerError(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100107 (void)ctx;
108 (void)fn_name;
Roger Meier3b771a12010-11-17 22:11:26 +0000109 }
David Reissd7192062010-10-06 17:09:33 +0000110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111protected:
David Reissd7192062010-10-06 17:09:33 +0000112 TProcessorEventHandler() {}
113};
114
115/**
David Reiss18cd0f02010-10-06 17:09:39 +0000116 * A helper class used by the generated code to free each context.
117 */
118class TProcessorContextFreer {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100119public:
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 Reiss18cd0f02010-10-06 17:09:39 +0000126 void unregister() { handler_ = NULL; }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127
128private:
David Reiss18cd0f02010-10-06 17:09:39 +0000129 apache::thrift::TProcessorEventHandler* handler_;
130 void* context_;
131 const char* method_;
132};
133
134/**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135 * 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 Slee8d7e1f62006-06-07 06:48:56 +0000140 */
141class TProcessor {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100142public:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000143 virtual ~TProcessor() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000144
James E. King, III82ae9572017-08-05 12:23:54 -0400145 virtual bool process(stdcxx::shared_ptr<protocol::TProtocol> in,
146 stdcxx::shared_ptr<protocol::TProtocol> out,
David Reiss23248712010-10-06 17:10:08 +0000147 void* connectionContext) = 0;
Mark Slee4af6ed72006-10-25 19:02:49 +0000148
James E. King, III82ae9572017-08-05 12:23:54 -0400149 bool process(stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> io, void* connectionContext) {
David Reiss23248712010-10-06 17:10:08 +0000150 return process(io, io, connectionContext);
Mark Slee4af6ed72006-10-25 19:02:49 +0000151 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000152
James E. King, III82ae9572017-08-05 12:23:54 -0400153 stdcxx::shared_ptr<TProcessorEventHandler> getEventHandler() const { return eventHandler_; }
David Reissd7192062010-10-06 17:09:33 +0000154
James E. King, III82ae9572017-08-05 12:23:54 -0400155 void setEventHandler(stdcxx::shared_ptr<TProcessorEventHandler> eventHandler) {
David Reissd7192062010-10-06 17:09:33 +0000156 eventHandler_ = eventHandler;
157 }
158
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100159protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000160 TProcessor() {}
David Reissd7192062010-10-06 17:09:33 +0000161
James E. King, III82ae9572017-08-05 12:23:54 -0400162 stdcxx::shared_ptr<TProcessorEventHandler> eventHandler_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000163};
164
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000165/**
James E. King, III82ae9572017-08-05 12:23:54 -0400166 * This is a helper class to allow stdcxx::shared_ptr to be used with handler
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000167 * 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 Grochowski16a23a62014-11-13 15:33:38 +0100177template <typename HandlerFactory_>
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000178class ReleaseHandler {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100179public:
James E. King, III82ae9572017-08-05 12:23:54 -0400180 ReleaseHandler(const stdcxx::shared_ptr<HandlerFactory_>& handlerFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100181 : handlerFactory_(handlerFactory) {}
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000182
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100183 void operator()(typename HandlerFactory_::Handler* handler) {
184 if (handler) {
185 handlerFactory_->releaseHandler(handler);
186 }
187 }
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000188
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100189private:
James E. King, III82ae9572017-08-05 12:23:54 -0400190 stdcxx::shared_ptr<HandlerFactory_> handlerFactory_;
Bryan Duxbury2173ce02011-09-01 18:00:37 +0000191};
192
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000193struct TConnectionInfo {
194 // The input and output protocols
James E. King, III82ae9572017-08-05 12:23:54 -0400195 stdcxx::shared_ptr<protocol::TProtocol> input;
196 stdcxx::shared_ptr<protocol::TProtocol> output;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000197 // 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, III82ae9572017-08-05 12:23:54 -0400201 stdcxx::shared_ptr<transport::TTransport> transport;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000202};
203
204class TProcessorFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100205public:
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000206 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, III82ae9572017-08-05 12:23:54 -0400215 virtual stdcxx::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) = 0;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000216};
217
218class TSingletonProcessorFactory : public TProcessorFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100219public:
James E. King, III82ae9572017-08-05 12:23:54 -0400220 TSingletonProcessorFactory(stdcxx::shared_ptr<TProcessor> processor) : processor_(processor) {}
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000221
James E. King, III82ae9572017-08-05 12:23:54 -0400222 stdcxx::shared_ptr<TProcessor> getProcessor(const TConnectionInfo&) { return processor_; }
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000223
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100224private:
James E. King, III82ae9572017-08-05 12:23:54 -0400225 stdcxx::shared_ptr<TProcessor> processor_;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000226};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100227}
228} // apache::thrift
Marc Slemko6f038a72006-08-03 18:58:09 +0000229
David Reiss5ddabb82010-10-06 17:09:37 +0000230#endif // #ifndef _THRIFT_TPROCESSOR_H_