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
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2022 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

use buttplug::{
  core::{
    connector::ButtplugConnector,
    errors::ButtplugError,
    message::{
      self,
      //ButtplugDeviceCommandMessageUnion,
      ButtplugClientMessage,
      ButtplugMessage,
      ButtplugMessageValidator,
      ButtplugServerMessage,
    },
  },
  server::{ButtplugServer, ButtplugServerBuilder},
  util::{
    async_manager, device_configuration::UserConfigDeviceIdentifier,
    stream::convert_broadcast_receiver_to_stream,
  },
};
use futures::{future::Future, pin_mut, select, FutureExt, Stream, StreamExt};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::{broadcast, mpsc, Notify};

// Clone derived here to satisfy tokio broadcast requirements.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ButtplugRemoteServerEvent {
  ClientConnected(String),
  ClientDisconnected,
  DeviceAdded {
    index: u32,
    identifier: UserConfigDeviceIdentifier,
    name: String,
    display_name: Option<String>,
  },
  DeviceRemoved {
    index: u32,
  },
  //DeviceCommand(ButtplugDeviceCommandMessageUnion)
}

#[derive(Error, Debug)]
pub enum ButtplugServerConnectorError {
  #[error("Cannot bring up server for connection: {0}")]
  ConnectorError(String),
}

pub struct ButtplugRemoteServer {
  server: Arc<ButtplugServer>,
  event_sender: broadcast::Sender<ButtplugRemoteServerEvent>,
  disconnect_notifier: Arc<Notify>,
}

async fn run_device_event_stream(
  server: Arc<ButtplugServer>,
  remote_event_sender: broadcast::Sender<ButtplugRemoteServerEvent>,
) {
  let server_receiver = server.event_stream();
  pin_mut!(server_receiver);
  loop {
    match server_receiver.next().await {
      None => {
        info!("Server disconnected via server disappearance, exiting loop.");
        break;
      }
      Some(msg) => {
        if remote_event_sender.receiver_count() > 0 {
          match &msg {
            ButtplugServerMessage::DeviceAdded(da) => {
              if let Some(device_info) = server.device_manager().device_info(da.device_index()) {
                let added_event = ButtplugRemoteServerEvent::DeviceAdded {
                  index: da.device_index(),
                  name: da.device_name().clone(),
                  identifier: device_info.identifier().clone().into(),
                  display_name: device_info.display_name().clone(),
                };
                if remote_event_sender.send(added_event).is_err() {
                  error!("Cannot send event to owner, dropping and assuming local server thread has exited.");
                }
              }
            }
            ButtplugServerMessage::DeviceRemoved(dr) => {
              let removed_event = ButtplugRemoteServerEvent::DeviceRemoved {
                index: dr.device_index(),
              };
              if remote_event_sender.send(removed_event).is_err() {
                error!("Cannot send event to owner, dropping and assuming local server thread has exited.");
              }
            }
            _ => {}
          }
        }
      }
    }
  }
}

async fn run_server<ConnectorType>(
  server: Arc<ButtplugServer>,
  remote_event_sender: broadcast::Sender<ButtplugRemoteServerEvent>,
  connector: ConnectorType,
  mut connector_receiver: mpsc::Receiver<ButtplugClientMessage>,
  disconnect_notifier: Arc<Notify>,
) where
  ConnectorType: ButtplugConnector<ButtplugServerMessage, ButtplugClientMessage> + 'static,
{
  info!("Starting remote server loop");
  let shared_connector = Arc::new(connector);
  let server_receiver = server.event_stream();
  pin_mut!(server_receiver);
  loop {
    select! {
      connector_msg = connector_receiver.recv().fuse() => match connector_msg {
        None => {
          info!("Connector disconnected, exiting loop.");
          if remote_event_sender.receiver_count() > 0 && remote_event_sender.send(ButtplugRemoteServerEvent::ClientDisconnected).is_err() {
            warn!("Cannot update remote about client disconnection");
          }
          break;
        }
        Some(client_message) => {
          trace!("Got message from connector: {:?}", client_message);
          let server_clone = server.clone();
          let connector_clone = shared_connector.clone();
          let remote_event_sender_clone = remote_event_sender.clone();
          async_manager::spawn(async move {
            if let Err(e) = client_message.is_valid() {
              error!("Message not valid: {:?} - Error: {}", client_message, e);
              let mut err_msg = message::Error::from(ButtplugError::from(e));
              err_msg.set_id(client_message.id());
              connector_clone.send(err_msg.into());
              return;
            }
            match server_clone.parse_message(client_message.clone()).await {
              Ok(ret_msg) => {
                if let ButtplugClientMessage::RequestServerInfo(rsi) = client_message {
                  if remote_event_sender_clone.receiver_count() > 0 && remote_event_sender_clone.send(ButtplugRemoteServerEvent::ClientConnected(rsi.client_name().clone())).is_err() {
                    error!("Cannot send event to owner, dropping and assuming local server thread has exited.");
                  }
                }
                if connector_clone.send(ret_msg).await.is_err() {
                  error!("Cannot send reply to server, dropping and assuming remote server thread has exited.");
                }
              },
              Err(err_msg) => {
                if connector_clone.send(err_msg.into()).await.is_err() {
                  error!("Cannot send reply to server, dropping and assuming remote server thread has exited.");
                }
              }
            }
          });
        }
      },
      _ = disconnect_notifier.notified().fuse() => {
        info!("Server disconnected via controller disappearance, exiting loop.");
        break;
      },
      server_msg = server_receiver.next().fuse() => match server_msg {
        None => {
          info!("Server disconnected via server disappearance, exiting loop.");
          break;
        }
        Some(msg) => {
          if remote_event_sender.receiver_count() > 0 {
            match &msg {
              ButtplugServerMessage::DeviceAdded(da) => {
                if let Some(device_info) = server.device_manager().device_info(da.device_index()) {
                  let added_event = ButtplugRemoteServerEvent::DeviceAdded { index: da.device_index(), name: da.device_name().clone(), identifier: device_info.identifier().clone().into(), display_name: device_info.display_name().clone() };
                  if remote_event_sender.send(added_event).is_err() {
                    error!("Cannot send event to owner, dropping and assuming local server thread has exited.");
                  }
                }
              },
              ButtplugServerMessage::DeviceRemoved(dr) => {
                let removed_event = ButtplugRemoteServerEvent::DeviceRemoved { index: dr.device_index() };
                if remote_event_sender.send(removed_event).is_err() {
                  error!("Cannot send event to owner, dropping and assuming local server thread has exited.");
                }
              },
              _ => {}
            }
          }
          let connector_clone = shared_connector.clone();
          if connector_clone.send(msg).await.is_err() {
            error!("Server disappeared, exiting remote server thread.");
          }
        }
      },
    };
  }
  if let Err(err) = server.disconnect().await {
    error!("Error disconnecting server: {:?}", err);
  }
  info!("Exiting remote server loop");
}

impl Default for ButtplugRemoteServer {
  fn default() -> Self {
    Self::new(
      ButtplugServerBuilder::default()
        .finish()
        .expect("Default is infallible"),
    )
  }
}

impl ButtplugRemoteServer {
  pub fn new(server: ButtplugServer) -> Self {
    let (event_sender, _) = broadcast::channel(256);
    let wrapped_server = Arc::new(server);
    // Thanks to the existence of the backdoor server, device updates can happen for the lifetime to
    // the RemoteServer instance, not just during client connect. We need to make sure these are
    // emitted to the frontend.
    tokio::spawn({
      let server = wrapped_server.clone();
      let event_sender = event_sender.clone();
      async move {
        run_device_event_stream(server, event_sender).await;
      }
    });
    Self {
      event_sender,
      server: wrapped_server.clone(),
      disconnect_notifier: Arc::new(Notify::new()),
    }
  }

  pub fn event_stream(&self) -> impl Stream<Item = ButtplugRemoteServerEvent> {
    convert_broadcast_receiver_to_stream(self.event_sender.subscribe())
  }

  pub fn start<ConnectorType>(
    &self,
    mut connector: ConnectorType,
  ) -> impl Future<Output = Result<(), ButtplugServerConnectorError>>
  where
    ConnectorType: ButtplugConnector<ButtplugServerMessage, ButtplugClientMessage> + 'static,
  {
    let server = self.server.clone();
    let event_sender = self.event_sender.clone();
    let disconnect_notifier = self.disconnect_notifier.clone();
    async move {
      let (connector_sender, connector_receiver) = mpsc::channel(256);
      // Due to the connect method requiring a mutable connector, we must connect before starting up
      // our server loop. Anything that needs to happen outside of the client connection session
      // should happen around this. This flow is locked.
      connector
        .connect(connector_sender)
        .await
        .map_err(|e| ButtplugServerConnectorError::ConnectorError(format!("{:?}", e)))?;
      run_server(
        server,
        event_sender,
        connector,
        connector_receiver,
        disconnect_notifier,
      )
      .await;
      Ok(())
    }
  }

  pub async fn disconnect(&self) -> Result<(), ButtplugError> {
    self.disconnect_notifier.notify_waiters();
    Ok(())
  }

  pub async fn shutdown(&self) -> Result<(), ButtplugError> {
    self.server.shutdown().await?;
    Ok(())
  }
}

impl Drop for ButtplugRemoteServer {
  fn drop(&mut self) {
    self.disconnect_notifier.notify_waiters();
  }
}