Skip to content

Commit fa21bb5

Browse files
committed
example: fifo communication
1 parent aa81b34 commit fa21bb5

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "bottles-core"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde = { version = "1.0.217", features = ["derive"] }
8+
serde_json = "1.0.134"
9+
thiserror = "2.0.9"
10+
tracing = "0.1.41"
11+
unix-named-pipe = "0.2.0"
12+
13+
[dev-dependencies]
14+
bottles-core = { path = "." }
15+
tracing-subscriber = "0.3.19"

examples/cli.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use bottles_core::{Error, Payload, WineBridge, WineBridgeAction};
2+
use tracing::Level;
3+
4+
pub fn main() -> Result<(), Error> {
5+
let wine_prefix = std::env::args()
6+
.nth(1)
7+
.expect("Wine prefix path is required");
8+
tracing_subscriber::fmt()
9+
.with_max_level(Level::DEBUG)
10+
.init();
11+
let mut bridge = WineBridge::new(wine_prefix);
12+
bridge.connect()?;
13+
bridge.send(Payload {
14+
action: WineBridgeAction::Run {
15+
executable: "explorer.exe".to_string(),
16+
},
17+
})
18+
}

src/error.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern crate unix_named_pipe;
2+
3+
use thiserror::Error;
4+
5+
#[derive(Error, Debug)]
6+
pub enum Error {
7+
#[error("I/O: {0}")]
8+
Io(#[from] std::io::Error),
9+
#[error("Serde: {0}")]
10+
Serde(#[from] serde_json::Error),
11+
#[error("Failed to create named pipe")]
12+
NamedPipeError,
13+
#[error("Failed to connect to bridge, be sure to call .connect() first")]
14+
ConnectToBridgeError,
15+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod error;
2+
mod wine;
3+
pub use error::Error;
4+
pub use wine::bridge::{Payload, WineBridge, WineBridgeAction};

src/wine/bridge.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::{fs::File, io::Write};
2+
3+
use serde::{Deserialize, Serialize};
4+
use serde_json as json;
5+
6+
use tracing::info;
7+
8+
use crate::Error;
9+
10+
#[derive(Debug)]
11+
pub struct WineBridge {
12+
pub wine_prefix: String,
13+
pub pipe: Option<File>,
14+
}
15+
16+
#[derive(Debug, Clone, Serialize, Deserialize)]
17+
pub struct Payload {
18+
pub action: WineBridgeAction,
19+
}
20+
21+
#[derive(Debug, Clone, Serialize, Deserialize)]
22+
pub enum WineBridgeAction {
23+
Run { executable: String },
24+
}
25+
26+
impl WineBridge {
27+
pub fn new(wine_prefix: impl ToString) -> Self {
28+
Self {
29+
wine_prefix: wine_prefix.to_string(),
30+
pipe: None,
31+
}
32+
}
33+
34+
pub fn connect(&mut self) -> Result<(), Error> {
35+
info!("Connecting to wine prefix: {}", self.wine_prefix);
36+
let pipe = unix_named_pipe::open_write(&self.wine_prefix)?;
37+
self.pipe = Some(pipe);
38+
Ok(())
39+
}
40+
41+
pub fn send(&mut self, message: Payload) -> Result<(), Error> {
42+
info!("Sending message to wine prefix: {:#?}", message);
43+
let Some(pipe) = self.pipe.as_mut() else {
44+
return Err(Error::ConnectToBridgeError);
45+
};
46+
let payload = json::to_string(&message)? + "\n";
47+
pipe.write_all(payload.as_bytes())?;
48+
Ok(())
49+
}
50+
}

src/wine/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod bridge;

0 commit comments

Comments
 (0)