trace2e_core/transport/
mod.rs

1//! # Transport Module
2//!
3//! This module provides the networking and communication layer for the trace2e framework,
4//! enabling distributed traceability operations between middleware instances across
5//! different nodes in a networked environment.
6//!
7//! ## Overview
8//!
9//! The transport layer abstracts the underlying communication mechanisms used for
10//! machine-to-machine (M2M) interactions in the trace2e system. It supports multiple
11//! transport implementations including gRPC for production environments, loopback and
12//! no-op for testing purposes.
13//!
14//! ## Transport Implementations
15//!
16//! - **gRPC Transport**: Production-ready implementation using Protocol Buffers and gRPC
17//! - **Loopback Transport**: Local testing implementation that routes calls internally
18//!   between mock middleware instances
19//! - **No-op Transport**: Stub implementation that performs no actual communication
20//!
21//! ## Remote IP Evaluation
22//!
23//! The module provides utilities for extracting remote IP addresses from M2M requests,
24//! enabling proper routing and connection management for distributed operations.
25//!
26//! ## Architecture
27//!
28//! The transport layer operates between the traceability middleware instances,
29//! facilitating the exchange of compliance policies, provenance information,
30//! and authorization decisions across network boundaries.
31
32use crate::traceability::{api::types::M2mRequest, error::TraceabilityError};
33
34pub mod grpc;
35pub mod loopback;
36pub mod nop;
37
38/// Extracts the remote IP address from an M2M request for routing purposes.
39///
40/// This function analyzes the request type and destination resource to determine
41/// the appropriate remote IP address that should handle the request. For stream
42/// resources, it extracts the IP from the local socket address. For source
43/// compliance requests, it uses the provided authority IP.
44///
45/// # Arguments
46///
47/// * `req` - The M2M request to analyze
48///
49/// # Returns
50///
51/// The remote IP address as a string, or an error if the IP cannot be determined.
52///
53/// # Errors
54///
55/// Returns `TransportFailedToEvaluateRemote` if:
56/// - The destination is not a stream resource
57/// - The socket address cannot be parsed
58/// - The request type doesn't contain routing information
59pub fn eval_remote_ip(req: M2mRequest) -> Result<String, TraceabilityError> {
60    match req {
61        M2mRequest::GetDestinationPolicy(destination)
62        | M2mRequest::UpdateProvenance { destination, .. } => Ok(destination.node_id().clone()),
63        M2mRequest::BroadcastDeletion(_) => Ok("*".to_string()),
64        _ => Err(TraceabilityError::TransportFailedToEvaluateRemote),
65    }
66}