http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/processor/TMultiplexedProcessor.h ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/processor/TMultiplexedProcessor.h b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/processor/TMultiplexedProcessor.h deleted file mode 100644 index 494ec10..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/processor/TMultiplexedProcessor.h +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef THRIFT_TMULTIPLEXEDPROCESSOR_H_ -#define THRIFT_TMULTIPLEXEDPROCESSOR_H_ 1 - -#include <thrift/protocol/TProtocolDecorator.h> -#include <thrift/TApplicationException.h> -#include <thrift/TProcessor.h> -#include <boost/tokenizer.hpp> - -namespace apache -{ - namespace thrift - { - using boost::shared_ptr; - - namespace protocol { - - /** - * To be able to work with any protocol, we needed - * to allow them to call readMessageBegin() and get a TMessage in exactly - * the standard format, without the service name prepended to TMessage.name. - */ - class StoredMessageProtocol : public TProtocolDecorator - { - public: - StoredMessageProtocol( shared_ptr<protocol::TProtocol> _protocol, - const std::string& _name, const TMessageType _type, - const int32_t _seqid) : - TProtocolDecorator(_protocol), - name(_name), - type(_type), - seqid(_seqid) - { - } - - uint32_t readMessageBegin_virt(std::string& _name, TMessageType& _type, int32_t& _seqid) - { - - _name = name; - _type = type; - _seqid = seqid; - - return 0; // (Normal TProtocol read functions return number of bytes read) - } - - std::string name; - TMessageType type; - int32_t seqid; - }; - } //namespace protocol - - /** - * <code>TMultiplexedProcessor</code> is a <code>TProcessor</code> allowing - * a single <code>TServer</code> to provide multiple services. - * - * <p>To do so, you instantiate the processor and then register additional - * processors with it, as shown in the following example:</p> - * - * <blockquote><code> - * shared_ptr<TMultiplexedProcessor> processor(new TMultiplexedProcessor()); - * - * processor->registerProcessor( - * "Calculator", - * shared_ptr<TProcessor>( new CalculatorProcessor( - * shared_ptr<CalculatorHandler>( new CalculatorHandler())))); - * - * processor->registerProcessor( - * "WeatherReport", - * shared_ptr<TProcessor>( new WeatherReportProcessor( - * shared_ptr<WeatherReportHandler>( new WeatherReportHandler())))); - * - * shared_ptr<TServerTransport> transport(new TServerSocket(9090)); - * TSimpleServer server(processor, transport); - * - * server.serve(); - * </code></blockquote> - */ - class TMultiplexedProcessor : public TProcessor - { - public: - typedef std::map< std::string, shared_ptr<TProcessor> > services_t; - - /** - * 'Register' a service with this <code>TMultiplexedProcessor</code>. This - * allows us to broker requests to individual services by using the service - * name to select them at request time. - * - * \param [in] serviceName Name of a service, has to be identical to the name - * declared in the Thrift IDL, e.g. "WeatherReport". - * \param [in] processor Implementation of a service, ususally referred to - * as "handlers", e.g. WeatherReportHandler, - * implementing WeatherReportIf interface. - */ - void registerProcessor( const std::string & serviceName, - shared_ptr<TProcessor> processor ) - { - services[serviceName] = processor; - } - - /** - * This implementation of <code>process</code> performs the following steps: - * - * <ol> - * <li>Read the beginning of the message.</li> - * <li>Extract the service name from the message.</li> - * <li>Using the service name to locate the appropriate processor.</li> - * <li>Dispatch to the processor, with a decorated instance of TProtocol - * that allows readMessageBegin() to return the original TMessage.</li> - * </ol> - * - * \throws TException If the message type is not T_CALL or T_ONEWAY, if - * the service name was not found in the message, or if the service - * name was not found in the service map. - */ - bool process( shared_ptr<protocol::TProtocol> in, - shared_ptr<protocol::TProtocol> out, - void *connectionContext) - { - std::string name; - protocol::TMessageType type; - int32_t seqid; - - // Use the actual underlying protocol (e.g. TBinaryProtocol) to read the - // message header. This pulls the message "off the wire", which we'll - // deal with at the end of this method. - in->readMessageBegin(name, type, seqid); - - if( type != protocol::T_CALL && type != protocol::T_ONEWAY ) { - // Unexpected message type. - in->skip(::apache::thrift::protocol::T_STRUCT); - in->readMessageEnd(); - in->getTransport()->readEnd(); - const std::string msg("TMultiplexedProcessor: Unexpected message type"); - ::apache::thrift::TApplicationException x( - ::apache::thrift::TApplicationException::PROTOCOL_ERROR, - msg); - out->writeMessageBegin(name, ::apache::thrift::protocol::T_EXCEPTION, seqid); - x.write(out.get()); - out->writeMessageEnd(); - out->getTransport()->writeEnd(); - out->getTransport()->flush(); - throw TException(msg); - } - - // Extract the service name - - boost::tokenizer<boost::char_separator<char> > tok( name, boost::char_separator<char>(":") ); - - std::vector<std::string> tokens; - std::copy( tok.begin(), tok.end(), std::back_inserter(tokens) ); - - // A valid message should consist of two tokens: the service - // name and the name of the method to call. - if( tokens.size() == 2 ) - { - // Search for a processor associated with this service name. - services_t::iterator it = services.find(tokens[0]); - - if( it != services.end() ) - { - shared_ptr<TProcessor> processor = it->second; - // Let the processor registered for this service name - // process the message. - return processor->process( - shared_ptr<protocol::TProtocol>( - new protocol::StoredMessageProtocol( in, tokens[1], type, seqid ) ), - out, connectionContext ); - } - else - { - // Unknown service. - in->skip(::apache::thrift::protocol::T_STRUCT); - in->readMessageEnd(); - in->getTransport()->readEnd(); - - std::string msg("TMultiplexedProcessor: Unknown service: "); - msg += tokens[0]; - ::apache::thrift::TApplicationException x( - ::apache::thrift::TApplicationException::PROTOCOL_ERROR, - msg); - out->writeMessageBegin(name, ::apache::thrift::protocol::T_EXCEPTION, seqid); - x.write(out.get()); - out->writeMessageEnd(); - out->getTransport()->writeEnd(); - out->getTransport()->flush(); - msg += ". Did you forget to call registerProcessor()?"; - throw TException(msg); - } - } - return false; - } - - private: - /** Map of service processor objects, indexed by service names. */ - services_t services; - }; - } -} - -#endif // THRIFT_TMULTIPLEXEDPROCESSOR_H_
http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.cpp ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.cpp b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.cpp deleted file mode 100644 index cd343ed..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include <thrift/protocol/TBase64Utils.h> - -#include <boost/static_assert.hpp> - -using std::string; - -namespace apache { namespace thrift { namespace protocol { - - -static const uint8_t *kBase64EncodeTable = (const uint8_t *) - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -void base64_encode(const uint8_t *in, uint32_t len, uint8_t *buf) { - buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3f]; - if (len == 3) { - buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)]; - buf[2] = kBase64EncodeTable[((in[1] << 2) & 0x3c) | ((in[2] >> 6) & 0x03)]; - buf[3] = kBase64EncodeTable[in[2] & 0x3f]; - } else if (len == 2) { - buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)]; - buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3c]; - } else { // len == 1 - buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x30]; - } -} - -static const uint8_t kBase64DecodeTable[256] ={ -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e,0xff,0xff,0xff,0x3f, -0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e, -0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xff,0xff,0xff,0xff,0xff, -0xff,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, -0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, -}; - -void base64_decode(uint8_t *buf, uint32_t len) { - buf[0] = (kBase64DecodeTable[buf[0]] << 2) | - (kBase64DecodeTable[buf[1]] >> 4); - if (len > 2) { - buf[1] = ((kBase64DecodeTable[buf[1]] << 4) & 0xf0) | - (kBase64DecodeTable[buf[2]] >> 2); - if (len > 3) { - buf[2] = ((kBase64DecodeTable[buf[2]] << 6) & 0xc0) | - (kBase64DecodeTable[buf[3]]); - } - } -} - - -}}} // apache::thrift::protocol http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.h ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.h b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.h deleted file mode 100644 index 3def733..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBase64Utils.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef _THRIFT_PROTOCOL_TBASE64UTILS_H_ -#define _THRIFT_PROTOCOL_TBASE64UTILS_H_ - -#include <stdint.h> -#include <string> - -namespace apache { namespace thrift { namespace protocol { - -// in must be at least len bytes -// len must be 1, 2, or 3 -// buf must be a buffer of at least 4 bytes and may not overlap in -// the data is not padded with '='; the caller can do this if desired -void base64_encode(const uint8_t *in, uint32_t len, uint8_t *buf); - -// buf must be a buffer of at least 4 bytes and contain base64 encoded values -// buf will be changed to contain output bytes -// len is number of bytes to consume from input (must be 2, 3, or 4) -// no '=' padding should be included in the input -void base64_decode(uint8_t *buf, uint32_t len); - -}}} // apache::thrift::protocol - -#endif // #define _THRIFT_PROTOCOL_TBASE64UTILS_H_ http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.h ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.h b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.h deleted file mode 100644 index a5b19e5..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.h +++ /dev/null @@ -1,282 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ -#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1 - -#include <thrift/protocol/TProtocol.h> -#include <thrift/protocol/TVirtualProtocol.h> - -#include <boost/shared_ptr.hpp> - -namespace apache { namespace thrift { namespace protocol { - -/** - * The default binary protocol for thrift. Writes all data in a very basic - * binary format, essentially just spitting out the raw bytes. - * - */ -template <class Transport_> -class TBinaryProtocolT - : public TVirtualProtocol< TBinaryProtocolT<Transport_> > { - protected: - static const int32_t VERSION_MASK = ((int32_t)0xffff0000); - static const int32_t VERSION_1 = ((int32_t)0x80010000); - // VERSION_2 (0x80020000) is taken by TDenseProtocol. - - public: - TBinaryProtocolT(boost::shared_ptr<Transport_> trans) : - TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans), - trans_(trans.get()), - string_limit_(0), - container_limit_(0), - strict_read_(false), - strict_write_(true), - string_buf_(NULL), - string_buf_size_(0) {} - - TBinaryProtocolT(boost::shared_ptr<Transport_> trans, - int32_t string_limit, - int32_t container_limit, - bool strict_read, - bool strict_write) : - TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans), - trans_(trans.get()), - string_limit_(string_limit), - container_limit_(container_limit), - strict_read_(strict_read), - strict_write_(strict_write), - string_buf_(NULL), - string_buf_size_(0) {} - - ~TBinaryProtocolT() { - if (string_buf_ != NULL) { - std::free(string_buf_); - string_buf_size_ = 0; - } - } - - void setStringSizeLimit(int32_t string_limit) { - string_limit_ = string_limit; - } - - void setContainerSizeLimit(int32_t container_limit) { - container_limit_ = container_limit; - } - - void setStrict(bool strict_read, bool strict_write) { - strict_read_ = strict_read; - strict_write_ = strict_write; - } - - /** - * Writing functions. - */ - - /*ol*/ uint32_t writeMessageBegin(const std::string& name, - const TMessageType messageType, - const int32_t seqid); - - /*ol*/ uint32_t writeMessageEnd(); - - - inline uint32_t writeStructBegin(const char* name); - - inline uint32_t writeStructEnd(); - - inline uint32_t writeFieldBegin(const char* name, - const TType fieldType, - const int16_t fieldId); - - inline uint32_t writeFieldEnd(); - - inline uint32_t writeFieldStop(); - - inline uint32_t writeMapBegin(const TType keyType, - const TType valType, - const uint32_t size); - - inline uint32_t writeMapEnd(); - - inline uint32_t writeListBegin(const TType elemType, const uint32_t size); - - inline uint32_t writeListEnd(); - - inline uint32_t writeSetBegin(const TType elemType, const uint32_t size); - - inline uint32_t writeSetEnd(); - - inline uint32_t writeBool(const bool value); - - inline uint32_t writeByte(const int8_t byte); - - inline uint32_t writeI16(const int16_t i16); - - inline uint32_t writeI32(const int32_t i32); - - inline uint32_t writeI64(const int64_t i64); - - inline uint32_t writeDouble(const double dub); - - template <typename StrType> - inline uint32_t writeString(const StrType& str); - - inline uint32_t writeBinary(const std::string& str); - - /** - * Reading functions - */ - - - /*ol*/ uint32_t readMessageBegin(std::string& name, - TMessageType& messageType, - int32_t& seqid); - - /*ol*/ uint32_t readMessageEnd(); - - inline uint32_t readStructBegin(std::string& name); - - inline uint32_t readStructEnd(); - - inline uint32_t readFieldBegin(std::string& name, - TType& fieldType, - int16_t& fieldId); - - inline uint32_t readFieldEnd(); - - inline uint32_t readMapBegin(TType& keyType, - TType& valType, - uint32_t& size); - - inline uint32_t readMapEnd(); - - inline uint32_t readListBegin(TType& elemType, uint32_t& size); - - inline uint32_t readListEnd(); - - inline uint32_t readSetBegin(TType& elemType, uint32_t& size); - - inline uint32_t readSetEnd(); - - inline uint32_t readBool(bool& value); - // Provide the default readBool() implementation for std::vector<bool> - using TVirtualProtocol< TBinaryProtocolT<Transport_> >::readBool; - - inline uint32_t readByte(int8_t& byte); - - inline uint32_t readI16(int16_t& i16); - - inline uint32_t readI32(int32_t& i32); - - inline uint32_t readI64(int64_t& i64); - - inline uint32_t readDouble(double& dub); - - template<typename StrType> - inline uint32_t readString(StrType& str); - - inline uint32_t readBinary(std::string& str); - - protected: - template<typename StrType> - uint32_t readStringBody(StrType& str, int32_t sz); - - Transport_* trans_; - - int32_t string_limit_; - int32_t container_limit_; - - // Enforce presence of version identifier - bool strict_read_; - bool strict_write_; - - // Buffer for reading strings, save for the lifetime of the protocol to - // avoid memory churn allocating memory on every string read - uint8_t* string_buf_; - int32_t string_buf_size_; - -}; - -typedef TBinaryProtocolT<TTransport> TBinaryProtocol; - -/** - * Constructs binary protocol handlers - */ -template <class Transport_> -class TBinaryProtocolFactoryT : public TProtocolFactory { - public: - TBinaryProtocolFactoryT() : - string_limit_(0), - container_limit_(0), - strict_read_(false), - strict_write_(true) {} - - TBinaryProtocolFactoryT(int32_t string_limit, int32_t container_limit, - bool strict_read, bool strict_write) : - string_limit_(string_limit), - container_limit_(container_limit), - strict_read_(strict_read), - strict_write_(strict_write) {} - - virtual ~TBinaryProtocolFactoryT() {} - - void setStringSizeLimit(int32_t string_limit) { - string_limit_ = string_limit; - } - - void setContainerSizeLimit(int32_t container_limit) { - container_limit_ = container_limit; - } - - void setStrict(bool strict_read, bool strict_write) { - strict_read_ = strict_read; - strict_write_ = strict_write; - } - - boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { - boost::shared_ptr<Transport_> specific_trans = - boost::dynamic_pointer_cast<Transport_>(trans); - TProtocol* prot; - if (specific_trans) { - prot = new TBinaryProtocolT<Transport_>(specific_trans, string_limit_, - container_limit_, strict_read_, - strict_write_); - } else { - prot = new TBinaryProtocol(trans, string_limit_, container_limit_, - strict_read_, strict_write_); - } - - return boost::shared_ptr<TProtocol>(prot); - } - - private: - int32_t string_limit_; - int32_t container_limit_; - bool strict_read_; - bool strict_write_; - -}; - -typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory; - -}}} // apache::thrift::protocol - -#include <thrift/protocol/TBinaryProtocol.tcc> - -#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.tcc ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.tcc b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.tcc deleted file mode 100644 index 54d79b7..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TBinaryProtocol.tcc +++ /dev/null @@ -1,465 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ -#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ 1 - -#include <thrift/protocol/TBinaryProtocol.h> - -#include <limits> - - -namespace apache { namespace thrift { namespace protocol { - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeMessageBegin(const std::string& name, - const TMessageType messageType, - const int32_t seqid) { - if (this->strict_write_) { - int32_t version = (VERSION_1) | ((int32_t)messageType); - uint32_t wsize = 0; - wsize += writeI32(version); - wsize += writeString(name); - wsize += writeI32(seqid); - return wsize; - } else { - uint32_t wsize = 0; - wsize += writeString(name); - wsize += writeByte((int8_t)messageType); - wsize += writeI32(seqid); - return wsize; - } -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeMessageEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeStructBegin(const char* name) { - (void) name; - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeStructEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeFieldBegin(const char* name, - const TType fieldType, - const int16_t fieldId) { - (void) name; - uint32_t wsize = 0; - wsize += writeByte((int8_t)fieldType); - wsize += writeI16(fieldId); - return wsize; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeFieldEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeFieldStop() { - return - writeByte((int8_t)T_STOP); -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeMapBegin(const TType keyType, - const TType valType, - const uint32_t size) { - uint32_t wsize = 0; - wsize += writeByte((int8_t)keyType); - wsize += writeByte((int8_t)valType); - wsize += writeI32((int32_t)size); - return wsize; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeMapEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeListBegin(const TType elemType, - const uint32_t size) { - uint32_t wsize = 0; - wsize += writeByte((int8_t) elemType); - wsize += writeI32((int32_t)size); - return wsize; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeListEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeSetBegin(const TType elemType, - const uint32_t size) { - uint32_t wsize = 0; - wsize += writeByte((int8_t)elemType); - wsize += writeI32((int32_t)size); - return wsize; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeSetEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeBool(const bool value) { - uint8_t tmp = value ? 1 : 0; - this->trans_->write(&tmp, 1); - return 1; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeByte(const int8_t byte) { - this->trans_->write((uint8_t*)&byte, 1); - return 1; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeI16(const int16_t i16) { - int16_t net = (int16_t)htons(i16); - this->trans_->write((uint8_t*)&net, 2); - return 2; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeI32(const int32_t i32) { - int32_t net = (int32_t)htonl(i32); - this->trans_->write((uint8_t*)&net, 4); - return 4; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeI64(const int64_t i64) { - int64_t net = (int64_t)htonll(i64); - this->trans_->write((uint8_t*)&net, 8); - return 8; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeDouble(const double dub) { - BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); - BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); - - uint64_t bits = bitwise_cast<uint64_t>(dub); - bits = htonll(bits); - this->trans_->write((uint8_t*)&bits, 8); - return 8; -} - - -template <class Transport_> -template<typename StrType> -uint32_t TBinaryProtocolT<Transport_>::writeString(const StrType& str) { - if(str.size() > static_cast<size_t>((std::numeric_limits<int32_t>::max)())) - throw TProtocolException(TProtocolException::SIZE_LIMIT); - uint32_t size = static_cast<uint32_t>(str.size()); - uint32_t result = writeI32((int32_t)size); - if (size > 0) { - this->trans_->write((uint8_t*)str.data(), size); - } - return result + size; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::writeBinary(const std::string& str) { - return TBinaryProtocolT<Transport_>::writeString(str); -} - -/** - * Reading functions - */ - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readMessageBegin(std::string& name, - TMessageType& messageType, - int32_t& seqid) { - uint32_t result = 0; - int32_t sz; - result += readI32(sz); - - if (sz < 0) { - // Check for correct version number - int32_t version = sz & VERSION_MASK; - if (version != VERSION_1) { - throw TProtocolException(TProtocolException::BAD_VERSION, "Bad version identifier"); - } - messageType = (TMessageType)(sz & 0x000000ff); - result += readString(name); - result += readI32(seqid); - } else { - if (this->strict_read_) { - throw TProtocolException(TProtocolException::BAD_VERSION, "No version identifier... old protocol client in strict mode?"); - } else { - // Handle pre-versioned input - int8_t type; - result += readStringBody(name, sz); - result += readByte(type); - messageType = (TMessageType)type; - result += readI32(seqid); - } - } - return result; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readMessageEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readStructBegin(std::string& name) { - name = ""; - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readStructEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readFieldBegin(std::string& name, - TType& fieldType, - int16_t& fieldId) { - (void) name; - uint32_t result = 0; - int8_t type; - result += readByte(type); - fieldType = (TType)type; - if (fieldType == T_STOP) { - fieldId = 0; - return result; - } - result += readI16(fieldId); - return result; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readFieldEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readMapBegin(TType& keyType, - TType& valType, - uint32_t& size) { - int8_t k, v; - uint32_t result = 0; - int32_t sizei; - result += readByte(k); - keyType = (TType)k; - result += readByte(v); - valType = (TType)v; - result += readI32(sizei); - if (sizei < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } else if (this->container_limit_ && sizei > this->container_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - size = (uint32_t)sizei; - return result; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readMapEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readListBegin(TType& elemType, - uint32_t& size) { - int8_t e; - uint32_t result = 0; - int32_t sizei; - result += readByte(e); - elemType = (TType)e; - result += readI32(sizei); - if (sizei < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } else if (this->container_limit_ && sizei > this->container_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - size = (uint32_t)sizei; - return result; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readListEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readSetBegin(TType& elemType, - uint32_t& size) { - int8_t e; - uint32_t result = 0; - int32_t sizei; - result += readByte(e); - elemType = (TType)e; - result += readI32(sizei); - if (sizei < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } else if (this->container_limit_ && sizei > this->container_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - size = (uint32_t)sizei; - return result; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readSetEnd() { - return 0; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readBool(bool& value) { - uint8_t b[1]; - this->trans_->readAll(b, 1); - value = *(int8_t*)b != 0; - return 1; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readByte(int8_t& byte) { - uint8_t b[1]; - this->trans_->readAll(b, 1); - byte = *(int8_t*)b; - return 1; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readI16(int16_t& i16) { - union bytes { - uint8_t b[2]; - int16_t all; - } theBytes; - this->trans_->readAll(theBytes.b, 2); - i16 = (int16_t)ntohs(theBytes.all); - return 2; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readI32(int32_t& i32) { - union bytes { - uint8_t b[4]; - int32_t all; - } theBytes; - this->trans_->readAll(theBytes.b, 4); - i32 = (int32_t)ntohl(theBytes.all); - return 4; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readI64(int64_t& i64) { - union bytes { - uint8_t b[8]; - int64_t all; - } theBytes; - this->trans_->readAll(theBytes.b, 8); - i64 = (int64_t)ntohll(theBytes.all); - return 8; -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readDouble(double& dub) { - BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); - BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); - - union bytes { - uint8_t b[8]; - uint64_t all; - } theBytes; - this->trans_->readAll(theBytes.b, 8); - theBytes.all = ntohll(theBytes.all); - dub = bitwise_cast<double>(theBytes.all); - return 8; -} - -template <class Transport_> -template<typename StrType> -uint32_t TBinaryProtocolT<Transport_>::readString(StrType& str) { - uint32_t result; - int32_t size; - result = readI32(size); - return result + readStringBody(str, size); -} - -template <class Transport_> -uint32_t TBinaryProtocolT<Transport_>::readBinary(std::string& str) { - return TBinaryProtocolT<Transport_>::readString(str); -} - -template <class Transport_> -template<typename StrType> -uint32_t TBinaryProtocolT<Transport_>::readStringBody(StrType& str, - int32_t size) { - uint32_t result = 0; - - // Catch error cases - if (size < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } - if (this->string_limit_ > 0 && size > this->string_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - - // Catch empty string case - if (size == 0) { - str.clear(); - return result; - } - - // Try to borrow first - const uint8_t* borrow_buf; - uint32_t got = size; - if ((borrow_buf = this->trans_->borrow(NULL, &got))) { - str.assign((const char*)borrow_buf, size); - this->trans_->consume(size); - return size; - } - - // Use the heap here to prevent stack overflow for v. large strings - if (size > this->string_buf_size_ || this->string_buf_ == NULL) { - void* new_string_buf = std::realloc(this->string_buf_, (uint32_t)size); - if (new_string_buf == NULL) { - throw std::bad_alloc(); - } - this->string_buf_ = (uint8_t*)new_string_buf; - this->string_buf_size_ = size; - } - this->trans_->readAll(this->string_buf_, size); - str.assign((char*)this->string_buf_, size); - return (uint32_t)size; -} - -}}} // apache::thrift::protocol - -#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.h ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.h b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.h deleted file mode 100644 index d6da745..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.h +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ -#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ 1 - -#include <thrift/protocol/TVirtualProtocol.h> - -#include <stack> -#include <boost/shared_ptr.hpp> - -namespace apache { namespace thrift { namespace protocol { - -/** - * C++ Implementation of the Compact Protocol as described in THRIFT-110 - */ -template <class Transport_> -class TCompactProtocolT - : public TVirtualProtocol< TCompactProtocolT<Transport_> > { - - protected: - static const int8_t PROTOCOL_ID = (int8_t)0x82u; - static const int8_t VERSION_N = 1; - static const int8_t VERSION_MASK = 0x1f; // 0001 1111 - static const int8_t TYPE_MASK = (int8_t)0xE0u; // 1110 0000 - static const int32_t TYPE_SHIFT_AMOUNT = 5; - - Transport_* trans_; - - /** - * (Writing) If we encounter a boolean field begin, save the TField here - * so it can have the value incorporated. - */ - struct { - const char* name; - TType fieldType; - int16_t fieldId; - } booleanField_; - - /** - * (Reading) If we read a field header, and it's a boolean field, save - * the boolean value here so that readBool can use it. - */ - struct { - bool hasBoolValue; - bool boolValue; - } boolValue_; - - /** - * Used to keep track of the last field for the current and previous structs, - * so we can do the delta stuff. - */ - - std::stack<int16_t> lastField_; - int16_t lastFieldId_; - - public: - TCompactProtocolT(boost::shared_ptr<Transport_> trans) : - TVirtualProtocol< TCompactProtocolT<Transport_> >(trans), - trans_(trans.get()), - lastFieldId_(0), - string_limit_(0), - string_buf_(NULL), - string_buf_size_(0), - container_limit_(0) { - booleanField_.name = NULL; - boolValue_.hasBoolValue = false; - } - - TCompactProtocolT(boost::shared_ptr<Transport_> trans, - int32_t string_limit, - int32_t container_limit) : - TVirtualProtocol< TCompactProtocolT<Transport_> >(trans), - trans_(trans.get()), - lastFieldId_(0), - string_limit_(string_limit), - string_buf_(NULL), - string_buf_size_(0), - container_limit_(container_limit) { - booleanField_.name = NULL; - boolValue_.hasBoolValue = false; - } - - ~TCompactProtocolT() { - free(string_buf_); - } - - - /** - * Writing functions - */ - - virtual uint32_t writeMessageBegin(const std::string& name, - const TMessageType messageType, - const int32_t seqid); - - uint32_t writeStructBegin(const char* name); - - uint32_t writeStructEnd(); - - uint32_t writeFieldBegin(const char* name, - const TType fieldType, - const int16_t fieldId); - - uint32_t writeFieldStop(); - - uint32_t writeListBegin(const TType elemType, - const uint32_t size); - - uint32_t writeSetBegin(const TType elemType, - const uint32_t size); - - virtual uint32_t writeMapBegin(const TType keyType, - const TType valType, - const uint32_t size); - - uint32_t writeBool(const bool value); - - uint32_t writeByte(const int8_t byte); - - uint32_t writeI16(const int16_t i16); - - uint32_t writeI32(const int32_t i32); - - uint32_t writeI64(const int64_t i64); - - uint32_t writeDouble(const double dub); - - uint32_t writeString(const std::string& str); - - uint32_t writeBinary(const std::string& str); - - /** - * These methods are called by structs, but don't actually have any wired - * output or purpose - */ - virtual uint32_t writeMessageEnd() { return 0; } - uint32_t writeMapEnd() { return 0; } - uint32_t writeListEnd() { return 0; } - uint32_t writeSetEnd() { return 0; } - uint32_t writeFieldEnd() { return 0; } - - protected: - int32_t writeFieldBeginInternal(const char* name, - const TType fieldType, - const int16_t fieldId, - int8_t typeOverride); - uint32_t writeCollectionBegin(const TType elemType, int32_t size); - uint32_t writeVarint32(uint32_t n); - uint32_t writeVarint64(uint64_t n); - uint64_t i64ToZigzag(const int64_t l); - uint32_t i32ToZigzag(const int32_t n); - inline int8_t getCompactType(const TType ttype); - - public: - uint32_t readMessageBegin(std::string& name, - TMessageType& messageType, - int32_t& seqid); - - uint32_t readStructBegin(std::string& name); - - uint32_t readStructEnd(); - - uint32_t readFieldBegin(std::string& name, - TType& fieldType, - int16_t& fieldId); - - uint32_t readMapBegin(TType& keyType, - TType& valType, - uint32_t& size); - - uint32_t readListBegin(TType& elemType, - uint32_t& size); - - uint32_t readSetBegin(TType& elemType, - uint32_t& size); - - uint32_t readBool(bool& value); - // Provide the default readBool() implementation for std::vector<bool> - using TVirtualProtocol< TCompactProtocolT<Transport_> >::readBool; - - uint32_t readByte(int8_t& byte); - - uint32_t readI16(int16_t& i16); - - uint32_t readI32(int32_t& i32); - - uint32_t readI64(int64_t& i64); - - uint32_t readDouble(double& dub); - - uint32_t readString(std::string& str); - - uint32_t readBinary(std::string& str); - - /* - *These methods are here for the struct to call, but don't have any wire - * encoding. - */ - uint32_t readMessageEnd() { return 0; } - uint32_t readFieldEnd() { return 0; } - uint32_t readMapEnd() { return 0; } - uint32_t readListEnd() { return 0; } - uint32_t readSetEnd() { return 0; } - - protected: - uint32_t readVarint32(int32_t& i32); - uint32_t readVarint64(int64_t& i64); - int32_t zigzagToI32(uint32_t n); - int64_t zigzagToI64(uint64_t n); - TType getTType(int8_t type); - - // Buffer for reading strings, save for the lifetime of the protocol to - // avoid memory churn allocating memory on every string read - int32_t string_limit_; - uint8_t* string_buf_; - int32_t string_buf_size_; - int32_t container_limit_; -}; - -typedef TCompactProtocolT<TTransport> TCompactProtocol; - -/** - * Constructs compact protocol handlers - */ -template <class Transport_> -class TCompactProtocolFactoryT : public TProtocolFactory { - public: - TCompactProtocolFactoryT() : - string_limit_(0), - container_limit_(0) {} - - TCompactProtocolFactoryT(int32_t string_limit, int32_t container_limit) : - string_limit_(string_limit), - container_limit_(container_limit) {} - - virtual ~TCompactProtocolFactoryT() {} - - void setStringSizeLimit(int32_t string_limit) { - string_limit_ = string_limit; - } - - void setContainerSizeLimit(int32_t container_limit) { - container_limit_ = container_limit; - } - - boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { - boost::shared_ptr<Transport_> specific_trans = - boost::dynamic_pointer_cast<Transport_>(trans); - TProtocol* prot; - if (specific_trans) { - prot = new TCompactProtocolT<Transport_>(specific_trans, string_limit_, - container_limit_); - } else { - prot = new TCompactProtocol(trans, string_limit_, container_limit_); - } - - return boost::shared_ptr<TProtocol>(prot); - } - - private: - int32_t string_limit_; - int32_t container_limit_; - -}; - -typedef TCompactProtocolFactoryT<TTransport> TCompactProtocolFactory; - -}}} // apache::thrift::protocol - -#include <thrift/protocol/TCompactProtocol.tcc> - -#endif http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.tcc ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.tcc b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.tcc deleted file mode 100644 index 62d6485..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TCompactProtocol.tcc +++ /dev/null @@ -1,818 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -#ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ -#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ 1 - -#include <limits> - -/* - * TCompactProtocol::i*ToZigzag depend on the fact that the right shift - * operator on a signed integer is an arithmetic (sign-extending) shift. - * If this is not the case, the current implementation will not work. - * If anyone encounters this error, we can try to figure out the best - * way to implement an arithmetic right shift on their platform. - */ -#if !defined(SIGNED_RIGHT_SHIFT_IS) || !defined(ARITHMETIC_RIGHT_SHIFT) -# error "Unable to determine the behavior of a signed right shift" -#endif -#if SIGNED_RIGHT_SHIFT_IS != ARITHMETIC_RIGHT_SHIFT -# error "TCompactProtocol currently only works if a signed right shift is arithmetic" -#endif - -#ifdef __GNUC__ -#define UNLIKELY(val) (__builtin_expect((val), 0)) -#else -#define UNLIKELY(val) (val) -#endif - -namespace apache { namespace thrift { namespace protocol { - -namespace detail { namespace compact { - -enum Types { - CT_STOP = 0x00, - CT_BOOLEAN_TRUE = 0x01, - CT_BOOLEAN_FALSE = 0x02, - CT_BYTE = 0x03, - CT_I16 = 0x04, - CT_I32 = 0x05, - CT_I64 = 0x06, - CT_DOUBLE = 0x07, - CT_BINARY = 0x08, - CT_LIST = 0x09, - CT_SET = 0x0A, - CT_MAP = 0x0B, - CT_STRUCT = 0x0C -}; - -const int8_t TTypeToCType[16] = { - CT_STOP, // T_STOP - 0, // unused - CT_BOOLEAN_TRUE, // T_BOOL - CT_BYTE, // T_BYTE - CT_DOUBLE, // T_DOUBLE - 0, // unused - CT_I16, // T_I16 - 0, // unused - CT_I32, // T_I32 - 0, // unused - CT_I64, // T_I64 - CT_BINARY, // T_STRING - CT_STRUCT, // T_STRUCT - CT_MAP, // T_MAP - CT_SET, // T_SET - CT_LIST, // T_LIST -}; - -}} // end detail::compact namespace - - -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeMessageBegin( - const std::string& name, - const TMessageType messageType, - const int32_t seqid) { - uint32_t wsize = 0; - wsize += writeByte(PROTOCOL_ID); - wsize += writeByte((VERSION_N & VERSION_MASK) | (((int32_t)messageType << TYPE_SHIFT_AMOUNT) & TYPE_MASK)); - wsize += writeVarint32(seqid); - wsize += writeString(name); - return wsize; -} - -/** - * Write a field header containing the field id and field type. If the - * difference between the current field id and the last one is small (< 15), - * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the - * field id will follow the type header as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name, - const TType fieldType, - const int16_t fieldId) { - if (fieldType == T_BOOL) { - booleanField_.name = name; - booleanField_.fieldType = fieldType; - booleanField_.fieldId = fieldId; - } else { - return writeFieldBeginInternal(name, fieldType, fieldId, -1); - } - return 0; -} - -/** - * Write the STOP symbol so we know there are no more fields in this struct. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeFieldStop() { - return writeByte(T_STOP); -} - -/** - * Write a struct begin. This doesn't actually put anything on the wire. We - * use it as an opportunity to put special placeholder markers on the field - * stack so we can get the field id deltas correct. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) { - (void) name; - lastField_.push(lastFieldId_); - lastFieldId_ = 0; - return 0; -} - -/** - * Write a struct end. This doesn't actually put anything on the wire. We use - * this as an opportunity to pop the last field from the current struct off - * of the field stack. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeStructEnd() { - lastFieldId_ = lastField_.top(); - lastField_.pop(); - return 0; -} - -/** - * Write a List header. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType, - const uint32_t size) { - return writeCollectionBegin(elemType, size); -} - -/** - * Write a set header. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType, - const uint32_t size) { - return writeCollectionBegin(elemType, size); -} - -/** - * Write a map header. If the map is empty, omit the key and value type - * headers, as we don't need any additional information to skip it. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType, - const TType valType, - const uint32_t size) { - uint32_t wsize = 0; - - if (size == 0) { - wsize += writeByte(0); - } else { - wsize += writeVarint32(size); - wsize += writeByte(getCompactType(keyType) << 4 | getCompactType(valType)); - } - return wsize; -} - -/** - * Write a boolean value. Potentially, this could be a boolean field, in - * which case the field header info isn't written yet. If so, decide what the - * right type header is for the value and then write the field header. - * Otherwise, write a single byte. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) { - uint32_t wsize = 0; - - if (booleanField_.name != NULL) { - // we haven't written the field header yet - wsize - += writeFieldBeginInternal(booleanField_.name, - booleanField_.fieldType, - booleanField_.fieldId, - static_cast<int8_t>(value - ? detail::compact::CT_BOOLEAN_TRUE - : detail::compact::CT_BOOLEAN_FALSE)); - booleanField_.name = NULL; - } else { - // we're not part of a field, so just write the value - wsize - += writeByte(static_cast<int8_t>(value - ? detail::compact::CT_BOOLEAN_TRUE - : detail::compact::CT_BOOLEAN_FALSE)); - } - return wsize; -} - -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) { - trans_->write((uint8_t*)&byte, 1); - return 1; -} - -/** - * Write an i16 as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) { - return writeVarint32(i32ToZigzag(i16)); -} - -/** - * Write an i32 as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) { - return writeVarint32(i32ToZigzag(i32)); -} - -/** - * Write an i64 as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) { - return writeVarint64(i64ToZigzag(i64)); -} - -/** - * Write a double to the wire as 8 bytes. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) { - BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); - BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); - - uint64_t bits = bitwise_cast<uint64_t>(dub); - bits = htolell(bits); - trans_->write((uint8_t*)&bits, 8); - return 8; -} - -/** - * Write a string to the wire with a varint size preceeding. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) { - return writeBinary(str); -} - -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) { - uint32_t ssize = str.size(); - uint32_t wsize = writeVarint32(ssize) + ssize; - trans_->write((uint8_t*)str.data(), ssize); - return wsize; -} - -// -// Internal Writing methods -// - -/** - * The workhorse of writeFieldBegin. It has the option of doing a - * 'type override' of the type header. This is used specifically in the - * boolean field case. - */ -template <class Transport_> -int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal( - const char* name, - const TType fieldType, - const int16_t fieldId, - int8_t typeOverride) { - (void) name; - uint32_t wsize = 0; - - // if there's a type override, use that. - int8_t typeToWrite = (typeOverride == -1 ? getCompactType(fieldType) : typeOverride); - - // check if we can use delta encoding for the field id - if (fieldId > lastFieldId_ && fieldId - lastFieldId_ <= 15) { - // write them together - wsize += writeByte(static_cast<int8_t>((fieldId - lastFieldId_) - << 4 | typeToWrite)); - } else { - // write them separate - wsize += writeByte(typeToWrite); - wsize += writeI16(fieldId); - } - - lastFieldId_ = fieldId; - return wsize; -} - -/** - * Abstract method for writing the start of lists and sets. List and sets on - * the wire differ only by the type indicator. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(const TType elemType, - int32_t size) { - uint32_t wsize = 0; - if (size <= 14) { - wsize += writeByte(static_cast<int8_t>(size - << 4 | getCompactType(elemType))); - } else { - wsize += writeByte(0xf0 | getCompactType(elemType)); - wsize += writeVarint32(size); - } - return wsize; -} - -/** - * Write an i32 as a varint. Results in 1-5 bytes on the wire. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) { - uint8_t buf[5]; - uint32_t wsize = 0; - - while (true) { - if ((n & ~0x7F) == 0) { - buf[wsize++] = (int8_t)n; - break; - } else { - buf[wsize++] = (int8_t)((n & 0x7F) | 0x80); - n >>= 7; - } - } - trans_->write(buf, wsize); - return wsize; -} - -/** - * Write an i64 as a varint. Results in 1-10 bytes on the wire. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) { - uint8_t buf[10]; - uint32_t wsize = 0; - - while (true) { - if ((n & ~0x7FL) == 0) { - buf[wsize++] = (int8_t)n; - break; - } else { - buf[wsize++] = (int8_t)((n & 0x7F) | 0x80); - n >>= 7; - } - } - trans_->write(buf, wsize); - return wsize; -} - -/** - * Convert l into a zigzag long. This allows negative numbers to be - * represented compactly as a varint. - */ -template <class Transport_> -uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) { - return (l << 1) ^ (l >> 63); -} - -/** - * Convert n into a zigzag int. This allows negative numbers to be - * represented compactly as a varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) { - return (n << 1) ^ (n >> 31); -} - -/** - * Given a TType value, find the appropriate detail::compact::Types value - */ -template <class Transport_> -int8_t TCompactProtocolT<Transport_>::getCompactType(const TType ttype) { - return detail::compact::TTypeToCType[ttype]; -} - -// -// Reading Methods -// - -/** - * Read a message header. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readMessageBegin( - std::string& name, - TMessageType& messageType, - int32_t& seqid) { - uint32_t rsize = 0; - int8_t protocolId; - int8_t versionAndType; - int8_t version; - - rsize += readByte(protocolId); - if (protocolId != PROTOCOL_ID) { - throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol identifier"); - } - - rsize += readByte(versionAndType); - version = (int8_t)(versionAndType & VERSION_MASK); - if (version != VERSION_N) { - throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version"); - } - - messageType = (TMessageType)((versionAndType >> TYPE_SHIFT_AMOUNT) & 0x03); - rsize += readVarint32(seqid); - rsize += readString(name); - - return rsize; -} - -/** - * Read a struct begin. There's nothing on the wire for this, but it is our - * opportunity to push a new struct begin marker on the field stack. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) { - name = ""; - lastField_.push(lastFieldId_); - lastFieldId_ = 0; - return 0; -} - -/** - * Doesn't actually consume any wire data, just removes the last field for - * this struct from the field stack. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readStructEnd() { - lastFieldId_ = lastField_.top(); - lastField_.pop(); - return 0; -} - -/** - * Read a field header off the wire. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name, - TType& fieldType, - int16_t& fieldId) { - (void) name; - uint32_t rsize = 0; - int8_t byte; - int8_t type; - - rsize += readByte(byte); - type = (byte & 0x0f); - - // if it's a stop, then we can return immediately, as the struct is over. - if (type == T_STOP) { - fieldType = T_STOP; - fieldId = 0; - return rsize; - } - - // mask off the 4 MSB of the type header. it could contain a field id delta. - int16_t modifier = (int16_t)(((uint8_t)byte & 0xf0) >> 4); - if (modifier == 0) { - // not a delta, look ahead for the zigzag varint field id. - rsize += readI16(fieldId); - } else { - fieldId = (int16_t)(lastFieldId_ + modifier); - } - fieldType = getTType(type); - - // if this happens to be a boolean field, the value is encoded in the type - if (type == detail::compact::CT_BOOLEAN_TRUE || - type == detail::compact::CT_BOOLEAN_FALSE) { - // save the boolean value in a special instance variable. - boolValue_.hasBoolValue = true; - boolValue_.boolValue = - (type == detail::compact::CT_BOOLEAN_TRUE ? true : false); - } - - // push the new field onto the field stack so we can keep the deltas going. - lastFieldId_ = fieldId; - return rsize; -} - -/** - * Read a map header off the wire. If the size is zero, skip reading the key - * and value type. This means that 0-length maps will yield TMaps without the - * "correct" types. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType, - TType& valType, - uint32_t& size) { - uint32_t rsize = 0; - int8_t kvType = 0; - int32_t msize = 0; - - rsize += readVarint32(msize); - if (msize != 0) - rsize += readByte(kvType); - - if (msize < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } else if (container_limit_ && msize > container_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - - keyType = getTType((int8_t)((uint8_t)kvType >> 4)); - valType = getTType((int8_t)((uint8_t)kvType & 0xf)); - size = (uint32_t)msize; - - return rsize; -} - -/** - * Read a list header off the wire. If the list size is 0-14, the size will - * be packed into the element type header. If it's a longer list, the 4 MSB - * of the element type header will be 0xF, and a varint will follow with the - * true size. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType, - uint32_t& size) { - int8_t size_and_type; - uint32_t rsize = 0; - int32_t lsize; - - rsize += readByte(size_and_type); - - lsize = ((uint8_t)size_and_type >> 4) & 0x0f; - if (lsize == 15) { - rsize += readVarint32(lsize); - } - - if (lsize < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } else if (container_limit_ && lsize > container_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - - elemType = getTType((int8_t)(size_and_type & 0x0f)); - size = (uint32_t)lsize; - - return rsize; -} - -/** - * Read a set header off the wire. If the set size is 0-14, the size will - * be packed into the element type header. If it's a longer set, the 4 MSB - * of the element type header will be 0xF, and a varint will follow with the - * true size. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType, - uint32_t& size) { - return readListBegin(elemType, size); -} - -/** - * Read a boolean off the wire. If this is a boolean field, the value should - * already have been read during readFieldBegin, so we'll just consume the - * pre-stored value. Otherwise, read a byte. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) { - if (boolValue_.hasBoolValue == true) { - value = boolValue_.boolValue; - boolValue_.hasBoolValue = false; - return 0; - } else { - int8_t val; - readByte(val); - value = (val == detail::compact::CT_BOOLEAN_TRUE); - return 1; - } -} - -/** - * Read a single byte off the wire. Nothing interesting here. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) { - uint8_t b[1]; - trans_->readAll(b, 1); - byte = *(int8_t*)b; - return 1; -} - -/** - * Read an i16 from the wire as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) { - int32_t value; - uint32_t rsize = readVarint32(value); - i16 = (int16_t)zigzagToI32(value); - return rsize; -} - -/** - * Read an i32 from the wire as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) { - int32_t value; - uint32_t rsize = readVarint32(value); - i32 = zigzagToI32(value); - return rsize; -} - -/** - * Read an i64 from the wire as a zigzag varint. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) { - int64_t value; - uint32_t rsize = readVarint64(value); - i64 = zigzagToI64(value); - return rsize; -} - -/** - * No magic here - just read a double off the wire. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) { - BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); - BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); - - union { - uint64_t bits; - uint8_t b[8]; - } u; - trans_->readAll(u.b, 8); - u.bits = letohll(u.bits); - dub = bitwise_cast<double>(u.bits); - return 8; -} - -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) { - return readBinary(str); -} - -/** - * Read a byte[] from the wire. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) { - int32_t rsize = 0; - int32_t size; - - rsize += readVarint32(size); - // Catch empty string case - if (size == 0) { - str = ""; - return rsize; - } - - // Catch error cases - if (size < 0) { - throw TProtocolException(TProtocolException::NEGATIVE_SIZE); - } - if (string_limit_ > 0 && size > string_limit_) { - throw TProtocolException(TProtocolException::SIZE_LIMIT); - } - - // Use the heap here to prevent stack overflow for v. large strings - if (size > string_buf_size_ || string_buf_ == NULL) { - void* new_string_buf = std::realloc(string_buf_, (uint32_t)size); - if (new_string_buf == NULL) { - throw std::bad_alloc(); - } - string_buf_ = (uint8_t*)new_string_buf; - string_buf_size_ = size; - } - trans_->readAll(string_buf_, size); - str.assign((char*)string_buf_, size); - - return rsize + (uint32_t)size; -} - -/** - * Read an i32 from the wire as a varint. The MSB of each byte is set - * if there is another byte to follow. This can read up to 5 bytes. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) { - int64_t val; - uint32_t rsize = readVarint64(val); - i32 = (int32_t)val; - return rsize; -} - -/** - * Read an i64 from the wire as a proper varint. The MSB of each byte is set - * if there is another byte to follow. This can read up to 10 bytes. - */ -template <class Transport_> -uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) { - uint32_t rsize = 0; - uint64_t val = 0; - int shift = 0; - uint8_t buf[10]; // 64 bits / (7 bits/byte) = 10 bytes. - uint32_t buf_size = sizeof(buf); - const uint8_t* borrowed = trans_->borrow(buf, &buf_size); - - // Fast path. - if (borrowed != NULL) { - while (true) { - uint8_t byte = borrowed[rsize]; - rsize++; - val |= (uint64_t)(byte & 0x7f) << shift; - shift += 7; - if (!(byte & 0x80)) { - i64 = val; - trans_->consume(rsize); - return rsize; - } - // Have to check for invalid data so we don't crash. - if (UNLIKELY(rsize == sizeof(buf))) { - throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes."); - } - } - } - - // Slow path. - else { - while (true) { - uint8_t byte; - rsize += trans_->readAll(&byte, 1); - val |= (uint64_t)(byte & 0x7f) << shift; - shift += 7; - if (!(byte & 0x80)) { - i64 = val; - return rsize; - } - // Might as well check for invalid data on the slow path too. - if (UNLIKELY(rsize >= sizeof(buf))) { - throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes."); - } - } - } -} - -/** - * Convert from zigzag int to int. - */ -template <class Transport_> -int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) { - return (n >> 1) ^ -static_cast<int32_t>(n & 1); -} - -/** - * Convert from zigzag long to long. - */ -template <class Transport_> -int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) { - return (n >> 1) ^ -static_cast<int32_t>(n & 1); -} - -template <class Transport_> -TType TCompactProtocolT<Transport_>::getTType(int8_t type) { - switch (type) { - case T_STOP: - return T_STOP; - case detail::compact::CT_BOOLEAN_FALSE: - case detail::compact::CT_BOOLEAN_TRUE: - return T_BOOL; - case detail::compact::CT_BYTE: - return T_BYTE; - case detail::compact::CT_I16: - return T_I16; - case detail::compact::CT_I32: - return T_I32; - case detail::compact::CT_I64: - return T_I64; - case detail::compact::CT_DOUBLE: - return T_DOUBLE; - case detail::compact::CT_BINARY: - return T_STRING; - case detail::compact::CT_LIST: - return T_LIST; - case detail::compact::CT_SET: - return T_SET; - case detail::compact::CT_MAP: - return T_MAP; - case detail::compact::CT_STRUCT: - return T_STRUCT; - default: - throw TException(std::string("don't know what type: ") + (char)type); - } - return T_STOP; -} - -}}} // apache::thrift::protocol - -#endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/d55608f1/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TDebugProtocol.cpp ---------------------------------------------------------------------- diff --git a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TDebugProtocol.cpp b/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TDebugProtocol.cpp deleted file mode 100644 index 63ea14d..0000000 --- a/airavata-api/airavata-client-sdks/airavata-cpp-sdk/src/main/resources/lib/thrift/protocol/TDebugProtocol.cpp +++ /dev/null @@ -1,358 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include <thrift/protocol/TDebugProtocol.h> - -#include <cassert> -#include <cctype> -#include <cstdio> -#include <stdexcept> -#include <boost/static_assert.hpp> -#include <boost/lexical_cast.hpp> - -using std::string; - - -static string byte_to_hex(const uint8_t byte) { - char buf[3]; - int ret = std::sprintf(buf, "%02x", (int)byte); - THRIFT_UNUSED_VARIABLE(ret); - assert(ret == 2); - assert(buf[2] == '\0'); - return buf; -} - - -namespace apache { namespace thrift { namespace protocol { - -string TDebugProtocol::fieldTypeName(TType type) { - switch (type) { - case T_STOP : return "stop" ; - case T_VOID : return "void" ; - case T_BOOL : return "bool" ; - case T_BYTE : return "byte" ; - case T_I16 : return "i16" ; - case T_I32 : return "i32" ; - case T_U64 : return "u64" ; - case T_I64 : return "i64" ; - case T_DOUBLE : return "double" ; - case T_STRING : return "string" ; - case T_STRUCT : return "struct" ; - case T_MAP : return "map" ; - case T_SET : return "set" ; - case T_LIST : return "list" ; - case T_UTF8 : return "utf8" ; - case T_UTF16 : return "utf16" ; - default: return "unknown"; - } -} - -void TDebugProtocol::indentUp() { - indent_str_ += string(indent_inc, ' '); -} - -void TDebugProtocol::indentDown() { - if (indent_str_.length() < (string::size_type)indent_inc) { - throw TProtocolException(TProtocolException::INVALID_DATA); - } - indent_str_.erase(indent_str_.length() - indent_inc); -} - -uint32_t TDebugProtocol::writePlain(const string& str) { - if(str.length() > (std::numeric_limits<uint32_t>::max)()) - throw TProtocolException(TProtocolException::SIZE_LIMIT); - trans_->write((uint8_t*)str.data(), static_cast<uint32_t>(str.length())); - return static_cast<uint32_t>(str.length()); -} - -uint32_t TDebugProtocol::writeIndented(const string& str) { - if(str.length() > (std::numeric_limits<uint32_t>::max)()) - throw TProtocolException(TProtocolException::SIZE_LIMIT); - if(indent_str_.length() > (std::numeric_limits<uint32_t>::max)()) - throw TProtocolException(TProtocolException::SIZE_LIMIT); - uint64_t total_len = indent_str_.length() + str.length(); - if(total_len > (std::numeric_limits<uint32_t>::max)()) - throw TProtocolException(TProtocolException::SIZE_LIMIT); - trans_->write((uint8_t*)indent_str_.data(), static_cast<uint32_t>(indent_str_.length())); - trans_->write((uint8_t*)str.data(), static_cast<uint32_t>(str.length())); - return static_cast<uint32_t>(indent_str_.length() + str.length()); -} - -uint32_t TDebugProtocol::startItem() { - uint32_t size; - - switch (write_state_.back()) { - case UNINIT: - // XXX figure out what to do here. - //throw TProtocolException(TProtocolException::INVALID_DATA); - //return writeIndented(str); - return 0; - case STRUCT: - return 0; - case SET: - return writeIndented(""); - case MAP_KEY: - return writeIndented(""); - case MAP_VALUE: - return writePlain(" -> "); - case LIST: - size = writeIndented( - "[" + boost::lexical_cast<string>(list_idx_.back()) + "] = "); - list_idx_.back()++; - return size; - default: - throw std::logic_error("Invalid enum value."); - } -} - -uint32_t TDebugProtocol::endItem() { - //uint32_t size; - - switch (write_state_.back()) { - case UNINIT: - // XXX figure out what to do here. - //throw TProtocolException(TProtocolException::INVALID_DATA); - //return writeIndented(str); - return 0; - case STRUCT: - return writePlain(",\n"); - case SET: - return writePlain(",\n"); - case MAP_KEY: - write_state_.back() = MAP_VALUE; - return 0; - case MAP_VALUE: - write_state_.back() = MAP_KEY; - return writePlain(",\n"); - case LIST: - return writePlain(",\n"); - default: - throw std::logic_error("Invalid enum value."); - } -} - -uint32_t TDebugProtocol::writeItem(const std::string& str) { - uint32_t size = 0; - size += startItem(); - size += writePlain(str); - size += endItem(); - return size; -} - -uint32_t TDebugProtocol::writeMessageBegin(const std::string& name, - const TMessageType messageType, - const int32_t seqid) { - (void) seqid; - string mtype; - switch (messageType) { - case T_CALL : mtype = "call" ; break; - case T_REPLY : mtype = "reply" ; break; - case T_EXCEPTION : mtype = "exn" ; break; - case T_ONEWAY : mtype = "oneway" ; break; - } - - uint32_t size = writeIndented("(" + mtype + ") " + name + "("); - indentUp(); - return size; -} - -uint32_t TDebugProtocol::writeMessageEnd() { - indentDown(); - return writeIndented(")\n"); -} - -uint32_t TDebugProtocol::writeStructBegin(const char* name) { - uint32_t size = 0; - size += startItem(); - size += writePlain(string(name) + " {\n"); - indentUp(); - write_state_.push_back(STRUCT); - return size; -} - -uint32_t TDebugProtocol::writeStructEnd() { - indentDown(); - write_state_.pop_back(); - uint32_t size = 0; - size += writeIndented("}"); - size += endItem(); - return size; -} - -uint32_t TDebugProtocol::writeFieldBegin(const char* name, - const TType fieldType, - const int16_t fieldId) { - // sprintf(id_str, "%02d", fieldId); - string id_str = boost::lexical_cast<string>(fieldId); - if (id_str.length() == 1) id_str = '0' + id_str; - - return writeIndented( - id_str + ": " + - name + " (" + - fieldTypeName(fieldType) + ") = "); -} - -uint32_t TDebugProtocol::writeFieldEnd() { - assert(write_state_.back() == STRUCT); - return 0; -} - -uint32_t TDebugProtocol::writeFieldStop() { - return 0; - //writeIndented("***STOP***\n"); -} - -uint32_t TDebugProtocol::writeMapBegin(const TType keyType, - const TType valType, - const uint32_t size) { - // TODO(dreiss): Optimize short maps? - uint32_t bsize = 0; - bsize += startItem(); - bsize += writePlain( - "map<" + fieldTypeName(keyType) + "," + fieldTypeName(valType) + ">" - "[" + boost::lexical_cast<string>(size) + "] {\n"); - indentUp(); - write_state_.push_back(MAP_KEY); - return bsize; -} - -uint32_t TDebugProtocol::writeMapEnd() { - indentDown(); - write_state_.pop_back(); - uint32_t size = 0; - size += writeIndented("}"); - size += endItem(); - return size; -} - -uint32_t TDebugProtocol::writeListBegin(const TType elemType, - const uint32_t size) { - // TODO(dreiss): Optimize short arrays. - uint32_t bsize = 0; - bsize += startItem(); - bsize += writePlain( - "list<" + fieldTypeName(elemType) + ">" - "[" + boost::lexical_cast<string>(size) + "] {\n"); - indentUp(); - write_state_.push_back(LIST); - list_idx_.push_back(0); - return bsize; -} - -uint32_t TDebugProtocol::writeListEnd() { - indentDown(); - write_state_.pop_back(); - list_idx_.pop_back(); - uint32_t size = 0; - size += writeIndented("}"); - size += endItem(); - return size; -} - -uint32_t TDebugProtocol::writeSetBegin(const TType elemType, - const uint32_t size) { - // TODO(dreiss): Optimize short sets. - uint32_t bsize = 0; - bsize += startItem(); - bsize += writePlain( - "set<" + fieldTypeName(elemType) + ">" - "[" + boost::lexical_cast<string>(size) + "] {\n"); - indentUp(); - write_state_.push_back(SET); - return bsize; -} - -uint32_t TDebugProtocol::writeSetEnd() { - indentDown(); - write_state_.pop_back(); - uint32_t size = 0; - size += writeIndented("}"); - size += endItem(); - return size; -} - -uint32_t TDebugProtocol::writeBool(const bool value) { - return writeItem(value ? "true" : "false"); -} - -uint32_t TDebugProtocol::writeByte(const int8_t byte) { - return writeItem("0x" + byte_to_hex(byte)); -} - -uint32_t TDebugProtocol::writeI16(const int16_t i16) { - return writeItem(boost::lexical_cast<string>(i16)); -} - -uint32_t TDebugProtocol::writeI32(const int32_t i32) { - return writeItem(boost::lexical_cast<string>(i32)); -} - -uint32_t TDebugProtocol::writeI64(const int64_t i64) { - return writeItem(boost::lexical_cast<string>(i64)); -} - -uint32_t TDebugProtocol::writeDouble(const double dub) { - return writeItem(boost::lexical_cast<string>(dub)); -} - - -uint32_t TDebugProtocol::writeString(const string& str) { - // XXX Raw/UTF-8? - - string to_show = str; - if (to_show.length() > (string::size_type)string_limit_) { - to_show = str.substr(0, string_prefix_size_); - to_show += "[...](" + boost::lexical_cast<string>(str.length()) + ")"; - } - - string output = "\""; - - for (string::const_iterator it = to_show.begin(); it != to_show.end(); ++it) { - if (*it == '\\') { - output += "\\\\"; - } else if (*it == '"') { - output += "\\\""; - } else if (std::isprint(*it)) { - output += *it; - } else { - switch (*it) { - case '\a': output += "\\a"; break; - case '\b': output += "\\b"; break; - case '\f': output += "\\f"; break; - case '\n': output += "\\n"; break; - case '\r': output += "\\r"; break; - case '\t': output += "\\t"; break; - case '\v': output += "\\v"; break; - default: - output += "\\x"; - output += byte_to_hex(*it); - } - } - } - - output += '\"'; - return writeItem(output); -} - -uint32_t TDebugProtocol::writeBinary(const string& str) { - // XXX Hex? - return TDebugProtocol::writeString(str); -} - -}}} // apache::thrift::protocol
