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
use std::process::Command;
use interactive_process::InteractiveProcess;
use jamsocket::{
JamsocketContext, JamsocketService, JamsocketServiceFactory, MessageFromProcess,
MessagePayload, MessageToProcess,
};
pub struct StdioProcessServiceFactory {
command: String,
}
impl StdioProcessServiceFactory {
#[must_use]
pub fn new(command: &str) -> Self {
StdioProcessServiceFactory {
command: command.to_string(),
}
}
}
impl<T: JamsocketContext> JamsocketServiceFactory<T> for StdioProcessServiceFactory {
type Service = StdioProcessService;
type Error = std::io::Error;
fn build(&self, _room_id: &str, context: T) -> Result<Self::Service, Self::Error> {
let process = InteractiveProcess::new(Command::new(&self.command), move |line| {
let line = line.expect("Error reading line from stdin.");
let message: MessageFromProcess =
serde_json::from_str(&line).expect("Couldn't parse message from process.");
match message {
MessageFromProcess::Message {
recipient,
message: MessagePayload::Bytes(message),
} => {
context.send_binary(recipient, &message);
}
MessageFromProcess::Message {
recipient,
message: MessagePayload::Text(message),
} => {
context.send_message(recipient, &message);
}
}
})?;
Ok(StdioProcessService { process })
}
}
pub struct StdioProcessService {
process: InteractiveProcess,
}
impl StdioProcessService {
fn send_to_process(&mut self, message: &MessageToProcess) {
self.process
.send(&serde_json::to_string(&message).expect("Could not jsonify message."))
.expect("Could not send message to process.");
}
}
impl JamsocketService for StdioProcessService {
fn connect(&mut self, client: jamsocket::ClientId) {
self.send_to_process(&MessageToProcess::Connect { client });
}
fn disconnect(&mut self, client: jamsocket::ClientId) {
self.send_to_process(&MessageToProcess::Disconnect { client });
}
fn message(&mut self, sender: jamsocket::ClientId, message: &str) {
self.send_to_process(&MessageToProcess::Message {
client: sender,
message: MessagePayload::Text(message.to_string()),
});
}
fn binary(&mut self, sender: jamsocket::ClientId, message: &[u8]) {
self.send_to_process(&MessageToProcess::Message {
client: sender,
message: MessagePayload::Bytes(message.to_vec()),
});
}
fn timer(&mut self) {
self.send_to_process(&MessageToProcess::Timer);
}
}