ipckit 0.1.6

A cross-platform IPC (Inter-Process Communication) library for Rust and Python
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! File-based IPC Channel
//!
//! A simple IPC mechanism using local files for communication.
//! This is useful for frontend-backend communication where:
//! - Backend (Python) writes to a file, Frontend reads it
//! - Frontend writes to another file, Backend reads it
//!
//! ## Protocol
//!
//! Each message file contains:
//! - Line 1: Message ID (UUID)
//! - Line 2: Timestamp (Unix epoch in milliseconds)
//! - Line 3: Message type (request/response/event)
//! - Line 4+: JSON payload
//!
//! ## File Structure
//!
//! ```text
//! {channel_dir}/
//! ├── backend_to_frontend.json   # Backend writes, Frontend reads
//! ├── frontend_to_backend.json   # Frontend writes, Backend reads
//! ├── backend_to_frontend.lock   # Lock file for atomic writes
//! ├── frontend_to_backend.lock   # Lock file for atomic writes
//! └── .channel_info              # Channel metadata
//! ```

use crate::error::{IpcError, Result};
use serde::{Deserialize, Serialize};
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Message types for file-based IPC
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum MessageType {
    Request,
    Response,
    Event,
}

/// A message in the file-based IPC protocol
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileMessage {
    /// Unique message ID
    pub id: String,
    /// Timestamp in milliseconds since Unix epoch
    pub timestamp: u64,
    /// Message type
    #[serde(rename = "type")]
    pub msg_type: MessageType,
    /// For responses, the ID of the request being responded to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_to: Option<String>,
    /// Method name (for requests)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,
    /// Message payload (JSON value)
    pub payload: serde_json::Value,
    /// Error message (for error responses)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

impl FileMessage {
    /// Create a new request message
    pub fn request(method: &str, payload: serde_json::Value) -> Self {
        Self {
            id: uuid_v4(),
            timestamp: current_timestamp_ms(),
            msg_type: MessageType::Request,
            reply_to: None,
            method: Some(method.to_string()),
            payload,
            error: None,
        }
    }

    /// Create a response message
    pub fn response(request_id: &str, payload: serde_json::Value) -> Self {
        Self {
            id: uuid_v4(),
            timestamp: current_timestamp_ms(),
            msg_type: MessageType::Response,
            reply_to: Some(request_id.to_string()),
            method: None,
            payload,
            error: None,
        }
    }

    /// Create an error response
    pub fn error_response(request_id: &str, error: &str) -> Self {
        Self {
            id: uuid_v4(),
            timestamp: current_timestamp_ms(),
            msg_type: MessageType::Response,
            reply_to: Some(request_id.to_string()),
            method: None,
            payload: serde_json::Value::Null,
            error: Some(error.to_string()),
        }
    }

    /// Create an event message (no response expected)
    pub fn event(name: &str, payload: serde_json::Value) -> Self {
        Self {
            id: uuid_v4(),
            timestamp: current_timestamp_ms(),
            msg_type: MessageType::Event,
            reply_to: None,
            method: Some(name.to_string()),
            payload,
            error: None,
        }
    }
}

/// File-based IPC channel for backend (Python/Rust) side
pub struct FileChannel {
    /// Channel directory
    dir: PathBuf,
    /// File for outgoing messages (backend -> frontend)
    outbox_path: PathBuf,
    /// File for incoming messages (frontend -> backend)
    inbox_path: PathBuf,
    /// Last processed message ID from inbox
    last_inbox_id: Option<String>,
    /// Last processed message timestamp
    last_inbox_timestamp: u64,
}

impl FileChannel {
    /// Create or open a file channel
    ///
    /// # Arguments
    /// * `dir` - Directory for channel files (will be created if not exists)
    /// * `is_backend` - True for backend side, false for frontend side
    pub fn new<P: AsRef<Path>>(dir: P, is_backend: bool) -> Result<Self> {
        let dir = dir.as_ref().to_path_buf();

        // Create directory if not exists
        fs::create_dir_all(&dir)?;

        // Determine file paths based on role
        let (outbox_path, inbox_path) = if is_backend {
            (
                dir.join("backend_to_frontend.json"),
                dir.join("frontend_to_backend.json"),
            )
        } else {
            (
                dir.join("frontend_to_backend.json"),
                dir.join("backend_to_frontend.json"),
            )
        };

        // Create channel info file
        let info_path = dir.join(".channel_info");
        if !info_path.exists() {
            let info = serde_json::json!({
                "version": "1.0",
                "created": current_timestamp_ms(),
                "protocol": "file-ipc"
            });
            fs::write(&info_path, serde_json::to_string_pretty(&info).unwrap())?;
        }

        // Initialize empty message files if not exist
        for path in [&outbox_path, &inbox_path] {
            if !path.exists() {
                fs::write(path, "[]")?;
            }
        }

        Ok(Self {
            dir,
            outbox_path,
            inbox_path,
            last_inbox_id: None,
            last_inbox_timestamp: 0,
        })
    }

    /// Create a backend-side channel
    pub fn backend<P: AsRef<Path>>(dir: P) -> Result<Self> {
        Self::new(dir, true)
    }

    /// Create a frontend-side channel
    pub fn frontend<P: AsRef<Path>>(dir: P) -> Result<Self> {
        Self::new(dir, false)
    }

    /// Get the channel directory
    pub fn dir(&self) -> &Path {
        &self.dir
    }

    /// Send a message (write to outbox)
    pub fn send(&self, message: &FileMessage) -> Result<()> {
        let lock_path = self.outbox_path.with_extension("lock");
        let _lock = FileLock::acquire(&lock_path)?;

        // Read existing messages
        let mut messages = self.read_message_file(&self.outbox_path)?;

        // Add new message
        messages.push(message.clone());

        // Keep only recent messages (last 100)
        if messages.len() > 100 {
            let skip_count = messages.len() - 100;
            messages = messages.into_iter().skip(skip_count).collect();
        }

        // Write back atomically
        let temp_path = self.outbox_path.with_extension("tmp");
        let content = serde_json::to_string_pretty(&messages)
            .map_err(|e| IpcError::serialization(e.to_string()))?;
        fs::write(&temp_path, &content)?;
        fs::rename(&temp_path, &self.outbox_path)?;

        Ok(())
    }

    /// Send a request and return the message ID
    pub fn send_request(&self, method: &str, params: serde_json::Value) -> Result<String> {
        let msg = FileMessage::request(method, params);
        let id = msg.id.clone();
        self.send(&msg)?;
        Ok(id)
    }

    /// Send a response to a request
    pub fn send_response(&self, request_id: &str, result: serde_json::Value) -> Result<()> {
        let msg = FileMessage::response(request_id, result);
        self.send(&msg)
    }

    /// Send an error response
    pub fn send_error(&self, request_id: &str, error: &str) -> Result<()> {
        let msg = FileMessage::error_response(request_id, error);
        self.send(&msg)
    }

    /// Send an event
    pub fn send_event(&self, name: &str, payload: serde_json::Value) -> Result<()> {
        let msg = FileMessage::event(name, payload);
        self.send(&msg)
    }

    /// Receive new messages from inbox
    pub fn recv(&mut self) -> Result<Vec<FileMessage>> {
        let messages = self.read_message_file(&self.inbox_path)?;

        // Filter to only new messages
        let new_messages: Vec<FileMessage> = messages
            .into_iter()
            .filter(|m| {
                m.timestamp > self.last_inbox_timestamp
                    || (m.timestamp == self.last_inbox_timestamp
                        && self.last_inbox_id.as_ref() != Some(&m.id))
            })
            .collect();

        // Update last processed
        if let Some(last) = new_messages.last() {
            self.last_inbox_timestamp = last.timestamp;
            self.last_inbox_id = Some(last.id.clone());
        }

        Ok(new_messages)
    }

    /// Receive a single new message (non-blocking)
    pub fn recv_one(&mut self) -> Result<Option<FileMessage>> {
        let messages = self.recv()?;
        Ok(messages.into_iter().next())
    }

    /// Wait for a response to a specific request
    pub fn wait_response(&mut self, request_id: &str, timeout: Duration) -> Result<FileMessage> {
        let start = std::time::Instant::now();
        let poll_interval = Duration::from_millis(50);

        loop {
            let messages = self.recv()?;

            for msg in messages {
                if msg.msg_type == MessageType::Response
                    && msg.reply_to.as_ref() == Some(&request_id.to_string())
                {
                    return Ok(msg);
                }
            }

            if start.elapsed() > timeout {
                return Err(IpcError::Timeout);
            }

            std::thread::sleep(poll_interval);
        }
    }

    /// Poll for new messages with a callback
    pub fn poll<F>(&mut self, interval: Duration, mut callback: F) -> Result<()>
    where
        F: FnMut(FileMessage) -> bool,
    {
        loop {
            let messages = self.recv()?;

            for msg in messages {
                if !callback(msg) {
                    return Ok(());
                }
            }

            std::thread::sleep(interval);
        }
    }

    /// Clear all messages in both inbox and outbox
    pub fn clear(&self) -> Result<()> {
        fs::write(&self.outbox_path, "[]")?;
        fs::write(&self.inbox_path, "[]")?;
        Ok(())
    }

    /// Read messages from a file
    fn read_message_file(&self, path: &Path) -> Result<Vec<FileMessage>> {
        if !path.exists() {
            return Ok(Vec::new());
        }

        let content = fs::read_to_string(path)?;
        if content.trim().is_empty() || content.trim() == "[]" {
            return Ok(Vec::new());
        }

        serde_json::from_str(&content).map_err(|e| IpcError::deserialization(e.to_string()))
    }
}

/// Simple file-based lock for atomic operations
struct FileLock {
    path: PathBuf,
}

impl FileLock {
    fn acquire(path: &Path) -> Result<Self> {
        let path = path.to_path_buf();
        let max_attempts = 50;
        let wait_time = Duration::from_millis(10);

        for _ in 0..max_attempts {
            match OpenOptions::new().write(true).create_new(true).open(&path) {
                Ok(mut file) => {
                    // Write PID to lock file
                    let _ = writeln!(file, "{}", std::process::id());
                    return Ok(Self { path });
                }
                Err(_) => {
                    std::thread::sleep(wait_time);
                }
            }
        }

        // Force acquire if lock is stale (older than 5 seconds)
        if let Ok(metadata) = fs::metadata(&path) {
            if let Ok(modified) = metadata.modified() {
                if modified.elapsed().unwrap_or_default() > Duration::from_secs(5) {
                    let _ = fs::remove_file(&path);
                    return Self::acquire(&path);
                }
            }
        }

        Err(IpcError::Timeout)
    }
}

impl Drop for FileLock {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

/// Generate a simple UUID v4
fn uuid_v4() -> String {
    use std::collections::hash_map::RandomState;
    use std::hash::{BuildHasher, Hasher};

    let state = RandomState::new();
    let mut hasher = state.build_hasher();
    hasher.write_u64(current_timestamp_ms());
    hasher.write_usize(std::process::id() as usize);
    let h1 = hasher.finish();

    let state2 = RandomState::new();
    let mut hasher2 = state2.build_hasher();
    hasher2.write_u64(h1);
    let h2 = hasher2.finish();

    format!(
        "{:08x}-{:04x}-4{:03x}-{:04x}-{:012x}",
        (h1 >> 32) as u32,
        (h1 >> 16) as u16,
        h1 as u16 & 0x0FFF,
        (h2 >> 48) as u16 & 0x3FFF | 0x8000,
        h2 & 0xFFFFFFFFFFFF
    )
}

/// Get current timestamp in milliseconds
fn current_timestamp_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as u64
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use tempfile::tempdir;

    #[test]
    fn test_file_channel_basic() {
        let dir = tempdir().unwrap();

        let mut backend = FileChannel::backend(dir.path()).unwrap();
        let mut frontend = FileChannel::frontend(dir.path()).unwrap();

        // Backend sends request
        let msg = FileMessage::request("ping", serde_json::json!({}));
        backend.send(&msg).unwrap();

        // Frontend receives
        let received = frontend.recv().unwrap();
        assert_eq!(received.len(), 1);
        assert_eq!(received[0].method.as_ref().unwrap(), "ping");

        // Frontend sends response
        frontend
            .send_response(&received[0].id, serde_json::json!({"pong": true}))
            .unwrap();

        // Backend receives response
        let responses = backend.recv().unwrap();
        assert_eq!(responses.len(), 1);
        assert_eq!(responses[0].reply_to.as_ref().unwrap(), &received[0].id);
    }

    #[test]
    fn test_file_channel_concurrent() {
        let dir = tempdir().unwrap();
        let dir_path = dir.path().to_path_buf();

        let handle = thread::spawn({
            let dir_path = dir_path.clone();
            move || {
                let mut frontend = FileChannel::frontend(&dir_path).unwrap();
                thread::sleep(Duration::from_millis(100));

                // Wait for request
                loop {
                    let msgs = frontend.recv().unwrap();
                    for msg in msgs {
                        if msg.method.as_ref() == Some(&"test".to_string()) {
                            frontend
                                .send_response(&msg.id, serde_json::json!({"ok": true}))
                                .unwrap();
                            return;
                        }
                    }
                    thread::sleep(Duration::from_millis(50));
                }
            }
        });

        let mut backend = FileChannel::backend(&dir_path).unwrap();
        let request_id = backend
            .send_request("test", serde_json::json!({"value": 42}))
            .unwrap();

        let response = backend
            .wait_response(&request_id, Duration::from_secs(5))
            .unwrap();
        assert!(response.payload.get("ok").unwrap().as_bool().unwrap());

        handle.join().unwrap();
    }
}