| 1 |
#-- |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
#++ |
| 19 |
|
| 20 |
require 'qpid_proton' |
| 21 |
require 'optparse' |
| 22 |
require 'pathname' |
| 23 |
|
| 24 |
# Thread safe message queue that notifies waiting senders when messages arrive. |
| 25 |
class MessageQueue |
| 26 |
|
| 27 |
def initialize |
| 28 |
@lock = Mutex.new # Make ations on the queue atomic |
| 29 |
@messages = [] # Messages on the queue |
| 30 |
@waiting = [] # Senders that are waiting for messages |
| 31 |
end |
| 32 |
|
| 33 |
# Push a message onto the queue and notify any waiting senders |
| 34 |
def push(message) |
| 35 |
@lock.synchronize do |
| 36 |
@messages << message |
| 37 |
unless @waiting.empty? # Notify waiting senders |
| 38 |
# NOTE: the call to self.send_to is added to the sender's work_queue, |
| 39 |
# and will be executed in the sender's thread |
| 40 |
@waiting.each { |s| s.work_queue.add { self.send_to(s); } } |
| 41 |
@waiting.clear |
| 42 |
end |
| 43 |
end |
| 44 |
end |
| 45 |
|
| 46 |
# Pop a message off the queue. |
| 47 |
# If no messages available, record sender as waiting and return nil. |
| 48 |
def pop(sender) |
| 49 |
@lock.synchronize do |
| 50 |
if @messages.empty? |
| 51 |
@waiting << sender |
| 52 |
nil |
| 53 |
else |
| 54 |
@messages.shift |
| 55 |
end |
| 56 |
end |
| 57 |
end |
| 58 |
|
| 59 |
# NOTE: Called in sender's thread. |
| 60 |
# Pull messages from the queue as long as sender has credit. |
| 61 |
# If queue runs out of messages, record sender as waiting. |
| 62 |
def send_to(sender) |
| 63 |
while sender.credit > 0 && (message = pop(sender)) |
| 64 |
sender.send(message) |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
def forget(sender) |
| 69 |
@lock.synchronize { @waiting.delete(sender) } |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
# Handler for broker connections. In a multi-threaded application you should |
| 74 |
# normally create a separate handler instance for each connection. |
| 75 |
class BrokerHandler < Qpid::Proton::MessagingHandler |
| 76 |
|
| 77 |
def initialize(broker) |
| 78 |
@broker = broker |
| 79 |
end |
| 80 |
|
| 81 |
def on_sender_open(sender) |
| 82 |
if sender.remote_source.dynamic? |
| 83 |
sender.source.address = SecureRandom.uuid |
| 84 |
elsif sender.remote_source.address |
| 85 |
sender.source.address = sender.remote_source.address |
| 86 |
else |
| 87 |
sender.connection.close("no source address") |
| 88 |
return |
| 89 |
end |
| 90 |
q = @broker.queue(sender.source.address) |
| 91 |
q.send_to(sender) |
| 92 |
end |
| 93 |
|
| 94 |
def on_receiver_open(receiver) |
| 95 |
if receiver.remote_target.address |
| 96 |
receiver.target.address = receiver.remote_target.address |
| 97 |
else |
| 98 |
receiver.connection.close("no target address") |
| 99 |
end |
| 100 |
end |
| 101 |
|
| 102 |
def on_sender_close(sender) |
| 103 |
q = @broker.queue(sender.source.address) |
| 104 |
q.forget(sender) if q |
| 105 |
end |
| 106 |
|
| 107 |
def on_connection_close(connection) |
| 108 |
connection.each_sender { |s| on_sender_close(s) } |
| 109 |
end |
| 110 |
|
| 111 |
def on_transport_close(transport) |
| 112 |
transport.connection.each_sender { |s| on_sender_close(s) } |
| 113 |
end |
| 114 |
|
| 115 |
def on_sendable(sender) |
| 116 |
@broker.queue(sender.source.address).send_to(sender) |
| 117 |
end |
| 118 |
|
| 119 |
def on_message(delivery, message) |
| 120 |
@broker.queue(delivery.receiver.target.address).push(message) |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
# Broker manages the queues and accepts incoming connections. |
| 125 |
class Broker < Qpid::Proton::Listener::Handler |
| 126 |
|
| 127 |
def initialize |
| 128 |
@queues = {} |
| 129 |
@connection_options = {} |
| 130 |
ssl_setup |
| 131 |
end |
| 132 |
|
| 133 |
def ssl_setup |
| 134 |
# Optional SSL setup |
| 135 |
ssl = Qpid::Proton::SSLDomain.new(Qpid::Proton::SSLDomain::MODE_SERVER) |
| 136 |
cert_passsword = "tserverpw" |
| 137 |
if Gem.win_platform? # Use P12 certs for windows schannel |
| 138 |
ssl.credentials("ssl-certs/tserver-certificate.p12", "", cert_passsword) |
| 139 |
else |
| 140 |
ssl.credentials("ssl-certs/tserver-certificate.pem", "ssl-certs/tserver-private-key.pem", cert_passsword) |
| 141 |
end |
| 142 |
ssl.allow_unsecured_client # SSL is optional, this is not secure. |
| 143 |
@connection_options[:ssl_domain] = ssl if ssl |
| 144 |
rescue |
| 145 |
# Don't worry if we can't set up SSL. |
| 146 |
end |
| 147 |
|
| 148 |
def on_open(l) |
| 149 |
STDOUT.puts "Listening on #{l.port}\n"; STDOUT.flush |
| 150 |
end |
| 151 |
|
| 152 |
# Create a new BrokerHandler instance for each connection we accept |
| 153 |
def on_accept(l) |
| 154 |
{ :handler => BrokerHandler.new(self) }.update(@connection_options) |
| 155 |
end |
| 156 |
|
| 157 |
def queue(address) |
| 158 |
@queues[address] ||= MessageQueue.new |
| 159 |
end |
| 160 |
|
| 161 |
end |
| 162 |
|
| 163 |
if ARGV.size != 1 |
| 164 |
STDERR.puts "Usage: #{__FILE__} URL |
| 165 |
Start an example broker listening on URL" |
| 166 |
return 1 |
| 167 |
end |
| 168 |
url, = ARGV |
| 169 |
container = Qpid::Proton::Container.new |
| 170 |
container.listen(url, Broker.new) |
| 171 |
|
| 172 |
# Run the container in multiple threads. |
| 173 |
threads = 4.times.map { Thread.new { container.run }} |
| 174 |
threads.each { |t| t.join } |