blob: 3772bff58c9ce6108fdd910ef563acbb19d479b6 [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 */
David Reisse0e3d1b2008-04-08 05:06:45 +000019
20#ifndef _THRIFT_PROTOCOL_TPROTOCOLTAP_H_
21#define _THRIFT_PROTOCOL_TPROTOCOLTAP_H_ 1
22
David Reiss6806fb82010-10-06 17:09:52 +000023#include <protocol/TVirtualProtocol.h>
David Reisse0e3d1b2008-04-08 05:06:45 +000024
T Jake Lucianib5e62212009-01-31 22:36:20 +000025namespace apache { namespace thrift { namespace protocol {
David Reisse0e3d1b2008-04-08 05:06:45 +000026
T Jake Lucianib5e62212009-01-31 22:36:20 +000027using apache::thrift::transport::TTransport;
David Reisse0e3d1b2008-04-08 05:06:45 +000028
29/**
30 * Puts a wiretap on a protocol object. Any reads to this class are passed
31 * through to an enclosed protocol object, but also mirrored as write to a
32 * second protocol object.
33 *
David Reisse0e3d1b2008-04-08 05:06:45 +000034 */
David Reiss6806fb82010-10-06 17:09:52 +000035class TProtocolTap : public TVirtualProtocol<TProtocolTap> {
David Reisse0e3d1b2008-04-08 05:06:45 +000036 public:
37 TProtocolTap(boost::shared_ptr<TProtocol> source,
38 boost::shared_ptr<TProtocol> sink)
David Reiss6806fb82010-10-06 17:09:52 +000039 : TVirtualProtocol<TProtocolTap>(source->getTransport())
David Reisse0e3d1b2008-04-08 05:06:45 +000040 , source_(source)
41 , sink_(sink)
42 {}
43
David Reiss6806fb82010-10-06 17:09:52 +000044 uint32_t readMessageBegin(std::string& name,
45 TMessageType& messageType,
46 int32_t& seqid) {
David Reisse0e3d1b2008-04-08 05:06:45 +000047 uint32_t rv = source_->readMessageBegin(name, messageType, seqid);
48 sink_->writeMessageBegin(name, messageType, seqid);
49 return rv;
50 }
51
David Reiss6806fb82010-10-06 17:09:52 +000052 uint32_t readMessageEnd() {
David Reisse0e3d1b2008-04-08 05:06:45 +000053 uint32_t rv = source_->readMessageEnd();
54 sink_->writeMessageEnd();
55 return rv;
56 }
57
David Reiss6806fb82010-10-06 17:09:52 +000058 uint32_t readStructBegin(std::string& name) {
David Reisse0e3d1b2008-04-08 05:06:45 +000059 uint32_t rv = source_->readStructBegin(name);
David Reiss64120002008-04-29 23:12:24 +000060 sink_->writeStructBegin(name.c_str());
David Reisse0e3d1b2008-04-08 05:06:45 +000061 return rv;
62 }
63
David Reiss6806fb82010-10-06 17:09:52 +000064 uint32_t readStructEnd() {
David Reisse0e3d1b2008-04-08 05:06:45 +000065 uint32_t rv = source_->readStructEnd();
66 sink_->writeStructEnd();
67 return rv;
68 }
69
David Reiss6806fb82010-10-06 17:09:52 +000070 uint32_t readFieldBegin(std::string& name,
71 TType& fieldType,
72 int16_t& fieldId) {
David Reisse0e3d1b2008-04-08 05:06:45 +000073 uint32_t rv = source_->readFieldBegin(name, fieldType, fieldId);
74 if (fieldType == T_STOP) {
75 sink_->writeFieldStop();
76 } else {
David Reiss64120002008-04-29 23:12:24 +000077 sink_->writeFieldBegin(name.c_str(), fieldType, fieldId);
David Reisse0e3d1b2008-04-08 05:06:45 +000078 }
79 return rv;
80 }
81
82
David Reiss6806fb82010-10-06 17:09:52 +000083 uint32_t readFieldEnd() {
David Reisse0e3d1b2008-04-08 05:06:45 +000084 uint32_t rv = source_->readFieldEnd();
85 sink_->writeFieldEnd();
86 return rv;
87 }
88
David Reiss6806fb82010-10-06 17:09:52 +000089 uint32_t readMapBegin(TType& keyType,
90 TType& valType,
91 uint32_t& size) {
David Reisse0e3d1b2008-04-08 05:06:45 +000092 uint32_t rv = source_->readMapBegin(keyType, valType, size);
93 sink_->writeMapBegin(keyType, valType, size);
94 return rv;
95 }
96
97
David Reiss6806fb82010-10-06 17:09:52 +000098 uint32_t readMapEnd() {
David Reisse0e3d1b2008-04-08 05:06:45 +000099 uint32_t rv = source_->readMapEnd();
100 sink_->writeMapEnd();
101 return rv;
102 }
103
David Reiss6806fb82010-10-06 17:09:52 +0000104 uint32_t readListBegin(TType& elemType, uint32_t& size) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000105 uint32_t rv = source_->readListBegin(elemType, size);
106 sink_->writeListBegin(elemType, size);
107 return rv;
108 }
109
110
David Reiss6806fb82010-10-06 17:09:52 +0000111 uint32_t readListEnd() {
David Reisse0e3d1b2008-04-08 05:06:45 +0000112 uint32_t rv = source_->readListEnd();
113 sink_->writeListEnd();
114 return rv;
115 }
116
David Reiss6806fb82010-10-06 17:09:52 +0000117 uint32_t readSetBegin(TType& elemType, uint32_t& size) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000118 uint32_t rv = source_->readSetBegin(elemType, size);
119 sink_->writeSetBegin(elemType, size);
120 return rv;
121 }
122
123
David Reiss6806fb82010-10-06 17:09:52 +0000124 uint32_t readSetEnd() {
David Reisse0e3d1b2008-04-08 05:06:45 +0000125 uint32_t rv = source_->readSetEnd();
126 sink_->writeSetEnd();
127 return rv;
128 }
129
David Reiss6806fb82010-10-06 17:09:52 +0000130 uint32_t readBool(bool& value) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000131 uint32_t rv = source_->readBool(value);
132 sink_->writeBool(value);
133 return rv;
134 }
135
David Reiss8dfc7322010-10-06 17:09:58 +0000136 // Provide the default readBool() implementation for std::vector<bool>
137 using TVirtualProtocol<TProtocolTap>::readBool;
138
David Reiss6806fb82010-10-06 17:09:52 +0000139 uint32_t readByte(int8_t& byte) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000140 uint32_t rv = source_->readByte(byte);
141 sink_->writeByte(byte);
142 return rv;
143 }
144
David Reiss6806fb82010-10-06 17:09:52 +0000145 uint32_t readI16(int16_t& i16) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000146 uint32_t rv = source_->readI16(i16);
147 sink_->writeI16(i16);
148 return rv;
149 }
150
David Reiss6806fb82010-10-06 17:09:52 +0000151 uint32_t readI32(int32_t& i32) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000152 uint32_t rv = source_->readI32(i32);
153 sink_->writeI32(i32);
154 return rv;
155 }
156
David Reiss6806fb82010-10-06 17:09:52 +0000157 uint32_t readI64(int64_t& i64) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000158 uint32_t rv = source_->readI64(i64);
159 sink_->writeI64(i64);
160 return rv;
161 }
162
David Reiss6806fb82010-10-06 17:09:52 +0000163 uint32_t readDouble(double& dub) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000164 uint32_t rv = source_->readDouble(dub);
165 sink_->writeDouble(dub);
166 return rv;
167 }
168
David Reiss6806fb82010-10-06 17:09:52 +0000169 uint32_t readString(std::string& str) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000170 uint32_t rv = source_->readString(str);
171 sink_->writeString(str);
172 return rv;
173 }
174
David Reiss6806fb82010-10-06 17:09:52 +0000175 uint32_t readBinary(std::string& str) {
David Reisse0e3d1b2008-04-08 05:06:45 +0000176 uint32_t rv = source_->readBinary(str);
177 sink_->writeBinary(str);
178 return rv;
179 }
180
181 private:
182 boost::shared_ptr<TProtocol> source_;
183 boost::shared_ptr<TProtocol> sink_;
184};
185
T Jake Lucianib5e62212009-01-31 22:36:20 +0000186}}} // apache::thrift::protocol
David Reisse0e3d1b2008-04-08 05:06:45 +0000187
188#endif // #define _THRIFT_PROTOCOL_TPROTOCOLTAP_H_ 1