class Qrack::Client

Client ancestor class

Constants

CONNECT_TIMEOUT
RETRY_DELAY

Attributes

__opts__[R]

Temporary hack to make Bunny 0.7 work with port number in AMQP URL. This is not necessary on Bunny 0.8 as it removes support of AMQP 0.8.

channel[RW]
channels[RW]
connecting[RW]
exchanges[RW]
heartbeat[R]
host[R]
logfile[RW]
logging[R]
message_in[RW]
message_out[RW]
port[R]
queues[RW]
spec[R]
status[R]
vhost[R]

Public Class Methods

new(connection_string_or_opts = Hash.new, opts = Hash.new) click to toggle source
# File lib/qrack/client.rb, line 29
def initialize(connection_string_or_opts = Hash.new, opts = Hash.new)
  opts = case connection_string_or_opts
  when String then
    AMQ::Client::Settings.parse_amqp_url(connection_string_or_opts)
  when Hash then
    connection_string_or_opts
  else
    Hash.new
  end.merge(opts)

  # Temporary hack to make Bunny 0.7 work with port number in AMQP URL.
  # This is not necessary on Bunny 0.8 as it removes support of AMQP 0.8.
  @__opts__ = opts


  @host   = opts[:host] || 'localhost'
  @user   = opts[:user]  || 'guest'
  @pass   = opts[:pass]  || 'guest'
  @vhost  = opts[:vhost] || '/'
  @logfile = opts[:logfile] || nil
  @logging = opts[:logging] || false
  @ssl = opts[:ssl] || false
  @verify_ssl = opts[:verify_ssl].nil? || opts[:verify_ssl]
  @status = :not_connected
  @frame_max = opts[:frame_max] || 131072
  @channel_max = opts[:channel_max] || 0
  @heartbeat = opts[:heartbeat] || 0
  @connect_timeout = opts[:connect_timeout] || CONNECT_TIMEOUT
  @read_write_timeout = opts[:socket_timeout]
  @read_write_timeout = nil if @read_write_timeout == 0
  @disconnect_timeout = @read_write_timeout || @connect_timeout
  @logger = nil
  create_logger if @logging
  @message_in = false
  @message_out = false
  @connecting = false
  @channels ||= []
  # Create channel 0
  @channel = create_channel()
  @exchanges ||= {}
  @queues ||= {}
end

Public Instance Methods

close() click to toggle source

@return [Symbol] @:not_connected@ if successful.

# File lib/qrack/client.rb, line 76
def close
  return if @socket.nil? || @socket.closed?

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

  # Close connection to AMQP server
  Bunny::Timer::timeout(@disconnect_timeout) { close_connection }

rescue Exception
  # http://cheezburger.com/Asset/View/4033311488
ensure
  # Clear the channels
  @channels = []

  # Create channel 0
  @channel = create_channel()

  # Close TCP Socket
  close_socket
end
Also aliased as: stop
connected?() click to toggle source
# File lib/qrack/client.rb, line 102
def connected?
  status == :connected
end
connecting?() click to toggle source
# File lib/qrack/client.rb, line 106
def connecting?
  connecting
end
logging=(bool) click to toggle source
# File lib/qrack/client.rb, line 110
def logging=(bool)
  @logging = bool
  create_logger if @logging
end
next_method(options = {}) click to toggle source
Alias for: next_payload
next_payload(options = {}) click to toggle source
# File lib/qrack/client.rb, line 115
def next_payload(options = {})
  res = next_frame(options)
  res.payload if res
end
Also aliased as: next_method
read(*args) click to toggle source
# File lib/qrack/client.rb, line 122
def read(*args)
  send_command(:read, *args)
  # Got a SIGINT while waiting; give any traps a chance to run
rescue Errno::EINTR
  retry
end
returned_message(opts = {}) click to toggle source

@param [Hash] opts Options. @option opts [Numeric] :timeout (0.1) The method will wait for a return message until this timeout interval is reached. @return [Hash] @{:header => nil, :payload => :no_return, :return_details => nil}@ if message is not returned before timeout. @{:header, :return_details, :payload}@ if message is returned. @:return_details@ is a hash @{:reply_code, :reply_text, :exchange, :routing_key}@.

# File lib/qrack/client.rb, line 135
def returned_message(opts = {})

  begin
    frame = next_frame(:timeout => opts[:timeout] || 0.1)
  rescue Qrack::FrameTimeout
    return {:header => nil, :payload => :no_return, :return_details => nil}
  end

  method = frame.payload
  header = next_payload

  # If maximum frame size is smaller than message payload body then message
  # will have a message header and several message bodies
  msg = ''
  while msg.length < header.size
    msg << next_payload
  end

  # Return the message and related info
  {:header => header, :payload => msg, :return_details => method.arguments}
end
stop() click to toggle source
Alias for: close
switch_channel(chann) click to toggle source
# File lib/qrack/client.rb, line 157
def switch_channel(chann)
  if (0...channels.size).include? chann
    @channel = channels[chann]
    chann
  else
    raise RuntimeError, "Invalid channel number - #{chann}"
  end
end
write(*args) click to toggle source
# File lib/qrack/client.rb, line 166
def write(*args)
  send_command(:write, *args)
end