diff options
author | Ted Ross <tross@apache.org> | 2008-12-23 19:38:25 +0000 |
---|---|---|
committer | Ted Ross <tross@apache.org> | 2008-12-23 19:38:25 +0000 |
commit | 12d7d4125a42a7f0ab26a89c3c34e88135cf5869 (patch) | |
tree | 5970de22eb3027833688156a4116d71ef6b44442 /cpp/examples | |
parent | 912a6db37456524c60e1b7f3236de4dca3c77636 (diff) | |
download | qpid-python-12d7d4125a42a7f0ab26a89c3c34e88135cf5869.tar.gz |
QPID-1412 Updates and fixes for the c++ console API:
- Added event support
- Converted raw pointers to shared_ptrs in references to Values.
This fixes a memory leak in the original code.
- Added wrappers to make value access more convenient.
- Added timeout handling for synchronous operations.
Timeout values are configurable.
- Fixed a bug in getObjects whereby waitForStable was not called and
the operation could fail if called too early.
- Added examples "printevents" and "ping" to illustrate the usage of
different aspects of the API.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@729075 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/examples')
-rw-r--r-- | cpp/examples/qmf-console/Makefile.am | 14 | ||||
-rw-r--r-- | cpp/examples/qmf-console/console.cpp | 16 | ||||
-rw-r--r-- | cpp/examples/qmf-console/ping.cpp | 129 | ||||
-rw-r--r-- | cpp/examples/qmf-console/printevents.cpp | 105 |
4 files changed, 251 insertions, 13 deletions
diff --git a/cpp/examples/qmf-console/Makefile.am b/cpp/examples/qmf-console/Makefile.am index 05db50f734..e115fc0fb0 100644 --- a/cpp/examples/qmf-console/Makefile.am +++ b/cpp/examples/qmf-console/Makefile.am @@ -22,12 +22,20 @@ examplesdir=$(pkgdatadir)/examples/qmf-console MAKELDFLAG = qmfconsole include $(top_srcdir)/examples/makedist.mk -noinst_PROGRAMS=qmfc +noinst_PROGRAMS=console printevents ping -qmfc_SOURCES=console.cpp -qmfc_LDADD=$(CONSOLE_LIB) +console_SOURCES=console.cpp +console_LDADD=$(CONSOLE_LIB) + +printevents_SOURCES=printevents.cpp +printevents_LDADD=$(CONSOLE_LIB) + +ping_SOURCES=ping.cpp +ping_LDADD=$(CONSOLE_LIB) examples_DATA= \ console.cpp \ + printevents.cpp \ + ping.cpp \ $(MAKEDIST) diff --git a/cpp/examples/qmf-console/console.cpp b/cpp/examples/qmf-console/console.cpp index c98f1ace34..5700d5556f 100644 --- a/cpp/examples/qmf-console/console.cpp +++ b/cpp/examples/qmf-console/console.cpp @@ -21,17 +21,9 @@ #include "qpid/console/ConsoleListener.h" #include "qpid/console/SessionManager.h" -#include "qpid/console/Value.h" -#include <unistd.h> -#include <cstdlib> -#include <iostream> - -#include <sstream> using namespace std; using namespace qpid::console; -using std::cout; -using std::endl; class Listener : public ConsoleListener { public: @@ -68,6 +60,10 @@ public: void objectStats(Broker& broker, Object& object) { cout << "objectStats: broker=" << broker << " object=" << object << endl; } + + void event(Event& event) { + cout << "event: " << event << endl; + } }; @@ -127,8 +123,8 @@ int main_int(int /*argc*/, char** /*argv*/) Object::AttributeMap args; MethodResponse result; - args["sequence"] = new UintValue(1); - args["body"] = new StringValue("Testing..."); + args.addUint("sequence", 1); + args.addString("body", "Testing..."); cout << "Call echo method..." << endl; broker.invokeMethod("echo", args, result); diff --git a/cpp/examples/qmf-console/ping.cpp b/cpp/examples/qmf-console/ping.cpp new file mode 100644 index 0000000000..debca7428a --- /dev/null +++ b/cpp/examples/qmf-console/ping.cpp @@ -0,0 +1,129 @@ +/* + * + * 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/console/SessionManager.h" + +using namespace std; +using namespace qpid::console; + +//============================================================== +// Main program +//============================================================== +int main_int(int /*argc*/, char** /*argv*/) +{ + // + // Declare connection settings for the messaging broker. The settings default to + // localhost:5672 with user guest (password guest). Refer to the header file + // <qpid/client/ConnectionSettings.h> for full details. + // + qpid::client::ConnectionSettings connSettings; + + // + // Declare the (optional) session manager settings. Override the default timeout + // for methods calls to be 2 seconds. + // + SessionManager::Settings smSettings; + smSettings.methodTimeout = 2; + + // + // Declare the console session manager. With a null listener argument, it defaults to + // synchronous-only access mode. + // + SessionManager sm(0, smSettings); + + // + // Add a broker connection to the session manager. + // + Broker* broker = sm.addBroker(connSettings); + + uint32_t count = 5; // The number of echo requests we will send to the broker. + Object::Vector list; // A container for holding objects retrieved from the broker. + + // + // Query for a list of 'broker' objects from the Management Database + // + sm.getObjects(list, "broker"); + + // + // We expect one object (since we are connected to only one broker) + // + if (list.size() == 1) { + Object& brokerObject = *(list.begin()); + + for (uint32_t iter = 0; iter < count; iter++) { + // + // Declare a container for arguments to be sent with the "echo" method + // that we will invoke on the remote "broker" object. + // + Object::AttributeMap args; + + // + // Declare a container for the result of the method invocation. + // + MethodResponse result; + + // + // Set the values of the input arguments. + // + args.addUint("sequence", iter); + args.addString("body", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + + cout << "Ping Broker: " << broker->getUrl() << "... "; + cout.flush(); + + // + // Invoke the method. This is a synchronous operation that will block until + // the method completes and returns a result. + // + brokerObject.invokeMethod("echo", args, result); + + // + // result.code is the return code (0 => Success) + // result.text is the return text + // result.arguments is a container (of type Object::AttributeMap) that holds + // the output arguments (if any) from the method. + // + cout << "Result: code=" << result.code << " text=" << result.text; + if (result.code == 0) + cout << " seq=" << result.arguments["sequence"]->asUint(); + cout << endl; + + if (result.code == 0 && iter < count - 1) + ::sleep(1); + } + } + + // + // Disconnect the broker from the session manager. + // + sm.delBroker(broker); + return 0; +} + +int main(int argc, char** argv) +{ + try { + return main_int(argc, argv); + } catch(std::exception& e) { + cout << "Top Level Exception: " << e.what() << endl; + } +} + diff --git a/cpp/examples/qmf-console/printevents.cpp b/cpp/examples/qmf-console/printevents.cpp new file mode 100644 index 0000000000..bbec2c1af0 --- /dev/null +++ b/cpp/examples/qmf-console/printevents.cpp @@ -0,0 +1,105 @@ +/* + * + * 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/console/ConsoleListener.h" +#include "qpid/console/SessionManager.h" +#include "qpid/sys/Time.h" + +using namespace std; +using namespace qpid::console; + +// +// Define a listener class to receive asynchronous events. +// +class Listener : public ConsoleListener { +public: + void brokerConnected(const Broker& broker) { + cout << qpid::sys::now() << " NOTIC qpid-printevents:brokerConnected broker=" << + broker.getUrl() << endl; + } + + void brokerDisconnected(const Broker& broker) { + cout << qpid::sys::now() << " NOTIC qpid-printevents:brokerDisonnected broker=" << + broker.getUrl() << endl; + } + + void event(Event& event) { + cout << event << endl; + } +}; + + +//============================================================== +// Main program +//============================================================== +int main_int(int /*argc*/, char** /*argv*/) +{ + // + // Declare an instance of our listener. + // + Listener listener; + + // + // Declare connection settings for the messaging broker. The settings default to + // localhost:5672 with user guest (password guest). Refer to the header file + // <qpid/client/ConnectionSettings.h> for full details. + // + qpid::client::ConnectionSettings connSettings; + + // + // Declare the (optional) session manager settings. Disable the reception of + // object updates and heartbeats. Note that by disabling the reception of things + // we don't need, we don't unnecessarily use network bandwidth. + // + SessionManager::Settings smSettings; + smSettings.rcvObjects = false; + smSettings.rcvHeartbeats = false; + + // + // Declare the console session manager. + // + SessionManager sm(&listener, smSettings); + + // + // Add a broker connection to the session manager. If desired, multiple brokers may + // be connected. + // + Broker* broker = sm.addBroker(connSettings); + + // + // Sleep indefinitely while asynchronous events are handled by the listener. + // + for (;;) + ::sleep(1); + + sm.delBroker(broker); + return 0; +} + +int main(int argc, char** argv) +{ + try { + return main_int(argc, argv); + } catch(std::exception& e) { + cout << "Top Level Exception: " << e.what() << endl; + } +} + |