Skip to content

Commit

Permalink
feat: Add proper exceptions and means of setting per-feature values
Browse files Browse the repository at this point in the history
  • Loading branch information
blackspherefollower committed Mar 19, 2023
1 parent 883c38b commit 37688af
Show file tree
Hide file tree
Showing 29 changed files with 347 additions and 94 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,26 @@ In depth testing is still pending, so your mileage may vary.
## Development

Snapshot libraries from the buttplug4j repo are available via Maven from the following
repository: https://github.com/blackspherefollower/buttplug4j/packages
repository: https://s01.oss.sonatype.org/content/repositories/snapshots
Releases will be available from maven central.

```xml
<repositories>
<repository>
<id>blackspherefollower-buttplug4j</id>
<url>https://maven.pkg.github.com/lackspherefollower/buttplug4j</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>OSSRH</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.github.blackspherefollower</groupId>
<artifactId>buttplug4j</artifactId>
<version>[3.0-SNAPSHOT,)</version>
</dependency>
</dependencies>
```

## Support The Project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.blackspherefollower.buttplug4j;

public class ButtplugException extends Exception {

private String errorMessage = "";

protected final void setMessage(final String errorMessage) {
this.errorMessage = errorMessage;
}

@Override
public final String getMessage() {
return errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.blackspherefollower.buttplug4j.Util;

public final class Pair<A, B> {

private A a;
private B b;

public Pair(final A a, final B b) {
this.a = a;
this.b = b;
}
public A getLeft() {
return a;
}

public B getRight() {
return b;
}

public A setLeft(final A value) {
a = value;
return a;
}

public B setRight(final B value) {
b = value;
return b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package io.github.blackspherefollower.buttplug4j.Util;
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ public void run() {
}

} else if (res instanceof Error) {
throw new Exception(((Error) res).getErrorMessage());
throw new ButtplugClientException(((Error) res).getErrorMessage());
} else {
throw new Exception("Unexpected message returned: " + res.getClass().getName());
throw new ButtplugClientException("Unexpected message returned: " + res.getClass().getName());
}
} catch (Exception e) {
} catch (ButtplugClientException | InterruptedException | ExecutionException e) {
if (getErrorReceived() != null) {
getErrorReceived().errorReceived(new Error(e.getMessage(), Error.ErrorClass.ERROR_UNKNOWN, -1));
}
Expand All @@ -151,23 +151,23 @@ public void run() {
}
}

private void onPingTimer() throws Exception {
private void onPingTimer() throws ButtplugClientException, ExecutionException, InterruptedException {
try {
ButtplugMessage msg = sendMessage(new Ping(msgId.incrementAndGet())).get();
if (msg instanceof Error) {
throw new Exception(((Error) msg).getErrorMessage());
throw new ButtplugClientException(((Error) msg).getErrorMessage());
}
} catch (Throwable e) {
} catch (ButtplugClientException | InterruptedException | ExecutionException e) {
disconnect();
throw e;
}
}

public final void requestDeviceList() throws Exception {
public final void requestDeviceList() throws ButtplugClientException, ExecutionException, InterruptedException {
ButtplugMessage res = sendMessage(new RequestDeviceList(msgId.incrementAndGet())).get();
if (!(res instanceof DeviceList) || ((DeviceList) res).getDevices() == null) {
if (res instanceof Error) {
throw new Exception(((Error) res).getErrorMessage());
throw new ButtplugClientException(((Error) res).getErrorMessage());
}
return;
}
Expand All @@ -194,32 +194,28 @@ public final boolean startScanning() throws ExecutionException, InterruptedExcep
return waitForOk(startScanningAsync());
}

public final Future<ButtplugMessage> startScanningAsync()
throws ExecutionException, InterruptedException, IOException {
public final Future<ButtplugMessage> startScanningAsync() {
return sendMessage(new StartScanning(msgId.incrementAndGet()));
}

public final boolean stopScanning() throws ExecutionException, InterruptedException, IOException {
public final boolean stopScanning() throws ExecutionException, InterruptedException {
return waitForOk(stopScanningAsync());
}

public final Future<ButtplugMessage> stopScanningAsync()
throws ExecutionException, InterruptedException, IOException {
public final Future<ButtplugMessage> stopScanningAsync() {
return sendMessage(new StopScanning(msgId.incrementAndGet()));
}

public final boolean stopAllDevices() throws ExecutionException, InterruptedException, IOException {
return waitForOk(stopAllDevicesAsync());
}

public final Future<ButtplugMessage> stopAllDevicesAsync()
throws IOException, ExecutionException, InterruptedException {
public final Future<ButtplugMessage> stopAllDevicesAsync() {
return sendMessage(new StopAllDevices(getNextMsgId()));
}

public final CompletableFuture<ButtplugMessage> sendDeviceMessage(
final ButtplugClientDevice device, final ButtplugDeviceMessage deviceMsg)
throws ExecutionException, InterruptedException, IOException {
final ButtplugClientDevice device, final ButtplugDeviceMessage deviceMsg) {
ButtplugClientDevice dev = devices.get(device.getDeviceIndex());
if (dev != null) {
if (!dev.getDeviceMessages().containsKey(deviceMsg.getClass().getSimpleName())) {
Expand All @@ -239,7 +235,7 @@ public final CompletableFuture<ButtplugMessage> sendDeviceMessage(
}

protected final boolean waitForOk(final Future<ButtplugMessage> msg)
throws ExecutionException, InterruptedException, IOException {
throws ExecutionException, InterruptedException {
return msg.get() instanceof Ok;
}

Expand Down
Loading

0 comments on commit 37688af

Please sign in to comment.