Class: Carnivore::UnixSocket::Util::Connection

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/carnivore-unixsocket/util/socket_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Connection



12
13
14
15
16
17
# File 'lib/carnivore-unixsocket/util/socket_connection.rb', line 12

def initialize(args={})
  @io = args[:io]
  @io.sync
  @srv = args[:server]
  async.consume! if args[:auto_consume]
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



10
11
12
# File 'lib/carnivore-unixsocket/util/socket_connection.rb', line 10

def io
  @io
end

#srvObject (readonly)

Returns the value of attribute srv.



10
11
12
# File 'lib/carnivore-unixsocket/util/socket_connection.rb', line 10

def srv
  @srv
end

Instance Method Details

#consume!Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/carnivore-unixsocket/util/socket_connection.rb', line 19

def consume!
  defer do
    loop do
      line = receive
      unless(line.empty?)
        srv.add_lines([line])
      end
    end
  end
end

#receiveObject



35
36
37
38
39
40
41
42
43
# File 'lib/carnivore-unixsocket/util/socket_connection.rb', line 35

def receive
  line = io.gets
  if(line)
    line.strip
  else
    io.close
    terminate
  end
end

#write_line(s) ⇒ Object



30
31
32
33
# File 'lib/carnivore-unixsocket/util/socket_connection.rb', line 30

def write_line(s)
  io.puts s
  io.flush
end