Class: Eye::Server

Inherits:
Object
  • 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



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

def close_socket
  @server.close if @server
  unlink_socket_file
end

#command(cmd, *args) ⇒ Object



36
37
38
# File 'lib/eye/server.rb', line 36

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
# File 'lib/eye/server.rb', line 23

def handle_connection(socket)
  command, *args = socket.readline.strip.split('|')
  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


40
41
42
43
# File 'lib/eye/server.rb', line 40

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