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
pub mod process_messages;
mod websocket_frontend;
use crate::{error::IntifaceError, options::EngineOptions};
use async_trait::async_trait;
use buttplug::server::ButtplugRemoteServerEvent;
use futures::{pin_mut, Stream, StreamExt};
pub use process_messages::{EngineMessage, IntifaceMessage};
use std::sync::Arc;
use tokio::{
select,
sync::{broadcast, Notify},
};
use tokio_util::sync::CancellationToken;
use websocket_frontend::WebsocketFrontend;
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[async_trait]
pub trait Frontend: Sync + Send {
async fn send(&self, msg: EngineMessage);
async fn connect(&self) -> Result<(), IntifaceError>;
fn disconnect_notifier(&self) -> Arc<Notify>;
fn disconnect(&self);
fn event_stream(&self) -> broadcast::Receiver<IntifaceMessage>;
}
pub async fn frontend_external_event_loop(
frontend: Arc<dyn Frontend>,
connection_cancellation_token: Arc<CancellationToken>,
) {
let mut external_receiver = frontend.event_stream();
loop {
select! {
external_message = external_receiver.recv() => {
match external_message {
Ok(message) => match message {
IntifaceMessage::RequestEngineVersion{expected_version:_} => {
info!("Engine version request received from frontend.");
frontend
.send(EngineMessage::EngineVersion{ version: VERSION.to_owned() })
.await;
},
IntifaceMessage::Stop{} => {
connection_cancellation_token.cancel();
info!("Got external stop request");
break;
}
},
Err(_) => {
info!("Frontend sender dropped, assuming connection lost, breaking.");
break;
}
}
},
_ = connection_cancellation_token.cancelled() => {
info!("Connection cancellation token activated, breaking from frontend external event loop.");
break;
}
}
}
}
pub async fn frontend_server_event_loop(
receiver: impl Stream<Item = ButtplugRemoteServerEvent>,
frontend: Arc<dyn Frontend>,
connection_cancellation_token: CancellationToken,
) {
pin_mut!(receiver);
loop {
select! {
maybe_event = receiver.next() => {
match maybe_event {
Some(event) => match event {
ButtplugRemoteServerEvent::ClientConnected(client_name) => {
info!("Client connected: {}", client_name);
frontend.send(EngineMessage::ClientConnected{client_name}).await;
}
ButtplugRemoteServerEvent::ClientDisconnected => {
info!("Client disconnected.");
frontend
.send(EngineMessage::ClientDisconnected{})
.await;
}
ButtplugRemoteServerEvent::DeviceAdded(device_id, device_name, device_address, device_display_name) => {
info!("Device Added: {} - {} - {}", device_id, device_name, device_address);
frontend
.send(EngineMessage::DeviceConnected { name: device_name, index: device_id, address: device_address, display_name: device_display_name })
.await;
}
ButtplugRemoteServerEvent::DeviceRemoved(device_id) => {
info!("Device Removed: {}", device_id);
frontend
.send(EngineMessage::DeviceDisconnected{index: device_id})
.await;
}
},
None => {
info!("Lost connection with main thread, breaking.");
break;
},
}
},
_ = connection_cancellation_token.cancelled() => {
info!("Connection cancellation token activated, breaking from frontend server event loop");
break;
}
}
}
info!("Exiting server event receiver loop");
}
#[derive(Default)]
struct NullFrontend {
notify: Arc<Notify>,
}
#[async_trait]
impl Frontend for NullFrontend {
async fn send(&self, _: EngineMessage) {}
async fn connect(&self) -> Result<(), IntifaceError> {
Ok(())
}
fn disconnect(&self) {
self.notify.notify_waiters();
}
fn disconnect_notifier(&self) -> Arc<Notify> {
self.notify.clone()
}
fn event_stream(&self) -> broadcast::Receiver<IntifaceMessage> {
let (_, receiver) = broadcast::channel(255);
receiver
}
}
pub async fn setup_frontend(
options: &EngineOptions,
cancellation_token: &Arc<CancellationToken>,
) -> Arc<dyn Frontend> {
if let Some(frontend_websocket_port) = options.frontend_websocket_port() {
Arc::new(WebsocketFrontend::new(
frontend_websocket_port,
cancellation_token.clone(),
))
} else {
Arc::new(NullFrontend::default())
}
}