-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker
executable file
·109 lines (98 loc) · 3.87 KB
/
broker
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
#!/usr/bin/python
#
# 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.
#
import optparse, socket
from brokerlib import Broker
from queue import Queue
from selector import Selector
parser = optparse.OptionParser(usage="usage: %prog [options] QUEUE_1 ... QUEUE_n",
description="Prototype amqp broker.")
parser.add_option("-i", "--interface", default="0.0.0.0",
help="interface to listen on (default %default)")
parser.add_option("-c", "--container", default=socket.gethostname(),
help="container-id for the broker (default %default)")
parser.add_option("-n", "--nodes", default=[], action="append",
help="load nodes from specified file")
parser.add_option("-u", "--users", default=[], action="append",
help="load user definitions from specified file")
parser.add_option("-p", "--port", type=int, default=5672,
help="port to listen on (default %default)")
parser.add_option("-a", "--auth", action="store_true",
help="enable sasl authentication layer")
parser.add_option("-f", "--frame-size", type=int, default=4294967295,
help="specify the maximum frame size")
parser.add_option("-w", "--window", type=int, default=65536,
help="session window size")
parser.add_option("-e", "--period", type=float, default=None,
help="update period for session window")
parser.add_option("-t", "--trace", default="err",
help="enable tracing for specified categories")
parser.add_option("-T", "--threshold", dest="thresholds", default=[],
action="append", metavar="NAME=THRESHOLD",
help="specify flow control threshold for a queue")
parser.add_option("-g", "--graphics", action="store_true",
help="launch the broker with graphics enabled")
opts, args = parser.parse_args()
if opts.graphics:
from window import Window
else:
class Window:
def __init__(self, *args, **kwargs):
pass
def add(self, *args, **kwargs):
raise RuntimeError("graphics are not enabled")
def redraw(self):
pass
try:
mechanisms = ["ANONYMOUS", "PLAIN"]
passwords = {}
for u in opts.users:
exec open(u) in globals(), passwords
broker = Broker(opts.container)
broker.window = opts.window
broker.period = opts.period
broker.frame_size = opts.frame_size
broker.auth = opts.auth
broker.mechanisms = mechanisms
broker.passwords = passwords
broker.traces = opts.trace.split()
window = Window(lambda *args: selector.stop())
nodes = {}
for n in opts.nodes:
exec open(n) in globals(), nodes
for name, value in nodes.items():
if name.startswith("_"):
continue
broker.nodes[name] = value
thresholds = {}
for value in opts.thresholds:
name, threshold = value.split("=", 2)
thresholds[name] = int(threshold)
for a in args:
broker.nodes[a] = Queue(thresholds.get(a))
selector = Selector()
broker.listener = window.redraw
broker.bind(opts.interface, opts.port)
selector.register(broker)
if opts.graphics:
selector.run(window.redraw, 0.1)
else:
selector.run()
except KeyboardInterrupt:
pass