Class: Qrack::Client

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

Overview

Client ancestor class

Direct Known Subclasses

Bunny::Client, Bunny::Client09

Constant Summary collapse

CONNECT_TIMEOUT =
5.0
RETRY_DELAY =
10.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 16

def initialize(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
	@logger = nil
	create_logger if @logging
	@message_in = false
	@message_out = false
	@connecting = false
	@channels ||= []
	# Create channel 0
    @channel = create_channel()
	@exchanges ||= {}
	@queues ||= {}
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def channel
  @channel
end

#channelsObject

Returns the value of attribute channels.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def channels
  @channels
end

#connectingObject

Returns the value of attribute connecting.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def connecting
  @connecting
end

#exchangesObject

Returns the value of attribute exchanges.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def exchanges
  @exchanges
end

#heartbeatObject (readonly)

Returns the value of attribute heartbeat.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def heartbeat
  @heartbeat
end

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def host
  @host
end

#logfileObject

Returns the value of attribute logfile.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def logfile
  @logfile
end

#loggingObject

Returns the value of attribute logging.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def logging
  @logging
end

#message_inObject

Returns the value of attribute message_in.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def message_in
  @message_in
end

#message_outObject

Returns the value of attribute message_out.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def message_out
  @message_out
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def port
  @port
end

#queuesObject

Returns the value of attribute queues.



13
14
15
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 13

def queues
  @queues
end

#specObject (readonly)

Returns the value of attribute spec.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def spec
  @spec
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def status
  @status
end

#vhostObject (readonly)

Returns the value of attribute vhost.



12
13
14
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 12

def vhost
  @vhost
end

Instance Method Details

#closeObject Also known as: stop

DESCRIPTION:

Closes all active communication channels and connection. If an error occurs a Bunny::ProtocolError is raised. If successful, Client.status is set to :not_connected.

RETURNS:

:not_connected if successful.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 55

def close
			# Close all active channels
			channels.each do |c|
c.close if c.open?
			end

			# Close connection to AMQP server
			close_connection

			# Close TCP Socket
  close_socket
end

#connected?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 70

def connected?
	status == :connected
end

#connecting?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 74

def connecting?
	connecting
end

#next_payload(options = {}) ⇒ Object Also known as: next_method



83
84
85
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 83

def next_payload(options = {})
  next_frame(options).payload
end

#read(*args) ⇒ Object



89
90
91
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 89

def read(*args)
  send_command(:read, *args)
end

#returned_message(opts = {}) ⇒ Object

DESCRIPTION:

Checks to see whether or not an undeliverable message has been returned as a result of a publish with the :immediate or :mandatory options.

OPTIONS:

  • <tt>:timeout => number of seconds (default = 0.1) - The method will wait for a return message until this timeout interval is reached.

RETURNS:

{: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}.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 114

def returned_message(opts = {})
	
	begin		
		frame = next_frame(:timeout => opts[:timeout] || 0.1)
	rescue Qrack::ClientTimeout
		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

#switch_channel(chann) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 136

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) ⇒ Object



145
146
147
# File 'lib/ext/bunny-0.6.0/lib/qrack/client.rb', line 145

def write(*args)
  send_command(:write, *args)
end