from abc import ABC, abstractmethod
from typing import Callable, Any

class IMeshTransport(ABC):
    """
    Abstract interface for bidirectional message passing between 
    a Mesh Node and a Mesh Hub.
    """
    @abstractmethod
    def handshake(self) -> bool:
        """Performs initial authentication/registration handshake."""
        pass

    @abstractmethod
    def connect(self):
        """Initializes the connection/stream."""
        pass

    @abstractmethod
    def set_listener(self, listener: 'IMeshListener'):
        """Sets the listener for inbound messages."""
        pass

    @abstractmethod
    def send(self, message: Any, priority: int = 1):
        """Sends a message to the remote peer with optional priority."""
        pass

    @abstractmethod
    def close(self):
        """Closes the connection and cleans up resources."""
        pass

    @abstractmethod
    def is_connected(self) -> bool:
        """Returns True if the transport is active."""
        pass

class IMeshListener(ABC):
    """
    Interface for handling inbound messages from a MeshTransport.
    """
    @abstractmethod
    def on_message(self, message: Any):
        """Called when a new message arrives."""
        pass

    @abstractmethod
    def on_error(self, error: Exception):
        """Called when a transport-level error occurs."""
        pass

    @abstractmethod
    def on_close(self):
        """Called when the transport is closed."""
        pass
