1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! Subscription lifecycle management
//!
//! Manages active subscriptions, handles connection lifecycle, and forwards
//! events from AimDB instances as MCP notifications.
use crate::error::{McpError, McpResult};
use crate::protocol::Notification;
use aimdb_client::AimxClient;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex, RwLock};
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};
/// Subscription metadata
#[derive(Debug, Clone)]
pub struct SubscriptionInfo {
/// Unique subscription ID
pub subscription_id: String,
/// Socket path to AimDB instance
pub socket_path: PathBuf,
/// Record name being monitored
pub record_name: String,
/// AimX subscription ID (from server)
pub aimx_subscription_id: String,
/// Creation timestamp (Unix millis)
pub created_at: i64,
/// Sequence counter for notifications
pub sequence: Arc<std::sync::atomic::AtomicU64>,
/// Maximum number of samples before auto-unsubscribe (None = unlimited)
pub max_samples: Option<usize>,
}
/// Manager for handling AimDB subscriptions and forwarding events
pub struct SubscriptionManager {
/// Active subscriptions
subscriptions: Arc<RwLock<HashMap<String, SubscriptionInfo>>>,
/// Notification sender (sends to MCP client)
notification_tx: mpsc::UnboundedSender<Notification>,
/// Background task handles
tasks: Arc<Mutex<HashMap<String, JoinHandle<()>>>>,
}
impl SubscriptionManager {
/// Create a new subscription manager
pub fn new(notification_tx: mpsc::UnboundedSender<Notification>) -> Self {
Self {
subscriptions: Arc::new(RwLock::new(HashMap::new())),
notification_tx,
tasks: Arc::new(Mutex::new(HashMap::new())),
}
}
/// Subscribe to a record
///
/// Creates a new subscription and spawns a background task to receive events.
pub async fn subscribe(
&self,
socket_path: PathBuf,
record_name: String,
queue_size: usize,
max_samples: Option<usize>,
) -> McpResult<String> {
// Generate unique subscription ID
let subscription_id = format!("sub_{}", chrono::Utc::now().timestamp_millis());
debug!(
"📡 Creating subscription {} for {} at {}",
subscription_id,
record_name,
socket_path.display()
);
// Connect to AimDB instance
let mut client = AimxClient::connect(&socket_path).await.map_err(|e| {
McpError::Internal(format!(
"Failed to connect to {}: {}",
socket_path.display(),
e
))
})?;
// Subscribe to record
let aimx_subscription_id =
client
.subscribe(&record_name, queue_size)
.await
.map_err(|e| {
McpError::Internal(format!("Failed to subscribe to {}: {}", record_name, e))
})?;
debug!(
"✅ AimX subscription created: {} -> {}",
subscription_id, aimx_subscription_id
);
// Create subscription info
let info = SubscriptionInfo {
subscription_id: subscription_id.clone(),
socket_path: socket_path.clone(),
record_name: record_name.clone(),
aimx_subscription_id: aimx_subscription_id.clone(),
created_at: chrono::Utc::now().timestamp_millis(),
sequence: Arc::new(std::sync::atomic::AtomicU64::new(0)),
max_samples,
};
// Store subscription
self.subscriptions
.write()
.await
.insert(subscription_id.clone(), info.clone());
// Spawn background task to receive events
let handle = self.spawn_event_listener(
subscription_id.clone(),
client,
record_name.clone(),
socket_path.clone(),
);
// Store task handle
self.tasks
.lock()
.await
.insert(subscription_id.clone(), handle);
info!(
"🎉 Subscription {} created for {} at {}",
subscription_id,
record_name,
socket_path.display()
);
Ok(subscription_id)
}
/// Unsubscribe from a record
pub async fn unsubscribe(&self, subscription_id: &str) -> McpResult<()> {
debug!("🔕 Unsubscribing: {}", subscription_id);
// Remove subscription info
let info = self
.subscriptions
.write()
.await
.remove(subscription_id)
.ok_or_else(|| {
McpError::InvalidParams(format!("Unknown subscription: {}", subscription_id))
})?;
// Cancel background task
if let Some(handle) = self.tasks.lock().await.remove(subscription_id) {
handle.abort();
debug!("✅ Cancelled event listener task for {}", subscription_id);
}
info!(
"✅ Unsubscribed {} from {} at {}",
subscription_id,
info.record_name,
info.socket_path.display()
);
Ok(())
}
/// List all active subscriptions
pub async fn list_subscriptions(&self) -> Vec<SubscriptionInfo> {
self.subscriptions.read().await.values().cloned().collect()
}
/// Get subscription info
pub async fn get_subscription(&self, subscription_id: &str) -> Option<SubscriptionInfo> {
self.subscriptions
.read()
.await
.get(subscription_id)
.cloned()
}
/// Cleanup all subscriptions (called on shutdown)
pub async fn cleanup(&self) {
info!("🧹 Cleaning up all subscriptions");
// Abort all background tasks
let mut tasks = self.tasks.lock().await;
for (id, handle) in tasks.drain() {
handle.abort();
debug!("Aborted task for subscription {}", id);
}
// Clear subscriptions
self.subscriptions.write().await.clear();
info!("✅ All subscriptions cleaned up");
}
/// Spawn a background task to listen for events
fn spawn_event_listener(
&self,
subscription_id: String,
mut client: AimxClient,
record_name: String,
socket_path: PathBuf,
) -> JoinHandle<()> {
let notification_tx = self.notification_tx.clone();
let subscriptions = self.subscriptions.clone();
let tasks = self.tasks.clone();
tokio::spawn(async move {
debug!(
"🎧 Event listener started for {} ({})",
subscription_id, record_name
);
loop {
// Check if subscription still exists (may have been cancelled)
if !subscriptions.read().await.contains_key(&subscription_id) {
debug!(
"Subscription {} no longer exists, stopping listener",
subscription_id
);
break;
}
// Receive next event
match client.receive_event().await {
Ok(event) => {
debug!(
"📬 Received event for {}: sequence={}",
subscription_id, event.sequence
);
// Get subscription info to access sequence counter and max_samples
let (sequence, max_samples) =
if let Some(info) = subscriptions.read().await.get(&subscription_id) {
let seq = info
.sequence
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
(seq, info.max_samples)
} else {
(0, None) // Subscription was removed
};
// Forward as MCP notification
let notification = Notification::record_changed(
&subscription_id,
&record_name,
event.data,
sequence,
);
if let Err(e) = notification_tx.send(notification) {
error!("Failed to send notification for {}: {}", subscription_id, e);
break;
}
// Check if sample limit reached (sequence is 0-based, so check >= max_samples)
if let Some(max) = max_samples {
let samples_received = sequence + 1;
if samples_received >= max as u64 {
info!(
"🎯 Sample limit reached for {}: {}/{} samples",
subscription_id, samples_received, max
);
// Send completion notification
let completion_notification = Notification::subscription_completed(
&subscription_id,
samples_received as usize,
);
if let Err(e) = notification_tx.send(completion_notification) {
error!(
"Failed to send completion notification for {}: {}",
subscription_id, e
);
}
// Remove subscription (auto-unsubscribe)
subscriptions.write().await.remove(&subscription_id);
tasks.lock().await.remove(&subscription_id);
info!(
"✅ Auto-unsubscribed {} after {} samples",
subscription_id, samples_received
);
break;
}
}
}
Err(e) => {
warn!(
"⚠️ Error receiving event for {} at {}: {}",
subscription_id,
socket_path.display(),
e
);
// Send error notification
let error_notification = Notification::subscription_error(
&subscription_id,
&format!("Event receive error: {}", e),
);
if let Err(e) = notification_tx.send(error_notification) {
error!("Failed to send error notification: {}", e);
}
// Remove subscription on error
subscriptions.write().await.remove(&subscription_id);
tasks.lock().await.remove(&subscription_id);
break;
}
}
}
debug!(
"🔚 Event listener stopped for {} ({})",
subscription_id, record_name
);
})
}
}
impl Drop for SubscriptionManager {
fn drop(&mut self) {
// Note: We can't use async in Drop, so subscriptions will be
// cleaned up when tasks are aborted naturally
debug!("SubscriptionManager dropped");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_subscription_manager_creation() {
let (tx, _rx) = mpsc::unbounded_channel();
let manager = SubscriptionManager::new(tx);
let subs = manager.list_subscriptions().await;
assert_eq!(subs.len(), 0);
}
#[tokio::test]
async fn test_subscription_info_clone() {
let info = SubscriptionInfo {
subscription_id: "sub_123".to_string(),
socket_path: PathBuf::from("/tmp/test.sock"),
record_name: "test::Record".to_string(),
aimx_subscription_id: "aimx_sub_456".to_string(),
created_at: 1234567890,
sequence: Arc::new(std::sync::atomic::AtomicU64::new(0)),
max_samples: Some(100),
};
let cloned = info.clone();
assert_eq!(cloned.subscription_id, info.subscription_id);
assert_eq!(cloned.record_name, info.record_name);
}
#[tokio::test]
async fn test_unsubscribe_nonexistent() {
let (tx, _rx) = mpsc::unbounded_channel();
let manager = SubscriptionManager::new(tx);
let result = manager.unsubscribe("nonexistent").await;
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), McpError::InvalidParams(_)));
}
}