diff --git a/examples/protocols/amqp/proton-cpp/compile.sh b/examples/protocols/amqp/proton-cpp/compile.sh index cc16a27c04..b4de2b4cd0 100755 --- a/examples/protocols/amqp/proton-cpp/compile.sh +++ b/examples/protocols/amqp/proton-cpp/compile.sh @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. # This requires g++ and qpid-cpp-client-devel -g++ src/main/cpp/hello.cpp -o hello -l qpidmessaging -l qpidtypes +g++ src/main/cpp/hello.cpp -o hello -l qpid-proton-cpp -l qpidtypes diff --git a/examples/protocols/amqp/proton-cpp/readme.md b/examples/protocols/amqp/proton-cpp/readme.md index 2acf171b62..4b8a056590 100644 --- a/examples/protocols/amqp/proton-cpp/readme.md +++ b/examples/protocols/amqp/proton-cpp/readme.md @@ -9,7 +9,7 @@ To run this example simply run the command **mvn verify -Pexample**, execute the # first make sure you have the dependencies you need to compile and run the client # You will have to adapt this step according to your platform. Consult the [qpid docs](http://qpid.apache.org/releases/qpid-0.30/programming/book/) for more information. # There is a list of [packages](http://qpid.apache.org/packages.html) you can install as well. - [proton-cpp]$ sudo yum install qpid-cpp-client-devel + [proton-cpp]$ sudo yum install qpid-proton-cpp-devel qpid-proton-cpp-docs # on a first window [proton-cpp]$ mvn verify -Pexample diff --git a/examples/protocols/amqp/proton-cpp/src/main/cpp/fake_cpp11.hpp b/examples/protocols/amqp/proton-cpp/src/main/cpp/fake_cpp11.hpp new file mode 100644 index 0000000000..693a934812 --- /dev/null +++ b/examples/protocols/amqp/proton-cpp/src/main/cpp/fake_cpp11.hpp @@ -0,0 +1,34 @@ +#ifndef FAKE_CPP11_HPP +#define FAKE_CPP11_HPP + +/* + * 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. + */ + +/// These definitions allow us to use some new C++11 features in previous compilers +/// +/// It is strongly recommended not to copy this - just use C++11/C++14 instead! + +#if __cplusplus < 201103L +#define OVERRIDE +#else +#define OVERRIDE override +#endif + + +#endif // FAKE_CPP11_HPP \ No newline at end of file diff --git a/examples/protocols/amqp/proton-cpp/src/main/cpp/hello.cpp b/examples/protocols/amqp/proton-cpp/src/main/cpp/hello.cpp index bfe18ffe38..c1abd5b32d 100644 --- a/examples/protocols/amqp/proton-cpp/src/main/cpp/hello.cpp +++ b/examples/protocols/amqp/proton-cpp/src/main/cpp/hello.cpp @@ -7,9 +7,9 @@ * 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 @@ -18,63 +18,55 @@ * under the License. * */ - - -// Step 2. Make the proper C++ imports -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include -using namespace qpid::messaging; +#include "fake_cpp11.hpp" -int main(int argc, char** argv) { - std::string broker = argc > 1 ? argv[1] : "localhost:61616"; - std::string address = argc > 2 ? argv[2] : "exampleQueue"; +class hello_world : public proton::messaging_handler { + std::string conn_url_; + std::string addr_; - // Connection options documented at http://qpid.apache.org/releases/qpid-0.30/programming/book/connections.html#connection-options - std::string connectionOptions = argc > 3 ? argv[3] : "{protocol:amqp1.0}"; + public: + hello_world(const std::string& u, const std::string& a) : + conn_url_(u), addr_(a) {} - try { - // Step 3. Create an amqp qpid 1.0 connection - Connection connection(broker, connectionOptions); - connection.open(); - - // Step 4. Create a session - Session session = connection.createSession(); - - // Step 5. Create a sender - Sender sender = session.createSender(address); - - //create a receiver - Receiver receiver = session.createReceiver(address); - - - for (int i = 0; i < 10; i++) { - Message message; - message.getContentObject() = "Hello world!"; - - message.getContentObject().setEncoding("utf8"); - message.setContentType("text/plain"); - - sender.send(message); - - // receive the simple message - message = receiver.fetch(Duration::SECOND * 1); - std::cout << "Received a message with this following content \"" << message.getContent() << "\"" << std::endl; - - // acknowledge the message - session.acknowledge(); - } - - // close the connection - connection.close(); - return 0; - } catch(const std::exception& error) { - std::cerr << error.what() << std::endl; - return 1; + void on_container_start(proton::container& c) OVERRIDE { + c.connect(conn_url_); } -} + + void on_connection_open(proton::connection& c) OVERRIDE { + c.open_receiver(addr_); + c.open_sender(addr_); + } + + void on_sendable(proton::sender &s) OVERRIDE { + proton::message m("Hello World!"); + s.send(m); + s.close(); + } + + void on_message(proton::delivery &d, proton::message &m) OVERRIDE { + std::cout << m.body() << std::endl; + d.connection().close(); + } +}; + +int main(int argc, char **argv) { + try { + std::string conn_url = argc > 1 ? argv[1] : "//127.0.0.1:5672"; + std::string addr = argc > 2 ? argv[2] : "exampleQueue"; + hello_world hw(conn_url, addr); + proton::container(hw).run(); + return 0; + } catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + } + return 1; +} \ No newline at end of file