Class: Listen::TCP::Listener

Inherits:
Listener show all
Defined in:
lib/listen/tcp/listener.rb

Constant Summary collapse

DEFAULT_HOST =
'localhost'

Constants inherited from Listener

Listener::RELATIVE_PATHS_WITH_MULTIPLE_DIRECTORIES_WARNING_MESSAGE

Instance Attribute Summary collapse

Attributes inherited from Listener

#changes, #directories, #options, #paused, #registry, #stopping, #supervisor, #thread

Instance Method Summary collapse

Methods inherited from Listener

#_init_actors, #_init_debug, #_pop_changes, #_wait_for_changes, #ignore, #ignore!, #listen?, #only, #pause, #paused?, #stop, #unpause

Constructor Details

#initialize(target, mode, *args, &block) ⇒ Listener

Initializes a listener to broadcast or receive modifications over TCP

Parameters:

  • target (String/Fixnum)

    to listen on (hostname:port or port)

  • mode (Symbol)

    (either :broadcaster or :recipient)



19
20
21
22
23
24
# File 'lib/listen/tcp/listener.rb', line 19

def initialize(target, mode, *args, &block)
  self.mode = mode
  self.target = target

  super *args, &block
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/listen/tcp/listener.rb', line 10

def host
  @host
end

#modeObject

Returns the value of attribute mode.



10
11
12
# File 'lib/listen/tcp/listener.rb', line 10

def mode
  @mode
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/listen/tcp/listener.rb', line 10

def port
  @port
end

Instance Method Details

#_init_options(options = {}) ⇒ Object (private)



65
66
67
68
69
# File 'lib/listen/tcp/listener.rb', line 65

def _init_options(options = {})
  options = super(options)
  options[:force_tcp] = true if recipient?
  options
end

#blockObject

Hook to broadcast changes over TCP



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/listen/tcp/listener.rb', line 44

def block
  if broadcaster?
    Proc.new { |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
    }
  else
    super
  end
end

#broadcaster?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/listen/tcp/listener.rb', line 26

def broadcaster?
  @mode == :broadcaster
end

#recipient?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/listen/tcp/listener.rb', line 30

def recipient?
  @mode == :recipient
end

#startObject

Initializes and starts TCP broadcaster



35
36
37
38
39
40
41
# File 'lib/listen/tcp/listener.rb', line 35

def start
  super
  if broadcaster?
    supervisor.add(Broadcaster, as: :broadcaster, args: [host, port])
    registry[:broadcaster].start
  end
end

#target=(target) ⇒ Object (private)

Sets listener target

Parameters:

  • target (String/Fixnum)

    to listen on (hostname:port or port)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/listen/tcp/listener.rb', line 86

def target=(target)
  unless target
    raise ArgumentError, 'TCP::Listener requires target to be given'
  end

  @host = DEFAULT_HOST if recipient?

  if target.is_a? Fixnum
    @port = target
  else
    @host, @port = target.split(':')
    @port = @port.to_i
  end
end