from . import base
from source import packets
from .. import roles
from ...utils.crypto.type import CipherType


class DiscoveryEvent(base.BaseEvent):
    """
    Event reacting to a machine trying to discover the network.
    """

    def handle(self, packet: packets.DiscoveryPacket, address: tuple):
        # create a peer packet containing our important information
        peerPacket = packets.PeerPacket(
            self.manager.communication.public_key,
            isinstance(self.manager.role, roles.MasterRole)
        )
        # send our information back
        # don't use any encryption to share the RSA key for further communication
        self.manager.communication.send(peerPacket, CipherType.PLAIN, address)
