use crate::buffer::PayloadStorage;
use crate::envelope::{Did, EnvelopeHeader, MessageId, PayloadType, Priority, SignedEnvelope};
use crate::runtime::transport::interface::DEFAULT_PAYLOAD_CAPACITY;
use std::sync::Mutex;
pub mod certificate {
pub const DEFAULT_CN: &str = "omnimesh.local";
pub const DEFAULT_ORG: &str = "OMNI-MESH";
pub const DEFAULT_COUNTRY: &str = "US";
pub const ALT_NAMES: &[&str] = &["omnimesh.local", "localhost", "127.0.0.1"];
pub const DEFAULT_VALIDITY_DAYS: u32 = 365;
}
pub mod errors {
pub fn runtime_creation_failed(err: &dyn std::fmt::Display) -> String {
format!("Failed to create tokio runtime: {}", err)
}
pub fn cert_parse_failed(err: &dyn std::fmt::Display) -> String {
format!("Failed to parse certificate PEM: {}", err)
}
pub fn key_parse_failed(err: &dyn std::fmt::Display) -> String {
format!("Failed to parse private key PEM: {}", err)
}
pub fn cert_generation_failed(err: &dyn std::fmt::Display) -> String {
format!("Failed to generate certificate: {}", err)
}
pub fn cert_file_read_failed(path: &str, err: &dyn std::fmt::Display) -> String {
format!("Failed to read certificate file '{}': {}", path, err)
}
pub fn key_file_read_failed(path: &str, err: &dyn std::fmt::Display) -> String {
format!("Failed to read private key file '{}': {}", path, err)
}
pub fn lock_acquire_failed(err: &dyn std::fmt::Display) -> String {
format!("Failed to acquire lock: {}", err)
}
pub fn connect_failed(addr: &str, err: &dyn std::fmt::Display) -> String {
format!("Failed to connect to {}: {}", addr, err)
}
pub const INVALID_CERTIFICATES: &str = "Generated certificates are invalid";
pub fn compression_write_failed(err: &dyn std::fmt::Display) -> String {
format!("Compression write failed: {}", err)
}
pub fn compression_finish_failed(err: &dyn std::fmt::Display) -> String {
format!("Compression finish failed: {}", err)
}
pub fn decompression_failed(err: &dyn std::fmt::Display) -> String {
format!("Decompression failed: {}", err)
}
}
pub struct TransportUtils;
impl TransportUtils {
pub fn sample_envelope() -> SignedEnvelope<DEFAULT_PAYLOAD_CAPACITY> {
let mut payload = PayloadStorage::<DEFAULT_PAYLOAD_CAPACITY>::new();
let _ = payload.push_bytes(b"hello omnimesh");
let header = EnvelopeHeader::new(
1, MessageId::new([0x01; 16]), Did::new([0x02; 32]), Did::new([0x03; 32]), 1, 1_700_000_000_000_000, Priority::Normal,
PayloadType::Raw,
);
SignedEnvelope::new(header, payload, [1u8; 64]) }
pub fn create_runtime() -> Result<tokio::runtime::Runtime, String> {
tokio::runtime::Runtime::new().map_err(|e| errors::runtime_creation_failed(&e))
}
pub fn acquire_lock<'a, T>(
mutex: &'a Mutex<T>,
) -> Result<std::sync::MutexGuard<'a, T>, String> {
mutex.lock().map_err(|e| errors::lock_acquire_failed(&e))
}
}
pub mod logging {
pub fn layer_initialized(layer_name: &str, transport_kind: &str) {
println!("Initializing {} layer: {}", layer_name, transport_kind);
}
pub fn tcp_listener_started(addr: impl std::fmt::Display) {
println!("TCP listener started on {}", addr);
}
pub fn tcp_connection_received(addr: impl std::fmt::Display) {
println!("TCP connection from {}", addr);
}
pub fn tcp_envelope_sent(addr: impl std::fmt::Display, active: usize, max: usize) {
println!(
"TCP transport: envelope sent to {} (pool: {}/{})",
addr, active, max
);
}
pub fn quic_endpoint_initialized(addr: impl std::fmt::Display) {
println!("QUIC endpoint initialized on {} with certificates", addr);
}
pub fn mock_envelope_sent() {
println!("Mock transport: envelope sent (simulated)");
}
pub fn error_queue_failed() {
eprintln!("Failed to queue received envelope");
}
pub fn error_deserialization(err: impl std::fmt::Debug) {
eprintln!("TCP deserialization failed: {:?}", err);
}
pub fn error_read(err: impl std::fmt::Display) {
eprintln!("TCP read failed: {}", err);
}
pub fn error_accept(err: impl std::fmt::Display) {
eprintln!("TCP accept failed: {}", err);
}
pub fn error_listener_bind(err: impl std::fmt::Display) {
eprintln!("TCP listener bind failed: {}", err);
}
pub fn error_write(err: impl std::fmt::Display) {
eprintln!("TCP write failed: {}", err);
}
pub fn error_connect(addr: impl std::fmt::Display, err: impl std::fmt::Display) {
eprintln!("TCP connect failed to {}: {}", addr, err);
}
pub fn error_quic_endpoint(err: impl std::fmt::Display) {
eprintln!("QUIC endpoint error: {}", err);
}
}