Sha256: 703dc230d35f65fff928cbdf65df8be5d1bb0321098255a3b022426639fcae55

Contents?: true

Size: 934 Bytes

Versions: 4

Compression:

Stored size: 934 Bytes

Contents

# The demultiplexer in the reactor pattern is implemented in this class.
# The object needs to be initiated with a queue and a dispatcher.
# Demultiplexer#start defines an event loop which pops items from the queue
# and passes it on to the dispatcher for dispatching to the appropriate message handler.
# The demultiplexer can be stopped by calling the Demultiplexer#stop method.
module DispatchRider
  class Demultiplexer
    attr_reader :queue, :dispatcher

    def initialize(queue, dispatcher)
      @queue = queue
      @dispatcher = dispatcher
      @continue = true
    end

    def start
      catch(:done) do
        loop do
          throw :done unless @continue
          queue.pop do |message|
            dispatch_message(message)
          end
        end
      end
      self
    end

    def stop
      @continue = false
    end

    def dispatch_message(message)
      dispatcher.dispatch(message)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dispatch-rider-0.0.7 lib/dispatch-rider/demultiplexer.rb
dispatch-rider-0.0.5 lib/dispatch-rider/demultiplexer.rb
dispatch-rider-0.0.4 lib/dispatch-rider/demultiplexer.rb
dispatch-rider-0.0.3 lib/dispatch-rider/demultiplexer.rb