blob: 7858166ee70144c43ab92391e103873ab2565023 [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>
Mark Slee4af6ed72006-10-25 19:02:49 +000024#include <protocol/TProtocol.h>
Marc Slemko16698852006-08-04 03:16:10 +000025#include <boost/shared_ptr.hpp>
Mark Slee8d7e1f62006-06-07 06:48:56 +000026
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace 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 {
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 */
47 virtual void* getContext(const char* fn_name) { return NULL; }
48
49 /**
50 * Expected to free resources associated with a context.
51 */
52 virtual void freeContext(void* ctx, const char* fn_name) { }
53
54 /**
55 * Called before reading arguments.
56 */
57 virtual void preRead(void* ctx, const char* fn_name) {}
58
59 /**
60 * Called between reading arguments and calling the handler.
61 */
David Reissef7200f2010-10-06 17:09:42 +000062 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {}
David Reissd7192062010-10-06 17:09:33 +000063
64 /**
65 * Called between calling the handler and writing the response.
66 */
67 virtual void preWrite(void* ctx, const char* fn_name) {}
68
69 /**
70 * Called after writing the response.
71 */
David Reissef7200f2010-10-06 17:09:42 +000072 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {}
David Reissd7192062010-10-06 17:09:33 +000073
74 /**
75 * Called when an async function call completes successfully.
76 */
77 virtual void asyncComplete(void* ctx, const char* fn_name) {}
78
79 /**
80 * Called if the handler throws an undeclared exception.
81 */
82 virtual void handlerError(void* ctx, const char* fn_name) {}
83
84 protected:
85 TProcessorEventHandler() {}
86};
87
88/**
David Reiss18cd0f02010-10-06 17:09:39 +000089 * A helper class used by the generated code to free each context.
90 */
91class TProcessorContextFreer {
92 public:
93 TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method) :
94 handler_(handler), context_(context), method_(method) {}
95 ~TProcessorContextFreer() { if (handler_ != NULL) handler_->freeContext(context_, method_); }
96 void unregister() { handler_ = NULL; }
97 private:
98 apache::thrift::TProcessorEventHandler* handler_;
99 void* context_;
100 const char* method_;
101};
102
103/**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104 * A processor is a generic object that acts upon two streams of data, one
105 * an input and the other an output. The definition of this object is loose,
106 * though the typical case is for some sort of server that either generates
107 * responses to an input stream or forwards data from one pipe onto another.
108 *
Mark Slee8d7e1f62006-06-07 06:48:56 +0000109 */
110class TProcessor {
111 public:
112 virtual ~TProcessor() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000113
Mark Slee5ea15f92007-03-05 22:55:59 +0000114 virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
115 boost::shared_ptr<protocol::TProtocol> out) = 0;
Mark Slee4af6ed72006-10-25 19:02:49 +0000116
T Jake Lucianib5e62212009-01-31 22:36:20 +0000117 bool process(boost::shared_ptr<apache::thrift::protocol::TProtocol> io) {
Mark Slee4af6ed72006-10-25 19:02:49 +0000118 return process(io, io);
119 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000120
David Reissd7192062010-10-06 17:09:33 +0000121 boost::shared_ptr<TProcessorEventHandler> getEventHandler() {
122 return eventHandler_;
123 }
124
125 void setEventHandler(boost::shared_ptr<TProcessorEventHandler> eventHandler) {
126 eventHandler_ = eventHandler;
127 }
128
Mark Slee8d7e1f62006-06-07 06:48:56 +0000129 protected:
130 TProcessor() {}
David Reissd7192062010-10-06 17:09:33 +0000131
132 boost::shared_ptr<TProcessorEventHandler> eventHandler_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000133};
134
T Jake Lucianib5e62212009-01-31 22:36:20 +0000135}} // apache::thrift
Marc Slemko6f038a72006-08-03 18:58:09 +0000136
David Reiss5ddabb82010-10-06 17:09:37 +0000137#endif // #ifndef _THRIFT_TPROCESSOR_H_