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 | */ |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TDEBUGPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TDEBUGPROTOCOL_H_ 1 |
| 22 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 23 | #include "TVirtualProtocol.h" |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 24 | |
| 25 | #include <boost/shared_ptr.hpp> |
| 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { namespace protocol { |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | |
| 31 | !!! EXPERIMENTAL CODE !!! |
| 32 | |
| 33 | This protocol is very much a work in progress. |
| 34 | It doesn't handle many cases properly. |
| 35 | It throws exceptions in many cases. |
| 36 | It probably segfaults in many cases. |
| 37 | Bug reports and feature requests are welcome. |
| 38 | Complaints are not. :R |
| 39 | |
| 40 | */ |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Protocol that prints the payload in a nice human-readable format. |
| 45 | * Reading from this protocol is not supported. |
| 46 | * |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 47 | */ |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 48 | class TDebugProtocol : public TVirtualProtocol<TDebugProtocol> { |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 49 | private: |
David Reiss | 322e595 | 2008-12-05 02:54:09 +0000 | [diff] [blame] | 50 | enum write_state_t |
| 51 | { UNINIT |
| 52 | , STRUCT |
| 53 | , LIST |
| 54 | , SET |
| 55 | , MAP_KEY |
| 56 | , MAP_VALUE |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | public: |
| 60 | TDebugProtocol(boost::shared_ptr<TTransport> trans) |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 61 | : TVirtualProtocol<TDebugProtocol>(trans) |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 62 | , trans_(trans.get()) |
David Reiss | a80f0fb | 2008-04-08 05:06:15 +0000 | [diff] [blame] | 63 | , string_limit_(DEFAULT_STRING_LIMIT) |
| 64 | , string_prefix_size_(DEFAULT_STRING_PREFIX_SIZE) |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 65 | { |
| 66 | write_state_.push_back(UNINIT); |
| 67 | } |
| 68 | |
David Reiss | a80f0fb | 2008-04-08 05:06:15 +0000 | [diff] [blame] | 69 | static const int32_t DEFAULT_STRING_LIMIT = 256; |
| 70 | static const int32_t DEFAULT_STRING_PREFIX_SIZE = 16; |
| 71 | |
| 72 | void setStringSizeLimit(int32_t string_limit) { |
| 73 | string_limit_ = string_limit; |
| 74 | } |
| 75 | |
| 76 | void setStringPrefixSize(int32_t string_prefix_size) { |
| 77 | string_prefix_size_ = string_prefix_size; |
| 78 | } |
| 79 | |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 80 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 81 | uint32_t writeMessageBegin(const std::string& name, |
| 82 | const TMessageType messageType, |
| 83 | const int32_t seqid); |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 84 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 85 | uint32_t writeMessageEnd(); |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 86 | |
| 87 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 88 | uint32_t writeStructBegin(const char* name); |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 89 | |
| 90 | uint32_t writeStructEnd(); |
| 91 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 92 | uint32_t writeFieldBegin(const char* name, |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 93 | const TType fieldType, |
| 94 | const int16_t fieldId); |
| 95 | |
| 96 | uint32_t writeFieldEnd(); |
| 97 | |
| 98 | uint32_t writeFieldStop(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 99 | |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 100 | uint32_t writeMapBegin(const TType keyType, |
| 101 | const TType valType, |
| 102 | const uint32_t size); |
| 103 | |
| 104 | uint32_t writeMapEnd(); |
| 105 | |
| 106 | uint32_t writeListBegin(const TType elemType, |
| 107 | const uint32_t size); |
| 108 | |
| 109 | uint32_t writeListEnd(); |
| 110 | |
| 111 | uint32_t writeSetBegin(const TType elemType, |
| 112 | const uint32_t size); |
| 113 | |
| 114 | uint32_t writeSetEnd(); |
| 115 | |
| 116 | uint32_t writeBool(const bool value); |
| 117 | |
| 118 | uint32_t writeByte(const int8_t byte); |
| 119 | |
| 120 | uint32_t writeI16(const int16_t i16); |
| 121 | |
| 122 | uint32_t writeI32(const int32_t i32); |
| 123 | |
| 124 | uint32_t writeI64(const int64_t i64); |
| 125 | |
| 126 | uint32_t writeDouble(const double dub); |
| 127 | |
| 128 | uint32_t writeString(const std::string& str); |
| 129 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 130 | uint32_t writeBinary(const std::string& str); |
| 131 | |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | void indentUp(); |
| 135 | void indentDown(); |
| 136 | uint32_t writePlain(const std::string& str); |
| 137 | uint32_t writeIndented(const std::string& str); |
| 138 | uint32_t startItem(); |
| 139 | uint32_t endItem(); |
| 140 | uint32_t writeItem(const std::string& str); |
| 141 | |
| 142 | static std::string fieldTypeName(TType type); |
| 143 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 144 | TTransport* trans_; |
| 145 | |
David Reiss | a80f0fb | 2008-04-08 05:06:15 +0000 | [diff] [blame] | 146 | int32_t string_limit_; |
| 147 | int32_t string_prefix_size_; |
| 148 | |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 149 | std::string indent_str_; |
| 150 | static const int indent_inc = 2; |
| 151 | |
| 152 | std::vector<write_state_t> write_state_; |
| 153 | std::vector<int> list_idx_; |
| 154 | }; |
| 155 | |
| 156 | /** |
| 157 | * Constructs debug protocol handlers |
| 158 | */ |
| 159 | class TDebugProtocolFactory : public TProtocolFactory { |
| 160 | public: |
| 161 | TDebugProtocolFactory() {} |
| 162 | virtual ~TDebugProtocolFactory() {} |
| 163 | |
| 164 | boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { |
| 165 | return boost::shared_ptr<TProtocol>(new TDebugProtocol(trans)); |
| 166 | } |
| 167 | |
| 168 | }; |
| 169 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 170 | }}} // apache::thrift::protocol |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 171 | |
| 172 | |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 173 | // TODO(dreiss): Move (part of) ThriftDebugString into a .cpp file and remove this. |
| 174 | #include <transport/TBufferTransports.h> |
| 175 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 176 | namespace apache { namespace thrift { |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 177 | |
| 178 | template<typename ThriftStruct> |
| 179 | std::string ThriftDebugString(const ThriftStruct& ts) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 180 | using namespace apache::thrift::transport; |
| 181 | using namespace apache::thrift::protocol; |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 182 | TMemoryBuffer* buffer = new TMemoryBuffer; |
| 183 | boost::shared_ptr<TTransport> trans(buffer); |
| 184 | TDebugProtocol protocol(trans); |
| 185 | |
| 186 | ts.write(&protocol); |
| 187 | |
| 188 | uint8_t* buf; |
| 189 | uint32_t size; |
| 190 | buffer->getBuffer(&buf, &size); |
| 191 | return std::string((char*)buf, (unsigned int)size); |
| 192 | } |
| 193 | |
| 194 | // TODO(dreiss): This is badly broken. Don't use it unless you are me. |
| 195 | #if 0 |
| 196 | template<typename Object> |
| 197 | std::string DebugString(const std::vector<Object>& vec) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 198 | using namespace apache::thrift::transport; |
| 199 | using namespace apache::thrift::protocol; |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 200 | TMemoryBuffer* buffer = new TMemoryBuffer; |
| 201 | boost::shared_ptr<TTransport> trans(buffer); |
| 202 | TDebugProtocol protocol(trans); |
| 203 | |
| 204 | // I am gross! |
| 205 | protocol.writeStructBegin("SomeRandomVector"); |
| 206 | |
| 207 | // TODO: Fix this with a trait. |
| 208 | protocol.writeListBegin((TType)99, vec.size()); |
| 209 | typename std::vector<Object>::const_iterator it; |
| 210 | for (it = vec.begin(); it != vec.end(); ++it) { |
| 211 | it->write(&protocol); |
| 212 | } |
| 213 | protocol.writeListEnd(); |
| 214 | |
| 215 | uint8_t* buf; |
| 216 | uint32_t size; |
| 217 | buffer->getBuffer(&buf, &size); |
| 218 | return std::string((char*)buf, (unsigned int)size); |
| 219 | } |
| 220 | #endif // 0 |
| 221 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 222 | }} // apache::thrift |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | #endif // #ifndef _THRIFT_PROTOCOL_TDEBUGPROTOCOL_H_ |
| 226 | |
| 227 | |