import asyncio
from ri import (
RiProtocolManager,
RiProtocolType,
RiProtocolConfig,
RiProtocolStatus,
RiProtocolStats,
RiConnectionState,
RiConnectionStats,
RiProtocolHealth,
RiFrame,
RiFrameHeader,
RiFrameType,
RiConnectionInfo,
RiMessageFlags,
RiSecurityLevel,
)
from ri.protocol import (
RiFrameParser,
RiFrameBuilder,
)
async def main():
config = RiProtocolConfig()
config.protocol_type = RiProtocolType.CUSTOM
config.version = "1.0.0"
config.enable_compression = True
config.enable_encryption = True
config.security_level = RiSecurityLevel.HIGH
config.max_frame_size = 65536
config.keepalive_interval_seconds = 30
print("Creating protocol manager...")
protocol_manager = RiProtocolManager(config)
print("\nCreating frames...")
frame_builder = RiFrameBuilder()
header = RiFrameHeader()
header.frame_type = RiFrameType.DATA
header.version = 1
header.sequence_number = 1
header.flags = RiMessageFlags.ACK_REQUIRED
header.payload_length = 100
data_frame = RiFrame()
data_frame.header = header
data_frame.payload = b'{"message": "Hello, Ri Protocol!"}'
data_frame.checksum = 12345
print(f"Created data frame:")
print(f" Type: {data_frame.header.frame_type}")
print(f" Sequence: {data_frame.header.sequence_number}")
print(f" Payload length: {len(data_frame.payload)}")
handshake_header = RiFrameHeader()
handshake_header.frame_type = RiFrameType.HANDSHAKE
handshake_header.sequence_number = 0
handshake_frame = RiFrame()
handshake_frame.header = handshake_header
handshake_frame.payload = b'{"version": "1.0.0", "capabilities": ["compression", "encryption"]}'
heartbeat_header = RiFrameHeader()
heartbeat_header.frame_type = RiFrameType.HEARTBEAT
heartbeat_header.sequence_number = 999
heartbeat_frame = RiFrame()
heartbeat_frame.header = heartbeat_header
heartbeat_frame.payload = b''
print(f"\nCreated {3} frames (DATA, HANDSHAKE, HEARTBEAT)")
print("\nParsing frames...")
frame_parser = RiFrameParser()
serialized = frame_builder.build(data_frame)
print(f"Serialized frame: {len(serialized)} bytes")
parsed_frame = frame_parser.parse(serialized)
if parsed_frame:
print(f"Parsed frame type: {parsed_frame.header.frame_type}")
print(f"Parsed payload: {parsed_frame.payload}")
print("\nCreating connection info...")
conn_info = RiConnectionInfo()
conn_info.connection_id = "conn_001"
conn_info.remote_address = "192.168.1.100:8080"
conn_info.local_address = "0.0.0.0:8080"
conn_info.state = RiConnectionState.ESTABLISHED
conn_info.security_level = RiSecurityLevel.HIGH
conn_info.established_at = 0
conn_info.last_activity_at = 1000
print(f"Connection info:")
print(f" ID: {conn_info.connection_id}")
print(f" Remote: {conn_info.remote_address}")
print(f" State: {conn_info.state}")
print(f" Security: {conn_info.security_level}")
conn_stats = RiConnectionStats()
conn_stats.connection_id = "conn_001"
conn_stats.frames_sent = 100
conn_stats.frames_received = 95
conn_stats.bytes_sent = 1024000
conn_stats.bytes_received = 980000
conn_stats.errors_count = 0
conn_stats.latency_ms = 25.5
print(f"\nConnection statistics:")
print(f" Frames sent: {conn_stats.frames_sent}")
print(f" Frames received: {conn_stats.frames_received}")
print(f" Bytes sent: {conn_stats.bytes_sent}")
print(f" Bytes received: {conn_stats.bytes_received}")
print(f" Latency: {conn_stats.latency_ms}ms")
print("\nProtocol statistics:")
protocol_stats = RiProtocolStats()
protocol_stats.total_connections = 10
protocol_stats.active_connections = 5
protocol_stats.total_frames_sent = 1000
protocol_stats.total_frames_received = 950
protocol_stats.total_bytes_sent = 10240000
protocol_stats.total_bytes_received = 9500000
print(f" Total connections: {protocol_stats.total_connections}")
print(f" Active connections: {protocol_stats.active_connections}")
print(f" Total frames sent: {protocol_stats.total_frames_sent}")
print(f" Total frames received: {protocol_stats.total_frames_received}")
print("\nProtocol health check:")
health = RiProtocolHealth()
health.is_healthy = True
health.error_rate = 0.01
health.average_latency_ms = 25.0
health.throughput_mbps = 100.0
print(f" Healthy: {health.is_healthy}")
print(f" Error rate: {health.error_rate:.2%}")
print(f" Average latency: {health.average_latency_ms}ms")
print(f" Throughput: {health.throughput_mbps} Mbps")
print("\nProtocol status:")
status = RiProtocolStatus()
status.is_running = True
status.uptime_seconds = 3600
status.protocol_type = RiProtocolType.CUSTOM
status.version = "1.0.0"
print(f" Running: {status.is_running}")
print(f" Uptime: {status.uptime_seconds} seconds")
print(f" Protocol: {status.protocol_type}")
print(f" Version: {status.version}")
print("\nProtocol operations completed successfully!")
if __name__ == "__main__":
asyncio.run(main())