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

RECEIVE_WINDOW =

Number of bytes to receive at a time

1024

Constants inherited from Base

Base::DEFAULT_LATENCY

Instance Attribute Summary collapse

Attributes inherited from Base

#listener

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_directories_path, #_latency, #_notify_change, #initialize

Constructor Details

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



12
13
14
# File 'lib/listen/adapter/tcp.rb', line 12

def buffer
  @buffer
end

#socketObject (readonly)

Returns the value of attribute socket.



12
13
14
# File 'lib/listen/adapter/tcp.rb', line 12

def socket
  @socket
end

Class Method Details

.usable?Boolean

Returns:

  • (Boolean)


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

def self.usable?
  true
end

Instance Method Details

#finalizeObject

Cleans up buffer and socket



26
27
28
29
30
31
32
# File 'lib/listen/adapter/tcp.rb', line 26

def finalize
  @buffer = nil
  if @socket
    @socket.close
    @socket = nil
  end
end

#handle_data(data) ⇒ Object

Buffers incoming data and handles messages accordingly



45
46
47
48
49
50
# File 'lib/listen/adapter/tcp.rb', line 45

def handle_data(data)
  @buffer << data
  while message = Listen::TCP::Message.from_buffer(@buffer)
    handle_message(message)
  end
end

#handle_message(message) ⇒ Object

Handles incoming message by notifying of path changes



53
54
55
56
57
58
59
# File 'lib/listen/adapter/tcp.rb', line 53

def handle_message(message)
  message.object.each do |change, paths|
    paths.each do |path|
      _notify_change(path, change: change.to_sym)
    end
  end
end

#runObject

Continuously receive and asynchronously handle data



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

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

#startObject

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



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

def start
  @socket = TCPSocket.new(listener.host, listener.port)
  @buffer = String.new
  run
end