Permalink
Browse files

added graphics option to broker

  • Loading branch information...
1 parent 32c73e3 commit 78a55d907ee8f9e14199459fe5680f0e685c94a6 Rafael H. Schloming committed Jun 7, 2010
Showing with 52 additions and 26 deletions.
  1. +28 −7 broker
  2. +4 −9 rendered-nodes
  3. +14 −4 selector.py
  4. +6 −6 window.py
View
@@ -171,16 +171,30 @@ parser.add_option("-t", "--trace", default="err",
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:
broker = Broker(opts.container)
+ selector = Selector()
+ window = Window(lambda *args: selector.stop())
- thresholds = {}
- for value in opts.thresholds:
- name, threshold = value.split("=", 2)
- thresholds[name] = int(threshold)
+ def tick(c):
+ broker.tick(c)
+ window.redraw()
nodes = {}
for n in opts.nodes:
@@ -191,18 +205,25 @@ try:
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()
sock = bind(opts.interface, opts.port)
def handler(accepted):
conn = Connection(lambda properties: Session(link, properties))
conn.tracing(*opts.trace.split())
- selector.register(ConnectionSelectable(accepted, conn, broker.tick))
+ selector.register(ConnectionSelectable(accepted, conn, tick))
selector.register(Acceptor(sock, handler))
- selector.run()
+ if opts.graphics:
+ selector.run(window.redraw, 0.1)
+ else:
+ selector.run()
except KeyboardInterrupt:
pass
View
@@ -1,12 +1,7 @@
-rendered_queue = Queue(threshold=300)
-rendered_topic = Queue(threshold=300, ring=0, acquire=False, dequeue=False)
+rendered_queue = Queue(threshold=1000)
+rendered_topic = Queue(threshold=1000, ring=0, acquire=False, dequeue=False)
-from window import Window
from widgets import SizeGraph
-_w = Window()
-
-_w.add(SizeGraph(rendered_queue), 0.3, 0.1, 0.1, 0.8)
-_w.add(SizeGraph(rendered_topic), 0.6, 0.1, 0.1, 0.8)
-
-_w.start()
+window.add(SizeGraph(rendered_queue), 0.3, 0.1, 0.1, 0.8)
+window.add(SizeGraph(rendered_topic), 0.6, 0.1, 0.1, 0.8)
View
@@ -104,7 +104,7 @@ def start(self):
self.thread.setDaemon(True)
self.thread.start();
- def run(self):
+ def run(self, idle=None, period=None):
while not self.stopped:
wakeup = None
for sel in self.selectables.copy():
@@ -116,28 +116,38 @@ def run(self):
wakeup = min(wakeup, t)
if wakeup is None:
- timeout = None
+ timeout = period
else:
timeout = max(0, wakeup - time.time())
+ if period:
+ timeout = min(period, timeout)
rd, wr, ex = select(self.reading, self.writing, (), timeout)
+ is_idle = True
for sel in wr:
if sel.writing():
sel.writeable()
+ is_idle = False
for sel in rd:
if sel.reading():
sel.readable()
+ is_idle = False
now = time.time()
for sel in self.selectables.copy():
w = sel.timing()
if w is not None and now > w:
sel.timeout()
+ is_idle = False
+
+ if is_idle and idle:
+ idle()
def stop(self, timeout=None):
self.stopped = True
self.wakeup()
- self.thread.join(timeout)
- self.thread = None
+ if self.thread:
+ self.thread.join(timeout)
+ self.thread = None
View
@@ -57,10 +57,12 @@ def draw(self, cr, width, height):
class Window(Thread):
- def __init__(self):
+ def __init__(self, quit=None):
Thread.__init__(self)
+ self.quit = quit
self.window = gtk.Window()
- self.window.connect("delete-event", gtk.main_quit)
+ if self.quit:
+ self.window.connect("delete-event", quit)
self.screen = Screen()
self.screen.show()
self.window.add(self.screen)
@@ -71,8 +73,6 @@ def add(self, widget, x, y, w, h):
def redraw(self):
self.window.queue_draw()
+ while gtk.events_pending():
+ gtk.main_iteration(block=False)
return True
-
- def run(self):
- gobject.timeout_add(40, self.redraw)
- gtk.main()

0 comments on commit 78a55d9

Please sign in to comment.