From 41d33af55b9fbf4c664ccb56accb1a37bd1ef006 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Tue, 7 Oct 2008 17:27:06 +0000 Subject: broker: Fixed incorrect pass-by-reference of Queue::shared_ptr in several files. cluster: added FailoverExchange - send cluster membership to clients. client: added FailoverListener - receive cluster updates from failover exchange. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@702552 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/client/FailoverListener.cpp | 59 ++++++++++++++++++++++++++++++++ cpp/src/qpid/client/FailoverListener.h | 58 +++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 cpp/src/qpid/client/FailoverListener.cpp create mode 100644 cpp/src/qpid/client/FailoverListener.h (limited to 'cpp/src/qpid/client') diff --git a/cpp/src/qpid/client/FailoverListener.cpp b/cpp/src/qpid/client/FailoverListener.cpp new file mode 100644 index 0000000000..95c137d922 --- /dev/null +++ b/cpp/src/qpid/client/FailoverListener.cpp @@ -0,0 +1,59 @@ +/* + * + * 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 "FailoverListener.h" + +namespace qpid { +namespace client { + +static const std::string AMQ_FAILOVER("amq.failover"); + +FailoverListener::FailoverListener(Connection c) + : connection(c), session(c.newSession()), subscriptions(session) +{ + std::string qname=AMQ_FAILOVER + "." + session.getId().getName(); + if (session.exchangeQuery(arg::exchange=AMQ_FAILOVER).getType().empty()) + return; // Failover exchange not implemented. + session.queueDeclare(arg::queue=qname, arg::exclusive=true, arg::autoDelete=true); + session.exchangeBind(arg::queue=qname, arg::exchange=AMQ_FAILOVER); + subscriptions.subscribe(*this, qname, FlowControl::unlimited()); + thread = sys::Thread(subscriptions); +} + +FailoverListener::~FailoverListener() { + subscriptions.stop(); + if (thread.id()) thread.join(); +} + +void FailoverListener::received(Message& msg) { + sys::Mutex::ScopedLock l(lock); + knowBrokers.clear(); + framing::Array urlArray; + msg.getHeaders().getArray("amq.failover", urlArray); + for (framing::Array::ValueVector::const_iterator i = urlArray.begin(); i < urlArray.end(); ++i ) + knowBrokers.push_back(Url((*i)->get())); +} + +std::vector FailoverListener::getKnownBrokers() const { + sys::Mutex::ScopedLock l(lock); + return knowBrokers; +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/FailoverListener.h b/cpp/src/qpid/client/FailoverListener.h new file mode 100644 index 0000000000..2c06947300 --- /dev/null +++ b/cpp/src/qpid/client/FailoverListener.h @@ -0,0 +1,58 @@ +#ifndef QPID_CLIENT_FAILOVERLISTENER_H +#define QPID_CLIENT_FAILOVERLISTENER_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/client/Connection.h" +#include "qpid/client/Session.h" +#include "qpid/client/MessageListener.h" +#include "qpid/client/SubscriptionManager.h" +#include "qpid/Url.h" +#include "qpid/sys/Mutex.h" +#include "qpid/sys/Thread.h" +#include + +namespace qpid { +namespace client { + +/** + * @internal Listen for failover updates from the amq.failover exchange. + */ +class FailoverListener : public MessageListener +{ + public: + FailoverListener(Connection); + ~FailoverListener(); + std::vector getKnownBrokers() const; + void received(Message& msg); + + private: + mutable sys::Mutex lock; + Connection connection; + Session session; + SubscriptionManager subscriptions; + sys::Thread thread; + std::vector knowBrokers; +}; +}} // namespace qpid::client + +#endif /*!QPID_CLIENT_FAILOVERLISTENER_H*/ -- cgit v1.2.1