Fail gracefully when out of radio channels.

Fixes https://github.com/Khopa/dcs_liberation/issues/598

(cherry picked from commit 498af28efb2d0d556c4a7ec8433f846a23716649)
This commit is contained in:
Dan Albert 2020-12-16 19:12:18 -08:00
parent 6bb0bdf66e
commit 2288b7f7b2

View File

@ -1,5 +1,6 @@
"""Radio frequency types and allocators.""" """Radio frequency types and allocators."""
import itertools import itertools
import logging
from dataclasses import dataclass from dataclasses import dataclass
from typing import Dict, Iterator, List, Set from typing import Dict, Iterator, List, Set
@ -71,12 +72,9 @@ class Radio:
self.minimum.hertz, self.maximum.hertz, self.step.hertz self.minimum.hertz, self.maximum.hertz, self.step.hertz
)) ))
@property
class OutOfChannelsError(RuntimeError): def last_channel(self) -> RadioFrequency:
"""Raised when all channels usable by this radio have been allocated.""" return RadioFrequency(self.maximum.hertz - self.step.hertz)
def __init__(self, radio: Radio) -> None:
super().__init__(f"No available channels for {radio}")
class ChannelInUseError(RuntimeError): class ChannelInUseError(RuntimeError):
@ -215,7 +213,13 @@ class RadioRegistry:
self.reserve(channel) self.reserve(channel)
return channel return channel
except StopIteration: 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: def alloc_uhf(self) -> RadioFrequency:
"""Allocates a UHF radio channel suitable for inter-flight comms. """Allocates a UHF radio channel suitable for inter-flight comms.