From 9d96e0037a9d027fd286f771fa2a8db5def485c8 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Wed, 13 Oct 2010 10:09:46 +0200 Subject: Clean-up of the code related to attaching/detaching engines to sessions. Session base class now handles the engine events exclusively. It notifies derived session types using dedicated "attached" and "detached" events. Couple of bugs was fixed along the way. Signed-off-by: Martin Sustrik --- src/connect_session.cpp | 9 ++++----- src/connect_session.hpp | 3 ++- src/named_session.cpp | 15 +++++---------- src/named_session.hpp | 5 +---- src/session.cpp | 18 ++++++++---------- src/session.hpp | 12 ++++++------ src/transient_session.cpp | 4 ++++ src/transient_session.hpp | 3 ++- 8 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/connect_session.cpp b/src/connect_session.cpp index 833b551..4d8e322 100644 --- a/src/connect_session.cpp +++ b/src/connect_session.cpp @@ -105,14 +105,13 @@ void zmq::connect_session_t::start_connecting () zmq_assert (false); } -void zmq::connect_session_t::detached () +void zmq::connect_session_t::attached (const blob_t &peer_identity_) { - // Clean up the mess left over by the failed connection. - clean_pipes (); +} +void zmq::connect_session_t::detached () +{ // Reconnect. start_connecting (); - - session_t::detached (); } diff --git a/src/connect_session.hpp b/src/connect_session.hpp index 65037f8..11aa253 100644 --- a/src/connect_session.hpp +++ b/src/connect_session.hpp @@ -41,7 +41,8 @@ namespace zmq private: - // Hook into session's disconnection mechanism. + // Handlers for events from session base class. + void attached (const blob_t &peer_identity_); void detached (); // Start the connection process. diff --git a/src/named_session.cpp b/src/named_session.cpp index a430091..b6a3acf 100644 --- a/src/named_session.cpp +++ b/src/named_session.cpp @@ -41,14 +41,9 @@ zmq::named_session_t::named_session_t (class io_thread_t *io_thread_, zmq::named_session_t::~named_session_t () { -} - -void zmq::named_session_t::detach () -{ - // Clean up the mess left over by the failed connection. - clean_pipes (); - - // Do nothing. Wait till the connection comes up again. + // Unregister the session from the global list of named sessions. + if (!name.empty ()) + unregister_session (name); } void zmq::named_session_t::attached (const blob_t &peer_identity_) @@ -83,7 +78,7 @@ void zmq::named_session_t::attached (const blob_t &peer_identity_) void zmq::named_session_t::detached () { - unregister_session (name); - session_t::detached (); + // Do nothing. Named sessions are never destroyed because of disconnection, + // neither they have to actively reconnect. } diff --git a/src/named_session.hpp b/src/named_session.hpp index 335b4a9..9c8f814 100644 --- a/src/named_session.hpp +++ b/src/named_session.hpp @@ -38,10 +38,7 @@ namespace zmq const blob_t &name_); ~named_session_t (); - // i_inout interface implementation. - void detach (); - - // Handle events from session_t base class. + // Handlers for events from session base class. void attached (const blob_t &peer_identity_); void detached (); diff --git a/src/session.cpp b/src/session.cpp index 76e782b..d5c6fdd 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -260,7 +260,15 @@ void zmq::session_t::detach () // Engine is dead. Let's forget about it. engine = NULL; + // Remove any half-done messages from the pipes. + clean_pipes (); + + // Send the event to the derived class. detached (); + + // Just in case, there's only a delimiter in the inbound pipe. + if (in_pipe) + in_pipe->check_read (); } void zmq::session_t::process_term () @@ -293,16 +301,6 @@ void zmq::session_t::unregister_session (const blob_t &name_) socket->unregister_session (name_); } -void zmq::session_t::attached (const blob_t &peer_identity_) -{ -} - -void zmq::session_t::detached () -{ - if (in_pipe) - in_pipe->check_read (); -} - void zmq::session_t::terminate () { force_terminate = true; diff --git a/src/session.hpp b/src/session.hpp index 7e528de..0f90e80 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -70,8 +70,8 @@ namespace zmq // when session is attached to a peer, detached is triggered at the // beginning of the termination process when session is about to // be detached from the peer. - virtual void attached (const blob_t &peer_identity_); - virtual void detached (); + virtual void attached (const blob_t &peer_identity_) = 0; + virtual void detached () = 0; // Allows derives session types to (un)register session names. bool register_session (const blob_t &name_, class session_t *session_); @@ -79,10 +79,6 @@ namespace zmq ~session_t (); - // Remove any half processed messages. Flush unflushed messages. - // Call this function when engine disconnect to get rid of leftovers. - void clean_pipes (); - // Inherited socket options. These are visible to all session classes. options_t options; @@ -94,6 +90,10 @@ namespace zmq const blob_t &peer_identity_); void process_term (); + // Remove any half processed messages. Flush unflushed messages. + // Call this function when engine disconnect to get rid of leftovers. + void clean_pipes (); + // Call this function to move on with the delayed process_term. void proceed_with_term (); diff --git a/src/transient_session.cpp b/src/transient_session.cpp index 6711bc2..ff4b978 100644 --- a/src/transient_session.cpp +++ b/src/transient_session.cpp @@ -29,6 +29,10 @@ zmq::transient_session_t::~transient_session_t () { } +void zmq::transient_session_t::attached (const blob_t &peer_identity_) +{ +} + void zmq::transient_session_t::detached () { // There's no way to reestablish a transient session. Tear it down. diff --git a/src/transient_session.hpp b/src/transient_session.hpp index 2a7f3d5..d244982 100644 --- a/src/transient_session.hpp +++ b/src/transient_session.hpp @@ -38,7 +38,8 @@ namespace zmq private: - // Hook into session's disconnection mechanism. + // Handlers for events from session base class. + void attached (const blob_t &peer_identity_); void detached (); transient_session_t (const transient_session_t&); -- cgit v1.2.3