Class: Eye::Server
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#socket_path ⇒ Object
readonly
Returns the value of attribute socket_path.
Instance Method Summary collapse
- #close_socket ⇒ Object
- #command(cmd, *args) ⇒ Object
- #handle_connection(socket) ⇒ Object
-
#initialize(socket_path) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #unlink_socket_file ⇒ Object
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
#server ⇒ Object (readonly)
Returns the value of attribute server.
7 8 9 |
# File 'lib/eye/server.rb', line 7 def server @server end |
#socket_path ⇒ Object (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_socket ⇒ Object
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.}" return end response = command(command, *args) socket.write(Marshal.dump(response)) rescue Errno::EPIPE # client timeouted # do nothing ensure socket.close end |
#run ⇒ Object
19 20 21 |
# File 'lib/eye/server.rb', line 19 def run loop { async.handle_connection @server.accept } end |
#unlink_socket_file ⇒ Object
48 49 50 51 |
# File 'lib/eye/server.rb', line 48 def unlink_socket_file File.delete(@socket_path) if @socket_path rescue end |