Skip to content

Commit

Permalink
PROTON-2540: [cpp] Provide a way to query proton::connection for the url
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamPearl authored and astitcher committed May 27, 2022
1 parent 52116e0 commit e23bcf4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cpp/include/proton/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions cpp/src/contexts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/proactor_container_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/reconnect_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++;
}
Expand All @@ -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++;
}

Expand Down Expand Up @@ -249,7 +258,7 @@ int test_empty_failover() {
return 0;
}

}
} // namespace

class stop_reconnect_tester : public proton::messaging_handler {
public:
Expand Down

0 comments on commit e23bcf4

Please sign in to comment.