Skip to content

Commit 9063bd6

Browse files
chore: Cleanup checkstyle warnings
1 parent 019999d commit 9063bd6

68 files changed

Lines changed: 3147 additions & 766 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

buttplug4j.connectors.javax.websocket.client/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/client/ButtplugClientWSClient.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,36 @@
1414
import java.util.concurrent.CompletableFuture;
1515
import java.util.concurrent.ExecutionException;
1616

17+
/**
18+
* ButtplugClientWSClient using Javax WebSocket.
19+
*/
1720
@ClientEndpoint
1821
public final class ButtplugClientWSClient extends ButtplugClientWSEndpoint {
1922

23+
/**
24+
* WebSocket container.
25+
*/
2026
private WebSocketContainer client;
2127

28+
/**
29+
* Constructor.
30+
*
31+
* @param clientName client name
32+
*/
2233
public ButtplugClientWSClient(final String clientName) {
2334
super(clientName);
2435
}
2536

37+
/**
38+
* Connect to server.
39+
*
40+
* @param url server URL
41+
* @throws IllegalStateException if already open
42+
* @throws DeploymentException if deployment fails
43+
* @throws IOException if IO error occurs
44+
* @throws ExecutionException if execution fails
45+
* @throws InterruptedException if interrupted
46+
*/
2647
public void connect(final URI url) throws IllegalStateException, DeploymentException, IOException,
2748
ExecutionException, InterruptedException {
2849

@@ -31,7 +52,7 @@ public void connect(final URI url) throws IllegalStateException, DeploymentExcep
3152
}
3253
setConnectionState(ButtplugClient.ConnectionState.CONNECTING);
3354

34-
IConnectedEvent stashCallback = getOnConnected();
55+
IConnectedEvent stashCallback = getOnConnectedHandler();
3556

3657
CompletableFuture<Boolean> promise = new CompletableFuture<>();
3758
setOnConnected(client -> promise.complete(true));
@@ -42,12 +63,13 @@ public void connect(final URI url) throws IllegalStateException, DeploymentExcep
4263

4364
// Restore and echo down the line
4465
setOnConnected(stashCallback);
45-
if (stashCallback != null)
66+
if (stashCallback != null) {
4667
stashCallback.onConnected(this);
68+
}
4769
}
4870

71+
@Override
4972
protected void cleanup() {
50-
5173
if (getSession() != null) {
5274
try {
5375
getSession().close();
@@ -60,6 +82,4 @@ protected void cleanup() {
6082
LifeCycle.stop(client);
6183
client = null;
6284
}
63-
64-
6585
}

buttplug4j.connectors.javax.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/client/ButtplugWSClientMockTest.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,25 @@
33
import io.github.blackspherefollower.buttplug4j.client.ButtplugClientDevice;
44
import io.github.blackspherefollower.buttplug4j.client.ButtplugClientDeviceFeature;
55
import io.github.blackspherefollower.buttplug4j.client.ButtplugDeviceFeatureException;
6-
import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage;
7-
import io.github.blackspherefollower.buttplug4j.protocol.messages.InputReading;
86
import io.github.blackspherefollower.buttplug4j.utils.test.IntifaceEngineWrapper;
97
import io.github.blackspherefollower.buttplug4j.utils.test.WSDMClient;
10-
import org.junit.jupiter.api.Disabled;
118
import org.junit.jupiter.api.Test;
129

1310
import java.net.URI;
14-
import java.util.concurrent.TimeUnit;
1511

1612
import static org.junit.jupiter.api.Assertions.*;
1713

1814
public class ButtplugWSClientMockTest {
1915

2016
@Test
21-
public void TestConnect() throws Exception {
17+
public void testConnect() throws Exception {
2218
try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper()) {
2319
Thread.sleep(500);
24-
WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B");
20+
WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.getDport()), "LVS-Fake", "A9816725B");
2521
Thread.sleep(500);
2622

2723
ButtplugClientWSClient client = new ButtplugClientWSClient("Java Test");
28-
client.connect(new URI("ws://localhost:" + wrapper.cport + "/buttplug"));
24+
client.connect(new URI("ws://localhost:" + wrapper.getCport() + "/buttplug"));
2925
client.startScanning();
3026

3127
Thread.sleep(500);
@@ -34,48 +30,41 @@ public void TestConnect() throws Exception {
3430
assertEquals(1, client.getDevices().size());
3531
for (ButtplugClientDevice dev : client.getDevices()) {
3632
for (ButtplugClientDeviceFeature feat : dev.getDeviceFeatures().values()) {
37-
if (feat.HasVibrate()) {
38-
feat.VibrateFloat(0.5F).get();
33+
if (feat.hasVibrate()) {
34+
feat.runVibrateFloat(0.5F).get();
3935
}
4036
}
4137
}
42-
assertEquals(wsdev.messages.poll(), "Vibrate:10;");
38+
assertEquals(wsdev.getMessages().poll(), "Vibrate:10;");
4339

4440
Thread.sleep(500);
4541
assertTrue(client.stopAllDevices());
46-
assertEquals(wsdev.messages.poll(), "Vibrate:0;");
42+
assertEquals(wsdev.getMessages().poll(), "Vibrate:0;");
4743

4844
client.disconnect();
4945
}
5046
}
5147

5248
@Test
53-
public void TestBattery() throws Exception {
49+
public void testBattery() throws Exception {
5450
try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper()) {
5551
Thread.sleep(500);
56-
WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B");
52+
WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.getDport()), "LVS-Fake", "A9816725B");
5753
Thread.sleep(500);
5854

5955
ButtplugClientWSClient client = new ButtplugClientWSClient("Java Test");
60-
client.connect(new URI("ws://localhost:" + wrapper.cport + "/buttplug"));
56+
client.connect(new URI("ws://localhost:" + wrapper.getCport() + "/buttplug"));
6157
client.startScanning();
6258

6359
Thread.sleep(500);
6460
client.requestDeviceList();
6561
for (ButtplugClientDevice dev : client.getDevices()) {
6662
for (ButtplugClientDeviceFeature feat : dev.getDeviceFeatures().values()) {
67-
if (feat.HasBattery()) {
68-
ButtplugMessage res = feat.ReadBattery().get(2, TimeUnit.SECONDS);
69-
if (res instanceof InputReading && ((InputReading) res).getData() instanceof InputReading.BatteryData) {
70-
InputReading.BatteryData reading = (InputReading.BatteryData) ((InputReading) res).getData();
71-
int battery = reading.getValue();
72-
System.out.println("Battery is " + battery);
73-
assertTrue(battery >= 0);
74-
assertTrue(battery <= 100);
75-
}
63+
if (feat.hasBattery()) {
64+
feat.readBattery();
7665
} else {
7766
assertThrows(ButtplugDeviceFeatureException.class, () -> {
78-
feat.ReadBattery().get();
67+
feat.readBattery();
7968
});
8069
}
8170
}
@@ -84,4 +73,4 @@ public void TestBattery() throws Exception {
8473
client.disconnect();
8574
}
8675
}
87-
}
76+
}

buttplug4j.connectors.javax.websocket.common/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/common/ButtplugClientWSEndpoint.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,102 @@
11
package io.github.blackspherefollower.buttplug4j.connectors.javax.websocket.common;
22

33
import io.github.blackspherefollower.buttplug4j.client.ButtplugClient;
4+
import io.github.blackspherefollower.buttplug4j.client.ButtplugClientException;
45
import io.github.blackspherefollower.buttplug4j.protocol.ButtplugConsts;
56
import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage;
67
import io.github.blackspherefollower.buttplug4j.protocol.ButtplugProtocolException;
78
import io.github.blackspherefollower.buttplug4j.protocol.messages.Error;
89

9-
import javax.websocket.*;
10+
import javax.websocket.ClientEndpoint;
11+
import javax.websocket.CloseReason;
12+
import javax.websocket.OnClose;
13+
import javax.websocket.OnError;
14+
import javax.websocket.OnMessage;
15+
import javax.websocket.OnOpen;
16+
import javax.websocket.Session;
1017
import javax.websocket.server.ServerEndpoint;
1118
import java.io.IOException;
1219
import java.nio.ByteBuffer;
20+
import java.nio.charset.StandardCharsets;
1321
import java.util.List;
1422
import java.util.Timer;
1523
import java.util.TimerTask;
1624
import java.util.concurrent.CompletableFuture;
1725

26+
/**
27+
* ButtplugClientWSEndpoint.
28+
*/
1829
@ClientEndpoint
1930
@ServerEndpoint("/")
2031
public abstract class ButtplugClientWSEndpoint extends ButtplugClient {
32+
/**
33+
* Ping timeout.
34+
*/
2135
private static final int TENSEC = 10000;
36+
/**
37+
* Websocket session.
38+
*/
2239
private Session session;
40+
/**
41+
* Websocket ping timer.
42+
*/
2343
private Timer wsPingTimer;
2444

45+
/**
46+
* Constructor.
47+
*
48+
* @param aClientName client name
49+
*/
2550
public ButtplugClientWSEndpoint(final String aClientName) {
2651
super(aClientName);
2752
}
2853

54+
/**
55+
* Get session.
56+
*
57+
* @return session
58+
*/
2959
public final Session getSession() {
3060
return session;
3161
}
3262

63+
/**
64+
* Called when message received.
65+
*
66+
* @param sess session
67+
* @param message message
68+
*/
3369
@OnMessage
3470
public final void onMessage(final Session sess, final String message) {
3571
try {
3672
List<ButtplugMessage> msgs = getParser().parseJson(message);
3773
onMessage(msgs);
3874
} catch (ButtplugProtocolException e) {
39-
if (getErrorReceived() != null) {
40-
getErrorReceived().errorReceived(new Error(e));
75+
if (getErrorHandler() != null) {
76+
getErrorHandler().errorReceived(new Error(e));
4177
} else {
4278
e.printStackTrace();
4379
}
4480
}
4581
}
4682

83+
/**
84+
* Called when websocket closes.
85+
*
86+
* @param reason reason
87+
*/
4788
@OnClose
4889
@SuppressWarnings("unused")
4990
public final void onClose(final CloseReason reason) {
5091
this.session = null;
5192
setConnectionState(ConnectionState.DISCONNECTED);
5293
}
5394

95+
/**
96+
* Called when websocket connects.
97+
*
98+
* @param newSession session
99+
*/
54100
@OnOpen
55101
@SuppressWarnings("unused")
56102
public final void onConnect(final Session newSession) {
@@ -63,7 +109,7 @@ public final void onConnect(final Session newSession) {
63109
public void run() {
64110
try {
65111
if (session != null) {
66-
session.getAsyncRemote().sendPing(ByteBuffer.wrap("ping".getBytes()));
112+
session.getAsyncRemote().sendPing(ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8)));
67113
}
68114
} catch (IOException e) {
69115
wsPingTimer.cancel();
@@ -74,13 +120,18 @@ public void run() {
74120
}, 0, TENSEC);
75121

76122
// Don't block the WS thread
77-
new Thread(() -> doHandshake()).start();
123+
new Thread(this::doHandshake).start();
78124
}
79125

126+
/**
127+
* Called on websocket error.
128+
*
129+
* @param cause cause
130+
*/
80131
@OnError
81132
public final void onWebSocketError(final Throwable cause) {
82-
if (getErrorReceived() != null) {
83-
getErrorReceived().errorReceived(new Error(cause));
133+
if (getErrorHandler() != null) {
134+
getErrorHandler().errorReceived(new Error(new ButtplugClientException(cause.getMessage())));
84135
} else {
85136
cause.printStackTrace();
86137
}
@@ -93,18 +144,18 @@ protected final CompletableFuture<ButtplugMessage> sendMessage(final ButtplugMes
93144
if (session == null) {
94145
Error err = new Error("Bad WS state!",
95146
Error.ErrorClass.ERROR_UNKNOWN, ButtplugConsts.SYSTEM_MSG_ID);
96-
if (getErrorReceived() != null) {
97-
getErrorReceived().errorReceived(err);
147+
if (getErrorHandler() != null) {
148+
getErrorHandler().errorReceived(err);
98149
}
99150
return CompletableFuture.completedFuture(err);
100151
}
101152

102153
try {
103154
session.getAsyncRemote().sendText(getParser().formatJson(msg)).get();
104155
} catch (Exception e) {
105-
Error err = new Error(e, msg.getId());
106-
if (getErrorReceived() != null) {
107-
getErrorReceived().errorReceived(err);
156+
Error err = new Error(new ButtplugClientException(e.getMessage()), msg.getId());
157+
if (getErrorHandler() != null) {
158+
getErrorHandler().errorReceived(err);
108159
}
109160
return CompletableFuture.completedFuture(err);
110161
}

buttplug4j.connectors.javax.websocket.server/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/server/ButtplugClientWSServer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
import javax.websocket.server.ServerEndpoint;
66
import java.io.IOException;
77

8+
/**
9+
* ButtplugClientWSServer using Javax WebSocket.
10+
*/
811
@ServerEndpoint("/")
912
public final class ButtplugClientWSServer extends ButtplugClientWSEndpoint {
1013

14+
/**
15+
* Constructor.
16+
*
17+
* @param clientName client name
18+
*/
1119
public ButtplugClientWSServer(final String clientName) {
1220
super(clientName);
1321
setConnectionState(ConnectionState.CONNECTING);
1422
}
1523

24+
@Override
1625
protected void cleanup() {
17-
1826
if (getSession() != null) {
1927
try {
2028
getSession().close();
@@ -24,5 +32,4 @@ protected void cleanup() {
2432
}
2533
}
2634
}
27-
2835
}

0 commit comments

Comments
 (0)