Module: EventedBluepill::Socket

Extended by:
Socket
Included in:
Socket
Defined in:
lib/evented_bluepill/socket.rb

Defined Under Namespace

Classes: CmdListenerConnection

Constant Summary collapse

TIMEOUT =
10

Instance Method Summary collapse

Instance Method Details

#client(base_dir, name, &b) ⇒ Object



31
32
33
# File 'lib/evented_bluepill/socket.rb', line 31

def client(base_dir, name, &b)
  UNIXSocket.open(socket_path(base_dir, name), &b)
end

#client_command(base_dir, name, command) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/evented_bluepill/socket.rb', line 35

def client_command(base_dir, name, command)
  client(base_dir, name) do |socket|
    Timeout.timeout(TIMEOUT) do
      socket.puts command
      Marshal.load(socket)
    end
  end
rescue EOFError, Timeout::Error
  abort("Socket Timeout: Server may not be responding")
end

#server(base_dir, name, application) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/evented_bluepill/socket.rb', line 46

def server(base_dir, name, application)
  socket_path = self.socket_path(base_dir, name)
  begin
    Coolio::UNIXServer.new(socket_path, CmdListenerConnection, application)
  rescue Errno::EADDRINUSE
    # if sock file has been created. test to see if there is a server
    begin
      UNIXSocket.open(socket_path)
    rescue Errno::ECONNREFUSED
      File.delete(socket_path)
      Coolio::UNIXServer.new(socket_path, CmdListenerConnection, application)
    else
      logger.err("Server is already running!")
      exit(7)
    end
  end
end

#socket_path(base_dir, name) ⇒ Object



64
65
66
# File 'lib/evented_bluepill/socket.rb', line 64

def socket_path(base_dir, name)
  File.join(base_dir, 'socks', name + ".sock")
end