You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by mc...@apache.org on 2014/04/21 20:41:54 UTC

svn commit: r1588961 - in /qpid/proton/trunk/proton-c/bindings/ruby/lib: qpid_proton.rb qpid_proton/messenger.rb qpid_proton/selectable.rb

Author: mcpierce
Date: Mon Apr 21 18:41:54 2014
New Revision: 1588961

URL: http://svn.apache.org/r1588961
Log:
PROTON-531: Created the Selectable class for Ruby.

The class encapsulates the underlying file descriptors used by Messenger
when in passive mode.

Modified the Messenger class to manage instances of Selectable relating
to itself.

Added:
    qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/selectable.rb
Modified:
    qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton.rb
    qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb

Modified: qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton.rb
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton.rb?rev=1588961&r1=1588960&r2=1588961&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton.rb (original)
+++ qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton.rb Mon Apr 21 18:41:54 2014
@@ -33,5 +33,5 @@ require "qpid_proton/message"
 require "qpid_proton/subscription"
 require "qpid_proton/tracker_status"
 require "qpid_proton/tracker"
+require "qpid_proton/selectable"
 require "qpid_proton/messenger"
-

Modified: qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb?rev=1588961&r1=1588960&r2=1588961&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb (original)
+++ qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb Mon Apr 21 18:41:54 2014
@@ -70,6 +70,7 @@ module Qpid
       #
       def initialize(name = nil)
         @impl = Cproton.pn_messenger(name)
+        @selectables = {}
         ObjectSpace.define_finalizer(self, self.class.finalize!(@impl))
       end
 
@@ -143,10 +144,23 @@ module Qpid
       end
 
       # Turns passive mode on or off.
+      #
+      # When set to passive mode, Messenger will not attempt to perform I/O
+      # operations internally. In this mode it is necesssary to use the
+      # Selectable type to drive any I/O needed to perform requestioned
+      # actions.
+      #
+      # In this mode Messenger will never block.
+      #
       def passive=(mode)
         Cproton.pn_messenger_set_passive(@impl, mode)
       end
 
+      def deadline
+        tstamp = Cproton.pn_messenger_deadline(@impl)
+        return tstamp / 1000.0 unless tstamp.nil?
+      end
+
       # Reports whether an error occurred.
       #
       def error?
@@ -468,6 +482,22 @@ module Qpid
         check_for_error(Cproton.pn_messenger_rewrite(@impl, pattern, address))
       end
 
+      def selectable
+        impl = Cproton.pn_messenger_selectable(@impl)
+
+        # if we don't have any selectables, then return
+        return nil if impl.nil?
+
+        fd = Cproton.pn_selectable_fd(impl)
+
+        selectable = @selectables[fd]
+        if selectable.nil?
+          selectable = Selectable.new(self, impl)
+          @selectables[fd] = selectable
+        end
+        return selectable
+      end
+
       # Returns a +Tracker+ for the message most recently sent via the put
       # method.
       #
@@ -612,6 +642,11 @@ module Qpid
         Cproton.pn_messenger_get_outgoing_window(@impl)
       end
 
+      # Unregisters a selectable object.
+      def unregister_selectable(fileno) # :nodoc:
+        @selectables.delete(fileno)
+      end
+
       private
 
       def valid_tracker?(tracker)

Added: qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/selectable.rb
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/selectable.rb?rev=1588961&view=auto
==============================================================================
--- qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/selectable.rb (added)
+++ qpid/proton/trunk/proton-c/bindings/ruby/lib/qpid_proton/selectable.rb Mon Apr 21 18:41:54 2014
@@ -0,0 +1,128 @@
+#
+# 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
+
+  module Proton
+
+    # Selectable enables accessing the underlying file descriptors
+    # for Messenger.
+    class Selectable
+
+      include Qpid::Proton::Filters
+
+      call_before :check_is_initialized,
+      :fileno, :capacity, :pending, :deadline,
+      :readable, :writable, :expired,
+      :registered=, :registered?
+
+      def initialize(messenger, impl) # :nodoc:
+        @messenger = messenger
+        @impl = impl
+        @io = nil
+        @freed = false
+      end
+
+      # Returns the underlying file descriptor.
+      #
+      # This can be used in conjunction with the IO class.
+      #
+      def fileno
+        Cproton.pn_selectable_fd(@impl)
+      end
+
+      def to_io
+        @io ||= IO.new(fileno)
+      end
+
+      # The number of bytes the selectable is capable of consuming.
+      #
+      def capacity
+        Cproton.pn_selectable_capacity(@impl)
+      end
+
+      # The number of bytes waiting to be written to the file descriptor.
+      #
+      def pending
+        Cproton.pn_selectable_pending(@impl)
+      end
+
+      # The future expiry time at which control will be returned to the
+      # selectable.
+      #
+      def deadline
+        tstamp = Cproton.pn_selectable_deadline(@impl)
+        tstamp.nil? ? nil : tstamp / 1000
+      end
+
+      def readable
+        Cproton.pn_selectable_readable(@impl)
+      end
+
+      def writable
+        Cproton.pn_selectable_writable(@impl)
+      end
+
+      def expired?
+        Cproton.pn_selectable_expired(@impl)
+      end
+
+      def registered=(registered)
+        Cproton.pn_selectable_set_registered(@impl, registered)
+      end
+
+      def registered?
+        Cproton.pn_selectable_is_registered(@impl)
+      end
+
+      def terminal?
+        return true if @impl.nil?
+        Cproton.pn_selectable_is_terminal(@impl)
+      end
+
+      def to_s
+        "fileno=#{self.fileno} registered=#{self.registered?} terminal=#{self.terminal?}"
+      end
+
+      def free
+        return if @freed
+        @freed = true
+        @messenger.unregister_selectable(fileno)
+        @io.close unless @io.nil?
+        Cproton.pn_selectable_free(@impl)
+        @impl = nil
+      end
+
+      def freed? # :nodoc:
+        @freed
+      end
+
+      def killable?; !@impl.nil?; end
+
+      private
+
+      def check_is_initialized
+        raise RuntimeError.new("selectable freed") if @impl.nil?
+      end
+
+    end
+
+  end
+
+end



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