from source import packets
from source.behaviors import roles
from source.behaviors.events import base


class KeyEvent(base.BaseEvent):
    """
    Event reacting to a machine sending us their secret key
    """

    def handle(self, packet: packets.KeyPacket, address: tuple):
        # check if we are a slave
        if not isinstance(self.manager.role, roles.SlaveRole):
            return

        # TODO(Faraphel): check if this come from our server ?

        # use the secret key for further symmetric communication
        # TODO(Faraphel): should not be an attribute of the manager communication system, create a dictionary with the secret key of the servers ?
        self.manager.communication.secret_key = packet.secret_key
