diff options
| author | Gordon Sim <gsim@apache.org> | 2009-11-11 13:15:44 +0000 |
|---|---|---|
| committer | Gordon Sim <gsim@apache.org> | 2009-11-11 13:15:44 +0000 |
| commit | 856e5e92e6ad376cdbe3c8c2b8414415a17e579c (patch) | |
| tree | 4b60cb6515225326ac1c60c04ef42765ccd3aa5c /cpp/src/qpid/messaging | |
| parent | 36c30decd36c54d5d361cfa64818d10527960b85 (diff) | |
| download | qpid-python-856e5e92e6ad376cdbe3c8c2b8414415a17e579c.tar.gz | |
Added support for address parsing, create/assert/delete policies
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@834869 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/messaging')
| -rw-r--r-- | cpp/src/qpid/messaging/Address.cpp | 291 | ||||
| -rw-r--r-- | cpp/src/qpid/messaging/Filter.cpp | 39 | ||||
| -rw-r--r-- | cpp/src/qpid/messaging/Session.cpp | 25 | ||||
| -rw-r--r-- | cpp/src/qpid/messaging/SessionImpl.h | 6 | ||||
| -rw-r--r-- | cpp/src/qpid/messaging/Variant.cpp | 1 |
5 files changed, 289 insertions, 73 deletions
diff --git a/cpp/src/qpid/messaging/Address.cpp b/cpp/src/qpid/messaging/Address.cpp index 813a8e1377..edb1ddc79d 100644 --- a/cpp/src/qpid/messaging/Address.cpp +++ b/cpp/src/qpid/messaging/Address.cpp @@ -19,28 +19,293 @@ * */ #include "qpid/messaging/Address.h" +#include "qpid/framing/Uuid.h" +#include <sstream> +#include <boost/format.hpp> namespace qpid { namespace messaging { -Address::Address() {} -Address::Address(const std::string& address) : value(address) {} -Address::Address(const std::string& address, const std::string& t) : value(address), type(t) {} -Address::operator const std::string&() const { return value; } -const std::string& Address::toStr() const { return value; } -Address::operator bool() const { return !value.empty(); } -bool Address::operator !() const { return value.empty(); } +namespace { +const std::string SUBJECT_DIVIDER = "/"; +const std::string SPACE = " "; +const std::string TYPE = "type"; +} +class AddressImpl +{ + public: + std::string name; + std::string subject; + Variant::Map options; + + AddressImpl() {} + AddressImpl(const std::string& n, const std::string& s, const Variant::Map& o) : + name(n), subject(s), options(o) {} +}; + +class AddressParser +{ + public: + AddressParser(const std::string&); + bool parse(Address& address); + private: + const std::string& input; + std::string::size_type current; + static const std::string RESERVED; + + bool readChar(char c); + bool readQuotedString(Variant& value); + bool readString(Variant& value, char delimiter); + bool readWord(std::string& word); + bool readSimpleValue(Variant& word); + bool readKey(std::string& key); + bool readValue(Variant& value); + bool readKeyValuePair(Variant::Map& map); + bool readMap(Variant& value); + bool readList(Variant& value); + bool error(const std::string& message); + bool eos(); + bool iswhitespace(); + bool isreserved(); +}; + +Address::Address() : impl(new AddressImpl()) {} +Address::Address(const std::string& address) : impl(new AddressImpl()) +{ + AddressParser parser(address); + parser.parse(*this); +} +Address::Address(const std::string& name, const std::string& subject, const Variant::Map& options, + const std::string& type) + : impl(new AddressImpl(name, subject, options)) { setType(type); } +Address::Address(const Address& a) : + impl(new AddressImpl(a.impl->name, a.impl->subject, a.impl->options)) {} +Address::~Address() { delete impl; } + +Address& Address::operator=(const Address& a) { *impl = *a.impl; return *this; } + + +std::string Address::toStr() const +{ + std::stringstream out; + out << impl->name; + if (!impl->subject.empty()) out << SUBJECT_DIVIDER << impl->subject; + if (!impl->options.empty()) out << " {" << impl->options << "}"; + return out.str(); +} +Address::operator bool() const { return !impl->name.empty(); } +bool Address::operator !() const { return impl->name.empty(); } + +const std::string& Address::getName() const { return impl->name; } +void Address::setName(const std::string& name) { impl->name = name; } +const std::string& Address::getSubject() const { return impl->subject; } +bool Address::hasSubject() const { return !(impl->subject.empty()); } +void Address::setSubject(const std::string& subject) { impl->subject = subject; } +const Variant::Map& Address::getOptions() const { return impl->options; } +Variant::Map& Address::getOptions() { return impl->options; } +void Address::setOptions(const Variant::Map& options) { impl->options = options; } + + +namespace{ +const Variant EMPTY_VARIANT; +const std::string EMPTY_STRING; +} -const std::string TYPE_SEPARATOR(":"); +std::string Address::getType() const +{ + const Variant& type = getOption(TYPE); + return type.isVoid() ? EMPTY_STRING : type.asString(); +} +void Address::setType(const std::string& type) { impl->options[TYPE] = type; } + +const Variant& Address::getOption(const std::string& key) const +{ + Variant::Map::const_iterator i = impl->options.find(key); + if (i == impl->options.end()) return EMPTY_VARIANT; + else return i->second; +} std::ostream& operator<<(std::ostream& out, const Address& address) { - if (!address.type.empty()) { - out << address.type; - out << TYPE_SEPARATOR; - } - out << address.value; + out << address.toStr(); return out; } +InvalidAddress::InvalidAddress(const std::string& msg) : Exception(msg) {} + +MalformedAddress::MalformedAddress(const std::string& msg) : Exception(msg) {} + +AddressParser::AddressParser(const std::string& s) : input(s), current(0) {} + +bool AddressParser::error(const std::string& message) +{ + throw MalformedAddress(message);//TODO: add more debug detail to error message (position etc) +} + +bool AddressParser::parse(Address& address) +{ + std::string name; + if (readWord(name)) { + if (name.find('#') == 0) name = qpid::framing::Uuid(true).str() + name; + address.setName(name); + if (readChar('/')) { + std::string subject; + if (readWord(subject)) { + address.setSubject(subject); + } else { + return error("Expected subject after /"); + } + } + Variant options = Variant::Map(); + if (readMap(options)) { + address.setOptions(options.asMap()); + } + return true; + } else { + return input.empty() || error("Expected name"); + } +} + +bool AddressParser::readList(Variant& value) +{ + if (readChar('[')) { + value = Variant::List(); + Variant item; + while (readValue(item)) { + value.asList().push_back(item); + if (!readChar(',')) break; + } + return readChar(']') || error("Unmatched '['!"); + } else { + return false; + } +} + +bool AddressParser::readMap(Variant& value) +{ + if (readChar('{')) { + value = Variant::Map(); + while (readKeyValuePair(value.asMap()) && readChar(',')) {} + return readChar('}') || error("Unmatched '{'!"); + } else { + return false; + } +} + +bool AddressParser::readKeyValuePair(Variant::Map& map) +{ + std::string key; + Variant value; + if (readKey(key)) { + if (readChar(':') && readValue(value)) { + map[key] = value; + return true; + } else { + return error("Bad key-value pair!"); + } + } else { + return false; + } +} + +bool AddressParser::readKey(std::string& key) +{ + return readWord(key); +} + +bool AddressParser::readValue(Variant& value) +{ + return readSimpleValue(value) || readQuotedString(value) || + readMap(value) || readList(value) || error("Expected value"); +} + +bool AddressParser::readString(Variant& value, char delimiter) +{ + if (readChar(delimiter)) { + std::string::size_type start = current++; + while (!eos()) { + if (input.at(current) == delimiter) { + if (current > start) { + value = input.substr(start, current - start); + } else { + value = ""; + } + ++current; + return true; + } else { + ++current; + } + } + return error("Unmatched delimiter"); + } else { + return false; + } +} + +bool AddressParser::readQuotedString(Variant& value) +{ + return readString(value, '"') || readString(value, '\''); +} + +bool AddressParser::readSimpleValue(Variant& value) +{ + std::string s; + if (readWord(s)) { + value = s; + try { value = value.asInt64(); return true; } catch (const InvalidConversion&) {} + try { value = value.asDouble(); return true; } catch (const InvalidConversion&) {} + return true; + } else { + return false; + } +} + +bool AddressParser::readWord(std::string& value) +{ + //skip leading whitespace + while (!eos() && iswhitespace()) ++current; + + //read any number of non-whitespace, non-reserved chars into value + std::string::size_type start = current; + while (!eos() && !iswhitespace() && !isreserved()) ++current; + + if (current > start) { + value = input.substr(start, current - start); + return true; + } else { + return false; + } +} + +bool AddressParser::readChar(char c) +{ + while (!eos()) { + if (iswhitespace()) { + ++current; + } else if (input.at(current) == c) { + ++current; + return true; + } else { + return false; + } + } + return false; +} + +bool AddressParser::iswhitespace() +{ + return ::isspace(input.at(current)); +} + +bool AddressParser::isreserved() +{ + return RESERVED.find(input.at(current)) != std::string::npos; +} + +bool AddressParser::eos() +{ + return current >= input.size(); +} + +const std::string AddressParser::RESERVED = "\'\"{}[],:/"; }} // namespace qpid::messaging diff --git a/cpp/src/qpid/messaging/Filter.cpp b/cpp/src/qpid/messaging/Filter.cpp deleted file mode 100644 index b06cbdb373..0000000000 --- a/cpp/src/qpid/messaging/Filter.cpp +++ /dev/null @@ -1,39 +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 "qpid/messaging/Filter.h" - -namespace qpid { -namespace client { -} - -namespace messaging { - -Filter::Filter(std::string t, std::string pattern) : type(t) { patterns.push_back(pattern); } -Filter::Filter(std::string t, std::string pattern1, std::string pattern2) : type(t) -{ - patterns.push_back(pattern1); - patterns.push_back(pattern2); -} - -const std::string Filter::WILDCARD("WILDCARD"); -const std::string Filter::EXACT_MATCH("EXACT_MATCH"); - -}} // namespace qpid::messaging diff --git a/cpp/src/qpid/messaging/Session.cpp b/cpp/src/qpid/messaging/Session.cpp index 62b1ca0dcf..b69b575b26 100644 --- a/cpp/src/qpid/messaging/Session.cpp +++ b/cpp/src/qpid/messaging/Session.cpp @@ -20,7 +20,6 @@ */ #include "qpid/messaging/Session.h" #include "qpid/messaging/Address.h" -#include "qpid/messaging/Filter.h" #include "qpid/messaging/Message.h" #include "qpid/messaging/Sender.h" #include "qpid/messaging/Receiver.h" @@ -48,30 +47,22 @@ void Session::acknowledge() { impl->acknowledge(); } void Session::reject(Message& m) { impl->reject(m); } void Session::close() { impl->close(); } -Sender Session::createSender(const Address& address, const VariantMap& options) +Sender Session::createSender(const Address& address) { - return impl->createSender(address, options); + return impl->createSender(address); } -Receiver Session::createReceiver(const Address& address, const VariantMap& options) +Receiver Session::createReceiver(const Address& address) { - return impl->createReceiver(address, options); -} -Receiver Session::createReceiver(const Address& address, const Filter& filter, const VariantMap& options) -{ - return impl->createReceiver(address, filter, options); + return impl->createReceiver(address); } -Sender Session::createSender(const std::string& address, const VariantMap& options) -{ - return impl->createSender(Address(address), options); -} -Receiver Session::createReceiver(const std::string& address, const VariantMap& options) +Sender Session::createSender(const std::string& address) { - return impl->createReceiver(Address(address), options); + return impl->createSender(Address(address)); } -Receiver Session::createReceiver(const std::string& address, const Filter& filter, const VariantMap& options) +Receiver Session::createReceiver(const std::string& address) { - return impl->createReceiver(Address(address), filter, options); + return impl->createReceiver(Address(address)); } Address Session::createTempQueue(const std::string& baseName) diff --git a/cpp/src/qpid/messaging/SessionImpl.h b/cpp/src/qpid/messaging/SessionImpl.h index 0933cea9c8..e48e7a4d02 100644 --- a/cpp/src/qpid/messaging/SessionImpl.h +++ b/cpp/src/qpid/messaging/SessionImpl.h @@ -23,7 +23,6 @@ */ #include "qpid/RefCounted.h" #include <string> -#include "qpid/messaging/Variant.h" #include "qpid/sys/Time.h" namespace qpid { @@ -53,9 +52,8 @@ class SessionImpl : public virtual qpid::RefCounted virtual Message fetch(qpid::sys::Duration timeout) = 0; virtual bool dispatch(qpid::sys::Duration timeout) = 0; virtual Address createTempQueue(const std::string& baseName) = 0; - virtual Sender createSender(const Address& address, const VariantMap& options) = 0; - virtual Receiver createReceiver(const Address& address, const VariantMap& options) = 0; - virtual Receiver createReceiver(const Address& address, const Filter& filter, const VariantMap& options) = 0; + virtual Sender createSender(const Address& address) = 0; + virtual Receiver createReceiver(const Address& address) = 0; virtual uint32_t available() = 0; virtual uint32_t pendingAck() = 0; private: diff --git a/cpp/src/qpid/messaging/Variant.cpp b/cpp/src/qpid/messaging/Variant.cpp index 4e37134b39..3b0c3312ca 100644 --- a/cpp/src/qpid/messaging/Variant.cpp +++ b/cpp/src/qpid/messaging/Variant.cpp @@ -529,6 +529,7 @@ Variant& Variant::operator=(const Variant& v) } VariantType Variant::getType() const { return impl->getType(); } +bool Variant::isVoid() const { return impl->getType() == VAR_VOID; } bool Variant::asBool() const { return impl->asBool(); } uint8_t Variant::asUint8() const { return impl->asUint8(); } uint16_t Variant::asUint16() const { return impl->asUint16(); } |
