From 2288b7f7b2484574c81ab5e2321c659707a38c3f Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 16 Dec 2020 19:12:18 -0800 Subject: [PATCH] Fail gracefully when out of radio channels. Fixes https://github.com/Khopa/dcs_liberation/issues/598 (cherry picked from commit 498af28efb2d0d556c4a7ec8433f846a23716649) --- gen/radios.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/gen/radios.py b/gen/radios.py index 87b8661f..e6d413cc 100644 --- a/gen/radios.py +++ b/gen/radios.py @@ -1,5 +1,6 @@ """Radio frequency types and allocators.""" import itertools +import logging from dataclasses import dataclass from typing import Dict, Iterator, List, Set @@ -71,12 +72,9 @@ class Radio: self.minimum.hertz, self.maximum.hertz, self.step.hertz )) - -class OutOfChannelsError(RuntimeError): - """Raised when all channels usable by this radio have been allocated.""" - - def __init__(self, radio: Radio) -> None: - super().__init__(f"No available channels for {radio}") + @property + def last_channel(self) -> RadioFrequency: + return RadioFrequency(self.maximum.hertz - self.step.hertz) class ChannelInUseError(RuntimeError): @@ -215,7 +213,13 @@ class RadioRegistry: self.reserve(channel) return channel except StopIteration: - raise OutOfChannelsError(radio) + # In the event of too many channel users, fail gracefully by reusing + # the last channel. + # https://github.com/Khopa/dcs_liberation/issues/598 + channel = radio.last_channel + logging.warning( + f"No more free channels for {radio.name}. Reusing {channel}.") + return channel def alloc_uhf(self) -> RadioFrequency: """Allocates a UHF radio channel suitable for inter-flight comms.