Class: Listen::Adapter::TCP

Inherits:
Base
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/listen/adapter/tcp.rb

Overview

Adapter to receive file system modifications over TCP

Constant Summary collapse

OS_REGEXP =

match any

//
RECEIVE_WINDOW =

Number of bytes to receive at a time

1024

Instance Attribute Summary collapse

Attributes inherited from Base

#listener

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_configure, #_directories, #_log, #_notify_change, #initialize, usable?

Constructor Details

This class inherits a constructor from Listen::Adapter::Base

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



15
16
17
# File 'lib/listen/adapter/tcp.rb', line 15

def buffer
  @buffer
end

#socketObject (readonly)

Returns the value of attribute socket.



15
16
17
# File 'lib/listen/adapter/tcp.rb', line 15

def socket
  @socket
end

Class Method Details

.local_fs?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/listen/adapter/tcp.rb', line 73

def self.local_fs?
  false
end

Instance Method Details

#finalizeObject

Cleans up buffer and socket



38
39
40
41
42
43
44
# File 'lib/listen/adapter/tcp.rb', line 38

def finalize
  @buffer = nil
  return unless @socket

  @socket.close
  @socket = nil
end

#handle_data(data) ⇒ Object

Buffers incoming data and handles messages accordingly



57
58
59
60
61
62
63
64
65
# File 'lib/listen/adapter/tcp.rb', line 57

def handle_data(data)
  @buffer << data
  while (message = Listen::TCP::Message.from_buffer(@buffer))
    handle_message(message)
  end
rescue
  _log :error, "TCP.handle_data crashed: #{$!}:#{$@.join("\n")}"
  raise
end

#handle_message(message) ⇒ Object

Handles incoming message by notifying of path changes



68
69
70
71
# File 'lib/listen/adapter/tcp.rb', line 68

def handle_message(message)
  type, modification, path, _ = message.object
  _notify_change(type.to_sym, path, change: modification.to_sym)
end

#runObject

Continuously receive and asynchronously handle data



50
51
52
53
54
# File 'lib/listen/adapter/tcp.rb', line 50

def run
  while (data = @socket.recv(RECEIVE_WINDOW))
    async.handle_data(data)
  end
end

#startObject

Initializes and starts a Celluloid::IO-powered TCP-recipient



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/listen/adapter/tcp.rb', line 18

def start
  attempts = 3
  @socket = TCPSocket.new(listener.host, listener.port)
  @buffer = ''
  async.run
rescue Celluloid::Task::TerminatedError
  _log :debug, "TCP adapter was terminated: #{$!.inspect}"
rescue Errno::ECONNREFUSED
  sleep 1
  attempts -= 1
  _log :warn, "TCP.start: #{$!.inspect}"
  retry if retries > 0
  _log :error, "TCP.start: #{$!.inspect}:#{$@.join("\n")}"
  raise
rescue
  _log :error, "TCP.start: #{$!.inspect}:#{$@.join("\n")}"
  raise
end