diff options
Diffstat (limited to 'cpp/src/qpid/messaging')
-rw-r--r-- | cpp/src/qpid/messaging/Address.cpp | 203 | ||||
-rw-r--r-- | cpp/src/qpid/messaging/AddressParser.cpp | 263 | ||||
-rw-r--r-- | cpp/src/qpid/messaging/AddressParser.h | 65 | ||||
-rw-r--r-- | cpp/src/qpid/messaging/Connection.cpp | 24 | ||||
-rw-r--r-- | cpp/src/qpid/messaging/ConnectionImpl.h | 1 |
5 files changed, 346 insertions, 210 deletions
diff --git a/cpp/src/qpid/messaging/Address.cpp b/cpp/src/qpid/messaging/Address.cpp index ff72f62705..057196a957 100644 --- a/cpp/src/qpid/messaging/Address.cpp +++ b/cpp/src/qpid/messaging/Address.cpp @@ -156,207 +156,4 @@ 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((boost::format("%1%, character %2% of %3%") % message % current % input).str()); -} - -bool AddressParser::parse(Address& address) -{ - std::string name; - if (readName(name)) { - if (name.find('#') == 0) name = qpid::framing::Uuid(true).str() + name; - address.setName(name); - if (readChar('/')) { - std::string subject; - if (readSubject(subject)) { - address.setSubject(subject); - } else { - return error("Expected subject after /"); - } - } - if (readChar(';')) { - Variant options = Variant::Map(); - if (readMap(options)) { - address.setOptions(options.asMap()); - } - } - //skip trailing whitespace - while (!eos() && iswhitespace()) ++current; - return eos() || error("Unexpected chars in address: " + input.substr(current)); - } 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, expected ':'"); - } - } else { - return false; - } -} - -bool AddressParser::readKey(std::string& key) -{ - return readWord(key); -} - -bool AddressParser::readValue(Variant& value) -{ - return readSimpleValue(value) || readQuotedValue(value) || - readMap(value) || readList(value) || error("Expected value"); -} - -bool AddressParser::readString(std::string& 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::readName(std::string& name) -{ - return readQuotedString(name) || readWord(name, "/;"); -} - -bool AddressParser::readSubject(std::string& subject) -{ - return readQuotedString(subject) || readWord(subject, ";"); -} - -bool AddressParser::readQuotedString(std::string& s) -{ - return readString(s, '"') || readString(s, '\''); -} - -bool AddressParser::readQuotedValue(Variant& value) -{ - std::string s; - if (readQuotedString(s)) { - value = s; - return true; - } else { - return false; - } -} - -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, const std::string& delims) -{ - //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() && !in(delims)) ++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 in(RESERVED); -} - -bool AddressParser::in(const std::string& chars) -{ - return chars.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/AddressParser.cpp b/cpp/src/qpid/messaging/AddressParser.cpp new file mode 100644 index 0000000000..265b5fe195 --- /dev/null +++ b/cpp/src/qpid/messaging/AddressParser.cpp @@ -0,0 +1,263 @@ +/* + * + * 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 "AddressParser.h" +#include "qpid/framing/Uuid.h" +#include <boost/format.hpp> + +namespace qpid { +namespace messaging { + +AddressParser::AddressParser(const std::string& s) : input(s), current(0) {} + +bool AddressParser::error(const std::string& message) +{ + throw MalformedAddress((boost::format("%1%, character %2% of %3%") % message % current % input).str()); +} + +bool AddressParser::parse(Address& address) +{ + std::string name; + if (readName(name)) { + if (name.find('#') == 0) name = qpid::framing::Uuid(true).str() + name; + address.setName(name); + if (readChar('/')) { + std::string subject; + if (readSubject(subject)) { + address.setSubject(subject); + } else { + return error("Expected subject after /"); + } + } + if (readChar(';')) { + Variant options = Variant::Map(); + if (readMap(options)) { + address.setOptions(options.asMap()); + } + } + //skip trailing whitespace + while (!eos() && iswhitespace()) ++current; + return eos() || error("Unexpected chars in address: " + input.substr(current)); + } else { + return input.empty() || error("Expected name"); + } +} + +bool AddressParser::parseMap(Variant::Map& map) +{ + if (readChar('{')) { + readMapEntries(map); + return readChar('}') || error("Unmatched '{'!"); + } else { + return false; + } +} + +bool AddressParser::parseList(Variant::List& list) +{ + if (readChar('[')) { + readListItems(list); + return readChar(']') || error("Unmatched '['!"); + } else { + return false; + } +} + + +bool AddressParser::readList(Variant& value) +{ + if (readChar('[')) { + value = Variant::List(); + readListItems(value.asList()); + return readChar(']') || error("Unmatched '['!"); + } else { + return false; + } +} + +void AddressParser::readListItems(Variant::List& list) +{ + Variant item; + while (readValue(item)) { + list.push_back(item); + if (!readChar(',')) break; + } +} + +bool AddressParser::readMap(Variant& value) +{ + if (readChar('{')) { + value = Variant::Map(); + readMapEntries(value.asMap()); + return readChar('}') || error("Unmatched '{'!"); + } else { + return false; + } +} + +void AddressParser::readMapEntries(Variant::Map& map) +{ + while (readKeyValuePair(map) && readChar(',')) {} +} + +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, expected ':'"); + } + } else { + return false; + } +} + +bool AddressParser::readKey(std::string& key) +{ + return readWord(key); +} + +bool AddressParser::readValue(Variant& value) +{ + return readSimpleValue(value) || readQuotedValue(value) || + readMap(value) || readList(value) || error("Expected value"); +} + +bool AddressParser::readString(std::string& 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::readName(std::string& name) +{ + return readQuotedString(name) || readWord(name, "/;"); +} + +bool AddressParser::readSubject(std::string& subject) +{ + return readQuotedString(subject) || readWord(subject, ";"); +} + +bool AddressParser::readQuotedString(std::string& s) +{ + return readString(s, '"') || readString(s, '\''); +} + +bool AddressParser::readQuotedValue(Variant& value) +{ + std::string s; + if (readQuotedString(s)) { + value = s; + return true; + } else { + return false; + } +} + +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, const std::string& delims) +{ + //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() && !in(delims)) ++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 in(RESERVED); +} + +bool AddressParser::in(const std::string& chars) +{ + return chars.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/AddressParser.h b/cpp/src/qpid/messaging/AddressParser.h new file mode 100644 index 0000000000..801b5cead1 --- /dev/null +++ b/cpp/src/qpid/messaging/AddressParser.h @@ -0,0 +1,65 @@ +#ifndef QPID_MESSAGING_ADDRESSPARSER_H +#define QPID_MESSAGING_ADDRESSPARSER_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/messaging/Address.h" + +namespace qpid { +namespace messaging { + +class AddressParser +{ + public: + AddressParser(const std::string&); + bool parse(Address& address); + bool parseMap(Variant::Map& map); + bool parseList(Variant::List& list); + private: + const std::string& input; + std::string::size_type current; + static const std::string RESERVED; + + bool readChar(char c); + bool readQuotedString(std::string& s); + bool readQuotedValue(Variant& value); + bool readString(std::string& value, char delimiter); + bool readWord(std::string& word, const std::string& delims = RESERVED); + 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 readName(std::string& name); + bool readSubject(std::string& subject); + bool error(const std::string& message); + bool eos(); + bool iswhitespace(); + bool in(const std::string& delims); + bool isreserved(); + void readListItems(Variant::List& list); + void readMapEntries(Variant::Map& map); +}; + +}} // namespace qpid::messaging + +#endif /*!QPID_MESSAGING_ADDRESSPARSER_H*/ diff --git a/cpp/src/qpid/messaging/Connection.cpp b/cpp/src/qpid/messaging/Connection.cpp index 64ca962317..06006c9d20 100644 --- a/cpp/src/qpid/messaging/Connection.cpp +++ b/cpp/src/qpid/messaging/Connection.cpp @@ -19,6 +19,7 @@ * */ #include "qpid/messaging/Connection.h" +#include "qpid/messaging/AddressParser.h" #include "qpid/messaging/ConnectionImpl.h" #include "qpid/messaging/Session.h" #include "qpid/messaging/SessionImpl.h" @@ -37,18 +38,27 @@ namespace messaging { using qpid::client::PI; -Connection Connection::open(const std::string& url, const Variant::Map& options) -{ - //only support amqp 0-10 at present - Connection connection(new qpid::client::amqp0_10::ConnectionImpl(url, options)); - return connection; -} - Connection::Connection(ConnectionImpl* impl) { PI::ctor(*this, impl); } Connection::Connection(const Connection& c) : qpid::client::Handle<ConnectionImpl>() { PI::copy(*this, c); } Connection& Connection::operator=(const Connection& c) { return PI::assign(*this, c); } Connection::~Connection() { PI::dtor(*this); } +Connection::Connection(const std::string& o) +{ + Variant::Map options; + AddressParser parser(o); + if (parser.parseMap(options)) { + PI::ctor(*this, new qpid::client::amqp0_10::ConnectionImpl(options)); + } else { + throw InvalidOptionString(o); + } +} +Connection::Connection(const Variant::Map& options) +{ + PI::ctor(*this, new qpid::client::amqp0_10::ConnectionImpl(options)); +} + +void Connection::open(const std::string& url) { impl->open(url); } void Connection::close() { impl->close(); } Session Connection::newSession(const char* name) { return impl->newSession(false, name); } Session Connection::newSession(const std::string& name) { return impl->newSession(false, name); } diff --git a/cpp/src/qpid/messaging/ConnectionImpl.h b/cpp/src/qpid/messaging/ConnectionImpl.h index 4eff68ff9d..589c9fbe57 100644 --- a/cpp/src/qpid/messaging/ConnectionImpl.h +++ b/cpp/src/qpid/messaging/ConnectionImpl.h @@ -36,6 +36,7 @@ class ConnectionImpl : public virtual qpid::RefCounted { public: virtual ~ConnectionImpl() {} + virtual void open(const std::string& url) = 0; virtual void close() = 0; virtual Session newSession(bool transactional, const std::string& name) = 0; virtual Session getSession(const std::string& name) const = 0; |