From 5cc5685ae33313c0b8395812c1280c0360815e3f Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Thu, 3 Nov 2022 13:07:23 +0100 Subject: [PATCH] [WIP] Early code for handling BIRD's control socket. --- .gitignore | 2 ++ birdvisu/ospfsock.py | 39 ++++++++++++++++++++++++++++++++++++++ poor_mans_visualisation.py | 12 +++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 birdvisu/ospfsock.py diff --git a/.gitignore b/.gitignore index a847f8e..8535607 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ __pycache__/ current.ospf + +/venv/ diff --git a/birdvisu/ospfsock.py b/birdvisu/ospfsock.py new file mode 100644 index 0000000..9873b20 --- /dev/null +++ b/birdvisu/ospfsock.py @@ -0,0 +1,39 @@ +"""Simple utility to interface with BIRD using the control socket.""" + +from dataclasses import dataclass +import socket as sk + +@dataclass +class BirdResponse: + text: str = '' + codes: list[tuple[tuple[int, int], int]] = [] # List of line ranges (inclusive) and the respective code. + +class BirdSocketConnection: + def __init__(self, sockpath='/run/bird/bird.ctl'): + self.socket = sk.socket(sk.AF_UNIX, sk.SOCK_STREAM) + self.socket.setblocking(False) + self.socket.connect(sockpath) + + def request(self, req: str) -> BirdResponse: + if not req.endswith('\n'): + req = req + '\n' + binreq = req,encode() + self.socket.send(binreq) + return self._parse_response() + + def _parse_response(self): + # We even read from the socket here in order to have the whole protocol + # described at one place. Therefore other parts of the code do not need + # to know anything about the protocol (maybe apart from the meaning of + # status codes, if they are interested…) + MAXLINELENGTH = 1000 # Pretty please… + READSIZE = MAXLINELENGTH * 2 # Ensures that after the read at least one complete line has been read. + lines = [] + data = b'' + # There is no way to read line-wise, so we just read everything and split afterwards. + # This is probably very horrible. + try: + while True: + data += self.socket. + + diff --git a/poor_mans_visualisation.py b/poor_mans_visualisation.py index c5747fc..3641733 100755 --- a/poor_mans_visualisation.py +++ b/poor_mans_visualisation.py @@ -95,6 +95,16 @@ for tagsrc in [ scene.addItem(taglist[-1]) view = QtWidgets.QGraphicsView(scene) -view.show() +#view.show() + + +main_window = QtWidgets.QMainWindow() +main_window.setCentralWidget(view) + +menu = main_window.menuBar().addMenu('Hello') +act = QtGui.QAction('Hi') +#act.setToolTip('Howdy') +menu.addAction(act) +main_window.show() app.exec()