Class: Tochka::UnixSocketChannel

Inherits:
Channel
  • Object
show all
Defined in:
lib/tochka/channel.rb

Constant Summary collapse

SOCKPATH =
"/var/run/captured.sock"

Instance Method Summary collapse

Constructor Details

#initialize(recv_handler, sock_path = SOCKPATH) ⇒ UnixSocketChannel

Returns a new instance of UnixSocketChannel.



18
19
20
21
22
# File 'lib/tochka/channel.rb', line 18

def initialize recv_handler, sock_path=SOCKPATH
  @recv_handler = recv_handler
  @sock_path = sock_path || SOCKPATH
  @th = nil
end

Instance Method Details

#startObject



24
25
26
27
28
29
30
31
32
# File 'lib/tochka/channel.rb', line 24

def start
  @th = Thread.new do
    Socket.unix_server_loop(@sock_path) do |sock, addr|
      data = sock.gets
      resp = @recv_handler.call(data)
      sock.write(resp+"\n")
    end
  end
end

#stopObject



34
35
36
# File 'lib/tochka/channel.rb', line 34

def stop
  @th.kill
end