1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#--
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#++
require 'qpid_examples'
require "optparse"
DEFAULT_ADDRESS = "0.0.0.0:5672"
options = {
:address => DEFAULT_ADDRESS,
:debug => false,
:verbose => false,
:count => 1,
:content => "This message was sent #{Time.new}"
}
OptionParser.new do |opts|
opts.banner = "Usage: engine_recv.rb [options]"
opts.on("-a [address]", "--address [address]",
"The target address (def. #{DEFAULT_ADDRESS})") do |address|
options[:address] = address
end
opts.on("-C [content]", "--content [content]",
"The message content") do |content|
options[:content] = content
end
opts.on("-c [count]", "--count [count]",
"The number of messages to send (def. 1)") do |count|
options[:count] = count.to_i
end
opts.on("-v", "--verbose",
"Enable verbose output") do
options[:verbose] = true
end
opts.on("-d",
"--debug", "Enable debugging") do
options[:debug] = true
end
opts.parse!
end
driver = Driver.new
conn = Qpid::Proton::Connection.new
collector = Qpid::Proton::Event::Collector.new
conn.collect(collector)
session = conn.session
conn.open
session.open
sender = session.sender("tvc_15_1")
sender.target.address = "queue"
sender.open
transport = Qpid::Proton::Transport.new
transport.bind(conn)
address, port = options[:address].split(":")
socket = TCPSocket.new(address, port)
selectable = Selectable.new(transport, socket)
sent_count = 0
sent_count = 0
driver.add(selectable)
loop do
# let the driver process
driver.process
event = collector.peek
unless event.nil?
print "EVENT: #{event}\n" if options[:debug]
case event.type
when Qpid::Proton::Event::LINK_FLOW
sender = event.sender
credit = sender.credit
message = Qpid::Proton::Message.new
if credit > 0 && sent_count < options[:count]
sent_count = sent_count.next
message.clear
message.address = options[:address]
message.subject = "Message #{sent_count}..."
message.body = options[:content]
delivery = sender.delivery("#{sent_count}")
sender.send(message.encode)
delivery.settle
sender.advance
credit = sender.credit
else
sender.close
end
when Qpid::Proton::Event::LINK_LOCAL_CLOSE
link = event.link
link.close
link.session.close
when Qpid::Proton::Event::SESSION_LOCAL_CLOSE
session = event.session
session.connection.close
when Qpid::Proton::Event::CONNECTION_LOCAL_CLOSE
break
end
collector.pop
event = collector.peek
end
end
|