M2-PT-DRP/source/managers/PeerManager.py
study-faraphel 9480339b89 improved error handling
instead of having a component crashing, it will issue a warning with the error
2025-02-02 23:31:06 +01:00

36 lines
1,013 B
Python

import time
import traceback
import warnings
from source import packets, structures
from source.managers import Manager
from source.utils.crypto.type import CipherType
from source.utils.dict import TimestampedDict
class PeerManager:
"""
Manage the peers network
"""
def __init__(self, manager: "Manager"):
self.manager = manager
# set of addresses associated to their peer
self.peers: TimestampedDict[tuple, structures.Peer] = TimestampedDict()
def handle(self) -> None:
# send requests to discover new peers
packet = packets.DiscoveryPacket()
self.manager.communication.broadcast(packet, CipherType.PLAIN)
def loop(self) -> None:
while True:
try:
self.handle()
except Exception: # NOQA
warnings.warn(traceback.format_exc())
# TODO(Faraphel): adjust sleep time ? as much seconds as there are peer to avoid flooding the network ?
time.sleep(1)