You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by cl...@apache.org on 2015/07/30 02:05:57 UTC

[1/2] qpid-proton git commit: PROTON-865: C++ blocking receiver

Repository: qpid-proton
Updated Branches:
  refs/heads/cjansen-cpp-client c4e2e596d -> 8e1757ebc


PROTON-865: C++ blocking receiver


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/4339b1c7
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/4339b1c7
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/4339b1c7

Branch: refs/heads/cjansen-cpp-client
Commit: 4339b1c7a7d2813ec091578af5508885ab29f43c
Parents: c4e2e59
Author: Clifford Jansen <cl...@apache.org>
Authored: Sun Jul 26 10:13:30 2015 -0700
Committer: Clifford Jansen <cl...@apache.org>
Committed: Sun Jul 26 10:13:30 2015 -0700

----------------------------------------------------------------------
 examples/cpp/helloworld_blocking.cpp            |  35 ++-----
 proton-c/bindings/cpp/CMakeLists.txt            |   2 +
 .../cpp/include/proton/blocking_connection.hpp  |   3 +
 .../cpp/include/proton/blocking_receiver.hpp    |  60 +++++++++++
 .../bindings/cpp/include/proton/container.hpp   |   5 +-
 .../bindings/cpp/include/proton/delivery.hpp    |   3 +-
 proton-c/bindings/cpp/include/proton/event.hpp  |   2 +
 .../cpp/include/proton/messaging_event.hpp      |   1 +
 .../cpp/include/proton/proton_event.hpp         |   1 +
 .../bindings/cpp/include/proton/receiver.hpp    |   1 +
 .../bindings/cpp/src/blocking_connection.cpp    |  19 ++++
 .../cpp/src/blocking_connection_impl.hpp        |   6 +-
 proton-c/bindings/cpp/src/blocking_link.cpp     |   4 +-
 proton-c/bindings/cpp/src/blocking_receiver.cpp | 104 +++++++++++++++++++
 proton-c/bindings/cpp/src/blocking_sender.cpp   |   2 +-
 proton-c/bindings/cpp/src/connection_impl.cpp   |   4 +
 proton-c/bindings/cpp/src/container.cpp         |   5 +-
 proton-c/bindings/cpp/src/container_impl.cpp    |  25 ++++-
 proton-c/bindings/cpp/src/container_impl.hpp    |   3 +-
 proton-c/bindings/cpp/src/delivery.cpp          |   4 +
 proton-c/bindings/cpp/src/event.cpp             |   4 +
 proton-c/bindings/cpp/src/fetcher.cpp           |  92 ++++++++++++++++
 proton-c/bindings/cpp/src/fetcher.hpp           |  58 +++++++++++
 proton-c/bindings/cpp/src/messaging_event.cpp   |   8 ++
 proton-c/bindings/cpp/src/proton_event.cpp      |   7 ++
 proton-c/bindings/cpp/src/receiver.cpp          |   3 +
 26 files changed, 419 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/examples/cpp/helloworld_blocking.cpp
----------------------------------------------------------------------
diff --git a/examples/cpp/helloworld_blocking.cpp b/examples/cpp/helloworld_blocking.cpp
index 2b6b4db..9f56fa2 100644
--- a/examples/cpp/helloworld_blocking.cpp
+++ b/examples/cpp/helloworld_blocking.cpp
@@ -22,43 +22,28 @@
 #include "proton/container.hpp"
 #include "proton/messaging_handler.hpp"
 #include "proton/blocking_sender.hpp"
+#include "proton/blocking_receiver.hpp"
+#include "proton/duration.hpp"
 
 #include <iostream>
 
-class hello_world_blocking : public proton::messaging_handler {
-  private:
-    proton::url url;
-
-  public:
-
-    hello_world_blocking(const proton::url& u) : url(u) {}
-
-    void on_start(proton::event &e) {
-        proton::connection conn = e.container().connect(url);
-        e.container().create_receiver(conn, url.path());
-    }
-
-    void on_message(proton::event &e) {
-        std::cout << e.message().body() << std::endl;
-        e.connection().close();
-    }
-
-};
-
 int main(int argc, char **argv) {
     try {
         proton::url url(argc > 1 ? argv[1] : "127.0.0.1:5672/examples");
-
         proton::blocking_connection conn(url);
+        proton::blocking_receiver receiver = conn.create_receiver(url.path());
         proton::blocking_sender sender = conn.create_sender(url.path());
+
         proton::message m;
         m.body("Hello World!");
         sender.send(m);
-        conn.close();
 
-        // TODO Temporary hack until blocking receiver available
-        hello_world_blocking hw(url);
-        proton::container(hw).run();
+        proton::duration timeout(30000);
+        proton::message m2 = receiver.receive(timeout);
+        std::cout << m2.body() << std::endl;
+        receiver.accept();
+
+        conn.close();
         return 0;
     } catch (const std::exception& e) {
         std::cerr << e.what() << std::endl;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/CMakeLists.txt b/proton-c/bindings/cpp/CMakeLists.txt
index 6b56a8e..fcb0fb1 100644
--- a/proton-c/bindings/cpp/CMakeLists.txt
+++ b/proton-c/bindings/cpp/CMakeLists.txt
@@ -64,10 +64,12 @@ set(qpid-proton-cpp-source
   src/value.cpp
   src/values.cpp
   src/proton_bits.cpp
+  src/fetcher.cpp
   src/blocking_connection.cpp
   src/blocking_connection_impl.cpp
   src/blocking_link.cpp
   src/blocking_sender.cpp
+  src/blocking_receiver.cpp
   src/contexts.cpp
   src/types.cpp
   )

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/blocking_connection.hpp b/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
index 9aa456f..b6f0499 100644
--- a/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
+++ b/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
@@ -38,6 +38,7 @@ class container;
 class blocking_connection_impl;
 class ssl_domain;
 class blocking_sender;
+class blocking_receiver;
 class wait_condition;
 
 // TODO documentation
@@ -54,6 +55,8 @@ class blocking_connection : public handle<blocking_connection_impl>
     PN_CPP_EXTERN void close();
 
     PN_CPP_EXTERN blocking_sender create_sender(const std::string &address, handler *h=0);
+    PN_CPP_EXTERN blocking_receiver create_receiver(const std::string &address, int credit = 0,
+                                                    bool dynamic = false, std::string name = std::string());
     PN_CPP_EXTERN void wait(wait_condition &condition);
     PN_CPP_EXTERN void wait(wait_condition &condition, std::string &msg, duration timeout=duration::FOREVER);
     PN_CPP_EXTERN duration timeout();

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp b/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
new file mode 100644
index 0000000..319fd93
--- /dev/null
+++ b/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
@@ -0,0 +1,60 @@
+#ifndef PROTON_CPP_BLOCKING_RECEIVER_HPP
+#define PROTON_CPP_BLOCKING_RECEIVER_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.
+ *
+ */
+#include "proton/export.hpp"
+#include "proton/container.hpp"
+#include "proton/blocking_link.hpp"
+#include "proton/duration.hpp"
+#include "proton/messaging_handler.hpp"
+#include "proton/types.h"
+#include "proton/delivery.h"
+#include <string>
+
+namespace proton {
+
+class blocking_connection;
+class blocking_link;
+class fetcher;
+
+class blocking_receiver : public blocking_link
+{
+  public:
+    PN_CPP_EXTERN blocking_receiver(const blocking_receiver&);
+    PN_CPP_EXTERN blocking_receiver& operator=(const blocking_receiver&);
+    PN_CPP_EXTERN ~blocking_receiver();
+    PN_CPP_EXTERN message receive();
+    PN_CPP_EXTERN message receive(duration timeout);
+    PN_CPP_EXTERN void accept();
+    PN_CPP_EXTERN void reject();
+    PN_CPP_EXTERN void release(bool delivered = true);
+    PN_CPP_EXTERN void settle();
+    PN_CPP_EXTERN void settle(delivery::state state);
+  private:
+    blocking_receiver(blocking_connection &c, receiver &l, fetcher &f, int credit);
+    fetcher &fetcher_;
+    friend class blocking_connection;
+};
+
+}
+
+#endif  /*!PROTON_CPP_BLOCKING_RECEIVER_HPP*/

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/container.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/container.hpp b/proton-c/bindings/cpp/include/proton/container.hpp
index ec55167..a0ca59a 100644
--- a/proton-c/bindings/cpp/include/proton/container.hpp
+++ b/proton-c/bindings/cpp/include/proton/container.hpp
@@ -81,9 +81,9 @@ class container : public handle<container_impl>
     PN_CPP_EXTERN sender create_sender(const proton::url &);
 
     /** Create a receiver on connection with target=addr and optional handler h */
-    PN_CPP_EXTERN receiver create_receiver(connection &connection, const std::string &addr, handler *h=0);
+    PN_CPP_EXTERN receiver create_receiver(connection &connection, const std::string &addr, bool dynamic=false, handler *h=0);
 
-    /** Create a receiver on connection with source=addr and optional handler h */
+    /** Create a receiver on connection with source=url.path() */
     PN_CPP_EXTERN receiver create_receiver(const url &);
 
     /** Open a connection to url and create a receiver with source=url.path() */
@@ -103,6 +103,7 @@ class container : public handle<container_impl>
 
     PN_CPP_EXTERN void wakeup();
     PN_CPP_EXTERN bool is_quiesced();
+    PN_CPP_EXTERN void yield();
 private:
    friend class private_impl_ref<container>;
 };

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/delivery.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/delivery.hpp b/proton-c/bindings/cpp/include/proton/delivery.hpp
index 3f4dafc..3fa3069 100644
--- a/proton-c/bindings/cpp/include/proton/delivery.hpp
+++ b/proton-c/bindings/cpp/include/proton/delivery.hpp
@@ -56,7 +56,8 @@ class delivery : public proton_handle<pn_delivery_t>
     /** Settle the delivery, informs the remote end. */
     PN_CPP_EXTERN void settle();
 
-    // TODO aconway 2015-07-15: add update() here?
+    /** Set the local state of the delivery. */
+    PN_CPP_EXTERN void update(delivery::state state);
 
     PN_CPP_EXTERN pn_delivery_t *pn_delivery();
   private:

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/event.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/event.hpp b/proton-c/bindings/cpp/include/proton/event.hpp
index c4c5d7c..22866e1 100644
--- a/proton-c/bindings/cpp/include/proton/event.hpp
+++ b/proton-c/bindings/cpp/include/proton/event.hpp
@@ -52,6 +52,8 @@ class event {
     virtual PN_CPP_EXTERN class receiver receiver();
     /// Get link @throws error if no link.
     virtual PN_CPP_EXTERN class link link();
+    /// Get delivey @throws error if no delivery.
+    virtual PN_CPP_EXTERN class delivery delivery();
     /// Get message @throws error if no message.
     virtual PN_CPP_EXTERN class message message();
     /// Get message @throws error if no message.

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/messaging_event.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/messaging_event.hpp b/proton-c/bindings/cpp/include/proton/messaging_event.hpp
index 950c389..e27003c 100644
--- a/proton-c/bindings/cpp/include/proton/messaging_event.hpp
+++ b/proton-c/bindings/cpp/include/proton/messaging_event.hpp
@@ -89,6 +89,7 @@ class messaging_event : public proton_event
     virtual PN_CPP_EXTERN class receiver receiver();
     virtual PN_CPP_EXTERN class link link();
     virtual PN_CPP_EXTERN class message message();
+    virtual PN_CPP_EXTERN class delivery delivery();
     virtual PN_CPP_EXTERN void message(class message &);
     PN_CPP_EXTERN event_type type() const;
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/proton_event.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/proton_event.hpp b/proton-c/bindings/cpp/include/proton/proton_event.hpp
index d811ae5..1243717 100644
--- a/proton-c/bindings/cpp/include/proton/proton_event.hpp
+++ b/proton-c/bindings/cpp/include/proton/proton_event.hpp
@@ -274,6 +274,7 @@ class proton_event : public event
     virtual PN_CPP_EXTERN class sender sender();
     virtual PN_CPP_EXTERN class receiver receiver();
     virtual PN_CPP_EXTERN class link link();
+    virtual PN_CPP_EXTERN class delivery delivery();
 
     /** Get type of event */
     PN_CPP_EXTERN event_type type();

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/include/proton/receiver.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/receiver.hpp b/proton-c/bindings/cpp/include/proton/receiver.hpp
index 4f2df0f..1974cd0 100644
--- a/proton-c/bindings/cpp/include/proton/receiver.hpp
+++ b/proton-c/bindings/cpp/include/proton/receiver.hpp
@@ -38,6 +38,7 @@ class receiver : public link
     PN_CPP_EXTERN receiver(pn_link_t *lnk);
     PN_CPP_EXTERN receiver();
     PN_CPP_EXTERN receiver(const link& c);
+    PN_CPP_EXTERN void flow(int count);
   protected:
     PN_CPP_EXTERN virtual void verify_type(pn_link_t *l);
 };

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/blocking_connection.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection.cpp b/proton-c/bindings/cpp/src/blocking_connection.cpp
index 02cec85..dd1aebb 100644
--- a/proton-c/bindings/cpp/src/blocking_connection.cpp
+++ b/proton-c/bindings/cpp/src/blocking_connection.cpp
@@ -21,9 +21,11 @@
 #include "proton/container.hpp"
 #include "proton/blocking_connection.hpp"
 #include "proton/blocking_sender.hpp"
+#include "proton/blocking_receiver.hpp"
 #include "proton/messaging_handler.hpp"
 #include "proton/url.hpp"
 #include "proton/error.hpp"
+#include "fetcher.hpp"
 #include "msg.hpp"
 #include "blocking_connection_impl.hpp"
 #include "private_impl_ref.hpp"
@@ -57,6 +59,23 @@ blocking_sender blocking_connection::create_sender(const std::string &address, h
     return blocking_sender(*this, sender);
 }
 
+namespace {
+struct fetcher_guard{
+    fetcher_guard(fetcher &f) : fetcher_(f) { fetcher_.incref(); }
+    ~fetcher_guard() { fetcher_.decref(); }
+    fetcher& fetcher_;
+};
+}
+
+blocking_receiver blocking_connection::create_receiver(const std::string &address, int credit,
+                                                       bool dynamic, std::string name) {
+    fetcher *f = new fetcher(*this, credit);
+    fetcher_guard fg(*f);
+    receiver receiver = impl_->container_.create_receiver(impl_->connection_, address, dynamic, f);
+    blocking_receiver brcv(*this, receiver, *f, credit);
+    return brcv;
+}
+
 duration blocking_connection::timeout() { return impl_->timeout(); }
 
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection_impl.hpp b/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
index edb08a6..7e8c031 100644
--- a/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
+++ b/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
@@ -1,5 +1,5 @@
-#ifndef PROTON_CPP_CONNECTIONIMPL_H
-#define PROTON_CPP_CONNECTIONIMPL_H
+#ifndef PROTON_CPP_BLOCKINGCONNECTIONIMPL_H
+#define PROTON_CPP_BLOCKINGCONNECTIONIMPL_H
 
 /*
  *
@@ -59,4 +59,4 @@ class ssl_domain;
 
 }
 
-#endif  /*!PROTON_CPP_CONNECTIONIMPL_H*/
+#endif  /*!PROTON_CPP_BLOCKINGCONNECTIONIMPL_H*/

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/blocking_link.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_link.cpp b/proton-c/bindings/cpp/src/blocking_link.cpp
index b9f23c4..c7f3551 100644
--- a/proton-c/bindings/cpp/src/blocking_link.cpp
+++ b/proton-c/bindings/cpp/src/blocking_link.cpp
@@ -55,6 +55,7 @@ blocking_link::blocking_link(blocking_connection *c, pn_link_t *pnl) : connectio
     std::string msg = "Opening link " + link_.name();
     link_opened link_opened(link_.pn_link());
     connection_.wait(link_opened, msg);
+    check_closed();
 }
 
 blocking_link::~blocking_link() {}
@@ -70,8 +71,7 @@ void blocking_link::check_closed() {
     pn_link_t * pn_link = link_.pn_link();
     if (pn_link_state(pn_link) & PN_REMOTE_CLOSED) {
         link_.close();
-        // TODO: link_detached exception
-        throw error(MSG("link detached"));
+        throw error(MSG("Link detached: " << link_.name()));
     }
 }
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/blocking_receiver.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_receiver.cpp b/proton-c/bindings/cpp/src/blocking_receiver.cpp
new file mode 100644
index 0000000..042eb29
--- /dev/null
+++ b/proton-c/bindings/cpp/src/blocking_receiver.cpp
@@ -0,0 +1,104 @@
+/*
+ *
+ * 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 "proton/blocking_receiver.hpp"
+#include "proton/blocking_connection.hpp"
+#include "proton/wait_condition.hpp"
+#include "proton/error.hpp"
+#include "fetcher.hpp"
+#include "msg.hpp"
+
+
+namespace proton {
+
+namespace {
+
+struct fetcher_has_message : public wait_condition {
+    fetcher_has_message(fetcher &f) : fetcher_(f) {}
+    bool achieved() { return fetcher_.has_message(); }
+    fetcher &fetcher_;
+};
+
+} // namespace
+
+
+blocking_receiver::blocking_receiver(blocking_connection &c, receiver &l, fetcher &f, int credit)
+    : blocking_link(&c, l.pn_link()), fetcher_(f) {
+    std::string sa = link_.source().address();
+    std::string rsa = link_.remote_source().address();
+    if (sa.empty() || sa.compare(rsa) != 0) {
+        wait_for_closed();
+        link_.close();
+        std::string txt = "Failed to open receiver " + link_.name() + ", source does not match";
+        throw error(MSG(txt));
+    }
+    if (credit)
+        pn_link_flow(link_.pn_link(), credit);
+    fetcher_.incref();
+}
+
+blocking_receiver::blocking_receiver(const blocking_receiver& r) : blocking_link(r), fetcher_(r.fetcher_) {
+    fetcher_.incref();
+}
+blocking_receiver& blocking_receiver::operator=(const blocking_receiver& r) {
+    if (this == &r) return *this;
+    fetcher_ = r.fetcher_;
+    fetcher_.incref();
+    return *this;
+}
+blocking_receiver::~blocking_receiver() { fetcher_.decref(); }
+
+
+
+message blocking_receiver::receive(duration timeout) {
+    receiver rcv = link_;
+    if (!rcv.credit())
+        rcv.flow(1);
+    std::string txt = "Receiving on receiver " + link_.name();
+    fetcher_has_message cond(fetcher_);
+    connection_.wait(cond, txt, timeout);
+    return fetcher_.pop();
+}
+
+message blocking_receiver::receive() {
+    // Use default timeout
+    return receive(connection_.timeout());
+}
+
+void blocking_receiver::accept() {
+    settle(delivery::ACCEPTED);
+}
+
+void blocking_receiver::reject() {
+    settle(delivery::REJECTED);
+}
+
+void blocking_receiver::release(bool delivered) {
+    if (delivered)
+        settle(delivery::MODIFIED);
+    else
+        settle(delivery::RELEASED);
+}
+
+void blocking_receiver::settle(delivery::state state = delivery::NONE) {
+    fetcher_.settle(state);
+}
+
+} // namespace

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/blocking_sender.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_sender.cpp b/proton-c/bindings/cpp/src/blocking_sender.cpp
index 2ab1ef1..8fea98a 100644
--- a/proton-c/bindings/cpp/src/blocking_sender.cpp
+++ b/proton-c/bindings/cpp/src/blocking_sender.cpp
@@ -44,7 +44,7 @@ blocking_sender::blocking_sender(blocking_connection &c, sender &l) : blocking_l
         wait_for_closed();
         link_.close();
         std::string txt = "Failed to open sender " + link_.name() + ", target does not match";
-        throw error(MSG("container not started"));
+        throw error(MSG(txt));
     }
 }
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/connection_impl.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/connection_impl.cpp b/proton-c/bindings/cpp/src/connection_impl.cpp
index 51da569..e2f4608 100644
--- a/proton-c/bindings/cpp/src/connection_impl.cpp
+++ b/proton-c/bindings/cpp/src/connection_impl.cpp
@@ -103,6 +103,10 @@ connection &connection_impl::connection() {
 
 container &connection_impl::container() { return (container_); }
 
+
+// TODO: Rework this.  Rename and document excellently for user handlers on connections.
+// Better: provide general solution for handlers that delete before the C reactor object
+// has finished sending events.
 void connection_impl::reactor_detach() {
     // "save" goes out of scope last, preventing possible recursive destructor
     // confusion with reactor_reference.

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/container.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/container.cpp b/proton-c/bindings/cpp/src/container.cpp
index 07cb971..a424c0b 100644
--- a/proton-c/bindings/cpp/src/container.cpp
+++ b/proton-c/bindings/cpp/src/container.cpp
@@ -72,8 +72,8 @@ sender container::create_sender(const proton::url &url) {
     return impl_->create_sender(url);
 }
 
-receiver container::create_receiver(connection &connection, const std::string &addr, handler *h) {
-    return impl_->create_receiver(connection, addr, h);
+receiver container::create_receiver(connection &connection, const std::string &addr, bool dynamic, handler *h) {
+    return impl_->create_receiver(connection, addr, dynamic, h);
 }
 
 receiver container::create_receiver(const proton::url &url) {
@@ -91,5 +91,6 @@ bool container::process() { return impl_->process(); }
 void container::stop() { impl_->stop(); }
 void container::wakeup() { impl_->wakeup(); }
 bool container::is_quiesced() { return impl_->is_quiesced(); }
+void container::yield() { impl_->yield(); }
 
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/container_impl.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/container_impl.cpp b/proton-c/bindings/cpp/src/container_impl.cpp
index 0858d73..e328251 100644
--- a/proton-c/bindings/cpp/src/container_impl.cpp
+++ b/proton-c/bindings/cpp/src/container_impl.cpp
@@ -148,6 +148,9 @@ void cpp_handler_dispatch(pn_handler_t *c_handler, pn_event_t *cevent, pn_event_
     container c(inbound_context::get(c_handler)->container_impl_);
     messaging_event mevent(cevent, type, c);
     mevent.dispatch(*inbound_context::get(c_handler)->cpp_handler_);
+    // Possible decref and deletion via a handler action on this event.
+    // return without further processing.
+    return;
 }
 
 void cpp_handler_cleanup(pn_handler_t *c_handler)
@@ -199,7 +202,7 @@ container_impl::~container_impl() {
 connection container_impl::connect(const proton::url &url, handler *h) {
     if (!reactor_) throw error(MSG("container not started"));
     container ctnr(this);
-    connection conn(ctnr, handler_);
+    connection conn(ctnr, h);
     connector *ctor = new connector(conn);
     // connector self-deletes depending on reconnect logic
     ctor->address(url);  // TODO: url vector
@@ -238,7 +241,9 @@ sender container_impl::create_sender(connection &connection, const std::string &
     pn_terminus_set_address(pn_link_target(lnk), addr.c_str());
     if (h) {
         pn_record_t *record = pn_link_attachments(lnk);
-        pn_record_set_handler(record, wrap_handler(h));
+        pn_handler_t *chandler = wrap_handler(h);
+        pn_record_set_handler(record, chandler);
+        pn_decref(chandler);
     }
     snd.open();
     return snd;
@@ -255,16 +260,21 @@ sender container_impl::create_sender(const proton::url &url) {
     return snd;
 }
 
-receiver container_impl::create_receiver(connection &connection, const std::string &addr, handler *h) {
+receiver container_impl::create_receiver(connection &connection, const std::string &addr, bool dynamic, handler *h) {
     if (!reactor_) throw error(MSG("container not started"));
     connection_impl *conn_impl = impl(connection);
     session session = default_session(conn_impl->pn_connection_, &conn_impl->default_session_);
     receiver rcv = session.create_receiver(container_id_ + '-' + addr);
     pn_link_t *lnk = rcv.pn_link();
-    pn_terminus_set_address(pn_link_source(lnk), addr.c_str());
+    pn_terminus_t *src = pn_link_source(lnk);
+    pn_terminus_set_address(src, addr.c_str());
+    if (dynamic)
+        pn_terminus_set_dynamic(src, true);
     if (h) {
         pn_record_t *record = pn_link_attachments(lnk);
-        pn_record_set_handler(record, wrap_handler(h));
+        pn_handler_t *chandler = wrap_handler(h);
+        pn_record_set_handler(record, chandler);
+        pn_decref(chandler);
     }
     rcv.open();
     return rcv;
@@ -360,4 +370,9 @@ bool container_impl::is_quiesced() {
     return pn_reactor_quiesced(reactor_);
 }
 
+void container_impl::yield() {
+    if (!reactor_) throw error(MSG("container not started"));
+    pn_reactor_yield(reactor_);
+}
+
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/container_impl.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/container_impl.hpp b/proton-c/bindings/cpp/src/container_impl.hpp
index 4378f7d..1ce27ee 100644
--- a/proton-c/bindings/cpp/src/container_impl.hpp
+++ b/proton-c/bindings/cpp/src/container_impl.hpp
@@ -48,7 +48,7 @@ class container_impl
     PN_CPP_EXTERN pn_reactor_t *reactor();
     PN_CPP_EXTERN sender create_sender(connection &connection, const std::string &addr, handler *h);
     PN_CPP_EXTERN sender create_sender(const url&);
-    PN_CPP_EXTERN receiver create_receiver(connection &connection, const std::string &addr, handler *h);
+    PN_CPP_EXTERN receiver create_receiver(connection &connection, const std::string &addr, bool dynamic, handler *h);
     PN_CPP_EXTERN receiver create_receiver(const url&);
     PN_CPP_EXTERN class acceptor listen(const url&);
     PN_CPP_EXTERN std::string container_id();
@@ -59,6 +59,7 @@ class container_impl
     void stop();
     void wakeup();
     bool is_quiesced();
+    void yield();
     pn_handler_t *wrap_handler(handler *h);
     static void incref(container_impl *);
     static void decref(container_impl *);

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/delivery.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/delivery.cpp b/proton-c/bindings/cpp/src/delivery.cpp
index 7ff596f..6c87939 100644
--- a/proton-c/bindings/cpp/src/delivery.cpp
+++ b/proton-c/bindings/cpp/src/delivery.cpp
@@ -52,6 +52,10 @@ void delivery::settle() {
     pn_delivery_settle(impl_);
 }
 
+void delivery::update(delivery::state state) {
+    pn_delivery_update(impl_, state);
+}
+
 pn_delivery_t *delivery::pn_delivery() { return impl_; }
 
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/event.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/event.cpp b/proton-c/bindings/cpp/src/event.cpp
index e12d19c..2affed1 100644
--- a/proton-c/bindings/cpp/src/event.cpp
+++ b/proton-c/bindings/cpp/src/event.cpp
@@ -57,6 +57,10 @@ class link event::link() {
     throw error(MSG("No link context for event"));
 }
 
+class delivery event::delivery() {
+    throw error(MSG("No link context for event"));
+}
+
 class message event::message() {
     throw error(MSG("No message associated with event"));
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/fetcher.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/fetcher.cpp b/proton-c/bindings/cpp/src/fetcher.cpp
new file mode 100644
index 0000000..46ee18c
--- /dev/null
+++ b/proton-c/bindings/cpp/src/fetcher.cpp
@@ -0,0 +1,92 @@
+/*
+ *
+ * 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 "fetcher.hpp"
+#include "proton/event.hpp"
+
+namespace proton {
+
+fetcher::fetcher(blocking_connection &c, int prefetch) :
+    messaging_handler(prefetch, false), // no auto_accept
+    connection_(c), refcount_(0), pn_link_(0) {
+}
+
+void fetcher::incref() { refcount_++; }
+void fetcher::decref() {
+    refcount_--;
+    if (!refcount_) {
+        // fetcher needs to outlive its blocking_receiver unless disconnected from reactor
+        if (pn_link_) {
+            pn_record_set_handler(pn_link_attachments(pn_link_), 0);
+            pn_decref(pn_link_);
+        }
+        delete this;
+        return;
+    }
+}
+
+void fetcher::on_link_init(event &e) {
+    pn_link_ = e.link().pn_link();
+    pn_incref(pn_link_);
+}
+
+void fetcher::on_message(event &e) {
+    messages_.push_back(e.message());
+    deliveries_.push_back(e.delivery());
+    // Wake up enclosing blocking_connection.wait()
+    e.container().yield();
+}
+
+void fetcher::on_link_error(event &e) {
+    link lnk = e.link();
+    if (pn_link_state(lnk.pn_link()) & PN_LOCAL_ACTIVE) {
+        lnk.close();
+        throw error(MSG("Link detached: " << lnk.name()));
+    }
+}
+
+void fetcher::on_connection_error(event &e) {
+    throw error(MSG("Connection closed"));
+}
+
+bool fetcher::has_message() {
+    return !messages_.empty();
+}
+
+message fetcher::pop() {
+    if (messages_.empty())
+        throw error(MSG("blocking_receiver has no messages"));
+    delivery &dlv(deliveries_.front());
+    if (!dlv.settled())
+        unsettled_.push_back(dlv);
+    message m = messages_.front();
+    messages_.pop_front();
+    deliveries_.pop_front();
+    return m;
+}
+
+void fetcher::settle(delivery::state state) {
+    delivery &dlv = unsettled_.front();
+    if (state)
+        dlv.update(state);
+    dlv.settle();
+}
+
+} // namespace

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/fetcher.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/fetcher.hpp b/proton-c/bindings/cpp/src/fetcher.hpp
new file mode 100644
index 0000000..85200fe
--- /dev/null
+++ b/proton-c/bindings/cpp/src/fetcher.hpp
@@ -0,0 +1,58 @@
+#ifndef PROTON_CPP_FETCHER_H
+#define PROTON_CPP_FETCHER_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 "proton/container.hpp"
+#include "proton/messaging_handler.hpp"
+#include "proton/blocking_connection.hpp"
+#include "proton/error.hpp"
+#include "msg.hpp"
+#include <string>
+#include <deque>
+
+namespace proton {
+
+class fetcher : public messaging_handler {
+  private:
+    blocking_connection connection_;
+    std::deque<message> messages_;
+    std::deque<delivery> deliveries_;
+    std::deque<delivery> unsettled_;
+    int refcount_;
+    pn_link_t *pn_link_;
+  public:
+    fetcher(blocking_connection &c, int p);
+    void incref();
+    void decref();
+    void on_message(event &e);
+    void on_link_error(event &e);
+    void on_connection_error(event &e);
+    void on_link_init(event &e);
+    bool has_message();
+    message pop();
+    void settle(delivery::state state = delivery::NONE);
+};
+
+
+}
+
+#endif  /*!PROTON_CPP_FETCHER_H*/

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/messaging_event.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/messaging_event.cpp b/proton-c/bindings/cpp/src/messaging_event.cpp
index d79974d..5062d76 100644
--- a/proton-c/bindings/cpp/src/messaging_event.cpp
+++ b/proton-c/bindings/cpp/src/messaging_event.cpp
@@ -82,6 +82,14 @@ link messaging_event::link() {
     throw error(MSG("No link context for event"));
 }
 
+delivery messaging_event::delivery() {
+    if (type_ == messaging_event::PROTON)
+        return proton_event::delivery();
+    if (parent_event_)
+        return parent_event_->delivery();
+    throw error(MSG("No delivery context for event"));
+}
+
 message messaging_event::message() {
     if (parent_event_) {
         pn_message_t *m = event_context(parent_event_->pn_event());

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/proton_event.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/proton_event.cpp b/proton-c/bindings/cpp/src/proton_event.cpp
index 71f2a08..46b43ee 100644
--- a/proton-c/bindings/cpp/src/proton_event.cpp
+++ b/proton-c/bindings/cpp/src/proton_event.cpp
@@ -78,6 +78,13 @@ link proton_event::link() {
     throw error(MSG("No link context for this event"));
 }
 
+delivery proton_event::delivery() {
+    pn_delivery_t *dlv = pn_event_delivery(pn_event());
+    if (dlv)
+        return proton::delivery(dlv);
+    throw error(MSG("No delivery context for this event"));
+}
+
 
 
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/4339b1c7/proton-c/bindings/cpp/src/receiver.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/receiver.cpp b/proton-c/bindings/cpp/src/receiver.cpp
index 84412e6..b9ed680 100644
--- a/proton-c/bindings/cpp/src/receiver.cpp
+++ b/proton-c/bindings/cpp/src/receiver.cpp
@@ -40,5 +40,8 @@ void receiver::verify_type(pn_link_t *lnk) {
         throw error(MSG("Creating receiver with sender context"));
 }
 
+void receiver::flow(int count) {
+    pn_link_flow(pn_link(), count);
+}
 
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


[2/2] qpid-proton git commit: PROTON-865: sync_request_response

Posted by cl...@apache.org.
PROTON-865: sync_request_response


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/8e1757eb
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/8e1757eb
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/8e1757eb

Branch: refs/heads/cjansen-cpp-client
Commit: 8e1757ebcf200d1475ec927a0d8e7eb7e2a9e06e
Parents: 4339b1c
Author: Clifford Jansen <cl...@apache.org>
Authored: Wed Jul 29 16:54:42 2015 -0700
Committer: Clifford Jansen <cl...@apache.org>
Committed: Wed Jul 29 16:54:42 2015 -0700

----------------------------------------------------------------------
 examples/cpp/CMakeLists.txt                     |  2 +
 examples/cpp/example_test.py                    | 40 +++++++++
 examples/cpp/options.hpp                        |  2 +-
 examples/cpp/server.cpp                         | 95 ++++++++++++++++++++
 examples/cpp/simple_recv.cpp                    |  2 +-
 examples/cpp/sync_client.cpp                    | 65 ++++++++++++++
 proton-c/bindings/cpp/CMakeLists.txt            |  1 +
 .../cpp/include/proton/blocking_connection.hpp  |  5 +-
 .../cpp/include/proton/blocking_receiver.hpp    | 16 +++-
 proton-c/bindings/cpp/include/proton/link.hpp   |  2 +-
 .../include/proton/sync_request_response.hpp    | 58 ++++++++++++
 .../bindings/cpp/src/blocking_connection.cpp    | 23 +++--
 .../cpp/src/blocking_connection_impl.cpp        |  4 +
 .../cpp/src/blocking_connection_impl.hpp        |  1 +
 proton-c/bindings/cpp/src/blocking_receiver.cpp | 54 +++++++++--
 .../bindings/cpp/src/sync_request_response.cpp  | 77 ++++++++++++++++
 16 files changed, 423 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/examples/cpp/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt
index fde4eee..f32c38a 100644
--- a/examples/cpp/CMakeLists.txt
+++ b/examples/cpp/CMakeLists.txt
@@ -30,6 +30,8 @@ foreach(example
     simple_send
     direct_recv
     direct_send
+    sync_client
+    server
     encode_decode)
   add_executable(${example} ${example}.cpp)
   target_link_libraries(${example} qpid-proton-cpp)

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/examples/cpp/example_test.py
----------------------------------------------------------------------
diff --git a/examples/cpp/example_test.py b/examples/cpp/example_test.py
index 8dd4341..38e3c2c 100644
--- a/examples/cpp/example_test.py
+++ b/examples/cpp/example_test.py
@@ -102,6 +102,31 @@ class Broker(object):
         except Exception as e:
             raise Exception("Error running %s: %s", cmd, e)
 
+class Server(object):
+    """Run the test server"""
+
+    @classmethod
+    def get(cls, addr):
+        if not hasattr(cls, "_server"):
+            cls._server = Server(addr)
+        return cls._server
+
+    @classmethod
+    def stop(cls):
+        if cls.get(None) and cls._server.process:
+            cls._server.process.kill()
+            cls._server = None
+
+    def __init__(self, addr):
+        self.addr = addr
+        cmd = [exe_name("server"), "-a", self.addr]
+        try:
+            self.process = Popen(cmd, stdout=NULL, stderr=NULL)
+            time.sleep(0.3)
+
+        except Exception as e:
+            raise Exception("Error running %s: %s", cmd, e)
+
 class ExampleTest(unittest.TestCase):
     """Run the examples, verify they behave as expected."""
 
@@ -161,6 +186,21 @@ class ExampleTest(unittest.TestCase):
         recv_expect += "".join(['{"sequence"=%s}\n' % (i+1) for i in range(100)])
         self.assertEqual(recv_expect, verify(recv))
 
+    def test_sync_request_response(self):
+        """Start server first, then run sync_client"""
+        b = Broker.get()
+        s = Server.get(b.addr)
+        expect = """
+"Twas brillig, and the slithy toves" => "TWAS BRILLIG, AND THE SLITHY TOVES"
+"Did gire and gymble in the wabe." => "DID GIRE AND GYMBLE IN THE WABE."
+"All mimsy were the borogroves," => "ALL MIMSY WERE THE BOROGROVES,"
+"And the mome raths outgrabe." => "AND THE MOME RATHS OUTGRABE."
+"""
+        sc = "\n"
+        sc += execute("sync_client", "-a", b.addr)
+        self.assertEqual(expect, sc)
+        Server.stop()
+
     def test_encode_decode(self):
         expect="""
 == Simple values: int, string, bool

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/examples/cpp/options.hpp
----------------------------------------------------------------------
diff --git a/examples/cpp/options.hpp b/examples/cpp/options.hpp
index 5378507..bd477b5 100644
--- a/examples/cpp/options.hpp
+++ b/examples/cpp/options.hpp
@@ -125,7 +125,7 @@ class options {
             if (arg.compare(0, long_.size(), long_) == 0 && arg[long_.size()] == '=' ) {
                 set_value(long_, arg.substr(long_.size()+1));
                 return true;
-            } 
+            }
             return false;
         }
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/examples/cpp/server.cpp
----------------------------------------------------------------------
diff --git a/examples/cpp/server.cpp b/examples/cpp/server.cpp
new file mode 100644
index 0000000..78b78d3
--- /dev/null
+++ b/examples/cpp/server.cpp
@@ -0,0 +1,95 @@
+/*
+ *
+ * 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 "options.hpp"
+
+#include "proton/container.hpp"
+#include "proton/messaging_handler.hpp"
+#include "proton/url.hpp"
+
+#include <iostream>
+#include <map>
+#include <string>
+
+class server : public proton::messaging_handler {
+  private:
+    typedef std::map<std::string, proton::sender> sender_map;
+    proton::url url;
+    proton::connection connection;
+    sender_map senders;
+
+  public:
+
+    server(const proton::url &u) : url(u) {}
+
+    void on_start(proton::event &e) {
+        connection = e.container().connect(url);
+        e.container().create_receiver(connection, url.path());
+        std::cout << "server listening on " << url << std::endl;
+    }
+
+    std::string to_upper(const std::string &s) {
+        std::string uc(s);
+        size_t l = uc.size();
+        for (size_t i=0; i<l; i++) uc[i] = std::toupper(uc[i]);
+        return uc;
+    }
+
+    // TODO: on_connection_opened() and ANONYMOUS-RELAY
+
+    void on_message(proton::event &e) {
+        proton::sender sender;
+        proton::message msg = e.message();
+        std::cout << "Received " << msg.body() << std::endl;
+        std::string sender_id = msg.reply_to();
+        sender_map::iterator it = senders.find(sender_id);
+        if (it == senders.end()) {
+            sender = e.container().create_sender(connection, sender_id);
+            senders[sender_id] = sender;
+        }
+        else {
+            sender = it->second;
+        }
+        proton::message reply;
+        reply.body(to_upper(msg.body().get<std::string>()));
+        reply.correlation_id(msg.correlation_id());
+        reply.address(sender_id);
+        sender.send(reply);
+    }
+};
+
+int main(int argc, char **argv) {
+    // Command line options
+    proton::url url("amqp://127.0.0.1:5672/examples");
+    options opts(argc, argv);
+    opts.add_value(url, 'a', "address", "listen on URL", "URL");
+    try {
+        opts.parse();
+        server server(url);
+        proton::container(server).run();
+        return 0;
+    } catch (const bad_option& e) {
+        std::cout << opts << std::endl << e.what() << std::endl;
+    } catch (const std::exception& e) {
+        std::cerr << e.what() << std::endl;
+    }
+    return 1;
+}

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/examples/cpp/simple_recv.cpp
----------------------------------------------------------------------
diff --git a/examples/cpp/simple_recv.cpp b/examples/cpp/simple_recv.cpp
index b2cec63..94cd398 100644
--- a/examples/cpp/simple_recv.cpp
+++ b/examples/cpp/simple_recv.cpp
@@ -77,7 +77,7 @@ int main(int argc, char **argv) {
         opts.parse();
         simple_recv recv(address, message_count);
         proton::container(recv).run();
-        return 0; 
+        return 0;
     } catch (const bad_option& e) {
         std::cout << opts << std::endl << e.what() << std::endl;
     } catch (const std::exception& e) {

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/examples/cpp/sync_client.cpp
----------------------------------------------------------------------
diff --git a/examples/cpp/sync_client.cpp b/examples/cpp/sync_client.cpp
new file mode 100644
index 0000000..e141a3e
--- /dev/null
+++ b/examples/cpp/sync_client.cpp
@@ -0,0 +1,65 @@
+/*
+ *
+ * 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 "options.hpp"
+
+#include "proton/container.hpp"
+#include "proton/sync_request_response.hpp"
+#include "proton/url.hpp"
+#include "proton/value.hpp"
+#include "proton/types.hpp"
+
+#include <iostream>
+#include <string>
+
+int main(int argc, char **argv) {
+    // Command line options
+    proton::url url("127.0.0.1:5672/examples");
+    uint64_t timeout(5000);
+    options opts(argc, argv);
+    opts.add_value(url, 'a', "address", "connect to URL", "URL");
+    opts.add_value(timeout, 't', "timeout", "give up after this TIMEOUT (milliseconds)", "TIMEOUT");
+
+    std::string requests[] = { "Twas brillig, and the slithy toves",
+                               "Did gire and gymble in the wabe.",
+                               "All mimsy were the borogroves,",
+                               "And the mome raths outgrabe." };
+    int requests_size=4;
+
+    try {
+        opts.parse();
+        proton::duration d(timeout);
+        proton::blocking_connection conn(url, d);
+        proton::sync_request_response client(conn, url.path());
+        for (int i=0; i<requests_size; i++) {
+            proton::message request;
+            request.body(requests[i]);
+            proton::message response = client.call(request);
+            std::cout << request.body() << " => " << response.body() << std::endl;
+        }
+        return 0;
+    } catch (const bad_option& e) {
+        std::cout << opts << std::endl << e.what() << std::endl;
+    } catch (const std::exception& e) {
+        std::cerr << e.what() << std::endl;
+    }
+    return 1;
+}

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/CMakeLists.txt b/proton-c/bindings/cpp/CMakeLists.txt
index fcb0fb1..d1b1ebc 100644
--- a/proton-c/bindings/cpp/CMakeLists.txt
+++ b/proton-c/bindings/cpp/CMakeLists.txt
@@ -70,6 +70,7 @@ set(qpid-proton-cpp-source
   src/blocking_link.cpp
   src/blocking_sender.cpp
   src/blocking_receiver.cpp
+  src/sync_request_response.cpp
   src/contexts.cpp
   src/types.cpp
   )

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/blocking_connection.hpp b/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
index b6f0499..cba5f60 100644
--- a/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
+++ b/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
@@ -56,9 +56,10 @@ class blocking_connection : public handle<blocking_connection_impl>
 
     PN_CPP_EXTERN blocking_sender create_sender(const std::string &address, handler *h=0);
     PN_CPP_EXTERN blocking_receiver create_receiver(const std::string &address, int credit = 0,
-                                                    bool dynamic = false, std::string name = std::string());
+                                                    bool dynamic = false, handler *h=0, std::string name = std::string());
     PN_CPP_EXTERN void wait(wait_condition &condition);
-    PN_CPP_EXTERN void wait(wait_condition &condition, std::string &msg, duration timeout=duration::FOREVER);
+    PN_CPP_EXTERN void wait(wait_condition &condition, std::string &msg);
+    PN_CPP_EXTERN void wait(wait_condition &condition, std::string &msg, duration timeout);
     PN_CPP_EXTERN duration timeout();
   private:
     friend class private_impl_ref<blocking_connection>;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp b/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
index 319fd93..dbcc9d0 100644
--- a/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
+++ b/proton-c/bindings/cpp/include/proton/blocking_receiver.hpp
@@ -49,9 +49,21 @@ class blocking_receiver : public blocking_link
     PN_CPP_EXTERN void release(bool delivered = true);
     PN_CPP_EXTERN void settle();
     PN_CPP_EXTERN void settle(delivery::state state);
+    PN_CPP_EXTERN void flow(int count);
+    /** Credit available on the receiver link */
+    PN_CPP_EXTERN int credit();
+    /** Local source of the receiver link */
+    PN_CPP_EXTERN terminus source();
+    /** Local target of the receiver link */
+    PN_CPP_EXTERN terminus target();
+    /** Remote source of the receiver link */
+    PN_CPP_EXTERN terminus remote_source();
+    /** Remote target of the receiver link */
+    PN_CPP_EXTERN terminus remote_target();
+
   private:
-    blocking_receiver(blocking_connection &c, receiver &l, fetcher &f, int credit);
-    fetcher &fetcher_;
+    blocking_receiver(blocking_connection &c, receiver &l, fetcher *f, int credit);
+    fetcher *fetcher_;
     friend class blocking_connection;
 };
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/include/proton/link.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/link.hpp b/proton-c/bindings/cpp/include/proton/link.hpp
index feabfec..46b8894 100644
--- a/proton-c/bindings/cpp/include/proton/link.hpp
+++ b/proton-c/bindings/cpp/include/proton/link.hpp
@@ -64,7 +64,7 @@ class link : public endpoint, public proton_handle<pn_link_t>
     PN_CPP_EXTERN terminus target();
     /** Remote source of the link */
     PN_CPP_EXTERN terminus remote_source();
-    /** Remote source of the link */
+    /** Remote target of the link */
     PN_CPP_EXTERN terminus remote_target();
     /** Link name */
     PN_CPP_EXTERN std::string name();

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/sync_request_response.hpp b/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
new file mode 100644
index 0000000..604af4b
--- /dev/null
+++ b/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
@@ -0,0 +1,58 @@
+#ifndef PROTON_CPP_SYNCREQUESTRESPONSE_H
+#define PROTON_CPP_SYNCREQUESTRESPONSE_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 "proton/export.hpp"
+#include "proton/messaging_handler.hpp"
+#include "proton/blocking_receiver.hpp"
+#include "proton/blocking_sender.hpp"
+#include "proton/wait_condition.hpp"
+#include <string>
+
+struct pn_message_t;
+struct pn_data_t;
+
+namespace proton {
+
+/// An implementation of the synchronous request-response pattern (aka RPC).
+class sync_request_response : public messaging_handler
+{
+  public:
+    PN_CPP_EXTERN sync_request_response(blocking_connection &, const std::string address=std::string());
+    /** Send a request message, wait for and return the response message. */
+    PN_CPP_EXTERN message call(message &);
+    /** Return the dynamic address of our receiver. */
+    PN_CPP_EXTERN std::string reply_to();
+    /** Called when we receive a message for our receiver. */
+    void on_message(event &e);
+  private:
+    blocking_connection connection_;
+    std::string address_;
+    blocking_sender sender_;
+    blocking_receiver receiver_;
+    message response_;
+    amqp_ulong correlation_id_;
+};
+
+}
+
+#endif  /*!PROTON_CPP_SYNCREQUESTRESPONSE_H*/

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/src/blocking_connection.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection.cpp b/proton-c/bindings/cpp/src/blocking_connection.cpp
index dd1aebb..c0c1477 100644
--- a/proton-c/bindings/cpp/src/blocking_connection.cpp
+++ b/proton-c/bindings/cpp/src/blocking_connection.cpp
@@ -50,6 +50,9 @@ blocking_connection::blocking_connection(const proton::url &url, duration d, ssl
 void blocking_connection::close() { impl_->close(); }
 
 void blocking_connection::wait(wait_condition &cond) { return impl_->wait(cond); }
+void blocking_connection::wait(wait_condition &cond, std::string &msg) {
+    return impl_->wait(cond, msg);
+}
 void blocking_connection::wait(wait_condition &cond, std::string &msg, duration timeout) {
     return impl_->wait(cond, msg, timeout);
 }
@@ -61,18 +64,22 @@ blocking_sender blocking_connection::create_sender(const std::string &address, h
 
 namespace {
 struct fetcher_guard{
-    fetcher_guard(fetcher &f) : fetcher_(f) { fetcher_.incref(); }
-    ~fetcher_guard() { fetcher_.decref(); }
-    fetcher& fetcher_;
+    fetcher_guard(fetcher *f) : fetcher_(f) { if (fetcher_) fetcher_->incref(); }
+    ~fetcher_guard() { if (fetcher_) fetcher_->decref(); }
+    fetcher* fetcher_;
 };
 }
 
 blocking_receiver blocking_connection::create_receiver(const std::string &address, int credit,
-                                                       bool dynamic, std::string name) {
-    fetcher *f = new fetcher(*this, credit);
-    fetcher_guard fg(*f);
-    receiver receiver = impl_->container_.create_receiver(impl_->connection_, address, dynamic, f);
-    blocking_receiver brcv(*this, receiver, *f, credit);
+                                                       bool dynamic, handler *handler, std::string name) {
+    fetcher *f = NULL;
+    if (!handler) {
+        f = new fetcher(*this, credit);
+        handler = f;
+    }
+    fetcher_guard fg(f);
+    receiver receiver = impl_->container_.create_receiver(impl_->connection_, address, dynamic, handler);
+    blocking_receiver brcv(*this, receiver, f, credit);
     return brcv;
 }
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection_impl.cpp b/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
index 26d0e78..0e78541 100644
--- a/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
+++ b/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
@@ -88,6 +88,10 @@ void blocking_connection_impl::wait(wait_condition &condition) {
     wait(condition, empty, timeout_);
 }
 
+void blocking_connection_impl::wait(wait_condition &condition, std::string &msg) {
+    wait(condition, msg, timeout_);
+}
+
 void blocking_connection_impl::wait(wait_condition &condition, std::string &msg, duration wait_timeout) {
     if (wait_timeout == duration::FOREVER) {
         while (!condition.achieved()) {

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection_impl.hpp b/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
index 7e8c031..11ff4fd 100644
--- a/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
+++ b/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
@@ -42,6 +42,7 @@ class ssl_domain;
     PN_CPP_EXTERN ~blocking_connection_impl();
     PN_CPP_EXTERN void close();
     PN_CPP_EXTERN void wait(wait_condition &condition);
+    PN_CPP_EXTERN void wait(wait_condition &condition, std::string &msg);
     PN_CPP_EXTERN void wait(wait_condition &condition, std::string &msg, duration timeout);
     PN_CPP_EXTERN pn_connection_t *pn_blocking_connection();
     duration timeout() { return timeout_; }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/src/blocking_receiver.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_receiver.cpp b/proton-c/bindings/cpp/src/blocking_receiver.cpp
index 042eb29..3ca53c0 100644
--- a/proton-c/bindings/cpp/src/blocking_receiver.cpp
+++ b/proton-c/bindings/cpp/src/blocking_receiver.cpp
@@ -39,11 +39,11 @@ struct fetcher_has_message : public wait_condition {
 } // namespace
 
 
-blocking_receiver::blocking_receiver(blocking_connection &c, receiver &l, fetcher &f, int credit)
+blocking_receiver::blocking_receiver(blocking_connection &c, receiver &l, fetcher *f, int credit)
     : blocking_link(&c, l.pn_link()), fetcher_(f) {
     std::string sa = link_.source().address();
     std::string rsa = link_.remote_source().address();
-    if (sa.empty() || sa.compare(rsa) != 0) {
+    if (!sa.empty() && sa.compare(rsa) != 0) {
         wait_for_closed();
         link_.close();
         std::string txt = "Failed to open receiver " + link_.name() + ", source does not match";
@@ -51,30 +51,38 @@ blocking_receiver::blocking_receiver(blocking_connection &c, receiver &l, fetche
     }
     if (credit)
         pn_link_flow(link_.pn_link(), credit);
-    fetcher_.incref();
+    if (fetcher_)
+        fetcher_->incref();
 }
 
 blocking_receiver::blocking_receiver(const blocking_receiver& r) : blocking_link(r), fetcher_(r.fetcher_) {
-    fetcher_.incref();
+    if (fetcher_)
+        fetcher_->incref();
 }
 blocking_receiver& blocking_receiver::operator=(const blocking_receiver& r) {
     if (this == &r) return *this;
     fetcher_ = r.fetcher_;
-    fetcher_.incref();
+    if (fetcher_)
+        fetcher_->incref();
     return *this;
 }
-blocking_receiver::~blocking_receiver() { fetcher_.decref(); }
+blocking_receiver::~blocking_receiver() {
+    if (fetcher_)
+        fetcher_->decref();
+}
 
 
 
 message blocking_receiver::receive(duration timeout) {
+    if (!fetcher_)
+        throw error(MSG("Can't call receive on this receiver as a handler was provided"));
     receiver rcv = link_;
     if (!rcv.credit())
         rcv.flow(1);
     std::string txt = "Receiving on receiver " + link_.name();
-    fetcher_has_message cond(fetcher_);
+    fetcher_has_message cond(*fetcher_);
     connection_.wait(cond, txt, timeout);
-    return fetcher_.pop();
+    return fetcher_->pop();
 }
 
 message blocking_receiver::receive() {
@@ -98,7 +106,35 @@ void blocking_receiver::release(bool delivered) {
 }
 
 void blocking_receiver::settle(delivery::state state = delivery::NONE) {
-    fetcher_.settle(state);
+    if (!fetcher_)
+        throw error(MSG("Can't call accept/reject etc on this receiver as a handler was provided"));
+    fetcher_->settle(state);
+}
+
+void blocking_receiver::flow(int count) {
+    receiver rcv(link_);
+    rcv.flow(count);
+}
+
+int blocking_receiver::credit() {
+    return link_.credit();
+}
+
+terminus blocking_receiver::source() {
+    return link_.source();
 }
 
+terminus blocking_receiver::target() {
+    return link_.target();
+}
+
+terminus blocking_receiver::remote_source() {
+    return link_.remote_source();
+}
+
+terminus blocking_receiver::remote_target() {
+    return link_.remote_target();
+}
+
+
 } // namespace

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8e1757eb/proton-c/bindings/cpp/src/sync_request_response.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/sync_request_response.cpp b/proton-c/bindings/cpp/src/sync_request_response.cpp
new file mode 100644
index 0000000..eebd62d
--- /dev/null
+++ b/proton-c/bindings/cpp/src/sync_request_response.cpp
@@ -0,0 +1,77 @@
+/*
+ *
+ * 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 "proton/sync_request_response.hpp"
+#include "proton/event.hpp"
+#include "proton/error.hpp"
+#include "msg.hpp"
+
+namespace proton {
+
+namespace {
+amqp_ulong global_correlation_id = 0;
+message null_message;
+
+struct response_received : public wait_condition {
+    response_received(message &m, amqp_ulong id) : message_(m), id_(id) {}
+    bool achieved() { return message_ && message_.correlation_id() == id_; }
+    message &message_;
+    value id_;
+};
+
+}
+
+sync_request_response::sync_request_response(blocking_connection &conn, const std::string addr):
+    connection_(conn), address_(addr),
+    sender_(connection_.create_sender(addr)),
+    receiver_(connection_.create_receiver("", 1, true, this)), // credit=1, dynamic=true
+    response_(null_message)
+{
+}
+
+message sync_request_response::call(message &request) {
+    if (address_.empty() && request.address().empty())
+        throw error(MSG("Request message has no address: " << request));
+    // TODO: thread safe increment.
+    correlation_id_ = global_correlation_id++;
+    request.correlation_id(value(correlation_id_));
+    request.reply_to(this->reply_to());
+    sender_.send(request);
+    std::string txt("Waiting for response");
+    response_received cond(response_, correlation_id_);
+    connection_.wait(cond, txt);
+    message resp = response_;
+    response_ = null_message;
+    receiver_.flow(1);
+    return resp;
+}
+
+std::string sync_request_response::reply_to() {
+    return receiver_.remote_source().address();
+}
+
+void sync_request_response::on_message(event &e) {
+    response_ = e.message();
+    // Wake up enclosing blocking_connection.wait() to handle the message
+    e.container().yield();
+}
+
+
+} // namespace


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org