umicp_core/
lib.rs

1/*!
2# UMICP Rust Bindings
3
4High-performance Rust bindings for the Universal Matrix Inter-Communication Protocol (UMICP).
5
6UMICP provides a universal communication framework for distributed systems, federated learning,
7and real-time applications with built-in matrix operations and type-safe messaging.
8
9## Features
10
11- **๐Ÿ”— Universal Communication**: WebSocket and HTTP/2 transport layers
12- **๐Ÿ“ฆ Type-Safe Envelopes**: Strongly-typed message serialization and validation
13- **โšก High Performance**: SIMD-optimized matrix operations with parallel processing
14- **๐Ÿ”„ Federated Learning**: Built-in support for ML model distribution and aggregation
15- **๐Ÿ›ก๏ธ Security First**: Input validation, authentication, and encrypted communication
16- **๐Ÿ“Š Real-time**: Low-latency communication for IoT and financial applications
17- **๐Ÿงช Well Tested**: Comprehensive test suite with async testing support
18
19## Quick Start
20
21### Basic Envelope Usage
22
23```rust,no_run
24use umicp_core::{Envelope, OperationType};
25
26# fn example() -> Result<(), Box<dyn std::error::Error>> {
27// Create a UMICP envelope
28let envelope = Envelope::builder()
29    .from("client-001")
30    .to("server-001")
31    .operation(OperationType::Data)
32    .message_id("msg-12345")
33    .capability_str("content-type", "application/json")
34    .build()?;
35
36// Serialize for transmission
37let serialized = envelope.serialize()?;
38
39// Deserialize received data
40let received = Envelope::deserialize(&serialized)?;
41# Ok(())
42# }
43```
44
45### WebSocket Transport
46
47```rust,no_run
48use umicp_core::{WebSocketTransport, Envelope, OperationType};
49
50# fn example() -> Result<(), Box<dyn std::error::Error>> {
51// Server setup (requires websocket feature)
52#[cfg(feature = "websocket")]
53let server = WebSocketTransport::new_server("127.0.0.1:8080")?;
54
55// Client setup (requires websocket feature)
56#[cfg(feature = "websocket")]
57let client = WebSocketTransport::new_client("ws://127.0.0.1:8080")?;
58
59// Message handling
60#[cfg(feature = "websocket")]
61server.set_message_handler(|envelope, conn_id| async move {
62    println!("Received: {:?}", envelope.capabilities());
63    // Echo response
64    let response = Envelope::builder()
65        .from("server")
66        .to(envelope.from())
67        .operation(OperationType::Ack)
68        .message_id(&format!("response-{}", uuid::Uuid::new_v4()))
69        .build()?;
70
71    server.send(response, &conn_id).await?;
72    Ok(())
73});
74
75// Send message
76let message = Envelope::builder()
77    .from("client")
78    .to("server")
79    .operation(OperationType::Data)
80    .message_id("hello-001")
81    .capability_str("message", "Hello UMICP!")
82    .build()?;
83
84#[cfg(feature = "websocket")]
85client.send(message, "")?;
86# Ok(())
87# }
88```
89
90### Matrix Operations
91
92```rust,no_run
93use umicp_core::Matrix;
94
95# fn example() -> Result<(), Box<dyn std::error::Error>> {
96// Create matrix instance
97let mut matrix = Matrix::new();
98
99// Vector operations
100let vector1 = vec![1.0f32, 2.0, 3.0, 4.0];
101let vector2 = vec![5.0f32, 6.0, 7.0, 8.0];
102let mut result = vec![0.0f32; 4];
103
104// Vector addition
105matrix.vector_add(&vector1, &vector2, &mut result)?;
106println!("Addition result: {:?}", result); // [6.0, 8.0, 10.0, 12.0]
107
108// Dot product
109let dot_product = matrix.dot_product(&vector1, &vector2)?;
110println!("Dot product: {:?}", dot_product); // 70.0
111
112// Matrix multiplication (2x2 * 2x2 = 2x2)
113let matrix_a = vec![1.0, 2.0, 3.0, 4.0]; // 2x2 matrix
114let matrix_b = vec![5.0, 6.0, 7.0, 8.0]; // 2x2 matrix
115let mut matrix_result = vec![0.0; 4]; // 2x2 result
116
117matrix.multiply(&matrix_a, &matrix_b, &mut matrix_result, 2, 2, 2)?;
118println!("Matrix multiplication: {:?}", matrix_result);
119# Ok(())
120# }
121```
122*/
123
124pub mod envelope;
125pub mod matrix;
126pub mod types;
127pub mod error;
128pub mod utils;
129pub mod events;
130pub mod discovery;
131pub mod tool_discovery;
132pub mod pool;
133pub mod load_balancer;
134
135// Transport module - new modular implementation
136#[cfg(any(feature = "websocket", feature = "http2"))]
137pub mod transport;
138
139// Legacy transport placeholders
140#[cfg(not(any(feature = "websocket", feature = "http2")))]
141#[path = "transport_legacy.rs"]
142pub mod transport;
143
144// Peer module - multiplexed peer architecture
145#[cfg(feature = "websocket")]
146pub mod peer;
147
148pub use envelope::Envelope;
149pub use matrix::Matrix;
150pub use types::*;
151pub use error::*;
152pub use events::{EventEmitter, EventType, EventData, EventListener};
153pub use discovery::{ServiceDiscovery, ServiceInfo};
154pub use tool_discovery::{DiscoverableService, OperationSchema, ServerInfo};
155pub use load_balancer::{LoadBalancer, LoadBalancingStrategy, BackendEndpoint, LoadBalancerStats, ConnectionGuard};
156
157#[cfg(feature = "websocket")]
158pub use pool::{ConnectionPool, PoolConfig, PoolStats, PooledConnection, PoolConnectionState};
159
160// Re-export transport types based on features
161#[cfg(feature = "websocket")]
162pub use transport::{WebSocketClient, WebSocketServer};
163
164#[cfg(feature = "http2")]
165pub use transport::{HttpClient, HttpServer};
166
167#[cfg(not(any(feature = "websocket", feature = "http2")))]
168pub use transport::{WebSocketTransport, Http2Transport};
169
170// Re-export peer types
171#[cfg(feature = "websocket")]
172pub use peer::{
173    ConnectionState, HandshakeMessage, HandshakeProtocol, HandshakeType,
174    PeerConnection, PeerInfo, PeerType, WebSocketPeer, WebSocketPeerConfig,
175};
176
177/// Version information
178pub const VERSION: &str = env!("CARGO_PKG_VERSION");
179pub const UMICP_VERSION: &str = "1.0";
180
181/// Utility functions and constants
182pub mod umicp {
183    use super::*;
184
185    /// Create a new envelope with default settings
186    pub fn create_envelope() -> envelope::EnvelopeBuilder {
187        Envelope::builder()
188    }
189
190    /// Create a new matrix instance
191    pub fn create_matrix() -> Matrix {
192        Matrix::new()
193    }
194
195    /// Check if WebSocket transport is available
196    pub fn has_websocket_transport() -> bool {
197        cfg!(feature = "websocket")
198    }
199
200    /// Check if HTTP/2 transport is available
201    pub fn has_http2_transport() -> bool {
202        cfg!(feature = "http2")
203    }
204
205    /// Get version information
206    pub fn version() -> &'static str {
207        VERSION
208    }
209
210    /// Get UMICP protocol version
211    pub fn umicp_version() -> &'static str {
212        UMICP_VERSION
213    }
214}