PROTON-781: Refactored the Ruby Selectable class. It now more closely matches the Python version.
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/51e85ea5 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/51e85ea5 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/51e85ea5 Branch: refs/heads/PROTON-781-ruby-reactor-apis Commit: 51e85ea53a0437a623a287a4ecce38b87e190e82 Parents: 9454f50 Author: Darryl L. Pierce <mcpie...@gmail.com> Authored: Thu Jun 4 13:56:49 2015 -0400 Committer: Darryl L. Pierce <mcpie...@gmail.com> Committed: Thu Jun 18 09:27:19 2015 -0400 ---------------------------------------------------------------------- proton-c/bindings/ruby/lib/core/selectable.rb | 124 +++++++++++---------- 1 file changed, 68 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51e85ea5/proton-c/bindings/ruby/lib/core/selectable.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/core/selectable.rb b/proton-c/bindings/ruby/lib/core/selectable.rb index 8a5b223..0ae2efe 100644 --- a/proton-c/bindings/ruby/lib/core/selectable.rb +++ b/proton-c/bindings/ruby/lib/core/selectable.rb @@ -25,13 +25,11 @@ module Qpid::Proton # @private class Selectable + # @private + include Util::SwigHelper - def initialize(messenger, impl) # :nodoc: - @messenger = messenger - @impl = impl - @io = nil - @freed = false - end + # @private + PROTON_METHOD_PREFIX = "pn_selectable" # Returns the underlying file descriptor. # @@ -41,76 +39,90 @@ module Qpid::Proton Cproton.pn_selectable_get_fd(@impl) end - def to_io - @io ||= IO.new(fileno) - end + proton_reader :reading, :is_or_get => :is - # The number of bytes the selectable is capable of consuming. - # - #def capacity - # Cproton.pn_selectable_capacity(@impl) - #end + proton_reader :writing, :is_or_get => :is - # The number of bytes waiting to be written to the file descriptor. - # - def pending - Cproton.pn_selectable_pending(@impl) - end + proton_caller :readable - # 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 + proton_caller :writable - def readable - Cproton.pn_selectable_readable(@impl) - end + proton_caller :expired - def writable - Cproton.pn_selectable_writable(@impl) - end + proton_accessor :registered, :is_or_get => :is - def expired? - Cproton.pn_selectable_expired(@impl) - end + proton_accessor :terminal, :is_or_get => :is - def registered=(registered) - Cproton.pn_selectable_set_registered(@impl, registered) + proton_caller :terminate + + proton_caller :release + + # @private + def self.wrap(impl) + return nil if impl.nil? + + self.fetch_instance(impl, :pn_selectable_attachments) || Selectable.new(impl) end - def registered? - Cproton.pn_selectable_is_registered(@impl) + # @private + include Util::Wrapper + + # @private + def initialize(impl) + @impl = impl + self.class.store_instance(self, :pn_selectable_attachments) end - def terminal? - return true if @impl.nil? - Cproton.pn_selectable_is_terminal(@impl) + private + + DEFAULT = Object.new + + public + + def fileno(fd = DEFAULT) + if fd == DEFAULT + Cproton.pn_selectable_get_fd(@impl) + elsif fd.nil? + Cproton.pn_selectable_set_fd(@impl, Cproton::PN_INVALID_SOCKET) + else + Cproton.pn_selectable_set_fd(@impl, fd) + end end - def to_s - "fileno=#{self.fileno} registered=#{self.registered?} terminal=#{self.terminal?}" + def reading=(reading) + if reading.nil? + reading = false + elsif reading == "0" + reading = false + else + reading = true + end + Cproton.pn_selectable_set_reading(@impl, reading ? true : false) end - def free - return if @freed - @freed = true - @messenger.unregister_selectable(fileno) - @io.close unless @io.nil? - Cproton.pn_selectable_free(@impl) - @impl = nil + def writing=(writing) + if writing.nil? + writing = false + elsif writing == "0" + writing = false + else + writing = true + end + Cproton.pn_selectable_set_writing(@impl, writing ? true : false) end - def freed? # :nodoc: - @freed + def deadline + tstamp = Cproton.pn_selectable_get_deadline(@impl) + return nil if tstamp.nil? + mills_to_sec(tstamp) end - private + def deadline=(deadline) + Cproton.pn_selectable_set_deadline(sec_to_millis(deadline)) + end - def check_is_initialized - raise RuntimeError.new("selectable freed") if @impl.nil? + def to_io + @io ||= IO.new(fileno) end end --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org