From b4f822ea6fcf5c956f102771faf29dbc7a401834 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Mon, 19 Jul 2021 21:00:46 +0200 Subject: [PATCH] attempt to run socket commands as threats asyncio seems to be blocking --- tnc/data_handler.py | 10 ++++++++-- tnc/sock.py | 20 +++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tnc/data_handler.py b/tnc/data_handler.py index 79b99c28..a94fe2c2 100644 --- a/tnc/data_handler.py +++ b/tnc/data_handler.py @@ -523,6 +523,10 @@ def open_dc_and_transmit(data_out, mode, n_frames): #on a new transmission we reset the timer static.ARQ_START_OF_TRANSMISSION = int(time.time()) + # lets wait a little bit so RX station is ready for receiving + wait_before_data_timer = time.time() + 0.5 + while time.time() < wait_before_data_timer: + pass # lets wait a little bit #time.sleep(5) @@ -634,7 +638,8 @@ def arq_received_channel_is_open(data_in): # PING HANDLER # ############################################################################################################ -async def transmit_ping(callsign): +#async def transmit_ping(callsign): +def transmit_ping(callsign): static.DXCALLSIGN = bytes(callsign, 'utf-8').rstrip(b'\x00') static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN) logging.info("PING [" + str(static.MYCALLSIGN, 'utf-8') + "] >>> [" + str(static.DXCALLSIGN, 'utf-8') + "] [SNR:" + str(static.SNR) + "]") @@ -687,7 +692,8 @@ def received_ping_ack(data_in): # ############################################################################################################ -async def transmit_cq(): +#async def transmit_cq(): +def transmit_cq(): logging.info("CQ CQ CQ") cq_frame = bytearray(14) diff --git a/tnc/sock.py b/tnc/sock.py index f9fe7c46..cfbb594d 100644 --- a/tnc/sock.py +++ b/tnc/sock.py @@ -24,7 +24,7 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): print("Client connected...") # loop through socket buffer until timeout is reached. then close buffer - socketTimeout = time.time() + 3 + socketTimeout = time.time() + 10 while socketTimeout > time.time(): time.sleep(0.01) @@ -50,9 +50,10 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): try: received_json = json.loads(data) - #print(received_json) + print(received_json) except: received_json = '' + # GET COMMANDS @@ -80,16 +81,21 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): # CQ CQ CQ ----------------------------------------------------- #if data == 'CQCQCQ': if received_json["command"] == "CQCQCQ": - asyncio.run(data_handler.transmit_cq()) - + socketTimeout = 0 + #asyncio.run(data_handler.transmit_cq()) + CQ_THREAD = threading.Thread(target=data_handler.transmit_cq, args=[], name="CQ") + CQ_THREAD.start() # PING ---------------------------------------------------------- #if data.startswith('PING:'): - if received_json["command"] == "PING": + if received_json["type"] == 'PING' and received_json["command"] == "PING": # send ping frame and wait for ACK + print(received_json) dxcallsign = received_json["dxcallsign"] - asyncio.run(data_handler.transmit_ping(dxcallsign)) - + #asyncio.run(data_handler.transmit_ping(dxcallsign)) + PING_THREAD = threading.Thread(target=data_handler.transmit_ping, args=[dxcallsign], name="CQ") + PING_THREAD.start() + # ARQ CONNECT TO CALLSIGN ---------------------------------------- #if data.startswith('ARQ:CONNECT:'): #if received_json["command"] == "ARQ:CONNECT":