| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
# or more contributor license agreements. See the NOTICE file |
| 5 |
# distributed with this work for additional information |
| 6 |
# regarding copyright ownership. The ASF licenses this file |
| 7 |
# to you under the Apache License, Version 2.0 (the |
| 8 |
# "License"); you may not use this file except in compliance |
| 9 |
# with the License. You may obtain a copy of the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, |
| 14 |
# software distributed under the License is distributed on an |
| 15 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
# KIND, either express or implied. See the License for the |
| 17 |
# specific language governing permissions and limitations |
| 18 |
# under the License. |
| 19 |
# |
| 20 |
|
| 21 |
import collections |
| 22 |
import optparse |
| 23 |
import uuid |
| 24 |
|
| 25 |
from proton import Endpoint |
| 26 |
from proton.handlers import MessagingHandler |
| 27 |
from proton.reactor import Container |
| 28 |
|
| 29 |
|
| 30 |
class Queue(object): |
| 31 |
def __init__(self, dynamic=False): |
| 32 |
self.dynamic = dynamic |
| 33 |
self.queue = collections.deque() |
| 34 |
self.consumers = [] |
| 35 |
|
| 36 |
def subscribe(self, consumer): |
| 37 |
self.consumers.append(consumer) |
| 38 |
|
| 39 |
def unsubscribe(self, consumer): |
| 40 |
""" |
| 41 |
:return: True if the queue is to be deleted |
| 42 |
""" |
| 43 |
if consumer in self.consumers: |
| 44 |
self.consumers.remove(consumer) |
| 45 |
return len(self.consumers) == 0 and (self.dynamic or len(self.queue) == 0) |
| 46 |
|
| 47 |
def publish(self, message): |
| 48 |
self.queue.append(message) |
| 49 |
self.dispatch() |
| 50 |
|
| 51 |
def dispatch(self, consumer=None): |
| 52 |
if consumer: |
| 53 |
c = [consumer] |
| 54 |
else: |
| 55 |
c = self.consumers |
| 56 |
while self._deliver_to(c): |
| 57 |
pass |
| 58 |
|
| 59 |
def _deliver_to(self, consumers): |
| 60 |
try: |
| 61 |
result = False |
| 62 |
for c in consumers: |
| 63 |
if c.credit: |
| 64 |
c.send(self.queue.popleft()) |
| 65 |
result = True |
| 66 |
return result |
| 67 |
except IndexError: # no more messages |
| 68 |
return False |
| 69 |
|
| 70 |
|
| 71 |
class Broker(MessagingHandler): |
| 72 |
def __init__(self, url): |
| 73 |
super(Broker, self).__init__() |
| 74 |
self.url = url |
| 75 |
self.queues = {} |
| 76 |
|
| 77 |
def on_start(self, event): |
| 78 |
self.acceptor = event.container.listen(self.url) |
| 79 |
|
| 80 |
def _queue(self, address): |
| 81 |
if address not in self.queues: |
| 82 |
self.queues[address] = Queue() |
| 83 |
return self.queues[address] |
| 84 |
|
| 85 |
def on_link_opening(self, event): |
| 86 |
if event.link.is_sender: |
| 87 |
if event.link.remote_source.dynamic: |
| 88 |
address = str(uuid.uuid4()) |
| 89 |
event.link.source.address = address |
| 90 |
q = Queue(True) |
| 91 |
self.queues[address] = q |
| 92 |
q.subscribe(event.link) |
| 93 |
elif event.link.remote_source.address: |
| 94 |
event.link.source.address = event.link.remote_source.address |
| 95 |
self._queue(event.link.source.address).subscribe(event.link) |
| 96 |
elif event.link.remote_target.address: |
| 97 |
event.link.target.address = event.link.remote_target.address |
| 98 |
|
| 99 |
def _unsubscribe(self, link): |
| 100 |
if link.source.address in self.queues and self.queues[link.source.address].unsubscribe(link): |
| 101 |
del self.queues[link.source.address] |
| 102 |
|
| 103 |
def on_link_closing(self, event): |
| 104 |
if event.link.is_sender: |
| 105 |
self._unsubscribe(event.link) |
| 106 |
|
| 107 |
def on_connection_closing(self, event): |
| 108 |
self.remove_stale_consumers(event.connection) |
| 109 |
|
| 110 |
def on_disconnected(self, event): |
| 111 |
self.remove_stale_consumers(event.connection) |
| 112 |
|
| 113 |
def remove_stale_consumers(self, connection): |
| 114 |
l = connection.link_head(Endpoint.REMOTE_ACTIVE) |
| 115 |
while l: |
| 116 |
if l.is_sender: |
| 117 |
self._unsubscribe(l) |
| 118 |
l = l.next(Endpoint.REMOTE_ACTIVE) |
| 119 |
|
| 120 |
def on_sendable(self, event): |
| 121 |
self._queue(event.link.source.address).dispatch(event.link) |
| 122 |
|
| 123 |
def on_message(self, event): |
| 124 |
address = event.link.target.address |
| 125 |
if address is None: |
| 126 |
address = event.message.address |
| 127 |
self._queue(address).publish(event.message) |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
parser = optparse.OptionParser(usage="usage: %prog [options]") |
| 132 |
parser.add_option("-a", "--address", default="localhost:5672", |
| 133 |
help="address router listens on (default %default)") |
| 134 |
opts, args = parser.parse_args() |
| 135 |
|
| 136 |
try: |
| 137 |
Container(Broker(opts.address)).run() |
| 138 |
except KeyboardInterrupt: |
| 139 |
pass |
| 140 |
|
| 141 |
|
| 142 |
if __name__ == '__main__': |
| 143 |
main() |