Sha256: dfac55e25acdef58f8270378962685ad7159e5dc5b0909d7ea0d9b7ce3169403

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'socket'

module Bluepill
  module Socket
<<<<<<< HEAD
    TIMEOUT = 10
    RETRY = 5
    @@timeout = 0
=======
>>>>>>> akzhan

    extend self

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

<<<<<<< HEAD
    def client_command(base_dir, name, command)
=======
    def client_command(base_dir, name, command, timeout)
>>>>>>> akzhan
      client(base_dir, name) do |socket|
        Timeout.timeout(timeout) do
          socket.puts command
          Marshal.load(socket)
        end
      end
    rescue EOFError, Timeout::Error
      @@timeout += 1
      puts "Retry #{@@timeout} of #{RETRY}"
      if @@timeout <= RETRY
        client_command(base_dir, name, command)
      else
        abort("Socket Timeout: Server may not be responding")
      end
    ensure
      @@timeout = 0
    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
<<<<<<< HEAD
=======

>>>>>>> akzhan

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ra-bluepill-0.0.45 lib/bluepill/socket.rb