Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
chore: Update rust examples for v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
qdot committed Jan 25, 2021
1 parent 111793a commit 8190e91
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 469 deletions.
666 changes: 238 additions & 428 deletions examples/rust/Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
buttplug = "0.11.1"
buttplug = { version = "2.0.4", features = ["async-std-runtime", "client", "server", "serialize-json", "btleplug-manager", "websockets", "xinput-manager", "serial-manager", "lovense-dongle-manager"] }
# buttplug = { path = "../../../buttplug-rs/buttplug", features = ["async-std-runtime", "client", "server", "serialize-json", "btleplug-manager", "websockets", "xinput-manager", "serial-manager", "lovense-dongle-manager"] }
async-std = { version = "1.7.0", features = ["attributes"] }
anyhow = "1.0.34"
tracing-subscriber = "0.2.15"
Expand Down
4 changes: 3 additions & 1 deletion examples/rust/src/bin/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ async fn main() -> anyhow::Result<()> {
// server requires the client and server to send information back and forth,
// so we'll await that while those (possibly somewhat slow, depending on if
// network is being used and other factors) transfers happen.
let (_client, mut event_stream) = ButtplugClient::connect("Example Client", connector)
let client = ButtplugClient::new("Example Client");
client.connect(connector)
.await
.expect("Can't connect to Buttplug Server, exiting!");

let mut event_stream = client.event_stream();
// As an example of event messages, we'll assume the server might
// send the client notifications about new devices that it has found.
// The client will let us know about this via events.
Expand Down
47 changes: 23 additions & 24 deletions examples/rust/src/bin/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_std::{
stream::StreamExt,
};
use buttplug::{
client::{ButtplugClient, ButtplugClientError, ButtplugClientEvent},
client::{ButtplugClient, ButtplugClientError},
connector::ButtplugInProcessClientConnector,
core::errors::ButtplugError,
};
Expand Down Expand Up @@ -33,35 +33,34 @@ async fn main() -> anyhow::Result<()> {
// being up, etc.
// - A ButtplugHandshakeError if there is a client/server version
// mismatch.
let client = ButtplugClient::connect("Example Client", connector).await;
let client = match client {
Ok(client) => client,
Err(ButtplugClientError::ButtplugConnectorError(error)) => {
// If our connection failed, because the server wasn't turned on,
// SSL/TLS wasn't turned off, etc, we'll just print and exit
// here.
println!("Can't connect, exiting! Message: {}", error);
wait_for_input().await;
return Ok(());
}
Err(ButtplugClientError::ButtplugError(error)) => match error {
ButtplugError::ButtplugHandshakeError(error) => {
// This means our client is newer than our server, and we need to
// upgrade the server we're connecting to.
println!("Handshake issue, exiting! Message: {}", error);
let client = ButtplugClient::new("Example Client");
if let Err(e) = client.connect(connector).await {
match e {
ButtplugClientError::ButtplugConnectorError(error) => {
// If our connection failed, because the server wasn't turned on,
// SSL/TLS wasn't turned off, etc, we'll just print and exit
// here.
println!("Can't connect, exiting! Message: {}", error);
wait_for_input().await;
return Ok(());
}
error => {
println!("Unexpected error type! {}", error);
wait_for_input().await;
return Ok(());
ButtplugClientError::ButtplugError(error) => match error {
ButtplugError::ButtplugHandshakeError(error) => {
// This means our client is newer than our server, and we need to
// upgrade the server we're connecting to.
println!("Handshake issue, exiting! Message: {}", error);
wait_for_input().await;
return Ok(());
}
error => {
println!("Unexpected error type! {}", error);
wait_for_input().await;
return Ok(());
}
}
},
}
};

let (client, events) = client;

// We're connected, yay!
println!("Connected! Check Server for Client Name.");

Expand Down
11 changes: 6 additions & 5 deletions examples/rust/src/bin/device_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use async_std::{
stream::StreamExt,
};
use buttplug::{
client::{device::VibrateCommand, ButtplugClient, ButtplugClientError, ButtplugClientEvent},
client::{device::VibrateCommand, ButtplugClient, ButtplugClientError, ButtplugClientDeviceMessageType},
connector::ButtplugInProcessClientConnector,
core::messages::ButtplugDeviceMessageType,
test::new_bluetoothle_test_device,
};

Expand All @@ -18,7 +17,8 @@ async fn main() -> anyhow::Result<()> {
let connector = ButtplugInProcessClientConnector::default();
let server = connector.server_ref();
server.add_test_comm_manager()?;
let (client, _) = ButtplugClient::connect("Example Client", connector).await?;
let client = ButtplugClient::new("Example Client");
client.connect(connector).await?;

// For this example, we'll use the Test device. This is included in Buttplug
// Rust >= v0.1.0. It emulates how a regular device manager would work,
Expand All @@ -42,7 +42,7 @@ async fn main() -> anyhow::Result<()> {

for device in client.devices() {
println!("{} supports these messages:", device.name);
for (r#type, attributes) in device.allowed_messages {
for (r#type, attributes) in &device.allowed_messages {
println!("- {}", r#type);
if let Some(count) = attributes.feature_count {
println!(" - Features: {}", count);
Expand Down Expand Up @@ -75,8 +75,9 @@ async fn main() -> anyhow::Result<()> {
// know it's 2 so we don't really have to use it.
let vibrator_count = test_client_device
.allowed_messages
.get(&ButtplugDeviceMessageType::VibrateCmd)
.get(&ButtplugClientDeviceMessageType::VibrateCmd)
.and_then(|attributes| attributes.feature_count);

test_client_device
.vibrate(VibrateCommand::SpeedVec(vec![1.0, 0.0]))
.await?;
Expand Down
5 changes: 3 additions & 2 deletions examples/rust/src/bin/device_enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ async fn wait_for_input() {
async fn main() -> anyhow::Result<()> {
// Usual embedded connector setup. We'll assume the server found all
// of the subtype managers for us (the default features include all of them).
let (client, events) =
ButtplugClient::connect_in_process("Example Client", &ButtplugServerOptions::default()).await?;
let client = ButtplugClient::new("Example Client");
let events = client.event_stream();
client.connect_in_process(&ButtplugServerOptions::default()).await?;

// Set up our DeviceAdded/DeviceRemoved event handlers before connecting.
let events = events.inspect(|event| match event {
Expand Down
8 changes: 5 additions & 3 deletions examples/rust/src/bin/embedded_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use buttplug::{
server::{comm_managers::btleplug::BtlePlugCommunicationManager, ButtplugServerOptions},
};

#[allow(dead_code)]
async fn main_the_hard_way() -> anyhow::Result<()> {
// First off, we'll set up our Embedded Connector.
let connector = ButtplugInProcessClientConnector::default();
Expand All @@ -12,16 +13,17 @@ async fn main_the_hard_way() -> anyhow::Result<()> {
.server_ref()
.add_comm_manager::<BtlePlugCommunicationManager>()?;

let _client = ButtplugClient::connect("Example Client", connector).await?;
let client = ButtplugClient::new("Example Client");
client.connect(connector).await?;

Ok(())
}

#[async_std::main]
async fn main() -> anyhow::Result<()> {
// This is the easy way, it sets up an embedded server with everything set up automatically
let _client =
ButtplugClient::connect_in_process("Example Client", &ButtplugServerOptions::default()).await?;
let client = ButtplugClient::new("Example Client");
client.connect_in_process(&ButtplugServerOptions::default()).await?;

Ok(())
}
1 change: 1 addition & 0 deletions examples/rust/src/bin/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use buttplug::{client::ButtplugClientError, core::errors::ButtplugError};

#[allow(dead_code)]
fn handle_error(error: ButtplugClientError) {
match error {
ButtplugClientError::ButtplugConnectorError(_details) => {}
Expand Down
3 changes: 2 additions & 1 deletion examples/rust/src/bin/external_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ async fn main() -> anyhow::Result<()> {
"ws://localhost:12345/buttplug",
));

let (_client, _event_stream) = ButtplugClient::connect("Example Client", connector).await?;
let client = ButtplugClient::new("Example Client");
client.connect(connector).await?;

Ok(())
}
7 changes: 3 additions & 4 deletions examples/rust/src/bin/logging.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use buttplug::{
client::ButtplugClient,
connector::ButtplugInProcessClientConnector,
server::{comm_managers::btleplug::BtlePlugCommunicationManager, ButtplugServerOptions},
server::ButtplugServerOptions,
};
use tracing_subscriber;

Expand All @@ -13,8 +12,8 @@ async fn main() -> anyhow::Result<()> {
// Now when you connect here, if you've set the RUST_LOG environment variable
// (set it to "Info" or "Debug"), you should see messages about connection
// setup.
let _client =
ButtplugClient::connect_in_process("Example Client", &ButtplugServerOptions::default()).await?;
let client = ButtplugClient::new("Example Client");
client.connect_in_process(&ButtplugServerOptions::default()).await?;

Ok(())
}

0 comments on commit 8190e91

Please sign in to comment.