Skip to content

Commit ac0e5c5

Browse files
committed
chore: Actually remove DeviceAdded/Removed from v4
Just send DeviceList every time we have an update and let the client deal with it. Fixes #544
1 parent f53d98f commit ac0e5c5

File tree

16 files changed

+272
-438
lines changed

16 files changed

+272
-438
lines changed

buttplug-schema/schema/buttplug-schema.json

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -509,37 +509,12 @@
509509
"Endpoint",
510510
"RawCommand"
511511
]
512-
},
513-
"DeviceAdded": {
514-
"type": "object",
515-
"description": "List of all available devices known to the system.",
516-
"properties": {
517-
"Id": { "$ref": "#/components/SystemId" },
518-
"DeviceName": { "$ref": "#/components/DeviceName" },
519-
"DeviceIndex": { "$ref": "#/components/DeviceIndex" },
520-
"DeviceDisplayName": { "type": "string" },
521-
"DeviceMessageTimingGap": { "type": "integer" },
522-
"DeviceFeatures": {
523-
"type": "array",
524-
"items": {
525-
"$ref": "#/components/DeviceFeatureV4"
526-
}
527-
}
528-
},
529-
"additionalProperties": false,
530-
"required": [
531-
"Id",
532-
"DeviceName",
533-
"DeviceIndex",
534-
"DeviceMessageTimingGap",
535-
"DeviceFeatures"
536-
]
537-
},
512+
},
538513
"DeviceList": {
539514
"type": "object",
540515
"description": "List of all available devices known to the system.",
541516
"properties": {
542-
"Id": { "$ref": "#/components/SystemId" },
517+
"Id": { "$ref": "#/components/ServerId" },
543518
"Devices": {
544519
"description": "Array of device ids and names.",
545520
"type": "array",
@@ -559,7 +534,6 @@
559534
},
560535
"additionalProperties": false,
561536
"required": [
562-
"Id",
563537
"DeviceName",
564538
"DeviceIndex",
565539
"DeviceMessageTimingGap",
@@ -1674,9 +1648,7 @@
16741648
"type": "object",
16751649
"description": "All messages valid in Buttplug Spec v4",
16761650
"properties": {
1677-
"DeviceAdded": { "$ref": "#/messages/SpecV4Messages/DeviceAdded" },
16781651
"DeviceList": { "$ref": "#/messages/SpecV4Messages/DeviceList" },
1679-
"DeviceRemoved": { "$ref": "#/messages/SpecV0Messages/DeviceRemoved" },
16801652
"Error": { "$ref": "#/messages/SpecV0Messages/Error" },
16811653
"Ok": { "$ref": "#/messages/SpecV0Messages/Ok" },
16821654
"Ping": { "$ref": "#/messages/SpecV0Messages/Ping" },

crates/buttplug_client/src/client_event_loop.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::{
1616
};
1717
use buttplug_core::{
1818
connector::{ButtplugConnector, ButtplugConnectorStateShared},
19-
errors::{ButtplugDeviceError, ButtplugError},
19+
errors::ButtplugError,
2020
message::{
2121
ButtplugClientMessageV4,
2222
ButtplugDeviceMessage,
@@ -214,30 +214,22 @@ where
214214
trace!("Message future not found, assuming server event.");
215215
info!("{:?}", msg);
216216
match msg {
217-
ButtplugServerMessageV4::DeviceAdded(dev) => {
218-
trace!("Device added, updating map and sending to client");
219-
// We already have this device. Emit an error to let the client know the
220-
// server is being weird.
221-
if self.device_map.get(&dev.device_index()).is_some() {
222-
self.send_client_event(ButtplugClientEvent::Error(
223-
ButtplugDeviceError::DeviceConnectionError(
224-
"Device already exists in client. Server may be in a weird state.".to_owned(),
225-
)
226-
.into(),
227-
));
228-
return;
217+
ButtplugServerMessageV4::DeviceList(list) => {
218+
trace!("Got device list, devices either added or removed");
219+
for dev in list.devices() {
220+
if self.device_map.contains_key(&dev.device_index()) {
221+
continue;
222+
}
223+
trace!("Device added, updating map and sending to client");
224+
let info = DeviceMessageInfoV4::from(dev.clone());
225+
let device = self.create_client_device(&info);
226+
self.send_client_event(ButtplugClientEvent::DeviceAdded(device));
229227
}
230-
let info = DeviceMessageInfoV4::from(dev);
231-
let device = self.create_client_device(&info);
232-
self.send_client_event(ButtplugClientEvent::DeviceAdded(device));
233-
}
234-
ButtplugServerMessageV4::DeviceRemoved(dev) => {
235-
if self.device_map.contains_key(&dev.device_index()) {
228+
let new_indexes: Vec<u32> = list.devices().iter().map(|x| x.device_index()).collect();
229+
let disconnected_indexes: Vec<u32> = self.device_map.iter().filter(|x| !new_indexes.contains(x.key())).map(|x| *x.key()).collect();
230+
for index in disconnected_indexes {
236231
trace!("Device removed, updating map and sending to client");
237-
self.disconnect_device(dev.device_index());
238-
} else {
239-
error!("Received DeviceRemoved for non-existent device index");
240-
self.send_client_event(ButtplugClientEvent::Error(ButtplugDeviceError::DeviceConnectionError("Device removal requested for a device the client does not know about. Server may be in a weird state.".to_owned()).into()));
232+
self.disconnect_device(index);
241233
}
242234
}
243235
ButtplugServerMessageV4::ScanningFinished(_) => {

crates/buttplug_core/src/message/v4/device_added.rs

Lines changed: 0 additions & 89 deletions
This file was deleted.

crates/buttplug_core/src/message/v4/device_message_info.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
66
// for full license information.
77

8-
use super::DeviceAddedV4;
98
use crate::message::DeviceFeature;
109
use getset::{CopyGetters, Getters, MutGetters};
1110
use serde::{Deserialize, Serialize};
@@ -47,15 +46,3 @@ impl DeviceMessageInfoV4 {
4746
}
4847
}
4948
}
50-
51-
impl From<DeviceAddedV4> for DeviceMessageInfoV4 {
52-
fn from(device_added: DeviceAddedV4) -> Self {
53-
Self {
54-
device_index: device_added.device_index(),
55-
device_name: device_added.device_name().clone(),
56-
device_display_name: device_added.device_display_name().clone(),
57-
device_message_timing_gap: device_added.device_message_timing_gap(),
58-
device_features: device_added.device_features().clone(),
59-
}
60-
}
61-
}

crates/buttplug_core/src/message/v4/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
66
// for full license information.
77

8-
mod device_added;
98
mod device_list;
109
mod device_message_info;
1110
mod input_cmd;
@@ -16,7 +15,6 @@ mod server_info;
1615
mod spec_enums;
1716

1817
pub use {
19-
device_added::DeviceAddedV4,
2018
device_list::DeviceListV4,
2119
device_message_info::DeviceMessageInfoV4,
2220
input_cmd::{InputCmdV4, InputCommandType},

crates/buttplug_core/src/message/v4/spec_enums.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::message::{
1111
ButtplugMessageError,
1212
ButtplugMessageFinalizer,
1313
ButtplugMessageValidator,
14-
DeviceRemovedV0,
1514
ErrorV0,
1615
OkV0,
1716
OutputCmdV4,
@@ -27,7 +26,7 @@ use crate::message::{
2726
};
2827
use serde::{Deserialize, Serialize};
2928

30-
use super::{DeviceAddedV4, DeviceListV4, InputReadingV4};
29+
use super::{DeviceListV4, InputReadingV4};
3130

3231
/// Represents all client-to-server messages in v3 of the Buttplug Spec
3332
#[derive(
@@ -75,8 +74,6 @@ pub enum ButtplugServerMessageV4 {
7574
ServerInfo(ServerInfoV4),
7675
// Device enumeration messages
7776
DeviceList(DeviceListV4),
78-
DeviceAdded(DeviceAddedV4),
79-
DeviceRemoved(DeviceRemovedV0),
8077
ScanningFinished(ScanningFinishedV0),
8178
// Sensor commands
8279
InputReading(InputReadingV4),
@@ -85,7 +82,6 @@ pub enum ButtplugServerMessageV4 {
8582
impl ButtplugMessageFinalizer for ButtplugServerMessageV4 {
8683
fn finalize(&mut self) {
8784
match self {
88-
ButtplugServerMessageV4::DeviceAdded(da) => da.finalize(),
8985
ButtplugServerMessageV4::DeviceList(dl) => dl.finalize(),
9086
_ => (),
9187
}

crates/buttplug_server/src/device/server_device_manager.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,22 @@ impl ServerDeviceManager {
243243
}
244244
}
245245

246+
fn generate_device_list(&self) -> DeviceListV4 {
247+
let devices = self
248+
.devices
249+
.iter()
250+
.map(|device| device.value().as_device_message_info(*device.key()))
251+
.collect();
252+
DeviceListV4::new(devices)
253+
}
254+
246255
fn parse_device_manager_message(
247256
&self,
248257
manager_msg: ButtplugDeviceManagerMessageUnion,
249258
) -> ButtplugServerResultFuture {
250259
match manager_msg {
251260
ButtplugDeviceManagerMessageUnion::RequestDeviceList(msg) => {
252-
let devices = self
253-
.devices
254-
.iter()
255-
.map(|device| device.value().as_device_message_info(*device.key()))
256-
.collect();
257-
let mut device_list = DeviceListV4::new(devices);
261+
let mut device_list = self.generate_device_list();
258262
device_list.set_id(msg.id());
259263
future::ready(Ok(device_list.into())).boxed()
260264
}

crates/buttplug_server/src/device/server_device_manager_event_loop.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// for full license information.
77

88
use buttplug_core::{
9-
message::{ButtplugServerMessageV4, DeviceAddedV4, DeviceRemovedV0, ScanningFinishedV0},
9+
message::{ButtplugServerMessageV4, DeviceListV4, ScanningFinishedV0},
1010
util::async_manager,
1111
};
1212
use buttplug_server_device_config::DeviceConfigurationManager;
@@ -55,7 +55,7 @@ pub(super) struct ServerDeviceManagerEventLoop {
5555
/// True if stop scanning message was sent, means we won't send scanning finished.
5656
stop_scanning_received: AtomicBool,
5757
/// Protocol map, for mapping user definitions to protocols
58-
protocol_manager: ProtocolManager
58+
protocol_manager: ProtocolManager,
5959
}
6060

6161
impl ServerDeviceManagerEventLoop {
@@ -83,7 +83,7 @@ impl ServerDeviceManagerEventLoop {
8383
connecting_devices: Arc::new(DashSet::new()),
8484
loop_cancellation_token,
8585
stop_scanning_received: AtomicBool::new(false),
86-
protocol_manager: ProtocolManager::default()
86+
protocol_manager: ProtocolManager::default(),
8787
}
8888
}
8989

@@ -256,6 +256,15 @@ impl ServerDeviceManagerEventLoop {
256256
}
257257
}
258258

259+
fn generate_device_list(&self) -> DeviceListV4 {
260+
let devices = self
261+
.device_map
262+
.iter()
263+
.map(|device| device.value().as_device_message_info(*device.key()))
264+
.collect();
265+
DeviceListV4::new(devices)
266+
}
267+
259268
async fn handle_device_event(&mut self, device_event: ServerDeviceEvent) {
260269
trace!("Got device event: {:?}", device_event);
261270
match device_event {
@@ -304,13 +313,15 @@ impl ServerDeviceManagerEventLoop {
304313
});
305314

306315
info!("Assigning index {} to {}", device_index, device.name());
307-
let device_added_message = DeviceAddedV4::from(device.as_device_message_info(device_index));
308-
self.device_map.insert(device_index, device);
316+
self.device_map.insert(device_index, device.clone());
317+
318+
let device_update_message: ButtplugServerMessageV4 = self.generate_device_list().into();
319+
309320
// After that, we can send out to the server's event listeners to let
310321
// them know a device has been added.
311322
if self
312323
.server_sender
313-
.send(device_added_message.into())
324+
.send(device_update_message.into())
314325
.is_err()
315326
{
316327
debug!("Server not currently available, dropping Device Added event.");
@@ -329,9 +340,10 @@ impl ServerDeviceManagerEventLoop {
329340
.device_map
330341
.remove(&device_index)
331342
.expect("Remove will always work.");
343+
let device_update_message: ButtplugServerMessageV4 = self.generate_device_list().into();
332344
if self
333345
.server_sender
334-
.send(DeviceRemovedV0::new(device_index).into())
346+
.send(device_update_message)
335347
.is_err()
336348
{
337349
debug!("Server not currently available, dropping Device Removed event.");

0 commit comments

Comments
 (0)