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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Server

Returns a new instance of Server.



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

def initialize(args={})
  @path = ::File.expand_path(args[:path])
  @notify_actor = args[:notify_actor]
  @supervisor = Celluloid::SupervisionGroup.run!
  @messages = []
end

Instance Attribute Details

#notify_actorObject (readonly)

Returns the value of attribute notify_actor.



12
13
14
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 12

def notify_actor
  @notify_actor
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 12

def path
  @path
end

#serverObject (readonly)

Returns the value of attribute server.



12
13
14
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 12

def server
  @server
end

#supervisorObject (readonly)

Returns the value of attribute supervisor.



12
13
14
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 12

def supervisor
  @supervisor
end

Instance Method Details

#add_lines(lines) ⇒ Object



46
47
48
49
50
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 46

def add_lines(lines)
  lines = [lines] unless lines.is_a?(Array)
  @messages += lines
  notify_actor.signal(:new_socket_lines)
end

#return_linesObject



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

def return_lines
  msgs = @messages.dup
  @messages.clear
  msgs
end

#setup_server!Object



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

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

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 21

def start
  srv_actor = current_actor
  defer do
    loop do
      setup_server!
      debug 'Waiting for new connection to server'
      connection = server.accept
      debug 'Received new connection to server. Setting up connection...'
      supervisor.supervise_as("con_#{connection.hash}", Connection,
        :io => connection, :server => srv_actor, :auto_consume => true
      )
      debug 'Connection setup complete and active'
    end
  end
end