Sha256: 63e7a453a3ac9d62fb95409f7e288a4ee0c0f9b172bdb86bd3c0e48da825cdfa

Contents?: true

Size: 965 Bytes

Versions: 5

Compression:

Stored size: 965 Bytes

Contents

require 'celluloid/io'
require 'celluloid/autostart'

class Eye::Server
  include Celluloid::IO
  
  attr_reader :socket_path, :server
  
  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

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

  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

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

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

  finalizer :close_socket
  
  def close_socket
    @server.close if @server
    unlink_socket_file
  end

end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
reel-eye-0.3.2 lib/eye/server.rb
eye-0.3.2 lib/eye/server.rb
reel-eye-0.3.1 lib/eye/server.rb
eye-0.3.1 lib/eye/server.rb
eye-0.3 lib/eye/server.rb