Sha256: 2bbad776643b4468ae1faefb0b4216c88ab2df17736215d785045fb8fddbe144

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

# -*- encoding: utf-8 -*-
require 'socket'

module Bluepill
  module Socket
    TIMEOUT = 60 # Used for client commands

    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

6 entries across 6 versions & 2 rubygems

Version Path
ryansch-bluepill-0.0.53 lib/bluepill/socket.rb
bluepill-0.0.51 lib/bluepill/socket.rb
bluepill-0.0.50 lib/bluepill/socket.rb
bluepill-0.0.49 lib/bluepill/socket.rb
bluepill-0.0.48 lib/bluepill/socket.rb
bluepill-0.0.47 lib/bluepill/socket.rb