Class: Listen::TCP::Listener
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Attributes inherited from Listener
#changes, #directories, #options, #paused, #registry, #stopping, #supervisor, #thread
Instance Method Summary collapse
- #_init_options(options = {}) ⇒ Object private
-
#block ⇒ Object
Hook to broadcast changes over TCP.
- #broadcaster? ⇒ Boolean
-
#initialize(target, mode, *args, &block) ⇒ Listener
constructor
Initializes a listener to broadcast or receive modifications over TCP.
- #recipient? ⇒ Boolean
-
#start ⇒ Object
Initializes and starts TCP broadcaster.
-
#target=(target) ⇒ Object
private
Sets listener target.
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
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/listen/tcp/listener.rb', line 10 def host @host end |
#mode ⇒ Object
Returns the value of attribute mode.
10 11 12 |
# File 'lib/listen/tcp/listener.rb', line 10 def mode @mode end |
#port ⇒ Object (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 ( = {}) = super() [:force_tcp] = true if recipient? end |
#block ⇒ Object
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.new(modified: modified, added: added, removed: removed) registry[:broadcaster].async.broadcast(.payload) # Invoke the original callback block @block.call(modified, added, removed) if @block } else super end end |
#broadcaster? ⇒ Boolean
26 27 28 |
# File 'lib/listen/tcp/listener.rb', line 26 def broadcaster? @mode == :broadcaster end |
#recipient? ⇒ Boolean
30 31 32 |
# File 'lib/listen/tcp/listener.rb', line 30 def recipient? @mode == :recipient end |
#start ⇒ Object
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
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 |