You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2017/11/30 21:42:42 UTC

[11/12] qpid-proton git commit: PROTON-1064: [ruby] allow multiple handlers to container

PROTON-1064: [ruby] allow multiple handlers to container


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

Branch: refs/heads/master
Commit: 06fbad394a48989e1028c684f374a4357854d97a
Parents: b883393
Author: Alan Conway <ac...@redhat.com>
Authored: Thu Nov 30 14:25:02 2017 -0500
Committer: Alan Conway <ac...@redhat.com>
Committed: Thu Nov 30 16:36:26 2017 -0500

----------------------------------------------------------------------
 .../bindings/ruby/lib/core/connection_driver.rb |   4 +-
 proton-c/bindings/ruby/lib/core/container.rb    |   8 +-
 .../bindings/ruby/lib/core/messaging_handler.rb |  15 ++-
 proton-c/bindings/ruby/lib/handler/adapter.rb   |   2 +-
 .../ruby/lib/handler/endpoint_state_handler.rb  | 118 -------------------
 .../ruby/lib/handler/flow_controller.rb         |  40 -------
 .../lib/handler/incoming_message_handler.rb     |  55 ---------
 .../lib/handler/outgoing_message_handler.rb     |  51 --------
 proton-c/bindings/ruby/lib/qpid_proton.rb       |   4 -
 proton-c/bindings/ruby/tests/test_container.rb  |  14 ++-
 proton-c/bindings/ruby/tests/test_tools.rb      |   2 -
 11 files changed, 27 insertions(+), 286 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/core/connection_driver.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/core/connection_driver.rb b/proton-c/bindings/ruby/lib/core/connection_driver.rb
index 1995f7d..969b558 100644
--- a/proton-c/bindings/ruby/lib/core/connection_driver.rb
+++ b/proton-c/bindings/ruby/lib/core/connection_driver.rb
@@ -168,7 +168,9 @@ module Qpid
       #   {#dispatch} and {#process}
       def initialize(io, handler)
         super(io)
-        @handler = handler
+        # Allow multiple handlers for backwards compatibility
+        a = Array(handler)
+        @handler = a.size > 1 ? MessagingHandlers.new(a) : handler
         @adapter = Handler::Adapter.try_convert(handler)
       end
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/core/container.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/core/container.rb b/proton-c/bindings/ruby/lib/core/container.rb
index df89d1a..fdc0247 100644
--- a/proton-c/bindings/ruby/lib/core/container.rb
+++ b/proton-c/bindings/ruby/lib/core/container.rb
@@ -106,11 +106,9 @@ module Qpid::Proton
     def initialize(handler = nil, id = nil)
       # Allow ID as sole argument
       (handler, id = nil, handler.to_str) if (id.nil? && handler.respond_to?(:to_str))
-      raise TypeError, "Expected MessagingHandler, got #{handler.class}" if
-        handler && !handler.is_a?(Qpid::Proton::Handler::MessagingHandler)
-
-      # TODO aconway 2017-11-08: allow multiple handlers, opts for backwards compat?
-      @handler = handler
+      # Allow multiple handlers for backwards compatibility
+      a = Array(handler)
+      @handler = a.size > 1 ? MessagingHandlers.new(a) : handler
       @id = ((id && id.to_s) || SecureRandom.uuid).freeze
 
       # Implementation note:

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/core/messaging_handler.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/core/messaging_handler.rb b/proton-c/bindings/ruby/lib/core/messaging_handler.rb
index b6b07c7..580a4ab 100644
--- a/proton-c/bindings/ruby/lib/core/messaging_handler.rb
+++ b/proton-c/bindings/ruby/lib/core/messaging_handler.rb
@@ -160,16 +160,15 @@ module Qpid::Proton
     # @param event [Event] the event, {Event#method} provides the original method name.
   end
 
-  # An array of {MessagingHandler}, events are dispatched to each in turn
+  # A {MessagingHandler} that delegates events to an array of handlers, in order.
   class MessagingHandlers < MessagingHandler
-    include Enumerable
+    # @param handlers [Array<MessagingHandler>] handler objects
+    def initialize(handlers) @handlers = handlers; end
 
-    # @param handlers an array of {MessagingHandler} objects
-    def initialize handlers; @handlers = handlers; end
-
-    def each(*args, &block) @handlers.each(*args, &block); end
-
-    def on_unhandled(event) each { |h| event.dispatch h }; end
+    # @return [Array<MessagingHandler>] array of handlers
+    attr_reader :handlers
 
+    # Dispatch events to each of {#handlers} in turn
+    def on_unhandled(event) @handlers.each { |h| event.dispatch h }; end
   end
 end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/handler/adapter.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/adapter.rb b/proton-c/bindings/ruby/lib/handler/adapter.rb
index 2efa2b7..6cf3831 100644
--- a/proton-c/bindings/ruby/lib/handler/adapter.rb
+++ b/proton-c/bindings/ruby/lib/handler/adapter.rb
@@ -28,7 +28,7 @@ module Qpid::Proton::Handler
 
     def initialize handler
       @handler = handler || MessagingHandler.new # Pick up default MH behavior
-      @opts = handler.respond_to?(:options) ? handler.options : {}
+      @opts = (handler.options if handler.respond_to?(:options)) || {}
       @opts[:prefetch] ||= 10
       @opts[:peer_close_is_error] = false unless @opts.include? :peer_close_is_error
       [:auto_accept, :auto_settle, :auto_open, :auto_close].each do |k|

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/handler/endpoint_state_handler.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/endpoint_state_handler.rb b/proton-c/bindings/ruby/lib/handler/endpoint_state_handler.rb
deleted file mode 100644
index 98b8d3a..0000000
--- a/proton-c/bindings/ruby/lib/handler/endpoint_state_handler.rb
+++ /dev/null
@@ -1,118 +0,0 @@
-#--
-# 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.
-#++
-
-# @private
-module Qpid::Proton::Handler
-
-  # Mixin to convert raw proton endpoint events to {#MessagingHandler} events
-  #
-  # A XXX_opening method will be called when the remote peer has requested
-  # an open that was not initiated locally. By default this will simply open
-  # locally, which then trigtgers the XXX_opened called.
-  #
-  # A XXX_opened method will be called when both local and remote peers have
-  # opened the link, session or connection. This can be used to confirm a
-  # locally initiated action for example.
-  #
-  # The same applies to close.
-  #
-  module EndpointStateHandler
-
-    def on_link_remote_close(event)
-      super
-      if !event.link.remote_condition.nil?
-        self.on_link_error(event)
-      elsif event.link.local_closed?
-        self.on_link_closed(event)
-      else
-        self.on_link_closing(event)
-      end
-      event.link.close
-    end
-
-    def on_session_remote_close(event)
-      super
-      if !event.session.remote_condition.nil?
-        self.on_session_error(event)
-      elsif event.session.local_closed?
-        self.on_session_closed(event)
-      else
-        self.on_session_closing(event)
-      end
-      event.session.close
-    end
-
-    def on_connection_remote_close(event)
-      super
-      if !event.connection.remote_condition.nil?
-        self.on_connection_error(event)
-      elsif event.connection.local_closed?
-        self.on_connection_closed(event)
-      else
-        self.on_connection_closing(event)
-      end
-      event.connection.close
-    end
-
-    def on_connection_local_open(event)
-      super
-      self.on_connection_opened(event) if event.connection.remote_active?
-    end
-
-    def on_connection_remote_open(event)
-      super
-      if event.connection.local_active?
-        self.on_connection_opened(event)
-      elsif event.connection.local_uninit?
-        self.on_connection_opening(event)
-        event.connection.open unless event.connection.local_active?
-      end
-    end
-
-    def on_session_local_open(event)
-      super
-      self.on_session_opened(event) if event.session.remote_active?
-    end
-
-    def on_session_remote_open(event)
-      super
-      if !(event.session.state & Qpid::Proton::Endpoint::LOCAL_ACTIVE).zero?
-        self.on_session_opened(event)
-      elsif event.session.local_uninit?
-        self.on_session_opening(event)
-        event.session.open
-      end
-    end
-
-    def on_link_local_open(event)
-      super
-      self.on_link_opened(event) if event.link.remote_active?
-    end
-
-    def on_link_remote_open(event)
-      super
-      if event.link.local_active?
-        self.on_link_opened(event)
-      elsif event.link.local_uninit?
-        self.on_link_opening(event)
-        event.link.open
-      end
-    end
-  end
-end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/handler/flow_controller.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/flow_controller.rb b/proton-c/bindings/ruby/lib/handler/flow_controller.rb
deleted file mode 100644
index 38d925f..0000000
--- a/proton-c/bindings/ruby/lib/handler/flow_controller.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-#--
-# 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.
-#++
-
-# @private
-module Qpid::Proton::Handler
-
-  # Mixin to establish automatic flow control for a prefetch window
-  # Uses {#@prefetch}
-  #
-  module FlowController
-
-    def on_link_local_open(event) topup(event); super; end
-    def on_link_remote_open(event) topup(event); super; end
-    def on_delivery(event) topup(event); super; end
-    def on_link_flow(event) topup(event); super; end
-
-    def add_credit(event)
-      r = event.receiver
-      if r && r.open? && (r.drained == 0) && @handler.prefetch && (@handler.prefetch > r.credit)
-        r.flow(@handler.prefetch - r.credit)
-      end
-    end
-  end
-end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/handler/incoming_message_handler.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/incoming_message_handler.rb b/proton-c/bindings/ruby/lib/handler/incoming_message_handler.rb
deleted file mode 100644
index 9f34d0d..0000000
--- a/proton-c/bindings/ruby/lib/handler/incoming_message_handler.rb
+++ /dev/null
@@ -1,55 +0,0 @@
-#--
-# 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.
-#++
-
-# @private
-module Qpid::Proton::Handler
-  private
-
-  # A utility for simpler and more intuitive handling of delivery events
-  # related to incoming messages.
-  #
-  # uses @auto_accept
-  #
-  module IncomingMessageHandler
-    def on_delivery(event)
-      super
-      delivery = event.delivery
-      return unless delivery.link.receiver?
-      if delivery.readable? && !delivery.partial?
-        m = Qpid::Proton::Message.new
-        m.receive(delivery)
-        event.message = m
-        if event.link.local_closed?
-          delivery.settle Qpid::Proton::Delivery::RELEASED if @auto_accept
-        else
-          begin
-            self.on_message(event)
-            delivery.settle Qpid::Proton::Delivery::ACCEPTED if @auto_accept
-          rescue Qpid::Proton::Reject
-            delivery.settle REJECTED
-          rescue Qpid::Proton::Release
-            delivery.settle MODIFIED
-          end
-        end
-      elsif delivery.updated? && delivery.settled?
-        self.on_settled(event)
-      end
-    end
-  end
-end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/handler/outgoing_message_handler.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/outgoing_message_handler.rb b/proton-c/bindings/ruby/lib/handler/outgoing_message_handler.rb
deleted file mode 100644
index cedcead..0000000
--- a/proton-c/bindings/ruby/lib/handler/outgoing_message_handler.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-#--
-# 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.
-#++
-
-module Qpid::Proton::Handler
-
-  # A utility for simpler and more intuitive handling of delivery events
-  # related to outgoing messages.
-  #
-  # Uses {#@auto_settle}
-  module OutgoingMessageHandler
-
-    def on_link_flow(event)
-      super
-      self.on_sendable(event) if event.link.sender? && event.link.credit > 0 &&
-                                 (event.link.state & Qpid::Proton::Endpoint::LOCAL_ACTIVE) &&
-                                 (event.link.state & Qpid::Proton::Endpoint::REMOTE_ACTIVE)
-    end
-
-    def on_delivery(event)
-      super
-      delivery = event.delivery
-      if delivery.link.sender? && delivery.updated?
-        if delivery.remote_accepted?
-          self.on_accepted(event)
-        elsif delivery.remote_rejected?
-          self.on_rejected(event)
-        elsif delivery.remote_released? || delivery.remote_modified?
-          self.on_released(event)
-        end
-        self.on_settled(event) if delivery.settled?
-        delivery.settle if @auto_settle
-      end
-    end
-  end
-end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/lib/qpid_proton.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton.rb b/proton-c/bindings/ruby/lib/qpid_proton.rb
index d3040a0..ab24dfd 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton.rb
@@ -82,10 +82,6 @@ require "messenger/tracker"
 require "messenger/messenger"
 
 # Handler classes
-require "handler/endpoint_state_handler"
-require "handler/incoming_message_handler"
-require "handler/outgoing_message_handler"
-require "handler/flow_controller"
 require "handler/adapter"
 
 # Core classes that depend on Handler

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/tests/test_container.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/tests/test_container.rb b/proton-c/bindings/ruby/tests/test_container.rb
index 173c421..519ca65 100644
--- a/proton-c/bindings/ruby/tests/test_container.rb
+++ b/proton-c/bindings/ruby/tests/test_container.rb
@@ -84,7 +84,19 @@ class ContainerTest < Minitest::Test
 
   class CloseOnOpenHandler < TestHandler
     def on_connection_opened(e) e.connection.close; end
-    def on_connection_closing(e) e.connection.close; end
+  end
+
+  def test_multi_handler
+    handler_class = Class.new(CloseOnOpenHandler) do
+      @@opened = 0
+      def self.opened; @@opened; end
+      def on_connection_opened(e) @@opened += 1; super; end
+    end
+    hs = 3.times.collect { handler_class.new }
+    c = TestContainer.new(hs)
+    c.connect(c.url)
+    c.run
+    assert_equal 6, handler_class.opened # Opened at each end * 3 handlers
   end
 
   def test_auto_stop_one

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/06fbad39/proton-c/bindings/ruby/tests/test_tools.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/tests/test_tools.rb b/proton-c/bindings/ruby/tests/test_tools.rb
index 120c488..eea0057 100644
--- a/proton-c/bindings/ruby/tests/test_tools.rb
+++ b/proton-c/bindings/ruby/tests/test_tools.rb
@@ -45,8 +45,6 @@ end
 
 # Handler that records some common events that are checked by tests
 class TestHandler < MessagingHandler
-  # TODO aconway 2017-10-28: make on_error stuff part of the default handler.
-
   attr_reader :errors, :connections, :sessions, :links, :messages
 
   # Pass optional extra handlers and options to the Container


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