Sha256: 9be4d013a6101ee2e23c83626e700d4b0c5c71c6f31e910f52596d8e96230005

Contents?: true

Size: 924 Bytes

Versions: 21

Compression:

Stored size: 924 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
      @isserver = false
    end

    def client
      self.socket = UNIXSocket.open(socket_name)
    end
    
    def server
      @isserver = true
      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) if @isserver      
    end
    
    def socket_name
      @socket_name ||= File.join(base_dir, 'socks', name + ".sock") 
    end
    
  end
end
 

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bluepill-0.0.21 lib/bluepill/socket.rb
bluepill-0.0.20 lib/bluepill/socket.rb
bluepill-0.0.19 lib/bluepill/socket.rb
bluepill-0.0.18 lib/bluepill/socket.rb
bluepill-0.0.17 lib/bluepill/socket.rb
bluepill-0.0.16 lib/bluepill/socket.rb
bluepill-0.0.15 lib/bluepill/socket.rb
bluepill-0.0.14 lib/bluepill/socket.rb
bluepill-0.0.13 lib/bluepill/socket.rb
bluepill-0.0.12 lib/bluepill/socket.rb
bluepill-0.0.11 lib/bluepill/socket.rb
bluepill-0.0.10 lib/bluepill/socket.rb
bluepill-0.0.9 lib/bluepill/socket.rb
bluepill-0.0.8 lib/bluepill/socket.rb
bluepill-0.0.7 lib/bluepill/socket.rb
bluepill-0.0.6 lib/bluepill/socket.rb
bluepill-0.0.5 lib/bluepill/socket.rb
bluepill-0.0.4 lib/bluepill/socket.rb
bluepill-0.0.3 lib/bluepill/socket.rb
bluepill-0.0.2 lib/bluepill/socket.rb