-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_broker_monitor.py
70 lines (61 loc) · 2.12 KB
/
old_broker_monitor.py
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
import textwrap
import curses
import sys
import zmq
print(zmq.zmq_version_info())
print(zmq.pyzmq_version_info())
class server(object):
def __init__(self):
self.subs = {}
self.servers = {}
def log_print(self, indent, msg):
try:
indent = indent.decode()
except:
pass
self.textwrapper.initial_indent = '{:22}'.format(indent)
self.textwrapper.subsequent_indent = ' '*max(22,len(indent))
lines = self.textwrapper.wrap(msg)
for l in lines:
self.logwin.scroll()
p = self.logwin.getmaxyx()
self.logwin.addstr(p[0]-2,1,l)
self.logwin.border()
self.logwin.refresh()
def setup_screen(self):
self.textwrapper = textwrap.TextWrapper(width=curses.COLS-4)
logwin= self.csrcn.subwin(curses.LINES-10,curses.COLS-2,10,1)
logwin.border()
logwin.refresh()
self.csrcn.idlok(True)
self.csrcn.scrollok(False)
logwin.idlok(True)
logwin.scrollok(True)
logwin.setscrreg(1,curses.LINES-12)
self.logwin = logwin
self.csrcn.addstr(8, 1, 'PING: ')
self.csrcn.addstr(9, 1, 'PONG: ')
self.csrcn.refresh()
def loop(self, csrcn):
self.csrcn = csrcn
self.setup_screen()
ctx = zmq.Context()
subby = ctx.socket(zmq.SUB)
subby.bind('tcp://127.0.0.1:5142')
subby.setsockopt(zmq.SUBSCRIBE, b"PING")
subby.setsockopt(zmq.SUBSCRIBE, b"PONG")
subby.setsockopt(zmq.SUBSCRIBE, b"TICKER")
subby.setsockopt(zmq.SUBSCRIBE, b"LOG")
while True:
pkt = subby.recv_multipart()
if pkt[0] == b'PING':
self.csrcn.insstr(8, 7, '{:15}'.format(pkt[1].decode()))
elif pkt[0] == b'PONG':
self.csrcn.insstr(9, 7, '{:15}'.format(pkt[1].decode()))
elif pkt[0] == b'TICKER':
self.csrcn.insstr(7, 7, pkt[1].decode())
elif pkt[0] == b'LOG':
self.log_print(pkt[1].decode(), pkt[2].decode())
self.csrcn.refresh()
s = server()
curses.wrapper(s.loop)