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> |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 24 | #include <protocol/TProtocol.h> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 25 | #include <boost/shared_ptr.hpp> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 28 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 29 | /** |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 30 | * 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 | */ |
| 36 | class TProcessorEventHandler { |
| 37 | public: |
| 38 | |
| 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) { |
| 48 | (void) fn_name; |
| 49 | (void) serverContext; |
| 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) { |
| 57 | (void) ctx; |
| 58 | (void) fn_name; |
| 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) { |
| 65 | (void) ctx; |
| 66 | (void) fn_name; |
| 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) { |
| 73 | (void) ctx; |
| 74 | (void) fn_name; |
| 75 | (void) bytes; |
| 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) { |
| 82 | (void) ctx; |
| 83 | (void) fn_name; |
| 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) { |
| 90 | (void) ctx; |
| 91 | (void) fn_name; |
| 92 | (void) bytes; |
| 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) { |
| 99 | (void) ctx; |
| 100 | (void) fn_name; |
| 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) { |
| 107 | (void) ctx; |
| 108 | (void) fn_name; |
| 109 | } |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 110 | |
| 111 | protected: |
| 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 { |
| 119 | public: |
| 120 | TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method) : |
| 121 | handler_(handler), context_(context), method_(method) {} |
| 122 | ~TProcessorContextFreer() { if (handler_ != NULL) handler_->freeContext(context_, method_); } |
| 123 | void unregister() { handler_ = NULL; } |
| 124 | private: |
| 125 | apache::thrift::TProcessorEventHandler* handler_; |
| 126 | void* context_; |
| 127 | const char* method_; |
| 128 | }; |
| 129 | |
| 130 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 131 | * A processor is a generic object that acts upon two streams of data, one |
| 132 | * an input and the other an output. The definition of this object is loose, |
| 133 | * though the typical case is for some sort of server that either generates |
| 134 | * responses to an input stream or forwards data from one pipe onto another. |
| 135 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 136 | */ |
| 137 | class TProcessor { |
| 138 | public: |
| 139 | virtual ~TProcessor() {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 140 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 141 | virtual bool process(boost::shared_ptr<protocol::TProtocol> in, |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 142 | boost::shared_ptr<protocol::TProtocol> out, |
| 143 | void* connectionContext) = 0; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 144 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 145 | bool process(boost::shared_ptr<apache::thrift::protocol::TProtocol> io, |
| 146 | void* connectionContext) { |
| 147 | return process(io, io, connectionContext); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 148 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 149 | |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 150 | boost::shared_ptr<TProcessorEventHandler> getEventHandler() { |
| 151 | return eventHandler_; |
| 152 | } |
| 153 | |
| 154 | void setEventHandler(boost::shared_ptr<TProcessorEventHandler> eventHandler) { |
| 155 | eventHandler_ = eventHandler; |
| 156 | } |
| 157 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 158 | protected: |
| 159 | TProcessor() {} |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 160 | |
| 161 | boost::shared_ptr<TProcessorEventHandler> eventHandler_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 164 | }} // apache::thrift |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 165 | |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 166 | #endif // #ifndef _THRIFT_TPROCESSOR_H_ |