Jake Farrell | 23bf35d | 2011-12-08 02:02:20 +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 | */ |
| 19 | #ifndef _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ |
| 20 | #define _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ 1 |
| 21 | |
| 22 | #include "TAsyncProcessor.h" |
| 23 | |
| 24 | namespace apache { namespace thrift { namespace async { |
| 25 | |
| 26 | /** |
| 27 | * TAsyncDispatchProcessor is a helper class to parse the message header then |
| 28 | * call another function to dispatch based on the function name. |
| 29 | * |
| 30 | * Subclasses must implement dispatchCall() to dispatch on the function name. |
| 31 | */ |
| 32 | template <class Protocol_> |
| 33 | class TAsyncDispatchProcessorT : public TAsyncProcessor { |
| 34 | public: |
| 35 | virtual void process(std::tr1::function<void(bool success)> _return, |
| 36 | boost::shared_ptr<protocol::TProtocol> in, |
| 37 | boost::shared_ptr<protocol::TProtocol> out) { |
| 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(_return, specificIn, specificOut); |
| 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 | return; |
| 67 | } |
| 68 | |
| 69 | return this->dispatchCall(_return, inRaw, outRaw, fname, seqid); |
| 70 | } |
| 71 | |
| 72 | void processFast(std::tr1::function<void(bool success)> _return, |
| 73 | Protocol_* in, Protocol_* out) { |
| 74 | std::string fname; |
| 75 | protocol::TMessageType mtype; |
| 76 | int32_t seqid; |
| 77 | in->readMessageBegin(fname, mtype, seqid); |
| 78 | |
| 79 | if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) { |
| 80 | GlobalOutput.printf("received invalid message type %d from client", |
| 81 | mtype); |
| 82 | _return(false); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | return this->dispatchCallTemplated(_return, in, out, fname, seqid); |
| 87 | } |
| 88 | |
| 89 | virtual void dispatchCall(std::tr1::function<void(bool ok)> _return, |
| 90 | apache::thrift::protocol::TProtocol* in, |
| 91 | apache::thrift::protocol::TProtocol* out, |
| 92 | const std::string& fname, int32_t seqid) = 0; |
| 93 | |
| 94 | virtual void dispatchCallTemplated(std::tr1::function<void(bool ok)> _return, |
| 95 | Protocol_* in, Protocol_* out, |
| 96 | const std::string& fname, |
| 97 | int32_t seqid) = 0; |
| 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * Non-templatized version of TAsyncDispatchProcessor, |
| 102 | * that doesn't bother trying to perform a dynamic_cast. |
| 103 | */ |
| 104 | class TAsyncDispatchProcessor : public TAsyncProcessor { |
| 105 | public: |
| 106 | virtual void process(std::tr1::function<void(bool success)> _return, |
| 107 | boost::shared_ptr<protocol::TProtocol> in, |
| 108 | boost::shared_ptr<protocol::TProtocol> out) { |
| 109 | protocol::TProtocol* inRaw = in.get(); |
| 110 | protocol::TProtocol* outRaw = out.get(); |
| 111 | |
| 112 | std::string fname; |
| 113 | protocol::TMessageType mtype; |
| 114 | int32_t seqid; |
| 115 | inRaw->readMessageBegin(fname, mtype, seqid); |
| 116 | |
| 117 | // If this doesn't look like a valid call, log an error and return false so |
| 118 | // that the server will close the connection. |
| 119 | // |
| 120 | // (The old generated processor code used to try to skip a T_STRUCT and |
| 121 | // continue. However, that seems unsafe.) |
| 122 | if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) { |
| 123 | GlobalOutput.printf("received invalid message type %d from client", |
| 124 | mtype); |
| 125 | _return(false); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | return dispatchCall(_return, inRaw, outRaw, fname, seqid); |
| 130 | } |
| 131 | |
| 132 | virtual void dispatchCall(std::tr1::function<void(bool ok)> _return, |
| 133 | apache::thrift::protocol::TProtocol* in, |
| 134 | apache::thrift::protocol::TProtocol* out, |
| 135 | const std::string& fname, int32_t seqid) = 0; |
| 136 | }; |
| 137 | |
| 138 | // Specialize TAsyncDispatchProcessorT for TProtocol and TDummyProtocol just to |
| 139 | // use the generic TDispatchProcessor. |
| 140 | template <> |
| 141 | class TAsyncDispatchProcessorT<protocol::TDummyProtocol> : |
| 142 | public TAsyncDispatchProcessor {}; |
| 143 | template <> |
| 144 | class TAsyncDispatchProcessorT<protocol::TProtocol> : |
| 145 | public TAsyncDispatchProcessor {}; |
| 146 | |
| 147 | }}} // apache::thrift::async |
| 148 | |
| 149 | #endif // _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ |