Class: Eye::Server

Inherits:
Object show all
Includes:
Celluloid::IO
Defined in:
lib/eye/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
16
17
18
# File 'lib/eye/server.rb', line 10

def initialize(socket_path)
  @socket_path = socket_path
  @server = begin
    UNIXServer.open(socket_path)
  rescue Errno::EADDRINUSE
    unlink_socket_file
    UNIXServer.open(socket_path)
  end
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/eye/server.rb', line 8

def server
  @server
end

#socket_pathObject (readonly)

Returns the value of attribute socket_path.



8
9
10
# File 'lib/eye/server.rb', line 8

def socket_path
  @socket_path
end

Instance Method Details

#close_socketObject



56
57
58
59
# File 'lib/eye/server.rb', line 56

def close_socket
  @server.close if @server
  unlink_socket_file
end

#command(cmd, *args) ⇒ Object



45
46
47
# File 'lib/eye/server.rb', line 45

def command(cmd, *args)
  Eye::Control.command(cmd, *args)
end

#handle_connection(socket) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/eye/server.rb', line 24

def handle_connection(socket)
  text = socket.read

  begin
    cmd, *args = Marshal.load(text)
  rescue => ex
    error "Failed to read from socket: #{ex.message}"
    return
  end

  response = command(cmd, *args)
  socket.write(Marshal.dump(response))

rescue Errno::EPIPE
  # client timeouted
  # do nothing

ensure
  socket.close
end

#runObject



20
21
22
# File 'lib/eye/server.rb', line 20

def run
  loop { async.handle_connection @server.accept }
end


49
50
51
52
# File 'lib/eye/server.rb', line 49

def unlink_socket_file
  File.delete(@socket_path) if @socket_path
rescue
end