Class: Carnivore::UnixSocket::Util::Server

Inherits:
Object
  • Object
show all
Includes:
Carnivore::Utils::Logging, Zoidberg::SoftShell, Zoidberg::Supervise
Defined in:
lib/carnivore-unixsocket/util/socket_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ self

Create a new server

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :path (String)

    socket path



31
32
33
34
35
36
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 31

def initialize(args={})
  @path = ::File.expand_path(args[:path])
  @source = args[:source]
  @connections = []
  @waker, @notifier = IO.pipe
end

Instance Attribute Details

#connectionsArray<IO> (readonly)

Returns:

  • (Array<IO>)


24
25
26
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 24

def connections
  @connections
end

#notifierIO (readonly)

Returns:

  • (IO)


22
23
24
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 22

def notifier
  @notifier
end

#pathString (readonly)

Returns socket path.

Returns:

  • (String)

    socket path



14
15
16
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 14

def path
  @path
end

#serverUNIXServer (readonly)

Returns:

  • (UNIXServer)


16
17
18
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 16

def server
  @server
end

#sourceSource (readonly)

Returns:



18
19
20
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 18

def source
  @source
end

#wakerIO (readonly)

Returns:

  • (IO)


20
21
22
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 20

def waker
  @waker
end

Instance Method Details

#add_connection(con) ⇒ NilClass

Add a new connection

Parameters:

  • con (UNIXSocket)

Returns:

  • (NilClass)


42
43
44
45
46
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 42

def add_connection(con)
  @connections.push(con)
  notifier.write '-'
  nil
end

#remove_connection(con) ⇒ NilClass

Remove a connection

Parameters:

  • con (UNIXSocket)

Returns:

  • (NilClass)


52
53
54
55
56
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 52

def remove_connection(con)
  @connections.delete(con)
  notifier.write '-'
  nil
end

#setup_server!UNIXServer

Setup the path for the server and create new server

Returns:

  • (UNIXServer)


90
91
92
93
94
95
96
97
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 90

def setup_server!
  unless(@server)
    if(::File.exists?(path))
      ::File.delete(path)
    end
    @server = UNIXServer.new(path)
  end
end

#start_collector!Object

Start the message collector loop



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 70

def start_collector!
  loop do
    IO.select(current_self.connections + [waker]).flatten.compact.each do |sock|
      if(sock == waker)
        sock.read
      else
        line = sock.gets
        if(line)
          source.signal(:message, line.strip)
        else
          sock.close
        end
      end
    end
  end
end

#start_server!Object

Start the server listener loop



59
60
61
62
63
64
65
66
67
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 59

def start_server!
  setup_server!
  loop do
    debug 'Waiting for new connection to socket'
    connection = server.accept
    debug 'Received new connection to socket, loading in...'
    add_connection(connection)
  end
end