Sha256: c3684117edc0773fee509b6d09ebb15c030656729c6631daf771c1d9af3df088

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 KB

Contents

require 'socket'

module Bluepill
  module Socket
    TIMEOUT = 10

    extend self

    def client(base_dir, name, &b)
      UNIXSocket.open(socket_path(base_dir, name), &b)
    end
    
    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
    
    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
    
    def socket_path(base_dir, name)
      File.join(base_dir, 'socks', name + ".sock") 
    end
  end
end
 

Version data entries

15 entries across 15 versions & 4 rubygems

Version Path
bluepill-0.0.46 lib/bluepill/socket.rb
bluepill-0.0.45 lib/bluepill/socket.rb
mwotton-bluepill-0.0.44 lib/bluepill/socket.rb
evented_bluepill-0.0.46 lib/bluepill/socket.rb
bluepill-0.0.44 lib/bluepill/socket.rb
bluepill-0.0.43 lib/bluepill/socket.rb
bluepill-0.0.42 lib/bluepill/socket.rb
bluepill-0.0.40 lib/bluepill/socket.rb
dylanvaughn-bluepill-0.0.40 lib/bluepill/socket.rb
dylanvaughn-bluepill-0.0.39 lib/bluepill/socket.rb
bluepill-0.0.39 lib/bluepill/socket.rb
bluepill-0.0.38 lib/bluepill/socket.rb
bluepill-0.0.37 lib/bluepill/socket.rb
bluepill-0.0.36 lib/bluepill/socket.rb
bluepill-0.0.35 lib/bluepill/socket.rb