You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2022/05/27 22:24:59 UTC

[qpid-proton] branch main updated: PROTON-2540: [cpp] Provide a way to query proton::connection for the url

This is an automated email from the ASF dual-hosted git repository.

astitcher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git


The following commit(s) were added to refs/heads/main by this push:
     new e23bcf493 PROTON-2540: [cpp] Provide a way to query proton::connection for the url
e23bcf493 is described below

commit e23bcf4931fff6e549febba3a78787bcdc0c875e
Author: Rakhi Kumari <ra...@gmail.com>
AuthorDate: Tue May 3 19:59:19 2022 +0530

    PROTON-2540: [cpp] Provide a way to query proton::connection for the url
---
 cpp/include/proton/connection.hpp   |  3 +++
 cpp/src/connection.cpp              |  6 ++++++
 cpp/src/contexts.hpp                |  1 +
 cpp/src/proactor_container_impl.cpp |  2 ++
 cpp/src/reconnect_test.cpp          | 11 ++++++++++-
 5 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/cpp/include/proton/connection.hpp b/cpp/include/proton/connection.hpp
index 7509d4312..bff8e21fb 100644
--- a/cpp/include/proton/connection.hpp
+++ b/cpp/include/proton/connection.hpp
@@ -83,6 +83,9 @@ PN_CPP_CLASS_EXTERN connection : public internal::object<pn_connection_t>, publi
     /// Note: The value returned is not stable until the on_transport_open event is received
     PN_CPP_EXTERN std::string user() const;
 
+    /// Return the url for the connection.
+    PN_CPP_EXTERN std::string url() const;
+
     /// Open the connection.
     /// @see messaging_handler
     PN_CPP_EXTERN void open();
diff --git a/cpp/src/connection.cpp b/cpp/src/connection.cpp
index 7d6c18d9e..6293779c8 100644
--- a/cpp/src/connection.cpp
+++ b/cpp/src/connection.cpp
@@ -93,6 +93,12 @@ session_range connection::sessions() const {
     return session_range(session_iterator(make_wrapper(pn_session_head(pn_object(), 0))));
 }
 
+std::string connection::url() const {
+    connection_context& cc = connection_context::get(pn_object());
+    if (!active()) throw proton::error("No active connection");
+    return cc.active_url_;
+}
+
 receiver_range connection::receivers() const {
   pn_link_t *lnk = pn_link_head(pn_object(), 0);
   while (lnk) {
diff --git a/cpp/src/contexts.hpp b/cpp/src/contexts.hpp
index 8d2283769..dbba31dc4 100644
--- a/cpp/src/contexts.hpp
+++ b/cpp/src/contexts.hpp
@@ -100,6 +100,7 @@ class connection_context : public context {
     std::unique_ptr<reconnect_context> reconnect_context_;
     listener_context* listener_context_;
     work_queue work_queue_;
+    std::string active_url_;
 };
 
 class reconnect_options_base;
diff --git a/cpp/src/proactor_container_impl.cpp b/cpp/src/proactor_container_impl.cpp
index 44dfffe11..088e3009f 100644
--- a/cpp/src/proactor_container_impl.cpp
+++ b/cpp/src/proactor_container_impl.cpp
@@ -194,6 +194,7 @@ pn_connection_t* container::impl::make_connection_lh(
     cc.handler = mh;
     cc.work_queue_ = new container::impl::connection_work_queue(*container_.impl_, pnc);
     cc.reconnect_url_ = url;
+    cc.active_url_ = url;
     cc.connection_options_.reset(new connection_options(opts));
 
     make_wrapper(pnc).open(*cc.connection_options_);
@@ -258,6 +259,7 @@ void container::impl::reconnect(pn_connection_t* pnc) {
     opts.update(co);
     messaging_handler* mh = opts.handler();
     cc.handler = mh;
+    cc.active_url_ = url;
 
     make_wrapper(pnc).open(co);
     start_connection(url, pnc);
diff --git a/cpp/src/reconnect_test.cpp b/cpp/src/reconnect_test.cpp
index 7029de2ec..f631b5709 100644
--- a/cpp/src/reconnect_test.cpp
+++ b/cpp/src/reconnect_test.cpp
@@ -41,6 +41,10 @@
 
 namespace {
 
+// The actual port used for making a url.
+// Gets updated whenever a connection is open on server side.
+int listening_port_;
+
 // Wait for N things to be done.
 class waiter {
     size_t count;
@@ -99,6 +103,7 @@ class server_connection_handler : public proton::messaging_handler {
     }
 
     void on_connection_open(proton::connection &c) override {
+        listening_port_ = listener_.port();
         // Only listen for a single connection
         listener_.stop();
         if (messages_==expect_) close(c);
@@ -129,10 +134,13 @@ class server_connection_handler : public proton::messaging_handler {
 
 class tester_base: public proton::messaging_handler {
   void on_connection_open(proton::connection& c) override {
+    std::string want_url = "amqp://localhost:" + std::to_string(listening_port_);
+
     if (!c.reconnected()) {
       start_count++;
       c.open_sender("messages");
     }
+    ASSERT_EQUAL(c.url(), want_url);
     ASSERT_EQUAL(bool(open_count), c.reconnected());
     open_count++;
   }
@@ -159,6 +167,7 @@ class tester_base: public proton::messaging_handler {
   }
 
   void on_transport_close(proton::transport& t) override {
+    ASSERT_THROWS_MSG(proton::error, "No active connection", t.connection().url());
     transport_close_count++;
   }
 
@@ -249,7 +258,7 @@ int test_empty_failover() {
     return 0;
 }
 
-}
+} // namespace
 
 class stop_reconnect_tester : public proton::messaging_handler {
   public:


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