| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- """
- Trixy Network System
- This module provides the complete network protocol implementation for the Trixy
- voice assistant system. It includes:
- - Custom Trixy Protocol implementation (binary streaming, not HTTP/REST)
- - Command socket management (Port 2101)
- - Audio streaming sockets (Ports 2102, 2103, 2104)
- - Multi-client support with thread safety
- - Serializable command classes for network communication
- - Server and client socket handling
- - Integration with event system and application container
- The network system implements the specifications from CLAUDE.md:
- - Binary streaming protocol with magic number "TRXI"
- - Message structure with version, datetime, option flags, MD5 checksum
- - Support for serialized Python classes and dictionaries
- - Hard-coded performance commands (TRXINOOP, TRXIPING, etc.)
- - Multi-socket architecture for different data types
- Usage:
- from trixy_core.network import NetworkManager, TrixyProtocol, TrixyMessage
-
- # Create network manager
- network_manager = NetworkManager(application_container)
-
- # Start server mode
- network_manager.start_server(command_port=2101)
-
- # Or start client mode
- network_manager.connect_to_server("192.168.1.100", 2101)
-
- # Send commands
- from trixy_core.network.cmd import SatelliteHelloCommand
- cmd = SatelliteHelloCommand(room="kitchen", alias="main")
- network_manager.send_command(cmd)
- """
- from .protocol import (
- # Core protocol classes
- TrixyProtocol,
- TrixyMessage,
- MessageOptions,
- MessageFlags,
- ProtocolVersion,
-
- # Protocol exceptions
- ProtocolError,
- InvalidMessageError,
- ChecksumMismatchError,
- UnsupportedVersionError,
-
- # Serialization utilities
- serialize_object,
- deserialize_object,
- calculate_checksum,
- compress_data,
- decompress_data,
- )
- from .network_manager import (
- # Main network manager
- NetworkManager,
- NetworkManagerError,
-
- # Connection management
- ConnectionManager,
- ConnectionState,
- ConnectionInfo,
-
- # Audio streaming
- AudioStreamManager,
- AudioStreamType,
- AudioStreamConfig,
- )
- from .server_socket import (
- # Server-side networking
- TrixyServer,
- ServerSocketHandler,
- ClientConnection,
-
- # Server exceptions
- ServerError,
- PortInUseError,
- ClientConnectionError,
- )
- from .client_socket import (
- # Client-side networking
- TrixyClient,
- ClientSocketHandler,
- ConnectionAttempt,
-
- # Client exceptions
- ClientError,
- ConnectionFailedError,
- AuthenticationError,
- )
- # Command classes - imported from cmd package
- from .cmd import (
- # Base command class
- TrixyCommand,
- CommandResponse,
-
- # Hard-coded commands
- NoOpCommand,
- PingCommand,
- PongCommand,
- PrintCommand,
- HelloCommand,
-
- # Satellite commands
- SatelliteHelloCommand,
- SatelliteRegistrationCommand,
- SatelliteConnectionCommand,
- SatelliteDisconnectCommand,
-
- # Wakeword commands
- WakewordDetectedCommand,
- WakewordConfigCommand,
-
- # Audio commands
- AudioStreamStartCommand,
- AudioStreamStopCommand,
- AudioDataCommand,
-
- # Configuration commands
- ConfigUpdateCommand,
- ConfigRequestCommand,
-
- # Status commands
- StatusRequestCommand,
- HealthCheckCommand,
- )
- # Network constants
- DEFAULT_COMMAND_PORT = 2101
- DEFAULT_AUDIO_INPUT_PORT = 2102
- DEFAULT_AUDIO_OUTPUT_PORT = 2103
- DEFAULT_MUSIC_PORT = 2104
- PROTOCOL_MAGIC = b"TRXI"
- PROTOCOL_VERSION_MAJOR = 1
- PROTOCOL_VERSION_MINOR = 0
- PROTOCOL_VERSION_REVISION = 0
- # Hard-coded command strings for performance optimization
- HARDCODED_COMMANDS = {
- "TRXINOOP": "no_op",
- "TRXIPING": "ping",
- "TRXIPONG": "pong",
- "TRXIPRNT": "print",
- "TRXYHELO": "hello",
- }
- # Version information
- __version__ = "1.0.0"
- __author__ = "Trixy Development Team"
- # Public API
- __all__ = [
- # Core protocol
- "TrixyProtocol",
- "TrixyMessage",
- "MessageOptions",
- "MessageFlags",
- "ProtocolVersion",
-
- # Network management
- "NetworkManager",
- "NetworkManagerError",
- "ConnectionManager",
- "ConnectionState",
- "ConnectionInfo",
-
- # Audio streaming
- "AudioStreamManager",
- "AudioStreamType",
- "AudioStreamConfig",
-
- # Server components
- "TrixyServer",
- "ServerSocketHandler",
- "ClientConnection",
-
- # Client components
- "TrixyClient",
- "ClientSocketHandler",
- "ConnectionAttempt",
-
- # Commands
- "TrixyCommand",
- "CommandResponse",
- "NoOpCommand",
- "PingCommand",
- "PongCommand",
- "PrintCommand",
- "HelloCommand",
- "SatelliteHelloCommand",
- "SatelliteRegistrationCommand",
- "SatelliteConnectionCommand",
- "SatelliteDisconnectCommand",
- "WakewordDetectedCommand",
- "WakewordConfigCommand",
- "AudioStreamStartCommand",
- "AudioStreamStopCommand",
- "AudioDataCommand",
- "ConfigUpdateCommand",
- "ConfigRequestCommand",
- "StatusRequestCommand",
- "HealthCheckCommand",
-
- # Protocol utilities
- "serialize_object",
- "deserialize_object",
- "calculate_checksum",
- "compress_data",
- "decompress_data",
-
- # Exceptions
- "ProtocolError",
- "InvalidMessageError",
- "ChecksumMismatchError",
- "UnsupportedVersionError",
- "ServerError",
- "PortInUseError",
- "ClientConnectionError",
- "ClientError",
- "ConnectionFailedError",
- "AuthenticationError",
-
- # Constants
- "DEFAULT_COMMAND_PORT",
- "DEFAULT_AUDIO_INPUT_PORT",
- "DEFAULT_AUDIO_OUTPUT_PORT",
- "DEFAULT_MUSIC_PORT",
- "PROTOCOL_MAGIC",
- "PROTOCOL_VERSION_MAJOR",
- "PROTOCOL_VERSION_MINOR",
- "PROTOCOL_VERSION_REVISION",
- "HARDCODED_COMMANDS",
- ]
- def pprint(message: str) -> None:
- """
- Network module logging function that adapts based on mode.
- Uses the same pattern as specified in CLAUDE.md.
- """
- # For now use simple printing - will integrate with proper logging
- print(f"[NETWORK] {message}")
- def create_default_network_manager(application_container):
- """
- Create a NetworkManager with default configuration.
-
- Args:
- application_container: The main application container
-
- Returns:
- NetworkManager: Configured network manager instance
- """
- return NetworkManager(application_container)
- def get_protocol_version() -> ProtocolVersion:
- """
- Get the current protocol version.
-
- Returns:
- ProtocolVersion: Current protocol version
- """
- return ProtocolVersion(
- major=PROTOCOL_VERSION_MAJOR,
- minor=PROTOCOL_VERSION_MINOR,
- revision=PROTOCOL_VERSION_REVISION
- )
- def validate_port(port: int) -> bool:
- """
- Validate if a port number is valid for the Trixy protocol.
-
- Args:
- port: Port number to validate
-
- Returns:
- bool: True if port is valid
- """
- return 1024 <= port <= 65535
- def get_default_ports() -> dict:
- """
- Get the default port configuration.
-
- Returns:
- dict: Default port mappings
- """
- return {
- "command": DEFAULT_COMMAND_PORT,
- "audio_input": DEFAULT_AUDIO_INPUT_PORT,
- "audio_output": DEFAULT_AUDIO_OUTPUT_PORT,
- "music": DEFAULT_MUSIC_PORT,
- }
- # Initialize logging
- def _init_logging():
- """Initialize logging for the network module."""
- # This will be enhanced when integrated with the main logging system
- pass
- # Initialize the module
- _init_logging()
- pprint("Network module initialized")
|