blob: f98751bb1c394193a7ffa60be18811eb01a65841 [file] [log] [blame]
Jake Farrell23bf35d2011-12-08 02:02:20 +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 */
19#ifndef _THRIFT_TDISPATCHPROCESSOR_H_
20#define _THRIFT_TDISPATCHPROCESSOR_H_ 1
21
22#include "TProcessor.h"
23
24namespace apache { namespace thrift {
25
26/**
27 * TDispatchProcessor is a helper class to parse the message header then call
28 * another function to dispatch based on the function name.
29 *
30 * Subclasses must implement dispatchCall() to dispatch on the function name.
31 */
32template <class Protocol_>
33class TDispatchProcessorT : public TProcessor {
34 public:
35 virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
36 boost::shared_ptr<protocol::TProtocol> out,
37 void* connectionContext) {
38 protocol::TProtocol* inRaw = in.get();
39 protocol::TProtocol* outRaw = out.get();
40
41 // Try to dynamic cast to the template protocol type
42 Protocol_* specificIn = dynamic_cast<Protocol_*>(inRaw);
43 Protocol_* specificOut = dynamic_cast<Protocol_*>(outRaw);
44 if (specificIn && specificOut) {
45 return processFast(specificIn, specificOut, connectionContext);
46 }
47
48 // Log the fact that we have to use the slow path
49 T_GENERIC_PROTOCOL(this, inRaw, specificIn);
50 T_GENERIC_PROTOCOL(this, outRaw, specificOut);
51
52 std::string fname;
53 protocol::TMessageType mtype;
54 int32_t seqid;
55 inRaw->readMessageBegin(fname, mtype, seqid);
56
57 // If this doesn't look like a valid call, log an error and return false so
58 // that the server will close the connection.
59 //
60 // (The old generated processor code used to try to skip a T_STRUCT and
61 // continue. However, that seems unsafe.)
62 if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
63 GlobalOutput.printf("received invalid message type %d from client",
64 mtype);
65 return false;
66 }
67
68 return this->dispatchCall(inRaw, outRaw, fname, seqid, connectionContext);
69 }
70
71 protected:
72 bool processFast(Protocol_* in, Protocol_* out, void* connectionContext) {
73 std::string fname;
74 protocol::TMessageType mtype;
75 int32_t seqid;
76 in->readMessageBegin(fname, mtype, seqid);
77
78 if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
79 GlobalOutput.printf("received invalid message type %d from client",
80 mtype);
81 return false;
82 }
83
84 return this->dispatchCallTemplated(in, out, fname,
85 seqid, connectionContext);
86 }
87
88 /**
89 * dispatchCall() methods must be implemented by subclasses
90 */
91 virtual bool dispatchCall(apache::thrift::protocol::TProtocol* in,
92 apache::thrift::protocol::TProtocol* out,
93 const std::string& fname, int32_t seqid,
94 void* callContext) = 0;
95
96 virtual bool dispatchCallTemplated(Protocol_* in, Protocol_* out,
97 const std::string& fname, int32_t seqid,
98 void* callContext) = 0;
99};
100
101/**
102 * Non-templatized version of TDispatchProcessor, that doesn't bother trying to
103 * perform a dynamic_cast.
104 */
105class TDispatchProcessor : public TProcessor {
106 public:
107 virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
108 boost::shared_ptr<protocol::TProtocol> out,
109 void* connectionContext) {
110 std::string fname;
111 protocol::TMessageType mtype;
112 int32_t seqid;
113 in->readMessageBegin(fname, mtype, seqid);
114
115 if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
116 GlobalOutput.printf("received invalid message type %d from client",
117 mtype);
118 return false;
119 }
120
121 return dispatchCall(in.get(), out.get(), fname, seqid, connectionContext);
122 }
123
124 protected:
125 virtual bool dispatchCall(apache::thrift::protocol::TProtocol* in,
126 apache::thrift::protocol::TProtocol* out,
127 const std::string& fname, int32_t seqid,
128 void* callContext) = 0;
129};
130
131// Specialize TDispatchProcessorT for TProtocol and TDummyProtocol just to use
132// the generic TDispatchProcessor.
133template <>
134class TDispatchProcessorT<protocol::TDummyProtocol> :
135 public TDispatchProcessor {};
136template <>
137class TDispatchProcessorT<protocol::TProtocol> :
138 public TDispatchProcessor {};
139
140}} // apache::thrift
141
142#endif // _THRIFT_TDISPATCHPROCESSOR_H_