qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject svn commit: r532430 - in /incubator/qpid/trunk/qpid/cpp/src: ./ qpid/ qpid/broker/ tests/
Date Wed, 25 Apr 2007 18:06:15 GMT
Author: aconway
Date: Wed Apr 25 11:06:14 2007
New Revision: 532430

URL: http://svn.apache.org/viewvc?view=rev&rev=532430
Log:

 * qpid/CommonOptions.h:
  - Convenience classs/functions to use boost::program_options.
  - CommonOptions class for options common to client/broker.
 * qpid/broker/Broker.h:
  - Replaced broker::Configuration with class Broker::Options,
    derived from CommonOptions.
 * qpidd.cpp: Updated options handling.
 * qpid/Exception.h: Added strError function to get std::string from errno.

Added:
    incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp   (with props)
    incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h   (with props)
Removed:
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Configuration.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Configuration.h
    incubator/qpid/trunk/qpid/cpp/src/tests/ConfigurationTest.cpp
Modified:
    incubator/qpid/trunk/qpid/cpp/src/Makefile.am
    incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.h
    incubator/qpid/trunk/qpid/cpp/src/qpidd.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am

Modified: incubator/qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/Makefile.am?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/Makefile.am Wed Apr 25 11:06:14 2007
@@ -7,7 +7,8 @@
 
 qpidd_LDADD = \
   libqpidbroker.la \
-  libqpidcommon.la
+  libqpidcommon.la \
+  -lboost_program_options 
 
 sbin_PROGRAMS = qpidd
 qpidd_SOURCES = qpidd.cpp
@@ -49,6 +50,7 @@
 lib_LTLIBRARIES = libqpidcommon.la libqpidbroker.la libqpidclient.la
 
 libqpidcommon_la_LIBADD = \
+  -lboost_program_options \
   $(APR_LIBS) \
   $(LIB_DLOPEN) \
   $(LIB_CLOCK_GETTIME)
@@ -92,9 +94,11 @@
   qpid/QpidError.cpp \
   qpid/sys/Runnable.cpp \
   qpid/sys/Time.cpp \
-  qpid/sys/ProducerConsumer.cpp
+  qpid/sys/ProducerConsumer.cpp \
+  qpid/CommonOptions.cpp
 
-libqpidbroker_la_LIBADD = libqpidcommon.la
+
+libqpidbroker_la_LIBADD = libqpidcommon.la 
 libqpidbroker_la_LDFLAGS = -version-info $(LIBTOOL_VERSION_INFO_ARG)
 libqpidbroker_la_SOURCES = \
   qpid/broker/AccumulatedAck.cpp \
@@ -106,7 +110,6 @@
   qpid/broker/BrokerMessage.cpp \
   qpid/broker/BrokerMessageMessage.cpp \
   qpid/broker/BrokerQueue.cpp \
-  qpid/broker/Configuration.cpp \
   qpid/broker/Connection.cpp \
   qpid/broker/ConnectionFactory.cpp \
   qpid/broker/DeliverableMessage.cpp \
@@ -160,7 +163,6 @@
   qpid/broker/BrokerMessageBase.h \
   qpid/broker/BrokerQueue.h \
   qpid/broker/CompletionHandler.h \
-  qpid/broker/Configuration.h \
   qpid/broker/Consumer.h \
   qpid/broker/Deliverable.h \
   qpid/broker/DeliverableMessage.h \
@@ -277,7 +279,8 @@
   qpid/ExceptionHolder.h \
   qpid/QpidError.h \
   qpid/SharedObject.h \
-  qpid/shared_ptr.h 
+  qpid/shared_ptr.h \
+  qpid/CommonOptions.h
 
 # Force build of qpidd during dist phase so help2man will work.
 dist-hook: qpidd

Added: incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp?view=auto&rev=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp Wed Apr 25 11:06:14 2007
@@ -0,0 +1,36 @@
+/*
+ *
+ * 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 "CommonOptions.h"
+
+namespace qpid {
+
+const int CommonOptions::DEFAULT_PORT=5672;
+
+CommonOptions::CommonOptions() : trace(false), port(DEFAULT_PORT) {}
+
+void CommonOptions::addTo(po::options_description& desc)
+{
+    using namespace po;
+    desc.add_options()
+        ("trace,t", optValue(trace), "Enable debug tracing" )
+        ("port,p", optValue(port,"PORT"), "Use PORT for AMQP connections.");
+}
+
+} // namespace qpid
+

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.cpp
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h?view=auto&rev=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h Wed Apr 25 11:06:14 2007
@@ -0,0 +1,91 @@
+#ifndef QPID_COMMONOPTIONS_H
+#define QPID_COMMONOPTIONS_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 <boost/program_options.hpp>
+#include <boost/format.hpp>
+
+namespace qpid { 
+
+/**@Qpid extensions to boost::program_options */
+namespace program_options {
+
+using namespace boost::program_options;
+
+/** @internal Normally only constructed by optValue() */
+template <class T>
+class OptionValue : public typed_value<T> {
+  public:
+    OptionValue(T& value, const std::string& arg)
+        : typed_value<T>(&value), argName(arg) {}
+    std::string name() const { return argName; }
+  private:
+    std::string argName;
+};
+
+/**
+ * Like boost::program_options::value() with more convenient signature
+ * for updating a value by reference and nicer help formatting.
+ * 
+ *@param value displayed as default in help, updated from options.
+ * Must support ostream << operator.
+ *@param arg name for arguments  in help.
+ *
+ *@see CommonOptions.cpp for example of use.
+ */ 
+template<class T>
+value_semantic* optValue(T& value, const char* arg) {
+    std::string val(boost::lexical_cast<std::string>(value));
+    std::string argName(
+        val.empty() ? std::string(arg) :
+        (boost::format("%s (=%s) ") % arg % val).str());
+    return new OptionValue<T>(value, argName);
+}
+
+/**
+ * Like boost::program_options::bool_switch but takes reference, not pointer.
+ */
+inline value_semantic* optValue(bool& value) { return bool_switch(&value); }
+
+} // namespace program_options
+
+namespace po=program_options;   // Convenience alias.
+
+/**
+ * Common options for client and broker
+ */
+struct CommonOptions {
+    static const int DEFAULT_PORT;
+
+    CommonOptions();
+
+    bool trace;
+    int port;
+
+    /** Add  members to program_options to be updated */
+    void addTo(po::options_description&);
+};
+
+} // namespace qpid
+
+#endif  /*!QPID_COMMONOPTIONS_H*/

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/CommonOptions.h
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.cpp?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.cpp Wed Apr 25 11:06:14 2007
@@ -20,9 +20,15 @@
  */
 
 #include "Exception.h"
+#include <errno.h>
 
 namespace qpid {
 
+std::string strError(int err) {
+    char buf[512];
+    return std::string(strerror_r(err, buf, sizeof(buf)));
+}
+    
 Exception::Exception() throw() {}
 
 Exception::Exception(const std::string& str) throw() : whatStr(str) {}

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.h?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Exception.h Wed Apr 25 11:06:14 2007
@@ -31,6 +31,10 @@
 
 namespace qpid
 {
+
+/** Get the error message for error number err. */
+std::string strError(int err);
+
 /**
  * Exception base class for all Qpid exceptions.
  */

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.cpp?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.cpp Wed Apr 25 11:06:14 2007
@@ -40,13 +40,38 @@
 namespace qpid {
 namespace broker {
 
+Broker::Options::Options() :
+    workerThreads(5),
+    maxConnections(500),
+    connectionBacklog(10),
+    store(),
+    stagingThreshold(5000000)
+{}
+
+void Broker::Options::addTo(po::options_description& desc)
+{
+    using namespace po;
+    CommonOptions::addTo(desc);
+    desc.add_options()
+        ("worker-threads", optValue(workerThreads, "N"),
+         "Broker thread pool size")
+        ("max-connections", optValue(maxConnections, "N"),
+         "Maximum allowed connections")
+        ("connection-backlog", optValue(connectionBacklog, "N"),
+         "Connection backlog limit for server socket.")
+        ("staging-threshold", optValue(stagingThreshold, "N"),
+         "Messages over N bytes are staged to disk.")
+        ("store", optValue(store,"LIBNAME"),
+         "Name of message store shared library.");
+}
+
 const std::string empty;
 const std::string amq_direct("amq.direct");
 const std::string amq_topic("amq.topic");
 const std::string amq_fanout("amq.fanout");
 const std::string amq_match("amq.match");
 
-Broker::Broker(const Configuration& conf) :
+Broker::Broker(const Broker::Options& conf) :
     config(conf),
     store(createStore(conf)),
     queues(store.get()),
@@ -63,7 +88,8 @@
     exchanges.declare(amq_match, HeadersExchange::typeName);
 
     if(store.get()) {
-        RecoveryManagerImpl recoverer(queues, exchanges, conf.getStagingThreshold());
+        RecoveryManagerImpl recoverer(
+            queues, exchanges, conf.stagingThreshold);
         store->recover(recoverer);
     }
 
@@ -73,20 +99,20 @@
 
 Broker::shared_ptr Broker::create(int16_t port) 
 {
-    Configuration config;
-    config.setPort(port);
+    Options config;
+    config.port=port;
     return create(config);
 }
 
-Broker::shared_ptr Broker::create(const Configuration& config) {
+Broker::shared_ptr Broker::create(const Options& config) {
     return Broker::shared_ptr(new Broker(config));
 }    
 
-MessageStore* Broker::createStore(const Configuration& config) {
-    if (config.getStore().empty())
-        return new NullMessageStore(config.isTrace());
+MessageStore* Broker::createStore(const Options& config) {
+    if (config.store.empty())
+        return new NullMessageStore(config.trace);
     else
-        return new MessageStoreModule(config.getStore());
+        return new MessageStoreModule(config.store);
 }
         
 void Broker::run() {
@@ -108,15 +134,12 @@
 Acceptor& Broker::getAcceptor() const {
     if (!acceptor) 
         const_cast<Acceptor::shared_ptr&>(acceptor) =
-            Acceptor::create(config.getPort(),
-                             config.getConnectionBacklog(),
-                             config.getWorkerThreads(),
-                             config.isTrace());
+            Acceptor::create(config.port,
+                             config.connectionBacklog,
+                             config.workerThreads,
+                             config.trace);
     return *acceptor;
 }
-
-
-const int16_t Broker::DEFAULT_PORT(5672);
 
 
 }} // namespace qpid::broker

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.h?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Broker.h Wed Apr 25 11:06:14 2007
@@ -22,7 +22,6 @@
  *
  */
 
-#include "Configuration.h"
 #include "ConnectionFactory.h"
 #include "qpid/sys/Runnable.h"
 #include "qpid/sys/Acceptor.h"
@@ -36,8 +35,9 @@
 #include "qpid/framing/OutputHandler.h"
 #include "qpid/framing/ProtocolInitiation.h"
 #include "QueueRegistry.h"
+#include "qpid/CommonOptions.h"
 
-namespace qpid {
+namespace qpid { 
 namespace broker {
 /**
  * A broker instance. 
@@ -46,20 +46,28 @@
                public SharedObject<Broker>
 {
   public:
-    static const int16_t DEFAULT_PORT;
-            
+    struct Options : public CommonOptions {
+        Options();
+        void addTo(po::options_description&);
+        int workerThreads;
+        int maxConnections;
+        int connectionBacklog;
+        std::string store;      
+        long stagingThreshold;
+    };
+    
     virtual ~Broker();
 
     /**
      * Create a broker.
      * @param port Port to listen on or 0 to pick a port dynamically.
      */
-    static shared_ptr create(int16_t port = DEFAULT_PORT);
+    static shared_ptr create(int16_t port = CommonOptions::DEFAULT_PORT);
 
     /**
-     * Create a broker using a Configuration.
+     * Create a broker with the options in config.
      */
-    static shared_ptr create(const Configuration& config);
+    static shared_ptr create(const Options& config);
 
     /**
      * Return listening port. If called before bind this is
@@ -87,10 +95,10 @@
     DtxManager& getDtxManager() { return dtxManager; }
     
   private:
-    Broker(const Configuration& config);
+    Broker(const Options& configuration);
     sys::Acceptor& getAcceptor() const;
 
-    Configuration config;
+    Options config;
     sys::Acceptor::shared_ptr acceptor;
     const std::auto_ptr<MessageStore> store;
     QueueRegistry queues;
@@ -101,7 +109,7 @@
     ConnectionFactory factory;
     DtxManager dtxManager;
 
-    static MessageStore* createStore(const Configuration& config);
+    static MessageStore* createStore(const Options& config);
 };
 
 }}

Modified: incubator/qpid/trunk/qpid/cpp/src/qpidd.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpidd.cpp?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpidd.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpidd.cpp Wed Apr 25 11:06:14 2007
@@ -19,62 +19,93 @@
  *
  */
 #include "qpid/broker/Broker.h"
-#include "qpid/broker/Configuration.h"
 #include <signal.h>
 #include <iostream>
 #include <memory>
 #include <cerrno>
 #include "config.h"
 #include <unistd.h>
+#include "qpid/sys/posix/check.h"
 
-static char const* programName = "qpidd";
-
+using namespace qpid;
 using namespace qpid::broker;
 using namespace qpid::sys;
+using namespace std;
+
+/** Command line options */
+struct QpiddOptions : public Broker::Options
+{
+    bool help;
+    bool version;
+    bool daemon;
+    po::options_description desc;
+    
+    QpiddOptions() :
+        help(false), version(false), daemon(false), desc("Options")
+    {
+        using namespace po;
+        desc.add_options()
+            ("daemon,d", optValue(daemon), "Run as a daemon");
+        Broker::Options::addTo(desc);
+        desc.add_options()
+            ("help,h", optValue(help), "Print help message")
+            ("version,v", optValue(version), "Print version information");
+    }
+
+    void parse(int argc, char* argv[]) {
+        po::variables_map vm;
+        po::store(po::parse_command_line(argc, argv, desc), vm);
+        po::notify(vm);
+    };
+    
+    void usage(std::ostream& out) const {
+        out << "Usage: qpidd [OPTIONS]" << endl 
+            << "Start the Qpid AMQP broker." << endl << endl
+            << desc << endl;
+    };
+};
 
-Broker::shared_ptr broker;
+std::ostream& operator<<(std::ostream& out, const QpiddOptions& config)
 {
+    config.usage(out); return out;
+}
+
+Broker::shared_ptr brokerPtr;
 
 void handle_signal(int /*signal*/){
-    std::cerr << "Shutting down..." << std::endl;
-    broker->shutdown();
+    if (brokerPtr) {
+        cerr << "Shutting down..." << endl;
+        brokerPtr->shutdown();
+    }
 }
 
-int main(int argc, char** argv)
+int main(int argc, char* argv[])
 {
-    Configuration config;
+    QpiddOptions config;
     try {
-        config.parse(programName, argc, argv);
-
-        if(config.isHelp()){
-            config.usage();
-        }else if(config.isVersion()){
-            std::cout << programName << " (" << PACKAGE_NAME << ")
version "
-                      << PACKAGE_VERSION << std::endl;
-        }else{
-            broker = Broker::create(config);
+        config.parse(argc, argv);
+        if(config.help) {
+            config.usage(cout);
+        }
+        else if (config.version) {
+            cout << "qpidd (" << PACKAGE_NAME << ") version "
+                 << PACKAGE_VERSION << endl;
+        }
+        else {
+            brokerPtr=Broker::create(config);
             signal(SIGINT, handle_signal);
-            if (config.isDaemon()) {
-                // Detach & run as daemon.
-                int chdirRoot = 0;  // 0 means chdir to root.
-                int closeStd = 0;   // 0 means close stdin/out/err
-                if (daemon(chdirRoot, closeStd) < 0) {
-                    char buf[512];
-                    
-                    std::cerr << "Failed to detach as daemon: "
-                              << strerror_r(errno, buf, sizeof(buf))
-                              << std::endl;;
-                    return 1;
-                }
+            if (config.daemon) {
+                if (daemon(0, 0) < 0) // daemon(nochdir, noclose)
+                    throw QPID_ERROR(
+                        INTERNAL_ERROR,
+                        "Failed to detach as daemon: "+ strError(errno));
             }
-            broker->run();
+            brokerPtr->run();
         }
         return 0;
     }
-    catch (const Configuration::BadOptionException& e) {
-        std::cerr << e.what() << std::endl << std::endl;
-        config.usage();
-    } catch(const std::exception& e) {
-        std::cerr << e.what() << std::endl;
+    catch(const exception& e) {
+        cerr << "Error: " << e.what() << endl
+             << "Type 'qpidd --help' for usage." << endl;
     }
     return 1;
 }

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am?view=diff&rev=532430&r1=532429&r2=532430
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am Wed Apr 25 11:06:14 2007
@@ -5,7 +5,6 @@
 broker_unit_tests =	\
   AccumulatedAckTest	\
   BrokerChannelTest 	\
-  ConfigurationTest	\
   DtxWorkRecordTest     \
   ExchangeTest		\
   HeadersExchangeTest	\



Mime
View raw message