diff options
author | Gordon Sim <gsim@apache.org> | 2007-11-06 17:27:27 +0000 |
---|---|---|
committer | Gordon Sim <gsim@apache.org> | 2007-11-06 17:27:27 +0000 |
commit | a2ded139f371d273afa858f49a5b7f6e0efc2394 (patch) | |
tree | 8fc04da8ffe3aa819843a101a75d98429f27eaa5 /cpp | |
parent | a1a0ecfbf02293cf917db5e56d65d367be5ad5a7 (diff) | |
download | qpid-python-a2ded139f371d273afa858f49a5b7f6e0efc2394.tar.gz |
Add support for array type to c++ (and python, decode only for now)
Change the type of the in-doubt field in dtx-coordination.recover to an array (to bring in line with amqp spec)
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@592494 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rwxr-xr-x | cpp/rubygen/cppgen.rb | 1 | ||||
-rw-r--r-- | cpp/src/Makefile.am | 2 | ||||
-rw-r--r-- | cpp/src/qpid/broker/DtxHandlerImpl.cpp | 22 | ||||
-rw-r--r-- | cpp/src/qpid/framing/Array.cpp | 114 | ||||
-rw-r--r-- | cpp/src/qpid/framing/Array.h | 75 | ||||
-rw-r--r-- | cpp/src/qpid/framing/FieldValue.cpp | 9 | ||||
-rw-r--r-- | cpp/src/qpid/framing/FieldValue.h | 2 | ||||
-rw-r--r-- | cpp/src/qpid/framing/amqp_types_full.h | 1 | ||||
-rw-r--r-- | cpp/src/tests/Array.cpp | 78 | ||||
-rw-r--r-- | cpp/src/tests/Makefile.am | 2 |
10 files changed, 287 insertions, 19 deletions
diff --git a/cpp/rubygen/cppgen.rb b/cpp/rubygen/cppgen.rb index 5db5793649..60a653e18d 100755 --- a/cpp/rubygen/cppgen.rb +++ b/cpp/rubygen/cppgen.rb @@ -122,6 +122,7 @@ class AmqpDomain "longstr"=>CppType.new("string").passcref.retcref.code("LongString"), "shortstr"=>CppType.new("string").passcref.retcref.code("ShortString"), "table"=>CppType.new("FieldTable").passcref.retcref, + "array"=>CppType.new("Array").passcref.retcref, "content"=>CppType.new("Content").passcref.retcref, "rfc1982-long-set"=>CppType.new("SequenceNumberSet").passcref.retcref, "long-struct"=>CppType.new("string").passcref.retcref.code("LongString"), diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index 09bdb351b1..681b8ed8ed 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -98,6 +98,7 @@ libqpidcommon_la_SOURCES = \ qpid/framing/AMQFrame.cpp \ qpid/framing/AMQHeaderBody.cpp \ qpid/framing/AMQHeartbeatBody.cpp \ + qpid/framing/Array.cpp \ qpid/framing/BasicHeaderProperties.cpp \ qpid/framing/BodyHandler.cpp \ qpid/framing/ChannelAdapter.cpp \ @@ -337,6 +338,7 @@ nobase_include_HEADERS = \ qpid/framing/AMQMethodBody.h \ qpid/framing/AMQP_HighestVersion.h \ qpid/framing/AccumulatedAck.h \ + qpid/framing/Array.h \ qpid/framing/BasicHeaderProperties.h \ qpid/framing/Blob.h \ qpid/framing/BodyHandler.h \ diff --git a/cpp/src/qpid/broker/DtxHandlerImpl.cpp b/cpp/src/qpid/broker/DtxHandlerImpl.cpp index ec042ff56a..533872e849 100644 --- a/cpp/src/qpid/broker/DtxHandlerImpl.cpp +++ b/cpp/src/qpid/broker/DtxHandlerImpl.cpp @@ -20,6 +20,7 @@ #include <boost/format.hpp> #include "Broker.h" #include "qpid/framing/constants.h" +#include "qpid/framing/Array.h" using namespace qpid::broker; using namespace qpid::framing; @@ -136,25 +137,14 @@ DtxCoordinationRecoverResult DtxHandlerImpl::recover(u_int16_t /*ticket*/, // strictly 'legal', but that is ok for testing std::set<std::string> xids; getBroker().getStore().collectPreparedXids(xids); - uint size(0); - for (std::set<std::string>::iterator i = xids.begin(); i != xids.end(); i++) { - size += i->size() + 1/*shortstr size*/; - } - char* bytes = static_cast<char*>(::alloca(size + 4/*longstr size*/)); - Buffer wbuffer(bytes, size + 4/*longstr size*/); - wbuffer.putLong(size); + //TODO: remove the need to copy from one container type to another + std::vector<std::string> data; for (std::set<std::string>::iterator i = xids.begin(); i != xids.end(); i++) { - wbuffer.putShortString(*i); + data.push_back(*i); } - - Buffer rbuffer(bytes, size + 4/*longstr size*/); - string data; - rbuffer.getLongString(data); - - FieldTable response; - response.setString("xids", data); - return DtxCoordinationRecoverResult(response); + Array indoubt(data); + return DtxCoordinationRecoverResult(indoubt); } void DtxHandlerImpl::forget(u_int16_t /*ticket*/, diff --git a/cpp/src/qpid/framing/Array.cpp b/cpp/src/qpid/framing/Array.cpp new file mode 100644 index 0000000000..1215c8a28b --- /dev/null +++ b/cpp/src/qpid/framing/Array.cpp @@ -0,0 +1,114 @@ +/* + * + * 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 "Array.h" +#include "Buffer.h" +#include "FieldValue.h" +#include "qpid/Exception.h" +#include "qpid/framing/reply_exceptions.h" +#include <assert.h> + +namespace qpid { +namespace framing { + +Array::Array() : typeOctet(0xF0/*void*/) {} + +Array::Array(const std::vector<std::string>& in) +{ + typeOctet = 0xA4; + for (std::vector<std::string>::const_iterator i = in.begin(); i != in.end(); ++i) { + ValuePtr value(new StringValue(*i)); + values.push_back(value); + } +} + +uint32_t Array::size() const { + //note: size is only included when used as a 'top level' type + uint32_t len(4/*size*/ + 1/*type*/ + 4/*count*/); + for(ValueVector::const_iterator i = values.begin(); i != values.end(); ++i) { + len += (*i)->getData().size(); + } + return len; +} + +int Array::count() const { + return values.size(); +} + +std::ostream& operator<<(std::ostream& out, const Array& t) { + out << "{"; + for(Array::ValueVector::const_iterator i = t.values.begin(); i != t.values.end(); ++i) { + if (i != t.values.begin()) out << ", "; + out << *(i->get()); + } + return out << "}"; +} + +void Array::encode(Buffer& buffer) const{ + buffer.putLong(size() - 4);//size added only when array is a top-level type + buffer.putOctet(typeOctet); + buffer.putLong(count()); + for (ValueVector::const_iterator i = values.begin(); i!=values.end(); ++i) { + (*i)->getData().encode(buffer); + } +} + +void Array::decode(Buffer& buffer){ + uint32_t size = buffer.getLong();//size added only when array is a top-level type + uint32_t available = buffer.available(); + if (available < size) { + throw SyntaxErrorException(QPID_MSG("Not enough data for array, expected " + << size << " bytes but only " << available << " available")); + } + typeOctet = buffer.getOctet(); + uint32_t count = buffer.getLong(); + + FieldValue dummy; + dummy.setType(typeOctet); + available = buffer.available(); + if (available < count * dummy.getData().size()) { + throw SyntaxErrorException(QPID_MSG("Not enough data for array, expected " + << count << " items of " << dummy.getData().size() + << " bytes each but only " << available << " bytes available")); + } + + for (uint32_t i = 0; i < count; i++) { + ValuePtr value(new FieldValue); + value->setType(typeOctet); + value->getData().decode(buffer); + values.push_back(ValuePtr(value)); + } +} + + +bool Array::operator==(const Array& x) const { + if (typeOctet != x.typeOctet) return false; + if (values.size() != x.values.size()) return false; + + for (ValueVector::const_iterator i = values.begin(), j = x.values.begin(); i != values.end(); ++i, ++j) { + if (*(i->get()) != *(j->get())) return false; + } + + return true; +} + + +} +} diff --git a/cpp/src/qpid/framing/Array.h b/cpp/src/qpid/framing/Array.h new file mode 100644 index 0000000000..6a13c63672 --- /dev/null +++ b/cpp/src/qpid/framing/Array.h @@ -0,0 +1,75 @@ +/* + * + * 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 "amqp_types.h" +#include "FieldValue.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; + + uint32_t size() const; + void encode(Buffer& buffer) const; + void decode(Buffer& buffer); + + int count() const; + bool operator==(const Array& other) const; + + Array(); + //only long string arrays can currently be created (any type can be decoded) + Array(const std::vector<std::string>& in); + + template <class T> + void collect(std::vector<T>& out) + { + for (ValueVector::const_iterator i = values.begin(); i != values.end(); ++i) { + out.push_back((*i)->get<T>()); + } + } + + private: + uint8_t typeOctet; + ValueVector values; + + ValueVector::const_iterator begin() const { return values.begin(); } + ValueVector::const_iterator end() const { return values.end(); } + + friend std::ostream& operator<<(std::ostream& out, const Array& body); +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/framing/FieldValue.cpp b/cpp/src/qpid/framing/FieldValue.cpp index 5526c9cb72..961b6017cd 100644 --- a/cpp/src/qpid/framing/FieldValue.cpp +++ b/cpp/src/qpid/framing/FieldValue.cpp @@ -25,9 +25,9 @@ namespace qpid { namespace framing { -void FieldValue::decode(Buffer& buffer) +void FieldValue::setType(uint8_t type) { - typeOctet = buffer.getOctet(); + typeOctet = type; uint8_t lenType = typeOctet >> 4; switch(lenType){ @@ -76,6 +76,11 @@ void FieldValue::decode(Buffer& buffer) default: throw SyntaxErrorException(QPID_MSG("Unknown field table value type: " << (int)typeOctet)); } +} + +void FieldValue::decode(Buffer& buffer) +{ + setType(buffer.getOctet()); data->decode(buffer); } diff --git a/cpp/src/qpid/framing/FieldValue.h b/cpp/src/qpid/framing/FieldValue.h index 3ea367c481..3ec95a99e1 100644 --- a/cpp/src/qpid/framing/FieldValue.h +++ b/cpp/src/qpid/framing/FieldValue.h @@ -78,6 +78,8 @@ class FieldValue { FieldValue(): data(0) {}; // Default assignment operator is fine + void setType(uint8_t type); + Data& getData() { return *data; } uint32_t size() const { return 1 + data->size(); }; bool empty() const { return data.get() == 0; } void encode(Buffer& buffer); diff --git a/cpp/src/qpid/framing/amqp_types_full.h b/cpp/src/qpid/framing/amqp_types_full.h index bf89d59980..f1ed44ec05 100644 --- a/cpp/src/qpid/framing/amqp_types_full.h +++ b/cpp/src/qpid/framing/amqp_types_full.h @@ -30,6 +30,7 @@ */ #include "amqp_types.h" +#include "Array.h" #include "FramingContent.h" #include "FieldTable.h" #include "SequenceNumberSet.h" diff --git a/cpp/src/tests/Array.cpp b/cpp/src/tests/Array.cpp new file mode 100644 index 0000000000..5bf7fadce0 --- /dev/null +++ b/cpp/src/tests/Array.cpp @@ -0,0 +1,78 @@ +/* + * + * 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 <sstream> +#include "qpid/framing/Array.h" +#include "qpid/framing/FieldValue.h" + +#include <boost/test/auto_unit_test.hpp> +BOOST_AUTO_TEST_SUITE(Array); + +using namespace qpid::framing; + +void populate(std::vector<std::string>& data, int count = 10) +{ + for (int i = 0; i < count; i++) { + std::stringstream out; + out << "item-" << i; + data.push_back(out.str()); + } +} + +BOOST_AUTO_TEST_CASE(testEncodeDecode) +{ + std::vector<std::string> data; + populate(data); + + Array a(data); + + char buff[200]; + Buffer wbuffer(buff, 200); + a.encode(wbuffer); + + Array b; + Buffer rbuffer(buff, 200); + b.decode(rbuffer); + BOOST_CHECK_EQUAL(a, b); + + std::vector<std::string> data2; + b.collect(data2); + //BOOST_CHECK_EQUAL(data, data2); + BOOST_CHECK(data == data2); +} + +BOOST_AUTO_TEST_CASE(testAssignment) +{ + std::vector<std::string> data; + populate(data); + Array b; + { + Array a(data); + b = a; + BOOST_CHECK_EQUAL(a, b); + } + std::vector<std::string> data2; + b.collect(data2); + //BOOST_CHECK_EQUAL(data, data2); + BOOST_CHECK(data == data2); +} + +BOOST_AUTO_TEST_SUITE_END(); diff --git a/cpp/src/tests/Makefile.am b/cpp/src/tests/Makefile.am index 37d59aa9e2..b4944c1294 100644 --- a/cpp/src/tests/Makefile.am +++ b/cpp/src/tests/Makefile.am @@ -31,7 +31,7 @@ unit_test_SOURCES= unit_test.cpp \ RefCounted.cpp RefCountedMap.cpp \ SessionState.cpp Blob.cpp logging.cpp \ Url.cpp Uuid.cpp \ - Shlib.cpp FieldValue.cpp FieldTable.cpp + Shlib.cpp FieldValue.cpp FieldTable.cpp Array.cpp check_LTLIBRARIES += libshlibtest.la libshlibtest_la_LDFLAGS = -module -rpath $(abs_builddir) |