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.



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

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.



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

def server
  @server
end

#socket_pathObject (readonly)

Returns the value of attribute socket_path.



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

def socket_path
  @socket_path
end

Instance Method Details

#close_socketObject



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

def close_socket
  @server.close if @server
  unlink_socket_file
end

#command(cmd, *args) ⇒ Object



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

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

#handle_connection(socket) ⇒ Object



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

def handle_connection(socket)
  text = socket.read

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

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

rescue Errno::EPIPE
  # client timeouted
  # do nothing

ensure
  socket.close
end

#runObject



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

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


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

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