From 2444f8796320cfd2e8187793160590cd76848640 Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 4 Nov 2015 17:31:16 +0100 Subject: server: Add automatic UDP client switching --- host/server.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/host/server.py b/host/server.py index c011c82..072b8ba 100755 --- a/host/server.py +++ b/host/server.py @@ -17,6 +17,7 @@ from ctypes import * from matelight import sendframe, DISPLAY_WIDTH, DISPLAY_HEIGHT, FRAME_SIZE UDP_TIMEOUT = 3.0 +UDP_SWITCH_INTERVAL = 30.0 class COLOR(Structure): _fields_ = [('r', c_uint8), ('g', c_uint8), ('b', c_uint8), ('a', c_uint8)] @@ -82,6 +83,7 @@ class MateLightUDPServer: def __init__(self, port=1337, ip=''): self.current_client = None self.last_timestamp = 0 + self.begin_timestamp = 0 self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind((ip, port)) @@ -107,8 +109,10 @@ class MateLightUDPServer: try: data, (addr, sport) = self.socket.recvfrom(FRAME_SIZE*3+4) timestamp = time() - if timestamp - self.last_timestamp > UDP_TIMEOUT: + if timestamp - self.last_timestamp > UDP_TIMEOUT \ + or timestamp - self.begin_timestamp > UDP_SWITCH_INTERVAL: self.current_client = addr + self.begin_timestamp = timestamp log('\x1B[91mAccepting UDP data from\x1B[0m', addr) if addr == self.current_client: if len(data) == FRAME_SIZE*3+4: -- cgit