| Class | Qrack::Client |
| In: |
lib/qrack/client.rb
|
| Parent: | Object |
Client ancestor class
| CONNECT_TIMEOUT | = | 5.0 |
| RETRY_DELAY | = | 10.0 |
| 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] |
# File lib/qrack/client.rb, line 16
16: def initialize(opts = {})
17: @host = opts[:host] || 'localhost'
18: @user = opts[:user] || 'guest'
19: @pass = opts[:pass] || 'guest'
20: @vhost = opts[:vhost] || '/'
21: @logfile = opts[:logfile] || nil
22: @logging = opts[:logging] || false
23: @ssl = opts[:ssl] || false
24: @verify_ssl = opts[:verify_ssl].nil? || opts[:verify_ssl]
25: @status = :not_connected
26: @frame_max = opts[:frame_max] || 131072
27: @channel_max = opts[:channel_max] || 0
28: @heartbeat = opts[:heartbeat] || 0
29: @connect_timeout = opts[:connect_timeout] || CONNECT_TIMEOUT
30: @logger = nil
31: create_logger if @logging
32: @message_in = false
33: @message_out = false
34: @connecting = false
35: @channels ||= []
36: # Create channel 0
37: @channel = create_channel()
38: @exchanges ||= {}
39: @queues ||= {}
40: end
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.
:not_connected if successful.
# File lib/qrack/client.rb, line 55
55: def close
56: # Close all active channels
57: channels.each do |c|
58: c.close if c.open?
59: end
60:
61: # Close connection to AMQP server
62: close_connection
63:
64: # Close TCP Socket
65: close_socket
66: end
# File lib/qrack/client.rb, line 78
78: def logging=(bool)
79: @logging = bool
80: create_logger if @logging
81: end
# File lib/qrack/client.rb, line 83
83: def next_payload(options = {})
84: next_frame(options).payload
85: end
# File lib/qrack/client.rb, line 89
89: def read(*args)
90: begin
91: send_command(:read, *args)
92: # Got a SIGINT while waiting; give any traps a chance to run
93: rescue Errno::EINTR
94: retry
95: end
96:
97: end
Checks to see whether or not an undeliverable message has been returned as a result of a publish with the :immediate or :mandatory options.
{: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 120
120: def returned_message(opts = {})
121:
122: begin
123: frame = next_frame(:timeout => opts[:timeout] || 0.1)
124: rescue Qrack::ClientTimeout
125: return {:header => nil, :payload => :no_return, :return_details => nil}
126: end
127:
128: method = frame.payload
129: header = next_payload
130:
131: # If maximum frame size is smaller than message payload body then message
132: # will have a message header and several message bodies
133: msg = ''
134: while msg.length < header.size
135: msg += next_payload
136: end
137:
138: # Return the message and related info
139: {:header => header, :payload => msg, :return_details => method.arguments}
140: end
# File lib/qrack/client.rb, line 142
142: def switch_channel(chann)
143: if (0...channels.size).include? chann
144: @channel = channels[chann]
145: chann
146: else
147: raise RuntimeError, "Invalid channel number - #{chann}"
148: end
149: end
# File lib/qrack/client.rb, line 151
151: def write(*args)
152: send_command(:write, *args)
153: end
# File lib/qrack/client.rb, line 157
157: def close_socket(reason=nil)
158: # Close the socket. The server is not considered dead.
159: @socket.close if @socket and not @socket.closed?
160: @socket = nil
161: @status = :not_connected
162: end
# File lib/qrack/client.rb, line 164
164: def create_logger
165: @logfile ? @logger = Logger.new("#{logfile}") : @logger = Logger.new(STDOUT)
166: @logger.level = Logger::INFO
167: @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
168: end
# File lib/qrack/client.rb, line 170
170: def send_command(cmd, *args)
171: begin
172: raise Bunny::ConnectionError, 'No connection - socket has not been created' if !@socket
173: @socket.__send__(cmd, *args)
174: rescue Errno::EPIPE, IOError => e
175: raise Bunny::ServerDownError, e.message
176: end
177: end
# File lib/qrack/client.rb, line 179
179: def socket
180: return @socket if @socket and (@status == :connected) and not @socket.closed?
181:
182: begin
183: # Attempt to connect.
184: @socket = timeout(@connect_timeout, ConnectionTimeout) do
185: TCPSocket.new(host, port)
186: end
187:
188: if Socket.constants.include? 'TCP_NODELAY'
189: @socket.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
190: end
191:
192: if @ssl
193: require 'openssl' unless defined? OpenSSL::SSL
194: @socket = OpenSSL::SSL::SSLSocket.new(@socket)
195: @socket.sync_close = true
196: @socket.connect
197: @socket.post_connection_check(host) if @verify_ssl
198: @socket
199: end
200: rescue => e
201: @status = :not_connected
202: raise Bunny::ServerDownError, e.message
203: end
204:
205: @socket
206: end