diff --git a/tnc/audio.py b/tnc/audio.py index 4ba521ee..061e64dd 100644 --- a/tnc/audio.py +++ b/tnc/audio.py @@ -10,6 +10,10 @@ import json from ctypes import * from contextlib import contextmanager import pyaudio +from multiprocessing import Process, Queue +AUDIO_DEVICES = Queue() + + ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p) @@ -29,8 +33,13 @@ def noalsaerr(): # p = pyaudio.PyAudio() ###################################################### - def get_audio_devices(): + p = Process(target=update_audio_devices) + p.start() + p.join() + return AUDIO_DEVICES.get() + +def update_audio_devices(): # UPDATE LIST OF AUDIO DEVICES try: # we need to "try" this, because sometimes libasound.so isn't in the default place @@ -61,5 +70,6 @@ def get_audio_devices(): output_devices.append({"id": i, "name": str(name)}) p.terminate() - - return [input_devices, output_devices] + AUDIO_DEVICES.put([input_devices, output_devices]) + + diff --git a/tnc/sock.py b/tnc/sock.py index ff51034b..b1f497be 100644 --- a/tnc/sock.py +++ b/tnc/sock.py @@ -78,17 +78,15 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): data = SOCKET_QUEUE.get() sock_data = bytes(data, 'utf-8') sock_data += b'\n' # append line limiter + # send data to all clients - try: - for client in CONNECTED_CLIENTS: - try: - client.send(sock_data) - except: - print("connection lost...") - CONNECTED_CLIENTS.remove(self.request) - except: - print("client not anymore in client list") - + #try: + for client in CONNECTED_CLIENTS: + try: + client.send(sock_data) + except: + print("connection lost...") + # we want to transmit scatter data only once to reduce network traffic static.SCATTER = [] # we want to display INFO messages only once @@ -106,8 +104,8 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): if chunk == b'': #print("connection broken. Closing...") self.connection_alive = False - - if data.startswith(b'{') and data.endswith(b'}\n'): + print(chunk) + if data.startswith(b'{') and data.endswith(b'}\n'): # split data by \n if we have multiple commands in socket buffer data = data.split(b'\n') # remove empty data @@ -119,7 +117,15 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): process_tnc_commands(commands) else: process_daemon_commands(commands) - + + # wait some time between processing multiple commands + # this is only a first test to avoid doubled transmission + # we might improve this by only processing one command or + # doing some kind of selection to determin which commands need to be dropped + # and which one can be processed during a running transmission + time.sleep(3) + + # finally delete our rx buffer to be ready for new commands data = bytes() except Exception as e: @@ -438,4 +444,4 @@ def send_daemon_state(): return jsondata except Exception as e: print(e) - return None \ No newline at end of file + return None