diff options
author | jaseg <code@jaseg.net> | 2015-11-04 17:31:16 +0100 |
---|---|---|
committer | jaseg <code@jaseg.net> | 2015-11-04 17:31:16 +0100 |
commit | 2444f8796320cfd2e8187793160590cd76848640 (patch) | |
tree | eaabebd183ed531e3d218fbcb126496b1c9a2836 | |
parent | 785fbf5c4545f5e97a2f3736c121a342e45a66ae (diff) | |
download | matelight-2444f8796320cfd2e8187793160590cd76848640.tar.gz matelight-2444f8796320cfd2e8187793160590cd76848640.tar.bz2 matelight-2444f8796320cfd2e8187793160590cd76848640.zip |
server: Add automatic UDP client switching
-rwxr-xr-x | host/server.py | 6 |
1 files changed, 5 insertions, 1 deletions
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: |