diff options
author | Uwe Kamper <me@uwekamper.de> | 2014-03-07 22:18:10 +0100 |
---|---|---|
committer | Uwe Kamper <me@uwekamper.de> | 2014-03-07 22:18:10 +0100 |
commit | b503b8fffc43dad16a64ec34799c28757bd127d2 (patch) | |
tree | 7bbf2191761ae45180e627ed5b950a4372699288 /host | |
parent | 8f86c5ac4576b36aff906b91e194e68eb343c536 (diff) | |
download | matelight-b503b8fffc43dad16a64ec34799c28757bd127d2.tar.gz matelight-b503b8fffc43dad16a64ec34799c28757bd127d2.tar.bz2 matelight-b503b8fffc43dad16a64ec34799c28757bd127d2.zip |
multithreaded frame-queueing and polling in the server classes
Diffstat (limited to 'host')
-rwxr-xr-x | host/server.py | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/host/server.py b/host/server.py index 56002fa..0de0edc 100755 --- a/host/server.py +++ b/host/server.py @@ -83,7 +83,29 @@ def log(*args): print(strftime('[%m-%d %H:%M:%S]'), ' '.join(str(arg) for arg in args), '\x1B[0m') printlock.release() + +class MateLightUDPServer(UDPServer): + """ + The server class for the SocketServer interface. + """ + def __init__(self, *args, **kwargs): + """ + Setup the deque for the frame + """ + super(MateLightUDPServer, self).__init__(*args, **kwargs) + self.frame_deque = collections.deque(maxlen=30) + + def get_next_frame(self): + try: + return self.frame_deque.popleft() + except IndexError: + return None + + class MateLightUDPHandler(BaseRequestHandler): + """ + Handles one UDP connection to the matelight + """ def handle(self): try: # Housekeeping - FIXME: This is *so* the wrong place for this. @@ -113,8 +135,9 @@ class MateLightUDPHandler(BaseRequestHandler): if current_entry.entrytype == 'udp' and current_entry.remote == addr: current_entry = conn frame = a.reshape((DISPLAY_HEIGHT, DISPLAY_WIDTH, 3)) - sendframe(frame) - printframe(np.pad(frame, ((0,0),(0,0),(0,1)), 'constant', constant_values=(0,0))) + self.server.frame_deque.append(frame) + #sendframe(frame) + #printframe(np.pad(frame, ((0,0),(0,0),(0,1)), 'constant', constant_values=(0,0))) except Exception as e: log('Error receiving UDP frame:', e) @@ -131,19 +154,25 @@ class MateLightTCPTextHandler(BaseRequestHandler): textqueue.append(QueueEntry('text', addr, timestamp, data)) self.request.sendall(b'KTHXBYE!\n') -TCPServer.allow_reuse_address = True -server = TCPServer(('', 1337), MateLightTCPTextHandler) -t = threading.Thread(target=server.serve_forever) -t.daemon = True -t.start() +#TCPServer.allow_reuse_address = True +#server = TCPServer(('', 1337), MateLightTCPTextHandler) +#t = threading.Thread(target=server.serve_forever) +#t.daemon = True +#t.start() UDPServer.allow_reuse_address = True -userver = UDPServer(('', 1337), MateLightUDPHandler) +userver = MateLightUDPServer(('', 1337), MateLightUDPHandler) t = threading.Thread(target=userver.serve_forever) t.daemon = True t.start() if __name__ == '__main__': + while True: + next_frame = userver.get_next_frame() + if next_frame: + sendframe(next_frame) + +def bla(): print('\033[2J'+'\n'*9) while True: if current_entry.entrytype == 'text': |