Sha256: 839ae4060a2e4d8b68c3db614ad9099e38c2f0706dcd2abad30d8bf2f7c4f8d8

Contents?: true

Size: 859 Bytes

Versions: 3

Compression:

Stored size: 859 Bytes

Contents

require 'socket'

module Bluepill
  class Socket
    attr_accessor :name, :base_dir, :socket
    
    def initialize(name, base_dir)
      self.name = name
      self.base_dir = base_dir
    end

    def client
      self.socket = UNIXSocket.open(socket_name)
    end
    
    def server
      begin
        self.socket = UNIXServer.open(socket_name)
      rescue Errno::EADDRINUSE
        # if sock file has been created.  test to see if there is a server
        tmp_socket = UNIXSocket.open(socket_name) rescue nil
        if tmp_socket.nil?
          cleanup
          retry
        else
          raise Exception.new("Server is already running")
        end
      end
    end
    
    def cleanup
      File.delete(socket_name)
    end
    
    def socket_name
      @socket_name ||= File.join(base_dir, 'socks', name + ".sock") 
    end
    
  end
end
 

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bluepill-0.0.24 lib/bluepill/socket.rb
bluepill-0.0.23 lib/bluepill/socket.rb
bluepill-0.0.22 lib/bluepill/socket.rb