FreeDATA/freedata_server/p2p_connection.py

316 lines
12 KiB
Python
Raw Normal View History

2024-03-09 09:47:27 +00:00
import threading
from enum import Enum
from modem_frametypes import FRAME_TYPE
from codec2 import FREEDV_MODE
import data_frame_factory
import structlog
import random
from queue import Queue
2024-03-19 10:19:13 +00:00
import time
from command_arq_raw import ARQRawCommand
import numpy as np
import base64
from arq_data_type_handler import ARQDataTypeHandler, ARQ_SESSION_TYPES
from arq_session_iss import ARQSessionISS
2024-03-09 09:47:27 +00:00
class States(Enum):
NEW = 0
CONNECTING = 1
CONNECT_SENT = 2
CONNECT_ACK_SENT = 3
CONNECTED = 4
#HEARTBEAT_SENT = 5
#HEARTBEAT_ACK_SENT = 6
2024-03-09 09:47:27 +00:00
PAYLOAD_SENT = 7
ARQ_SESSION = 8
DISCONNECTING = 9
DISCONNECTED = 10
FAILED = 11
2024-03-09 09:47:27 +00:00
class P2PConnection:
STATE_TRANSITION = {
States.NEW: {
FRAME_TYPE.P2P_CONNECTION_CONNECT.value: 'connected_irs',
},
States.CONNECTING: {
FRAME_TYPE.P2P_CONNECTION_CONNECT_ACK.value: 'connected_iss',
},
States.CONNECTED: {
FRAME_TYPE.P2P_CONNECTION_CONNECT.value: 'connected_irs',
FRAME_TYPE.P2P_CONNECTION_CONNECT_ACK.value: 'connected_iss',
FRAME_TYPE.P2P_CONNECTION_PAYLOAD.value: 'received_data',
2024-03-15 13:25:46 +00:00
FRAME_TYPE.P2P_CONNECTION_DISCONNECT.value: 'received_disconnect',
},
States.PAYLOAD_SENT: {
2024-03-19 10:19:13 +00:00
FRAME_TYPE.P2P_CONNECTION_PAYLOAD_ACK.value: 'transmitted_data',
2024-03-15 13:25:46 +00:00
},
States.DISCONNECTING: {
FRAME_TYPE.P2P_CONNECTION_DISCONNECT_ACK.value: 'received_disconnect_ack',
},
States.DISCONNECTED: {
FRAME_TYPE.P2P_CONNECTION_DISCONNECT.value: 'received_disconnect',
FRAME_TYPE.P2P_CONNECTION_DISCONNECT_ACK.value: 'received_disconnect_ack',
2024-03-09 09:47:27 +00:00
2024-03-15 13:25:46 +00:00
},
2024-03-09 09:47:27 +00:00
}
def __init__(self, config: dict, modem, origin: str, destination: str, state_manager, event_manager, socket_command_handler=None):
2024-03-09 09:47:27 +00:00
self.logger = structlog.get_logger(type(self).__name__)
self.config = config
2024-03-27 18:55:19 +00:00
2024-03-09 09:47:27 +00:00
self.frame_factory = data_frame_factory.DataFrameFactory(self.config)
2024-03-16 14:52:14 +00:00
self.socket_command_handler = socket_command_handler
2024-03-16 12:57:48 +00:00
2024-03-09 09:47:27 +00:00
self.destination = destination
self.origin = origin
2024-03-16 15:11:38 +00:00
self.bandwidth = 0
self.state_manager = state_manager
self.event_manager = event_manager
2024-03-09 09:47:27 +00:00
self.modem = modem
2024-03-27 18:55:19 +00:00
self.modem.demodulator.set_decode_mode()
2024-03-09 09:47:27 +00:00
2024-03-19 10:19:13 +00:00
self.p2p_data_rx_queue = Queue()
self.p2p_data_tx_queue = Queue()
2024-03-09 09:47:27 +00:00
self.arq_data_type_handler = ARQDataTypeHandler(self.event_manager, self.state_manager)
2024-03-09 09:47:27 +00:00
self.state = States.NEW
self.session_id = self.generate_id()
self.event_frame_received = threading.Event()
2024-03-27 18:55:19 +00:00
self.RETRIES_CONNECT = 5
self.TIMEOUT_CONNECT = 5
2024-03-09 09:47:27 +00:00
self.TIMEOUT_DATA = 5
self.RETRIES_DATA = 5
self.ENTIRE_CONNECTION_TIMEOUT = 100
self.is_ISS = False # Indicator, if we are ISS or IRS
2024-03-19 10:19:13 +00:00
self.last_data_timestamp= time.time()
self.start_data_processing_worker()
2024-03-19 10:19:13 +00:00
def start_data_processing_worker(self):
"""Starts a worker thread to monitor the transmit data queue and process data."""
def data_processing_worker():
while True:
if time.time() > self.last_data_timestamp + self.ENTIRE_CONNECTION_TIMEOUT and self.state is not States.ARQ_SESSION:
2024-03-19 10:19:13 +00:00
self.disconnect()
return
if not self.p2p_data_tx_queue.empty() and self.state == States.CONNECTED:
self.process_data_queue()
threading.Event().wait(0.1)
# Create and start the worker thread
worker_thread = threading.Thread(target=data_processing_worker, daemon=True)
worker_thread.start()
2024-03-09 09:47:27 +00:00
def generate_id(self):
while True:
random_int = random.randint(1,255)
if random_int not in self.state_manager.p2p_connection_sessions:
2024-03-09 09:47:27 +00:00
return random_int
if len(self.state_manager.p2p_connection_sessions) >= 255:
2024-03-09 09:47:27 +00:00
return False
def set_details(self, snr, frequency_offset):
self.snr = snr
self.frequency_offset = frequency_offset
def log(self, message, isWarning = False):
msg = f"[{type(self).__name__}][id={self.session_id}][state={self.state}][ISS={bool(self.is_ISS)}]: {message}"
logger = self.logger.warn if isWarning else self.logger.info
logger(msg)
def set_state(self, state):
if self.state == state:
self.log(f"{type(self).__name__} state {self.state.name} unchanged.")
else:
self.log(f"{type(self).__name__} state change from {self.state.name} to {state.name}")
self.state = state
def on_frame_received(self, frame):
2024-03-19 10:19:13 +00:00
self.last_data_timestamp = time.time()
2024-03-09 09:47:27 +00:00
self.event_frame_received.set()
self.log(f"Received {frame['frame_type']}")
frame_type = frame['frame_type_int']
if self.state in self.STATE_TRANSITION:
if frame_type in self.STATE_TRANSITION[self.state]:
action_name = self.STATE_TRANSITION[self.state][frame_type]
response = getattr(self, action_name)(frame)
return
self.log(f"Ignoring unknown transition from state {self.state.name} with frame {frame['frame_type']}")
def transmit_frame(self, frame: bytearray, mode='auto'):
self.log("Transmitting frame")
if mode in ['auto']:
mode = self.get_mode_by_speed_level(self.speed_level)
self.modem.transmit(mode, 1, 1, frame)
def transmit_wait_and_retry(self, frame_or_burst, timeout, retries, mode):
while retries > 0:
self.event_frame_received = threading.Event()
if isinstance(frame_or_burst, list): burst = frame_or_burst
else: burst = [frame_or_burst]
for f in burst:
self.transmit_frame(f, mode)
self.event_frame_received.clear()
self.log(f"Waiting {timeout} seconds...")
if self.event_frame_received.wait(timeout):
return
self.log("Timeout!")
retries = retries - 1
2024-03-19 10:19:13 +00:00
#self.connected_iss() # override connection state for simulation purposes
2024-03-09 09:47:27 +00:00
self.session_failed()
def launch_twr(self, frame_or_burst, timeout, retries, mode):
twr = threading.Thread(target = self.transmit_wait_and_retry, args=[frame_or_burst, timeout, retries, mode], daemon=True)
twr.start()
def transmit_and_wait_irs(self, frame, timeout, mode):
self.event_frame_received.clear()
self.transmit_frame(frame, mode)
self.log(f"Waiting {timeout} seconds...")
#if not self.event_frame_received.wait(timeout):
# self.log("Timeout waiting for ISS. Session failed.")
# self.transmission_failed()
def launch_twr_irs(self, frame, timeout, mode):
thread_wait = threading.Thread(target = self.transmit_and_wait_irs,
args = [frame, timeout, mode], daemon=True)
thread_wait.start()
def connect(self):
self.set_state(States.CONNECTING)
self.is_ISS = True
session_open_frame = self.frame_factory.build_p2p_connection_connect(self.origin, self.destination, self.session_id)
self.launch_twr(session_open_frame, self.TIMEOUT_CONNECT, self.RETRIES_CONNECT, mode=FREEDV_MODE.signalling)
return
2024-03-19 12:01:25 +00:00
def connected_iss(self, frame=None):
2024-03-09 09:47:27 +00:00
self.log("CONNECTED ISS...........................")
self.set_state(States.CONNECTED)
self.is_ISS = True
2024-03-19 10:19:13 +00:00
if self.socket_command_handler:
self.socket_command_handler.socket_respond_connected(self.origin, self.destination, self.bandwidth)
2024-03-09 09:47:27 +00:00
def connected_irs(self, frame):
self.log("CONNECTED IRS...........................")
self.state_manager.register_p2p_connection_session(self)
2024-03-09 09:47:27 +00:00
self.set_state(States.CONNECTED)
self.is_ISS = False
2024-03-16 15:11:38 +00:00
self.orign = frame["origin"]
2024-03-19 10:19:13 +00:00
self.destination = frame["destination_crc"]
2024-03-16 15:11:38 +00:00
2024-03-19 10:19:13 +00:00
if self.socket_command_handler:
self.socket_command_handler.socket_respond_connected(self.origin, self.destination, self.bandwidth)
2024-03-16 15:11:38 +00:00
2024-03-09 09:47:27 +00:00
session_open_frame = self.frame_factory.build_p2p_connection_connect_ack(self.destination, self.origin, self.session_id)
self.launch_twr_irs(session_open_frame, self.ENTIRE_CONNECTION_TIMEOUT, mode=FREEDV_MODE.signalling)
def session_failed(self):
self.set_state(States.FAILED)
2024-03-19 10:19:13 +00:00
if self.socket_command_handler:
self.socket_command_handler.socket_respond_disconnected()
2024-03-09 09:47:27 +00:00
def process_data_queue(self, frame=None):
2024-03-19 10:19:13 +00:00
if not self.p2p_data_tx_queue.empty():
2024-03-15 13:25:46 +00:00
print("processing data....")
self.set_state(States.PAYLOAD_SENT)
2024-03-19 10:19:13 +00:00
data = self.p2p_data_tx_queue.get()
2024-03-09 09:47:27 +00:00
sequence_id = random.randint(0,255)
data = data.encode('utf-8')
if len(data) <= 11:
mode = FREEDV_MODE.signalling
elif 11 < len(data) < 32:
mode = FREEDV_MODE.datac4
else:
self.transmit_arq(data)
return
2024-03-09 09:47:27 +00:00
payload = self.frame_factory.build_p2p_connection_payload(mode, self.session_id, sequence_id, data)
self.launch_twr(payload, self.TIMEOUT_DATA, self.RETRIES_DATA,mode=mode)
2024-03-09 09:47:27 +00:00
return
def prepare_data_chunk(self, data, mode):
return data
def received_data(self, frame):
print(frame)
2024-03-19 10:19:13 +00:00
self.p2p_data_rx_queue.put(frame['data'])
2024-03-19 12:19:13 +00:00
2024-03-09 09:47:27 +00:00
ack_data = self.frame_factory.build_p2p_connection_payload_ack(self.session_id, 0)
self.launch_twr_irs(ack_data, self.ENTIRE_CONNECTION_TIMEOUT, mode=FREEDV_MODE.signalling)
def transmit_data_ack(self, frame):
print(frame)
2024-03-15 13:25:46 +00:00
2024-03-19 10:19:13 +00:00
def transmitted_data(self, frame):
print("transmitted data...")
self.set_state(States.CONNECTED)
2024-03-15 13:25:46 +00:00
def disconnect(self):
2024-03-19 10:19:13 +00:00
if self.state not in [States.DISCONNECTING, States.DISCONNECTED]:
self.set_state(States.DISCONNECTING)
disconnect_frame = self.frame_factory.build_p2p_connection_disconnect(self.session_id)
self.launch_twr(disconnect_frame, self.TIMEOUT_CONNECT, self.RETRIES_CONNECT, mode=FREEDV_MODE.signalling)
2024-03-15 13:25:46 +00:00
return
def received_disconnect(self, frame):
self.log("DISCONNECTED...............")
self.set_state(States.DISCONNECTED)
2024-03-19 10:19:13 +00:00
if self.socket_command_handler:
self.socket_command_handler.socket_respond_disconnected()
2024-03-15 13:25:46 +00:00
self.is_ISS = False
disconnect_ack_frame = self.frame_factory.build_p2p_connection_disconnect_ack(self.session_id)
self.launch_twr_irs(disconnect_ack_frame, self.ENTIRE_CONNECTION_TIMEOUT, mode=FREEDV_MODE.signalling)
def received_disconnect_ack(self, frame):
self.log("DISCONNECTED...............")
self.set_state(States.DISCONNECTED)
2024-03-19 10:19:13 +00:00
if self.socket_command_handler:
self.socket_command_handler.socket_respond_disconnected()
def transmit_arq(self, data):
self.set_state(States.ARQ_SESSION)
print("----------------------------------------------------------------")
print(self.destination)
print(self.state_manager.p2p_connection_sessions)
prepared_data, type_byte = self.arq_data_type_handler.prepare(data, ARQ_SESSION_TYPES.p2p_connection)
iss = ARQSessionISS(self.config, self.modem, 'AA1AAA-1', self.state_manager, prepared_data, type_byte)
iss.id = self.session_id
if iss.id:
self.state_manager.register_arq_iss_session(iss)
iss.start()
return iss
def transmitted_arq(self):
self.last_data_timestamp = time.time()
self.set_state(States.CONNECTED)
2024-03-19 21:16:27 +00:00
def received_arq(self, data):
self.last_data_timestamp = time.time()
self.set_state(States.CONNECTED)
self.p2p_data_rx_queue.put(data)
2024-03-19 10:19:13 +00:00