diff options
Diffstat (limited to 'cpp/include/qpid/framing')
-rw-r--r-- | cpp/include/qpid/framing/Array.h | 97 | ||||
-rw-r--r-- | cpp/include/qpid/framing/Buffer.h | 136 | ||||
-rw-r--r-- | cpp/include/qpid/framing/FieldTable.h | 125 | ||||
-rw-r--r-- | cpp/include/qpid/framing/FieldValue.h | 321 | ||||
-rw-r--r-- | cpp/include/qpid/framing/ProtocolVersion.h | 58 | ||||
-rw-r--r-- | cpp/include/qpid/framing/SequenceNumber.h | 79 | ||||
-rw-r--r-- | cpp/include/qpid/framing/SequenceSet.h | 69 | ||||
-rw-r--r-- | cpp/include/qpid/framing/StructHelper.h | 57 | ||||
-rw-r--r-- | cpp/include/qpid/framing/Uuid.h | 91 | ||||
-rw-r--r-- | cpp/include/qpid/framing/amqp_types.h | 66 | ||||
-rw-r--r-- | cpp/include/qpid/framing/amqp_types_full.h | 38 |
11 files changed, 1137 insertions, 0 deletions
diff --git a/cpp/include/qpid/framing/Array.h b/cpp/include/qpid/framing/Array.h new file mode 100644 index 0000000000..d3bdd36aa6 --- /dev/null +++ b/cpp/include/qpid/framing/Array.h @@ -0,0 +1,97 @@ +/* + * + * 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 "qpid/framing/amqp_types.h" +#include "qpid/framing/FieldValue.h" +#include "qpid/framing/TypeCode.h" +#include <boost/shared_ptr.hpp> +#include <iostream> +#include <vector> +#include "qpid/CommonImportExport.h" + +#ifndef _Array_ +#define _Array_ + +namespace qpid { +namespace framing { + +class Buffer; + +class Array +{ + public: + typedef boost::shared_ptr<FieldValue> ValuePtr; + typedef std::vector<ValuePtr> ValueVector; + typedef ValueVector::const_iterator const_iterator; + typedef ValueVector::iterator iterator; + + QPID_COMMON_EXTERN uint32_t encodedSize() const; + QPID_COMMON_EXTERN void encode(Buffer& buffer) const; + QPID_COMMON_EXTERN void decode(Buffer& buffer); + + QPID_COMMON_EXTERN int count() const; + QPID_COMMON_EXTERN bool operator==(const Array& other) const; + + QPID_COMMON_EXTERN Array(); + QPID_COMMON_EXTERN Array(TypeCode type); + QPID_COMMON_EXTERN Array(uint8_t type); + //creates a longstr array + QPID_COMMON_EXTERN Array(const std::vector<std::string>& in); + + QPID_COMMON_EXTERN TypeCode getType() const { return type; } + + // std collection interface. + QPID_COMMON_EXTERN const_iterator begin() const { return values.begin(); } + QPID_COMMON_EXTERN const_iterator end() const { return values.end(); } + QPID_COMMON_EXTERN iterator begin() { return values.begin(); } + QPID_COMMON_EXTERN iterator end(){ return values.end(); } + + QPID_COMMON_EXTERN ValuePtr front() const { return values.front(); } + QPID_COMMON_EXTERN ValuePtr back() const { return values.back(); } + QPID_COMMON_EXTERN size_t size() const { return values.size(); } + + QPID_COMMON_EXTERN void insert(iterator i, ValuePtr value); + QPID_COMMON_EXTERN void erase(iterator i) { values.erase(i); } + QPID_COMMON_EXTERN void push_back(ValuePtr value) { values.insert(end(), value); } + QPID_COMMON_EXTERN void pop_back() { values.pop_back(); } + + // Non-std interface + QPID_COMMON_EXTERN void add(ValuePtr value) { push_back(value); } + + template <class T> + void collect(std::vector<T>& out) const + { + for (ValueVector::const_iterator i = values.begin(); i != values.end(); ++i) { + out.push_back((*i)->get<T>()); + } + } + + private: + TypeCode type; + ValueVector values; + + friend QPID_COMMON_EXTERN std::ostream& operator<<(std::ostream& out, const Array& body); +}; + +} +} + + +#endif diff --git a/cpp/include/qpid/framing/Buffer.h b/cpp/include/qpid/framing/Buffer.h new file mode 100644 index 0000000000..8a6a5c0d5f --- /dev/null +++ b/cpp/include/qpid/framing/Buffer.h @@ -0,0 +1,136 @@ +/* + * + * 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 "qpid/framing/amqp_types.h" +#include "qpid/Exception.h" +#include "qpid/CommonImportExport.h" +#include <boost/iterator/iterator_facade.hpp> + +#ifndef _Buffer_ +#define _Buffer_ + +namespace qpid { +namespace framing { + +struct OutOfBounds : qpid::Exception { + OutOfBounds() : qpid::Exception(std::string("Out of Bounds")) {} +}; + +class Content; +class FieldTable; + +class Buffer +{ + uint32_t size; + char* data; + uint32_t position; + uint32_t r_position; + + void checkAvailable(uint32_t count) { if (position + count > size) throw OutOfBounds(); } + + public: + + /** Buffer input/output iterator. + * Supports using an amqp_0_10::Codec with a framing::Buffer. + */ + class Iterator : public boost::iterator_facade< + Iterator, char, boost::random_access_traversal_tag> + { + public: + Iterator(Buffer& b) : buffer(&b) {} + + private: + friend class boost::iterator_core_access; + char& dereference() const { return buffer->data[buffer->position]; } + void increment() { ++buffer->position; } + bool equal(const Iterator& x) const { return buffer == x.buffer; } + + Buffer* buffer; + }; + + friend class Iterator; + + QPID_COMMON_EXTERN Buffer(char* data=0, uint32_t size=0); + + QPID_COMMON_EXTERN void record(); + QPID_COMMON_EXTERN void restore(bool reRecord = false); + QPID_COMMON_EXTERN void reset(); + + QPID_COMMON_EXTERN uint32_t available() { return size - position; } + QPID_COMMON_EXTERN uint32_t getSize() { return size; } + QPID_COMMON_EXTERN uint32_t getPosition() { return position; } + QPID_COMMON_EXTERN Iterator getIterator() { return Iterator(*this); } + QPID_COMMON_EXTERN char* getPointer() { return data; } + + QPID_COMMON_EXTERN void putOctet(uint8_t i); + QPID_COMMON_EXTERN void putShort(uint16_t i); + QPID_COMMON_EXTERN void putLong(uint32_t i); + QPID_COMMON_EXTERN void putLongLong(uint64_t i); + QPID_COMMON_EXTERN void putInt8(int8_t i); + QPID_COMMON_EXTERN void putInt16(int16_t i); + QPID_COMMON_EXTERN void putInt32(int32_t i); + QPID_COMMON_EXTERN void putInt64(int64_t i); + QPID_COMMON_EXTERN void putFloat(float f); + QPID_COMMON_EXTERN void putDouble(double f); + QPID_COMMON_EXTERN void putBin128(const uint8_t* b); + + QPID_COMMON_EXTERN uint8_t getOctet(); + QPID_COMMON_EXTERN uint16_t getShort(); + QPID_COMMON_EXTERN uint32_t getLong(); + QPID_COMMON_EXTERN uint64_t getLongLong(); + QPID_COMMON_EXTERN int8_t getInt8(); + QPID_COMMON_EXTERN int16_t getInt16(); + QPID_COMMON_EXTERN int32_t getInt32(); + QPID_COMMON_EXTERN int64_t getInt64(); + QPID_COMMON_EXTERN float getFloat(); + QPID_COMMON_EXTERN double getDouble(); + + template <int n> + QPID_COMMON_EXTERN uint64_t getUInt(); + + template <int n> + QPID_COMMON_EXTERN void putUInt(uint64_t); + + QPID_COMMON_EXTERN void putShortString(const string& s); + QPID_COMMON_EXTERN void putMediumString(const string& s); + QPID_COMMON_EXTERN void putLongString(const string& s); + QPID_COMMON_EXTERN void getShortString(string& s); + QPID_COMMON_EXTERN void getMediumString(string& s); + QPID_COMMON_EXTERN void getLongString(string& s); + QPID_COMMON_EXTERN void getBin128(uint8_t* b); + + QPID_COMMON_EXTERN void putRawData(const string& s); + QPID_COMMON_EXTERN void getRawData(string& s, uint32_t size); + + QPID_COMMON_EXTERN void putRawData(const uint8_t* data, size_t size); + QPID_COMMON_EXTERN void getRawData(uint8_t* data, size_t size); + + template <class T> void put(const T& data) { data.encode(*this); } + template <class T> void get(T& data) { data.decode(*this); } + + QPID_COMMON_EXTERN void dump(std::ostream&) const; +}; + +std::ostream& operator<<(std::ostream&, const Buffer&); + +}} // namespace qpid::framing + + +#endif diff --git a/cpp/include/qpid/framing/FieldTable.h b/cpp/include/qpid/framing/FieldTable.h new file mode 100644 index 0000000000..a3a5c8a4ee --- /dev/null +++ b/cpp/include/qpid/framing/FieldTable.h @@ -0,0 +1,125 @@ +/* + * + * 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 <iostream> +#include <vector> +#include <boost/shared_ptr.hpp> +#include <map> +#include "qpid/framing/amqp_types.h" +#include "qpid/CommonImportExport.h" + +#ifndef _FieldTable_ +#define _FieldTable_ + +namespace qpid { + /** + * The framing namespace contains classes that are used to create, + * send and receive the basic packets from which AMQP is built. + */ +namespace framing { + +class Array; +class FieldValue; +class Buffer; + +/** + * A set of name-value pairs. (See the AMQP spec for more details on + * AMQP field tables). + * + * \ingroup clientapi + */ +class FieldTable +{ + public: + typedef boost::shared_ptr<FieldValue> ValuePtr; + typedef std::map<std::string, ValuePtr> ValueMap; + typedef ValueMap::iterator iterator; + + QPID_COMMON_EXTERN FieldTable() {}; + QPID_COMMON_EXTERN FieldTable(const FieldTable& ft); + QPID_COMMON_EXTERN ~FieldTable(); + QPID_COMMON_EXTERN FieldTable& operator=(const FieldTable& ft); + QPID_COMMON_EXTERN uint32_t encodedSize() const; + QPID_COMMON_EXTERN void encode(Buffer& buffer) const; + QPID_COMMON_EXTERN void decode(Buffer& buffer); + + QPID_COMMON_EXTERN uint32_t qmfEncodedSize() const; + QPID_COMMON_EXTERN void qmfEncode(Buffer& buffer) const; + QPID_COMMON_EXTERN void qmfDecode(Buffer& buffer); + + QPID_COMMON_EXTERN int count() const; + QPID_COMMON_EXTERN void set(const std::string& name, const ValuePtr& value); + QPID_COMMON_EXTERN ValuePtr get(const std::string& name) const; + QPID_COMMON_EXTERN bool isSet(const std::string& name) const { return get(name).get() != 0; } + + QPID_COMMON_EXTERN void setString(const std::string& name, const std::string& value); + QPID_COMMON_EXTERN void setInt(const std::string& name, const int value); + QPID_COMMON_EXTERN void setInt64(const std::string& name, const int64_t value); + QPID_COMMON_EXTERN void setTimestamp(const std::string& name, const uint64_t value); + QPID_COMMON_EXTERN void setUInt64(const std::string& name, const uint64_t value); + QPID_COMMON_EXTERN void setTable(const std::string& name, const FieldTable& value); + QPID_COMMON_EXTERN void setArray(const std::string& name, const Array& value); + QPID_COMMON_EXTERN void setFloat(const std::string& name, const float value); + QPID_COMMON_EXTERN void setDouble(const std::string& name, const double value); + //void setDecimal(string& name, xxx& value); + + QPID_COMMON_EXTERN int getAsInt(const std::string& name) const; + QPID_COMMON_EXTERN uint64_t getAsUInt64(const std::string& name) const; + QPID_COMMON_EXTERN int64_t getAsInt64(const std::string& name) const; + QPID_COMMON_EXTERN std::string getAsString(const std::string& name) const; + + QPID_COMMON_EXTERN bool getTable(const std::string& name, FieldTable& value) const; + QPID_COMMON_EXTERN bool getArray(const std::string& name, Array& value) const; + QPID_COMMON_EXTERN bool getFloat(const std::string& name, float& value) const; + QPID_COMMON_EXTERN bool getDouble(const std::string& name, double& value) const; + //bool getTimestamp(const std::string& name, uint64_t& value) const; + //bool getDecimal(string& name, xxx& value); + QPID_COMMON_EXTERN void erase(const std::string& name); + + + QPID_COMMON_EXTERN bool operator==(const FieldTable& other) const; + + // Map-like interface. + // TODO: may need to duplicate into versions that return mutable iterator + ValueMap::const_iterator begin() const { return values.begin(); } + ValueMap::const_iterator end() const { return values.end(); } + ValueMap::const_iterator find(const std::string& s) const { return values.find(s); } + + std::pair <ValueMap::iterator, bool> insert(const ValueMap::value_type&); + void clear() { values.clear(); } + + // ### Hack Alert + + ValueMap::iterator getValues() { return values.begin(); } + + private: + ValueMap values; + + QPID_COMMON_EXTERN friend std::ostream& operator<<(std::ostream& out, const FieldTable& body); +}; + +//class FieldNotFoundException{}; +//class UnknownFieldName : public FieldNotFoundException{}; +//class IncorrectFieldType : public FieldNotFoundException{}; +} +} + + +#endif diff --git a/cpp/include/qpid/framing/FieldValue.h b/cpp/include/qpid/framing/FieldValue.h new file mode 100644 index 0000000000..97fc56d606 --- /dev/null +++ b/cpp/include/qpid/framing/FieldValue.h @@ -0,0 +1,321 @@ +#ifndef _framing_FieldValue_h +#define _framing_FieldValue_h +/* + * + * 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 "qpid/Exception.h" +#include "qpid/framing/amqp_types.h" +#include "qpid/framing/Buffer.h" +#include "qpid/framing/FieldTable.h" +#include "qpid/CommonImportExport.h" + +#include <iostream> +#include <memory> +#include <vector> + +#include <assert.h> + +namespace qpid { +namespace framing { + +//class Array; +/** + * Exception that is the base exception for all field table errors. + * + * \ingroup clientapi + */ +class FieldValueException : public qpid::Exception {}; + +/** + * Exception thrown when we can't perform requested conversion + * + * \ingroup clientapi + */ +struct InvalidConversionException : public FieldValueException { + InvalidConversionException() {} +}; + +/** + * Value that can appear in an AMQP field table + * + * \ingroup clientapi + */ +class FieldValue { + public: + /* + * Abstract type for content of different types + */ + class Data { + public: + virtual ~Data() {}; + virtual uint32_t encodedSize() const = 0; + virtual void encode(Buffer& buffer) = 0; + virtual void decode(Buffer& buffer) = 0; + virtual bool operator==(const Data&) const = 0; + + virtual bool convertsToInt() const { return false; } + virtual bool convertsToString() const { return false; } + virtual int64_t getInt() const { throw InvalidConversionException();} + virtual std::string getString() const { throw InvalidConversionException(); } + + virtual void print(std::ostream& out) const = 0; + }; + + FieldValue(): data(0) {}; + // Default assignment operator is fine + void setType(uint8_t type); + uint8_t getType(); + Data& getData() { return *data; } + uint32_t encodedSize() const { return 1 + data->encodedSize(); }; + bool empty() const { return data.get() == 0; } + void encode(Buffer& buffer); + void decode(Buffer& buffer); + QPID_COMMON_EXTERN bool operator==(const FieldValue&) const; + QPID_COMMON_EXTERN bool operator!=(const FieldValue& v) const { return !(*this == v); } + + void print(std::ostream& out) const; + + template <typename T> bool convertsTo() const { return false; } + template <typename T> T get() const { throw InvalidConversionException(); } + + protected: + FieldValue(uint8_t t, Data* d): typeOctet(t), data(d) {} + + private: + uint8_t typeOctet; + std::auto_ptr<Data> data; +}; + +template <> +inline bool FieldValue::convertsTo<int>() const { return data->convertsToInt(); } + +template <> +inline bool FieldValue::convertsTo<int64_t>() const { return data->convertsToInt(); } + +template <> +inline bool FieldValue::convertsTo<std::string>() const { return data->convertsToString(); } + +template <> +inline int FieldValue::get<int>() const { return data->getInt(); } + +template <> +inline int64_t FieldValue::get<int64_t>() const { return data->getInt(); } + +template <> +inline std::string FieldValue::get<std::string>() const { return data->getString(); } + +inline std::ostream& operator<<(std::ostream& out, const FieldValue& v) { + v.print(out); + return out; +} + +template <int width> +class FixedWidthValue : public FieldValue::Data { + uint8_t octets[width]; + + public: + FixedWidthValue() {} + FixedWidthValue(const uint8_t (&data)[width]) : octets(data) {} + FixedWidthValue(const uint8_t* const data) + { + for (int i = 0; i < width; i++) octets[i] = data[i]; + } + FixedWidthValue(uint64_t v) + { + for (int i = width; i > 1; --i) { + octets[i-1] = (uint8_t) (0xFF & v); v >>= 8; + } + octets[0] = (uint8_t) (0xFF & v); + } + uint32_t encodedSize() const { return width; } + void encode(Buffer& buffer) { buffer.putRawData(octets, width); } + void decode(Buffer& buffer) { buffer.getRawData(octets, width); } + bool operator==(const Data& d) const { + const FixedWidthValue<width>* rhs = dynamic_cast< const FixedWidthValue<width>* >(&d); + if (rhs == 0) return false; + else return std::equal(&octets[0], &octets[width], &rhs->octets[0]); + } + + bool convertsToInt() const { return true; } + int64_t getInt() const + { + int64_t v = 0; + for (int i = 0; i < width-1; ++i) { + v |= octets[i]; v <<= 8; + } + v |= octets[width-1]; + return v; + } + uint8_t* rawOctets() { return octets; } + + void print(std::ostream& o) const { o << "F" << width << ":"; }; +}; + +template <> +class FixedWidthValue<0> : public FieldValue::Data { + public: + // Implicit default constructor is fine + uint32_t encodedSize() const { return 0; } + void encode(Buffer&) {}; + void decode(Buffer&) {}; + bool operator==(const Data& d) const { + const FixedWidthValue<0>* rhs = dynamic_cast< const FixedWidthValue<0>* >(&d); + return rhs != 0; + } + void print(std::ostream& o) const { o << "F0"; }; +}; + +template <int lenwidth> +class VariableWidthValue : public FieldValue::Data { + std::vector<uint8_t> octets; + + public: + VariableWidthValue() {} + VariableWidthValue(const std::vector<uint8_t>& data) : octets(data) {} + VariableWidthValue(const uint8_t* start, const uint8_t* end) : octets(start, end) {} + uint32_t encodedSize() const { return lenwidth + octets.size(); } + void encode(Buffer& buffer) { + buffer.putUInt<lenwidth>(octets.size()); + if (octets.size() > 0) + buffer.putRawData(&octets[0], octets.size()); + }; + void decode(Buffer& buffer) { + uint32_t len = buffer.getUInt<lenwidth>(); + octets.resize(len); + if (len > 0) + buffer.getRawData(&octets[0], len); + } + bool operator==(const Data& d) const { + const VariableWidthValue<lenwidth>* rhs = dynamic_cast< const VariableWidthValue<lenwidth>* >(&d); + if (rhs == 0) return false; + else return octets==rhs->octets; + } + + bool convertsToString() const { return true; } + std::string getString() const { return std::string(octets.begin(), octets.end()); } + + void print(std::ostream& o) const { o << "V" << lenwidth << ":" << octets.size() << ":"; }; +}; + +template <class T> +class EncodedValue : public FieldValue::Data { + T value; + public: + + EncodedValue() {} + EncodedValue(const T& v) : value(v) {} + + T& getValue() { return value; } + const T& getValue() const { return value; } + + uint32_t encodedSize() const { return value.encodedSize(); } + + void encode(Buffer& buffer) { + value.encode(buffer); + }; + void decode(Buffer& buffer) { + value.decode(buffer); + } + bool operator==(const Data& d) const { + const EncodedValue<T>* rhs = dynamic_cast< const EncodedValue<T>* >(&d); + if (rhs == 0) return false; + else return value==rhs->value; + } + + void print(std::ostream& o) const { o << "[" << value << "]"; }; +}; + +class Str8Value : public FieldValue { + public: + QPID_COMMON_EXTERN Str8Value(const std::string& v); +}; + +class Str16Value : public FieldValue { + public: + QPID_COMMON_EXTERN Str16Value(const std::string& v); +}; + +class Struct32Value : public FieldValue { + public: + QPID_COMMON_EXTERN Struct32Value(const std::string& v); +}; + +class FloatValue : public FieldValue +{ + public: + QPID_COMMON_EXTERN FloatValue(float f); +}; +class DoubleValue : public FieldValue +{ + public: + QPID_COMMON_EXTERN DoubleValue(double f); +}; + +/* + * Basic integer value encodes as signed 32 bit + */ +class IntegerValue : public FieldValue { + public: + QPID_COMMON_EXTERN IntegerValue(int v); +}; + +class TimeValue : public FieldValue { + public: + QPID_COMMON_EXTERN TimeValue(uint64_t v); +}; + +class Integer64Value : public FieldValue { + public: + QPID_COMMON_EXTERN Integer64Value(int64_t v); +}; + +class Unsigned64Value : public FieldValue { + public: + QPID_COMMON_EXTERN Unsigned64Value(uint64_t v); +}; + +class FieldTableValue : public FieldValue { + public: + QPID_COMMON_EXTERN FieldTableValue(const FieldTable&); +}; + +class ArrayValue : public FieldValue { + public: + QPID_COMMON_EXTERN ArrayValue(const Array&); +}; + +template <class T> +bool getEncodedValue(FieldTable::ValuePtr vptr, T& value) +{ + if (vptr) { + const EncodedValue<T>* ev = dynamic_cast< EncodedValue<T>* >(&(vptr->getData())); + if (ev != 0) { + value = ev->getValue(); + return true; + } + } + return false; +} + + +}} // qpid::framing + +#endif diff --git a/cpp/include/qpid/framing/ProtocolVersion.h b/cpp/include/qpid/framing/ProtocolVersion.h new file mode 100644 index 0000000000..e7e75d75f6 --- /dev/null +++ b/cpp/include/qpid/framing/ProtocolVersion.h @@ -0,0 +1,58 @@ +/* + * + * 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 _ProtocolVersion_ +#define _ProtocolVersion_ + +#include "qpid/framing/amqp_types.h" +#include "qpid/CommonImportExport.h" + +namespace qpid +{ +namespace framing +{ + +class ProtocolVersion +{ +private: + uint8_t major_; + uint8_t minor_; + +public: + explicit ProtocolVersion(uint8_t _major=0, uint8_t _minor=0) + : major_(_major), minor_(_minor) {} + + QPID_COMMON_EXTERN uint8_t getMajor() const { return major_; } + QPID_COMMON_EXTERN void setMajor(uint8_t major) { major_ = major; } + QPID_COMMON_EXTERN uint8_t getMinor() const { return minor_; } + QPID_COMMON_EXTERN void setMinor(uint8_t minor) { minor_ = minor; } + QPID_COMMON_EXTERN const std::string toString() const; + + QPID_COMMON_EXTERN ProtocolVersion& operator=(ProtocolVersion p); + + QPID_COMMON_EXTERN bool operator==(ProtocolVersion p) const; + QPID_COMMON_EXTERN bool operator!=(ProtocolVersion p) const { return ! (*this == p); } +}; + +} // namespace framing +} // namespace qpid + + +#endif // ifndef _ProtocolVersion_ diff --git a/cpp/include/qpid/framing/SequenceNumber.h b/cpp/include/qpid/framing/SequenceNumber.h new file mode 100644 index 0000000000..1e53058df8 --- /dev/null +++ b/cpp/include/qpid/framing/SequenceNumber.h @@ -0,0 +1,79 @@ +/* + * + * 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 _framing_SequenceNumber_h +#define _framing_SequenceNumber_h + +#include "qpid/framing/amqp_types.h" +#include <boost/operators.hpp> +#include <iosfwd> +#include "qpid/CommonImportExport.h" + +namespace qpid { +namespace framing { + +class Buffer; + +/** + * 4-byte sequence number that 'wraps around'. + */ +class SequenceNumber : public +boost::equality_comparable< + SequenceNumber, boost::less_than_comparable< + SequenceNumber, boost::incrementable< + SequenceNumber, boost::decrementable<SequenceNumber> > > > +{ + int32_t value; + + public: + SequenceNumber(uint32_t v=0) : value(v) {} + + SequenceNumber& operator++() { ++value; return *this; } + SequenceNumber& operator--() { --value; return *this; } + bool operator==(const SequenceNumber& other) const { return value == other.value; } + bool operator<(const SequenceNumber& other) const { return (value - other.value) < 0; } + uint32_t getValue() const { return uint32_t(value); } + operator uint32_t() const { return uint32_t(value); } + + void encode(Buffer& buffer) const; + void decode(Buffer& buffer); + uint32_t encodedSize() const; + + template <class S> void serialize(S& s) { s(value); } + + friend inline int32_t operator-(const SequenceNumber& a, const SequenceNumber& b); +}; + +inline int32_t operator-(const SequenceNumber& a, const SequenceNumber& b) { + return int32_t(a.value - b.value); +} + +struct Window +{ + SequenceNumber hwm; + SequenceNumber lwm; +}; + +QPID_COMMON_EXTERN std::ostream& operator<<(std::ostream& o, const SequenceNumber& n); + +}} // namespace qpid::framing + + +#endif diff --git a/cpp/include/qpid/framing/SequenceSet.h b/cpp/include/qpid/framing/SequenceSet.h new file mode 100644 index 0000000000..39395e9ad7 --- /dev/null +++ b/cpp/include/qpid/framing/SequenceSet.h @@ -0,0 +1,69 @@ +/* + * + * 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 _framing_SequenceSet_h +#define _framing_SequenceSet_h + +#include "qpid/framing/SequenceNumber.h" +#include "qpid/RangeSet.h" +#include "qpid/CommonImportExport.h" + +namespace qpid { +namespace framing { +class Buffer; + +class SequenceSet : public RangeSet<SequenceNumber> { + public: + SequenceSet() {} + SequenceSet(const RangeSet<SequenceNumber>& r) + : RangeSet<SequenceNumber>(r) {} + SequenceSet(const SequenceNumber& s) { add(s); } + SequenceSet(const SequenceNumber& start, const SequenceNumber finish) { add(start,finish); } + + + void encode(Buffer& buffer) const; + void decode(Buffer& buffer); + uint32_t encodedSize() const; + + QPID_COMMON_EXTERN bool contains(const SequenceNumber& s) const; + QPID_COMMON_EXTERN void add(const SequenceNumber& s); + QPID_COMMON_EXTERN void add(const SequenceNumber& start, const SequenceNumber& finish); // Closed range + QPID_COMMON_EXTERN void add(const SequenceSet& set); + QPID_COMMON_EXTERN void remove(const SequenceNumber& s); + QPID_COMMON_EXTERN void remove(const SequenceNumber& start, const SequenceNumber& finish); // Closed range + QPID_COMMON_EXTERN void remove(const SequenceSet& set); + + template <class T> void for_each(T& t) const { + for (RangeIterator i = rangesBegin(); i != rangesEnd(); i++) + t(i->first(), i->last()); + } + + template <class T> void for_each(const T& t) const { + for (RangeIterator i = rangesBegin(); i != rangesEnd(); i++) + t(i->first(), i->last()); + } + + friend QPID_COMMON_EXTERN std::ostream& operator<<(std::ostream&, const SequenceSet&); +}; + +}} // namespace qpid::framing + + +#endif diff --git a/cpp/include/qpid/framing/StructHelper.h b/cpp/include/qpid/framing/StructHelper.h new file mode 100644 index 0000000000..fc9a7909cc --- /dev/null +++ b/cpp/include/qpid/framing/StructHelper.h @@ -0,0 +1,57 @@ +/* + * + * 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 _StructHelper_ +#define _StructHelper_ + +#include "qpid/Exception.h" +#include "qpid/CommonImportExport.h" +#include "qpid/framing/Buffer.h" + +#include <stdlib.h> // For alloca + +namespace qpid { +namespace framing { + +class StructHelper +{ +public: + + template <class T> void encode(const T t, std::string& data) { + uint32_t size = t.bodySize() + 2/*type*/; + data.resize(size); + Buffer wbuffer(const_cast<char*>(data.data()), size); + wbuffer.putShort(T::TYPE); + t.encodeStructBody(wbuffer); + } + + template <class T> void decode(T& t, const std::string& data) { + Buffer rbuffer(const_cast<char*>(data.data()), data.length()); + uint16_t type = rbuffer.getShort(); + if (type == T::TYPE) { + t.decodeStructBody(rbuffer); + } else { + throw Exception("Type code does not match"); + } + } +}; + +}} +#endif diff --git a/cpp/include/qpid/framing/Uuid.h b/cpp/include/qpid/framing/Uuid.h new file mode 100644 index 0000000000..0dfa7a58e7 --- /dev/null +++ b/cpp/include/qpid/framing/Uuid.h @@ -0,0 +1,91 @@ +#ifndef QPID_FRAMING_UUID_H +#define QPID_FRAMING_UUID_H + +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed 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 "qpid/CommonImportExport.h" +#include "qpid/sys/uuid.h" +#include "qpid/sys/IntegerTypes.h" + +#include <boost/array.hpp> + +#include <ostream> +#include <istream> + +namespace qpid { +namespace framing { + +class Buffer; + +/** + * A UUID is represented as a boost::array of 16 bytes. + * + * Full value semantics, operators ==, < etc. are provided by + * boost::array so Uuid can be the key type in a map etc. + */ +struct Uuid : public boost::array<uint8_t, 16> { + /** If unique is true, generate a unique ID else a null ID. */ + Uuid(bool unique=false) { if (unique) generate(); else clear(); } + + /** Copy from 16 bytes of data. */ + Uuid(const uint8_t* data) { assign(data); } + + /** Copy from 16 bytes of data. */ + void assign(const uint8_t* data) { + uuid_copy(c_array(), data); + } + + /** Set to a new unique identifier. */ + void generate() { uuid_generate(c_array()); } + + /** Set to all zeros. */ + void clear() { uuid_clear(c_array()); } + + /** Test for null (all zeros). */ + // Force int 0/!0 to false/true; avoids compile warnings. + bool isNull() { + return !!uuid_is_null(data()); + } + + // Default op= and copy ctor are fine. + // boost::array gives us ==, < etc. + + QPID_COMMON_EXTERN void encode(framing::Buffer& buf) const; + QPID_COMMON_EXTERN void decode(framing::Buffer& buf); + QPID_COMMON_EXTERN uint32_t encodedSize() const { return size(); } + + /** String value in format 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb. */ + QPID_COMMON_EXTERN std::string str() const; + + template <class S> void serialize(S& s) { + s.raw(begin(), size()); + } +}; + +/** Print in format 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb. */ +QPID_COMMON_EXTERN std::ostream& operator<<(std::ostream&, Uuid); + +/** Read from format 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb. */ +QPID_COMMON_EXTERN std::istream& operator>>(std::istream&, Uuid&); + +}} // namespace qpid::framing + + + +#endif /*!QPID_FRAMING_UUID_H*/ diff --git a/cpp/include/qpid/framing/amqp_types.h b/cpp/include/qpid/framing/amqp_types.h new file mode 100644 index 0000000000..d9088b7a12 --- /dev/null +++ b/cpp/include/qpid/framing/amqp_types.h @@ -0,0 +1,66 @@ +#ifndef AMQP_TYPES_H +#define AMQP_TYPES_H +/* + * + * 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. + * + */ + +/** \file + * Definitions and forward declarations of all types used + * in AMQP messages. + */ + +#include "qpid/sys/IntegerTypes.h" +#include <string> + +namespace qpid { +namespace framing { + +using std::string; +typedef uint8_t FrameType; +typedef uint16_t ChannelId; +typedef uint32_t BatchOffset; +typedef uint8_t ClassId; +typedef uint8_t MethodId; +typedef uint16_t ReplyCode; + +// Types represented by classes. +class Content; +class FieldTable; +class SequenceNumberSet; +struct Uuid; + +// Useful constants + +/** Maximum channel ID used by broker. Reserve high bit for internal use.*/ +const ChannelId CHANNEL_MAX=(ChannelId(~1))>>1; +const ChannelId CHANNEL_HIGH_BIT= ChannelId(~CHANNEL_MAX); + +// Forward declare class types +class FramingContent; +class FieldTable; +class SequenceNumberSet; +class SequenceSet; +struct Uuid; + +// Enum types +enum DeliveryMode { TRANSIENT = 1, PERSISTENT = 2}; + +}} // namespace qpid::framing +#endif diff --git a/cpp/include/qpid/framing/amqp_types_full.h b/cpp/include/qpid/framing/amqp_types_full.h new file mode 100644 index 0000000000..c5d84dedea --- /dev/null +++ b/cpp/include/qpid/framing/amqp_types_full.h @@ -0,0 +1,38 @@ +#ifndef _framing_amqp_types_decl_h +#define _framing_amqp_types_decl_h + +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed 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. + * + */ + +/** \file + * Definitions and full declarations of all types used + * in AMQP messages. + * + * It's better to include amqp_types.h in another header instead of this file + * unless the header actually needs the full declarations. Including + * full declarations when forward declarations would increase compile + * times. + */ + +#include "qpid/framing/amqp_types.h" +#include "qpid/framing/Array.h" +#include "qpid/framing/FieldTable.h" +#include "qpid/framing/SequenceSet.h" +#include "qpid/framing/Uuid.h" + +#endif /*!_framing_amqp_types_decl_h*/ |