[WIP] Early code for handling BIRD's control socket.

master
LEdoian 2 years ago
parent 822fc1c507
commit 5cc5685ae3

2
.gitignore vendored

@ -4,3 +4,5 @@
__pycache__/ __pycache__/
current.ospf current.ospf
/venv/

@ -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.

@ -95,6 +95,16 @@ for tagsrc in [
scene.addItem(taglist[-1]) scene.addItem(taglist[-1])
view = QtWidgets.QGraphicsView(scene) 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() app.exec()

Loading…
Cancel
Save