Listen::TCP::Listener

Constants

DEFAULT_HOST

Attributes

host[R]
mode[R]
port[R]

Public Class Methods

new(target, mode, *args, &block) click to toggle source

Initializes a listener to broadcast or receive modifications over TCP

@param [String/Fixnum] target to listen on (hostname:port or port) @param [Symbol] mode (either :broadcaster or :recipient)

@param (see Listen::Listener#new)

# File lib/listen/tcp/listener.rb, line 18
def initialize(target, mode, *args, &block)
  self.mode = mode
  self.target = target

  super *args, &block
end

Public Instance Methods

block() click to toggle source

Hook to broadcast changes over TCP

# File lib/listen/tcp/listener.rb, line 43
def block
  if broadcaster?
    proc do |modified, added, removed|

      # Honour paused and stopped states
      next if @paused || @stopping

      # Broadcast changes as a hash
      #
      # @see Listen::Adapter::TCP#handle_message
      message = Message.new(
        modified: modified,
        added: added,
        removed: removed)

      registry[:broadcaster].async.broadcast(message.payload)

      # Invoke the original callback block
      @block.call(modified, added, removed) if @block
    end
  else
    super
  end
end
broadcaster?() click to toggle source
# File lib/listen/tcp/listener.rb, line 25
def broadcaster?
  @mode == :broadcaster
end
recipient?() click to toggle source
# File lib/listen/tcp/listener.rb, line 29
def recipient?
  @mode == :recipient
end
start() click to toggle source

Initializes and starts TCP broadcaster

# File lib/listen/tcp/listener.rb, line 34
def start
  super
  if broadcaster?
    supervisor.add(Broadcaster, as: :broadcaster, args: [host, port])
    registry[:broadcaster].start
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.