Module: Bluepill::Socket

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

Constant Summary collapse

TIMEOUT =
10

Instance Method Summary collapse

Instance Method Details

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



9
10
11
# File 'lib/bluepill/socket.rb', line 9

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

#client_command(base_dir, name, command) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/bluepill/socket.rb', line 13

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bluepill/socket.rb', line 24

def server(base_dir, name)
  socket_path = self.socket_path(base_dir, name)
  begin
    UNIXServer.open(socket_path)
  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)
      return UNIXServer.open(socket_path)
    else
      logger.err("Server is already running!")
      exit(7)
    end
  end
end

#socket_path(base_dir, name) ⇒ Object



42
43
44
# File 'lib/bluepill/socket.rb', line 42

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