PROTON-1537: [ruby] Replace head/next with each Connection provides each_session, Connection and Session provide each_link, each_sender, each_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/1e256cf7 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/1e256cf7 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/1e256cf7 Branch: refs/heads/go1 Commit: 1e256cf72c283a6998c36baa98e069fa90275706 Parents: a4c50ca Author: Alan Conway <[email protected]> Authored: Thu Dec 14 11:35:32 2017 -0500 Committer: Alan Conway <[email protected]> Committed: Fri Dec 15 09:59:47 2017 -0500 ---------------------------------------------------------------------- examples/ruby/broker.rb | 6 +- proton-c/bindings/ruby/lib/core/connection.rb | 107 ++++++++----------- proton-c/bindings/ruby/lib/core/link.rb | 15 +-- proton-c/bindings/ruby/lib/core/session.rb | 34 ++++-- proton-c/bindings/ruby/lib/util/deprecation.rb | 9 +- .../ruby/tests/test_connection_driver.rb | 41 +++++++ 6 files changed, 121 insertions(+), 91 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e256cf7/examples/ruby/broker.rb ---------------------------------------------------------------------- diff --git a/examples/ruby/broker.rb b/examples/ruby/broker.rb index aad32a9..9595e2f 100644 --- a/examples/ruby/broker.rb +++ b/examples/ruby/broker.rb @@ -127,11 +127,7 @@ class Broker < Qpid::Proton::MessagingHandler end def remove_stale_consumers(connection) - l = connection.link_head(Qpid::Proton::Endpoint::REMOTE_ACTIVE) - while !l.nil? - self.unsubscribe(l) if l.sender? - l = l.next(Qpid::Proton::Endpoint::REMOTE_ACTIVE) - end + connection.each_sender { |s| unsubscribe(s) } end def on_sendable(sender) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e256cf7/proton-c/bindings/ruby/lib/core/connection.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/core/connection.rb b/proton-c/bindings/ruby/lib/core/connection.rb index 3ec7c26..2b97f41 100644 --- a/proton-c/bindings/ruby/lib/core/connection.rb +++ b/proton-c/bindings/ruby/lib/core/connection.rb @@ -197,78 +197,63 @@ module Qpid::Proton # @option opts (see Session#open_receiver) def open_receiver(opts=nil) default_session.open_receiver(opts) end - # Returns the first session from the connection that matches the specified - # state mask. - # - # Examines the state of each session owned by the connection, and returns - # the first session that matches the given state mask. If the state mask - # contains *both* local and remote flags, then an exact match against - # those flags is performed. If the state mask contains only local *or* - # remote flags, then a match occurs if a*any* of the local or remote flags - # are set, respectively. - # - # @param mask [Integer] The state mask to be matched. - # - # @return [Session] The first matching session, or nil if none matched. - # - # @see Endpoint#LOCAL_UNINIT - # @see Endpoint#LOCAL_ACTIVE - # @see Endpoint#LOCAL_CLOSED - # @see Endpoint#REMOTE_UNINIT - # @see Endpoint#REMOTE_ACTIVE - # @see Endpoint#REMOTE_CLOSED - # - def session_head(mask) - Session.wrap(Cproton.pn_session_header(@impl, mask)) + # @deprecated use {#each_session} + def session_head(mask) + deprecated __method__, "#each_session" + Session.wrap(Cproton.pn_session_head(@impl, mask)) end - # Returns the first link that matches the given state mask. - # - # Examines the state of each link owned by the connection and returns the - # first that matches the given state mask. If the state mask contains - # *both* local and remote flags, then an exact match against those flags - # is performed. If the state mask contains *only* local or remote flags, - # then a match occurs if *any* of the local ore remote flags are set, - # respectively. - # - # @param mask [Integer] The state mask to be matched. - # - # @return [Link] The first matching link, or nil if none matched. - # - # @see Endpoint#LOCAL_UNINIT - # @see Endpoint#LOCAL_ACTIVE - # @see Endpoint#LOCAL_CLOSED - # @see Endpoint#REMOTE_UNINIT - # @see Endpoint#REMOTE_ACTIVE - # @see Endpoint#REMOTE_CLOSED - # + # Get the sessions on this connection. + # @overload each_session + # @yieldparam s [Session] pass each session to block + # @overload each_session + # @return [Enumerator] enumerator over sessions + def each_session(&block) + return enum_for(:each_session) unless block_given? + s = Cproton.pn_session_head(@impl, 0); + while s + yield Session.wrap(s) + s = Cproton.pn_session_next(s, 0) + end + self + end + + # @deprecated use {#each_link} def link_head(mask) + deprecated __method__, "#each_link" Link.wrap(Cproton.pn_link_head(@impl, mask)) end - # Extracts the first delivery on the connection that has pending - # operations. - # - # A readable delivery indicates message data is waiting to be read. A - # A writable delivery indcates that message data may be sent. An updated - # delivery indicates that the delivery's disposition has changed. - # - # A delivery will never be *both* readable and writable, but it may be - # both readable or writable and updated. - # - # @return [Delivery] The delivery, or nil if none are available. - # - # @see Delivery#next - # + # Get the links on this connection. + # @overload each_link + # @yieldparam l [Link] pass each link to block + # @overload each_link + # @return [Enumerator] enumerator over links + def each_link + return enum_for(:each_link) unless block_given? + l = Cproton.pn_link_head(@impl, 0); + while l + yield Link.wrap(l) + l = Cproton.pn_link_next(l, 0) + end + self + end + + # Get the {Sender} links - see {#each_link} + def each_sender() each_link.select { |l| l.sender? }; end + + # Get the {Receiver} links - see {#each_link} + def each_receiver() each_link.select { |l| l.receiver? }; end + + # @deprecated use {#MessagingHandler} to handle work def work_head + deprecated __method__ Delivery.wrap(Cproton.pn_work_head(@impl)) end - # Returns the code for a connection error. - # - # @return [Integer] The error code. - # + # @deprecated use {#condition} def error + deprecated __method__, "#condition" Cproton.pn_error_code(Cproton.pn_connection_error(@impl)) end http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e256cf7/proton-c/bindings/ruby/lib/core/link.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/core/link.rb b/proton-c/bindings/ruby/lib/core/link.rb index 88c0e52..2e656c2 100644 --- a/proton-c/bindings/ruby/lib/core/link.rb +++ b/proton-c/bindings/ruby/lib/core/link.rb @@ -48,12 +48,7 @@ module Qpid::Proton # proton_caller :state - # @!method open - # - # Opens the link. Once this operation has completed, the state flag will be - # set. - # - # @see Endpoint::LOCAL_ACTIVE + # @deprecated use {Session#open_sender} and {#Session#open_receiver} proton_caller :open # Close the local end of the link. The remote end may or may not be closed. @@ -226,13 +221,9 @@ module Qpid::Proton Cproton.pn_link_error(@impl) end - # Returns the next link that matches the given state mask. - # - # @param state_mask [Integer] The state mask. - # - # @return [Sender, Receiver] The next link. - # + # @deprecated use {Session#each_link, Connection#each_link} def next(state_mask) + deprecated __method__, "Session#each_link, Connection#each_link" return Link.wrap(Cproton.pn_link_next(@impl, state_mask)) end http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e256cf7/proton-c/bindings/ruby/lib/core/session.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/core/session.rb b/proton-c/bindings/ruby/lib/core/session.rb index 14f1a67..78e78a2 100644 --- a/proton-c/bindings/ruby/lib/core/session.rb +++ b/proton-c/bindings/ruby/lib/core/session.rb @@ -90,17 +90,9 @@ module Qpid::Proton Cproton.pn_session_close(@impl) end - # Retrieves the next session from a given connection that matches the - # specified state mask. - # - # When uses with Connection#session_head an application can access all of - # the session son the connection that match the given state. - # - # @param state_mask [Integer] The state mask to match. - # - # @return [Session, nil] The next session if one matches, or nil. - # + # @deprecated use {Connection#each_session} def next(state_mask) + deprecated __method__, "Connection#each_session" Session.wrap(Cproton.pn_session_next(@impl, state_mask)) end @@ -150,6 +142,28 @@ module Qpid::Proton return sender end + # Get the links on this Session. + # @overload each_link + # @yieldparam l [Link] pass each link to block + # @overload each_link + # @return [Enumerator] enumerator over links + def each_link + return enum_for(:each_link) unless block_given? + l = Cproton.pn_link_head(Cproton.pn_session_connection(@impl), 0); + while l + link = Link.wrap(l) + yield link if link.session == self + l = Cproton.pn_link_next(l, 0) + end + self + end + + # Get the {Sender} links - see {#each_link} + def each_sender() each_link.select { |l| l.sender? }; end + + # Get the {Receiver} links - see {#each_link} + def each_receiver() each_link.select { |l| l.receiver? }; end + private def _local_condition http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e256cf7/proton-c/bindings/ruby/lib/util/deprecation.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/util/deprecation.rb b/proton-c/bindings/ruby/lib/util/deprecation.rb index 2e83783..8e96cd6 100644 --- a/proton-c/bindings/ruby/lib/util/deprecation.rb +++ b/proton-c/bindings/ruby/lib/util/deprecation.rb @@ -18,9 +18,12 @@ module Qpid::Proton::Util # @private module Deprecation - def self.deprecated(old, new=nil, skip=2) + MATCH_DIR = /#{File.dirname(File.dirname(__FILE__))}/ + + def self.deprecated(old, new=nil) replace = new ? "use `#{new}`" : "internal use only" - warn "[DEPRECATION] `#{old}` is deprecated, #{replace}. Called from\n #{caller(skip).first}" + line = caller.find { |l| not MATCH_DIR.match(l) } + warn "[DEPRECATION] `#{old}` is deprecated, #{replace}. Called from #{line}" end def deprecated(*arg) Deprecation.deprecated(*arg); end @@ -29,7 +32,7 @@ module Qpid::Proton::Util def deprecated_alias(bad, good) bad, good = bad.to_sym, good.to_sym define_method(bad) do |*args, &block| - self.deprecated bad, good, 3 + self.deprecated bad, good self.__send__(good, *args, &block) end end http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e256cf7/proton-c/bindings/ruby/tests/test_connection_driver.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/tests/test_connection_driver.rb b/proton-c/bindings/ruby/tests/test_connection_driver.rb index 3386e49..d8cce2a 100644 --- a/proton-c/bindings/ruby/tests/test_connection_driver.rb +++ b/proton-c/bindings/ruby/tests/test_connection_driver.rb @@ -86,4 +86,45 @@ class RawDriverTest < Minitest::Test d.run(start + secs*2) # After 2x timeout, connections should close assert_equal [[:on_transport_error, :on_transport_tail_closed, :on_transport_head_closed, :on_transport_closed], [:on_connection_remote_close, :on_transport_tail_closed, :on_transport_head_closed, :on_transport_closed]], d.collect { |x| x.handler.calls } end + + # Test each_session/each_link methods both with a block and returning Enumerator + def test_enumerators + connection = Connection.new() + (3.times.collect { connection.open_session }).each { |s| + s.open_sender; s.open_receiver + } + + assert_equal 3, connection.each_session.to_a.size + assert_equal 6, connection.each_link.to_a.size + + # Build Session => Set<Links> map using connection link enumerator + map1 = {} + connection.each_link { |l| map1[l.session] ||= Set.new; map1[l.session] << l } + assert_equal 3, map1.size + map1.each do |session,links| + assert_equal 2, links.size + links.each { |l| assert_equal session, l.session } + end + + # Build Session => Set<Links> map using connection and session blocks + map2 = {} + connection.each_session do |session| + map2[session] = Set.new + session.each_link { |l| map2[session] << l } + end + assert_equal map1, map2 + + # Build Session => Set<Links> map using connection session and session enumerators + map3 = Hash[connection.each_session.collect { |s| [s, Set.new(s.each_link)] }] + assert_equal map1, map3 + + assert_equal [true, true, true], connection.each_sender.collect { |l| l.is_a? Sender } + assert_equal [true, true, true], connection.each_receiver.collect { |l| l.is_a? Receiver } + connection.each_session { |session| + assert_equal [true], session.each_sender.collect { |l| l.is_a? Sender } + assert_equal [true], session.each_receiver.collect { |l| l.is_a? Receiver } + } + + + end end --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
