grove/Protocol/
mod.rs

1//! Protocol Module
2//!
3//! Handles protocol communication with Mountain and other services.
4//! Implements the Spine protocol for extension host communication.
5
6pub mod SpineConnection;
7
8// Re-exports for convenience - use module prefix to avoid E0255 conflicts
9// Note: SpineConnection must be accessed via SpineConnection::SpineConnectionImpl
10
11/// Protocol version
12pub const SPINE_PROTOCOL_VERSION:&str = "1.0.0";
13
14/// Default Mountain gRPC endpoint
15pub const DEFAULT_MOUNTAIN_ENDPOINT:&str = "127.0.0.1:50050";
16
17/// Default connection timeout in milliseconds
18pub const DEFAULT_CONNECTION_TIMEOUT_MS:u64 = 5000;
19
20/// Default heartbeat interval in seconds
21pub const DEFAULT_HEARTBEAT_INTERVAL_SEC:u64 = 30;
22
23/// Default message buffer size
24pub const DEFAULT_MESSAGE_BUFFER_SIZE:usize = 8192;
25
26/// Protocol configuration
27#[derive(Debug, Clone)]
28pub struct ProtocolConfig {
29	/// Protocol version
30	pub version:String,
31	/// Mountain endpoint
32	pub mountain_endpoint:String,
33	/// Connection timeout
34	pub connection_timeout_ms:u64,
35	/// Heartbeat interval
36	pub heartbeat_interval_sec:u64,
37	/// Message buffer size
38	pub message_buffer_size:usize,
39	/// Enable TLS
40	pub enable_tls:bool,
41	/// Enable compression
42	pub enable_compression:bool,
43}
44
45impl ProtocolConfig {
46	/// Create a new protocol configuration
47	pub fn new() -> Self {
48		Self {
49			version:SPINE_PROTOCOL_VERSION.to_string(),
50			mountain_endpoint:DEFAULT_MOUNTAIN_ENDPOINT.to_string(),
51			connection_timeout_ms:DEFAULT_CONNECTION_TIMEOUT_MS,
52			heartbeat_interval_sec:DEFAULT_HEARTBEAT_INTERVAL_SEC,
53			message_buffer_size:DEFAULT_MESSAGE_BUFFER_SIZE,
54			enable_tls:false,
55			enable_compression:false,
56		}
57	}
58
59	/// Set mountain endpoint
60	pub fn with_mountain_endpoint(mut self, endpoint:String) -> Self {
61		self.mountain_endpoint = endpoint;
62		self
63	}
64
65	/// Set connection timeout
66	pub fn with_connection_timeout(mut self, timeout_ms:u64) -> Self {
67		self.connection_timeout_ms = timeout_ms;
68		self
69	}
70
71	/// Set heartbeat interval
72	pub fn with_heartbeat_interval(mut self, interval_sec:u64) -> Self {
73		self.heartbeat_interval_sec = interval_sec;
74		self
75	}
76
77	/// Enable or disable TLS
78	pub fn with_tls(mut self, enable:bool) -> Self {
79		self.enable_tls = enable;
80		self
81	}
82
83	/// Enable or disable compression
84	pub fn with_compression(mut self, enable:bool) -> Self {
85		self.enable_compression = enable;
86		self
87	}
88}
89
90impl Default for ProtocolConfig {
91	fn default() -> Self { Self::new() }
92}
93
94/// Message types for Spine protocol
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
96pub enum MessageType {
97	/// Heartbeat message
98	Heartbeat = 0,
99	/// Registration message
100	Register = 1,
101	/// Unregistration message
102	Unregister = 2,
103	/// Event message
104	Event = 3,
105	/// Request message
106	Request = 4,
107	/// Response message
108	Response = 5,
109	/// Error message
110	Error = 6,
111}
112
113impl MessageType {
114	/// Convert to u32
115	pub fn as_u32(self) -> u32 { self as u32 }
116
117	/// Convert from u32
118	pub fn from_u32(value:u32) -> Option<Self> {
119		match value {
120			0 => Some(Self::Heartbeat),
121			1 => Some(Self::Register),
122			2 => Some(Self::Unregister),
123			3 => Some(Self::Event),
124			4 => Some(Self::Request),
125			5 => Some(Self::Response),
126			6 => Some(Self::Error),
127			_ => None,
128		}
129	}
130}
131
132/// Protocol error types
133#[derive(Debug, thiserror::Error)]
134pub enum ProtocolError {
135/// Connection error
136#[error("Connection error: {0}")]
137ConnectionError(String),
138
139/// Serialization error
140#[error("Serialization error: {0}")]
141SerializationError(String),
142
143/// Deserialization error
144#[error("Deserialization error: {0}")]
145DeserializationError(String),
146
147/// Invalid message error
148#[error("Invalid message: {0}")]
149InvalidMessage(String),
150
151/// Timeout error
152#[error("Timeout error")]
153Timeout,
154
155/// Protocol error
156#[error("Protocol error: {0}")]
157ProtocolError(String),
158}
159
160#[cfg(test)]
161mod tests {
162	use super::*;
163
164	#[test]
165	fn test_protocol_config_default() {
166		let config = ProtocolConfig::default();
167		assert_eq!(config.mountain_endpoint, DEFAULT_MOUNTAIN_ENDPOINT);
168		assert_eq!(config.connection_timeout_ms, DEFAULT_CONNECTION_TIMEOUT_MS);
169	}
170
171	#[test]
172	fn test_protocol_config_builder() {
173		let config = ProtocolConfig::default()
174			.with_mountain_endpoint("127.0.0.1:60000".to_string())
175			.with_connection_timeout(10000)
176			.with_heartbeat_interval(60);
177
178		assert_eq!(config.mountain_endpoint, "127.0.0.1:60000");
179		assert_eq!(config.connection_timeout_ms, 10000);
180		assert_eq!(config.heartbeat_interval_sec, 60);
181	}
182
183	#[test]
184	fn test_message_type_conversion() {
185		let msg_type = MessageType::Heartbeat;
186		assert_eq!(msg_type.as_u32(), 0);
187
188		let converted = MessageType::from_u32(0);
189		assert_eq!(converted, Some(MessageType::Heartbeat));
190
191		let invalid = MessageType::from_u32(999);
192		assert_eq!(invalid, None);
193	}
194}