Class: Qrack::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bunny-ext/qrack/client.rb

Direct Known Subclasses

Bunny::Client

Instance Method Summary collapse

Instance Method Details

#closeObject Also known as: stop



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bunny-ext/qrack/client.rb', line 67

def close
  return if @socket.nil? || @socket.closed?

  # Close all active channels
  channels.each { |c| Bunny::Timer::timeout(disconnect_timeout) { c.close if c.open? } }

  # Close connection to AMQP server
  Bunny::Timer::timeout(disconnect_timeout) { close_connection }
rescue Exception
  # http://http://cheezburger.com/Asset/View/4033311488
ensure
  @channels = []
  close_socket
end

#disconnect_timeoutObject



63
64
65
# File 'lib/bunny-ext/qrack/client.rb', line 63

def disconnect_timeout
  @read_write_timeout || @connect_timeout
end

#initialize_with_timeout_opts(opts = {}) ⇒ Object Also known as: initialize



8
9
10
11
12
# File 'lib/bunny-ext/qrack/client.rb', line 8

def initialize_with_timeout_opts(opts = {})
  initialize_without_timeout_opts(opts)
  @read_write_timeout = opts[:socket_timeout]
  @read_write_timeout = nil if @read_write_timeout == 0
end

#send_command(cmd, *args) ⇒ Object

Overwritten with a version that uses Bunny::Timer::timeout instead of Object#timeout which is either timeout.rb (ruby 1.9.x) or SystemTimer (ruby 1.8.x) read: ph7spot.com/musings/system-timer



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bunny-ext/qrack/client.rb', line 19

def send_command(cmd, *args)
  begin
    raise Bunny::ConnectionError, 'No connection - socket has not been created' if !@socket
    if @read_write_timeout
      Bunny::Timer::timeout(@read_write_timeout, Qrack::ClientTimeout) do
        @socket.__send__(cmd, *args)
      end
    else
      @socket.__send__(cmd, *args)
    end
  rescue Errno::EPIPE, Errno::EAGAIN, Qrack::ClientTimeout, IOError => e
    raise Bunny::ServerDownError, e.message
  end
end

#socketObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bunny-ext/qrack/client.rb', line 34

def socket
  return @socket if @socket and (@status == :connected) and not @socket.closed?

  begin
    # Attempt to connect.
    @socket = Bunny::Timer::timeout(@connect_timeout, ConnectionTimeout) do
      TCPSocket.new(host, port)
    end

    if Socket.constants.include? 'TCP_NODELAY'
      @socket.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
    end

    if @ssl
      require 'openssl' unless defined? OpenSSL::SSL
      @socket = OpenSSL::SSL::SSLSocket.new(@socket)
      @socket.sync_close = true
      @socket.connect
      @socket.post_connection_check(host) if @verify_ssl
      @socket
    end
  rescue => e
    @status = :not_connected
    raise Bunny::ServerDownError, e.message
  end

  @socket
end