Sha256: 268ccbb0c9b90c5faa70a4edc25bd8b128171afb31f4720567d4f5dceb2fe140

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'celluloid/io'
require 'celluloid/current'

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)
    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

  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

2 entries across 2 versions & 1 rubygems

Version Path
eye-0.8.pre2 lib/eye/server.rb
eye-0.8.pre lib/eye/server.rb