diff options
author | Ted Ross <tross@apache.org> | 2010-08-13 14:22:57 +0000 |
---|---|---|
committer | Ted Ross <tross@apache.org> | 2010-08-13 14:22:57 +0000 |
commit | 48956072b60c7ef08b5fb5df5d91d5bb0d9e0f25 (patch) | |
tree | 83b2d7b6a53fe2e514d484b2dce3dc8da6e43b87 /cpp | |
parent | 9eb2bdc304b336ad8dd6b4bfb7e6bf94cf4bc9c0 (diff) | |
download | qpid-python-48956072b60c7ef08b5fb5df5d91d5bb0d9e0f25.tar.gz |
QPID-2792 - Added C++ example agent.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@985207 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/bindings/qmf2/Makefile.am | 2 | ||||
-rw-r--r-- | cpp/bindings/qmf2/examples/cpp/Makefile.am | 26 | ||||
-rw-r--r-- | cpp/bindings/qmf2/examples/cpp/agent.cpp | 200 | ||||
-rwxr-xr-x | cpp/bindings/qmf2/examples/python/agent.py | 2 | ||||
-rw-r--r-- | cpp/configure.ac | 1 |
5 files changed, 229 insertions, 2 deletions
diff --git a/cpp/bindings/qmf2/Makefile.am b/cpp/bindings/qmf2/Makefile.am index 9a8fc11222..52b1bbd457 100644 --- a/cpp/bindings/qmf2/Makefile.am +++ b/cpp/bindings/qmf2/Makefile.am @@ -20,7 +20,7 @@ if HAVE_SWIG EXTRA_DIST = qmf2.i -SUBDIRS = +SUBDIRS = examples/cpp if HAVE_RUBY_DEVEL SUBDIRS += ruby diff --git a/cpp/bindings/qmf2/examples/cpp/Makefile.am b/cpp/bindings/qmf2/examples/cpp/Makefile.am new file mode 100644 index 0000000000..2244ddab3e --- /dev/null +++ b/cpp/bindings/qmf2/examples/cpp/Makefile.am @@ -0,0 +1,26 @@ +# +# 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 = -I$(top_srcdir)/include + +AM_CPPFLAGS = $(INCLUDE) + +noinst_PROGRAMS=agent +agent_SOURCES=agent.cpp +agent_LDADD=$(top_builddir)/src/libqmf2.la diff --git a/cpp/bindings/qmf2/examples/cpp/agent.cpp b/cpp/bindings/qmf2/examples/cpp/agent.cpp new file mode 100644 index 0000000000..e680ffeeeb --- /dev/null +++ b/cpp/bindings/qmf2/examples/cpp/agent.cpp @@ -0,0 +1,200 @@ +/* + * 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/Connection.h> +#include <qpid/messaging/Duration.h> +#include <qmf/AgentSession.h> +#include <qmf/AgentEvent.h> +#include <qmf/Schema.h> +#include <qmf/SchemaProperty.h> +#include <qmf/SchemaMethod.h> +#include <qmf/Data.h> +#include <qmf/DataAddr.h> +#include <qpid/types/Variant.h> +#include <string> +#include <iostream> + +using namespace std; +using namespace qmf; +using qpid::types::Variant; +using qpid::messaging::Duration; + +class ExampleAgent { +public: + ExampleAgent(const string& url); + ~ExampleAgent(); + + void setupSchema(); + void populateData(); + void run(); +private: + qpid::messaging::Connection connection; + AgentSession session; + Schema sch_exception; + Schema sch_control; + Data control; + DataAddr controlAddr; + + bool method(AgentEvent& event); +}; + + +ExampleAgent::ExampleAgent(const string& url) +{ + // + // Create and open a messaging connection to a broker. + // + connection = qpid::messaging::Connection(url); + connection.open(); + + // + // Create, configure, and open a QMFv2 agent session using the connection. + // + session = AgentSession(connection, "{interval:30}"); + session.setVendor("profitron.com"); + session.setProduct("gizmo"); + session.setAttribute("attr1", 2000); + session.open(); +} + +ExampleAgent::~ExampleAgent() +{ + // + // Clean up the QMF session and the AMQP connection. + // + session.close(); + connection.close(); +} + +void ExampleAgent::setupSchema() +{ + // + // Create and register schema for this agent. + // + string package("com.profitron.gizmo"); + + // + // Declare a schema for a structured exception that can be used in failed + // method invocations. + // + sch_exception = Schema(SCHEMA_TYPE_DATA, package, "exception"); + sch_exception.addProperty(SchemaProperty("whatHappened", SCHEMA_DATA_STRING)); + sch_exception.addProperty(SchemaProperty("howBad", SCHEMA_DATA_INT)); + sch_exception.addProperty(SchemaProperty("details", SCHEMA_DATA_MAP)); + + // + // Declare a control object to test methods against. + // + sch_control = Schema(SCHEMA_TYPE_DATA, package, "control"); + sch_control.addProperty(SchemaProperty("state", SCHEMA_DATA_STRING)); + sch_control.addProperty(SchemaProperty("methodCount", SCHEMA_DATA_INT)); + + SchemaMethod stopMethod("stop", "{desc:'Stop Agent'}"); + stopMethod.addArgument(SchemaProperty("message", SCHEMA_DATA_STRING)); + sch_control.addMethod(stopMethod); + + SchemaMethod echoMethod("echo", "{desc:'Echo Arguments'}"); + echoMethod.addArgument(SchemaProperty("sequence", SCHEMA_DATA_INT, "{dir:INOUT}")); + echoMethod.addArgument(SchemaProperty("map", SCHEMA_DATA_MAP, "{dir:INOUT}")); + sch_control.addMethod(echoMethod); + + SchemaMethod failMethod("fail", "{desc:'Expected to Fail'}"); + failMethod.addArgument(SchemaProperty("useString", SCHEMA_DATA_BOOL, "{dir:IN}")); + failMethod.addArgument(SchemaProperty("stringVal", SCHEMA_DATA_STRING, "{dir:IN}")); + failMethod.addArgument(SchemaProperty("details", SCHEMA_DATA_MAP, "{dir:IN}")); + sch_control.addMethod(failMethod); + + // + // Register our schemata with the agent session. + // + session.registerSchema(sch_exception); + session.registerSchema(sch_control); +} + +void ExampleAgent::populateData() +{ + // + // Create a control object and give it to the agent session to manage. + // + control = Data(sch_control.getSchemaId()); + control.setProperty("state", "OPERATIONAL"); + control.setProperty("methodCount", 0); + controlAddr = session.addData(control, "singleton"); +} + +void ExampleAgent::run() +{ + AgentEvent event; + bool running(true); + + while (running) { + bool valid(session.nextEvent(event, Duration::SECOND)); + if (valid && running) { + switch (event.getType()) { + case AGENT_METHOD: + running = method(event); + break; + } + } + } +} + +bool ExampleAgent::method(AgentEvent& event) +{ + const string& name(event.getMethodName()); + control.setProperty("methodCount", control.getProperty("methodCount").asUint32() + 1); + + if (controlAddr == event.getDataAddr()) { + if (name == "stop") { + cout << "Stopping: message=" << event.getArguments()["message"] << endl; + session.methodSuccess(event); + return false; + } + + if (name == "echo") { + event.addReturnArgument("sequence", event.getArguments()["sequence"]); + event.addReturnArgument("map", event.getArguments()["map"]); + session.methodSuccess(event); + return true; + } + + if (name == "fail") { + if (event.getArguments()["useString"]) + session.raiseException(event, event.getArguments()["stringVal"]); + else { + Data ex(sch_exception.getSchemaId()); + ex.setProperty("whatHappened", "It Failed"); + ex.setProperty("howBad", 75); + ex.setProperty("details", event.getArguments()["details"]); + session.raiseException(event, ex); + } + } + } + + return true; +} + +int main() +{ + ExampleAgent agent("localhost"); + agent.setupSchema(); + agent.populateData(); + agent.run(); +} + diff --git a/cpp/bindings/qmf2/examples/python/agent.py b/cpp/bindings/qmf2/examples/python/agent.py index c092c5ac30..a915ed59b1 100755 --- a/cpp/bindings/qmf2/examples/python/agent.py +++ b/cpp/bindings/qmf2/examples/python/agent.py @@ -94,7 +94,7 @@ class ExampleAgent(AgentHandler): package = "com.profitron.bntor" ## - ## Declare a schema for a structure exception that can be used in failed + ## Declare a schema for a structured exception that can be used in failed ## method invocations. ## self.sch_exception = Schema(SCHEMA_TYPE_DATA, package, "exception") diff --git a/cpp/configure.ac b/cpp/configure.ac index 7f98ac1550..60f10516f0 100644 --- a/cpp/configure.ac +++ b/cpp/configure.ac @@ -533,6 +533,7 @@ AC_CONFIG_FILES([ bindings/qmf2/Makefile bindings/qmf2/ruby/Makefile bindings/qmf2/python/Makefile + bindings/qmf2/examples/cpp/Makefile managementgen/Makefile etc/Makefile src/Makefile |