Skip to content

Commit 019999d

Browse files
feat: Refactoring to better reflect the other Buttplug clients
1 parent 91382d4 commit 019999d

19 files changed

Lines changed: 608 additions & 314 deletions

buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/ButtplugException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ public class ButtplugException extends Exception {
44

55
private String errorMessage = "";
66

7+
public ButtplugException(Exception exception) {
8+
super(exception);
9+
}
10+
public ButtplugException() {
11+
}
12+
713
@Override
814
public final String getMessage() {
915
return errorMessage;

buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClient.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public abstract class ButtplugClient {
3030
private final Object sendLock = new Object();
3131
private ConnectionState connectionState = ConnectionState.DISCONNECTED;
3232
private Timer pingTimer;
33-
private IDeviceAddedEvent deviceAdded;
34-
private IDeviceChangedEvent deviceChanged;
35-
private IDeviceRemovedEvent deviceRemoved;
36-
private IScanningEvent scanningFinished;
37-
private IErrorEvent errorReceived;
38-
private ISensorReadingEvent sensorReadingReceived;
39-
private IConnectedEvent onConnected;
33+
private IDeviceAddedEvent deviceAddedHandler;
34+
private IDeviceChangedEvent deviceChangedHandler;
35+
private IDeviceRemovedEvent deviceRemovedHandler;
36+
private IScanningEvent scanningFinishedHandler;
37+
private IErrorEvent errorHandler;
38+
private IInputEvent inputHandler;
39+
private IConnectedEvent onConnectedHandler;
4040

4141
public ButtplugClient(final String aClientName) {
4242
parser = new ButtplugJsonMessageParser();
@@ -84,14 +84,14 @@ public final void onMessage(final List<ButtplugMessage> msgs) {
8484

8585
if (compare > 0) {
8686
devices.put(newDevs.get(newIdx), new ButtplugClientDevice(this, newDevices.get(newDevs.get(newIdx))));
87-
if (getDeviceAdded() != null) {
88-
getDeviceAdded().deviceAdded(devices.get(newDevs.get(newIdx)));
87+
if (getDeviceAddedHandler() != null) {
88+
getDeviceAddedHandler().deviceAdded(devices.get(newDevs.get(newIdx)));
8989
}
9090
newIdx++;
9191
} else if (compare < 0) {
9292
devices.remove(curDevs.get(curIdx));
93-
if (getDeviceRemoved() != null) {
94-
getDeviceRemoved().deviceRemoved(curDevs.get(curIdx));
93+
if (getDeviceRemovedHandler() != null) {
94+
getDeviceRemovedHandler().deviceRemoved(curDevs.get(curIdx));
9595
}
9696
curIdx++;
9797
} else {
@@ -110,15 +110,15 @@ public final void onMessage(final List<ButtplugMessage> msgs) {
110110

111111
while (curIdx < curDevs.size()) {
112112
devices.remove(curDevs.get(curIdx));
113-
if (getDeviceRemoved() != null) {
114-
getDeviceRemoved().deviceRemoved(curDevs.get(curIdx));
113+
if (getDeviceRemovedHandler() != null) {
114+
getDeviceRemovedHandler().deviceRemoved(curDevs.get(curIdx));
115115
}
116116
curIdx++;
117117
}
118118
while (newIdx < newDevs.size()) {
119119
devices.put(newDevs.get(newIdx), new ButtplugClientDevice(this, newDevices.get(newDevs.get(newIdx))));
120-
if (getDeviceAdded() != null) {
121-
getDeviceAdded().deviceAdded(devices.get(newDevs.get(newIdx)));
120+
if (getDeviceAddedHandler() != null) {
121+
getDeviceAddedHandler().deviceAdded(devices.get(newDevs.get(newIdx)));
122122
}
123123
newIdx++;
124124
}
@@ -133,16 +133,16 @@ public final void onMessage(final List<ButtplugMessage> msgs) {
133133
}
134134

135135
if (msg instanceof ScanningFinished) {
136-
if (getScanningFinished() != null) {
137-
getScanningFinished().scanningFinished();
136+
if (getScanningFinishedHandler() != null) {
137+
getScanningFinishedHandler().scanningFinished();
138138
}
139139
} else if (msg instanceof Error) {
140-
if (getErrorReceived() != null) {
141-
getErrorReceived().errorReceived((Error) msg);
140+
if (getErrorHandler() != null) {
141+
getErrorHandler().errorReceived((Error) msg);
142142
}
143143
} else if (msg instanceof InputReading) {
144-
if (getSensorReadingReceived() != null) {
145-
getSensorReadingReceived().sensorReadingReceived((InputReading) msg);
144+
if (getInputHandler() != null) {
145+
getInputHandler().inputEvent((InputReading) msg);
146146
}
147147
}
148148
}
@@ -164,8 +164,8 @@ public void run() {
164164
try {
165165
onPingTimer();
166166
} catch (Exception e) {
167-
if (errorReceived != null) {
168-
errorReceived.errorReceived(new Error(e));
167+
if ( getErrorHandler() != null) {
168+
getErrorHandler().errorReceived(new Error(new ButtplugClientException(e)));
169169
} else {
170170
e.printStackTrace();
171171
}
@@ -183,17 +183,17 @@ public void run() {
183183
throw new ButtplugClientException("Unexpected message returned: " + res.getClass().getName());
184184
}
185185
} catch (ButtplugClientException | InterruptedException | ExecutionException | TimeoutException e) {
186-
if (getErrorReceived() != null) {
187-
getErrorReceived().errorReceived(new Error(e));
186+
if (getErrorHandler() != null) {
187+
getErrorHandler().errorReceived(new Error(new ButtplugClientException(e)));
188188
} else {
189189
e.printStackTrace();
190190
}
191191
}
192192

193193
connectionState = ConnectionState.CONNECTED;
194194

195-
if (getOnConnected() != null) {
196-
getOnConnected().onConnected(this);
195+
if (getOnConnectedHandler() != null) {
196+
getOnConnectedHandler().onConnected(this);
197197
}
198198

199199
}
@@ -273,60 +273,60 @@ protected final boolean waitForOk(final Future<ButtplugMessage> msg)
273273

274274
protected abstract CompletableFuture<ButtplugMessage> sendMessage(ButtplugMessage msg);
275275

276-
public final IDeviceAddedEvent getDeviceAdded() {
277-
return deviceAdded;
276+
public final IDeviceAddedEvent getDeviceAddedHandler() {
277+
return deviceAddedHandler;
278278
}
279279

280-
public final void setDeviceAdded(final IDeviceAddedEvent deviceAddedHandler) {
281-
this.deviceAdded = deviceAddedHandler;
280+
public final void setDeviceAddedHandler(final IDeviceAddedEvent deviceAddedHandler) {
281+
this.deviceAddedHandler = deviceAddedHandler;
282282
}
283283

284284
public final IDeviceChangedEvent getDeviceChanged() {
285-
return deviceChanged;
285+
return deviceChangedHandler;
286286
}
287287

288-
public final void setDeviceChanged(final IDeviceChangedEvent deviceChangedHandler) {
289-
this.deviceChanged = deviceChangedHandler;
288+
public final void setDeviceChangedHandler(final IDeviceChangedEvent deviceChangedHandler) {
289+
this.deviceChangedHandler = deviceChangedHandler;
290290
}
291291

292-
public final IDeviceRemovedEvent getDeviceRemoved() {
293-
return deviceRemoved;
292+
public final IDeviceRemovedEvent getDeviceRemovedHandler() {
293+
return deviceRemovedHandler;
294294
}
295295

296-
public final void setDeviceRemoved(final IDeviceRemovedEvent deviceRemovedHandler) {
297-
this.deviceRemoved = deviceRemovedHandler;
296+
public final void setDeviceRemovedHandler(final IDeviceRemovedEvent deviceRemovedHandler) {
297+
this.deviceRemovedHandler = deviceRemovedHandler;
298298
}
299299

300-
public final IScanningEvent getScanningFinished() {
301-
return scanningFinished;
300+
public final IScanningEvent getScanningFinishedHandler() {
301+
return scanningFinishedHandler;
302302
}
303303

304-
public final void setScanningFinished(final IScanningEvent scanningFinishedHandler) {
305-
this.scanningFinished = scanningFinishedHandler;
304+
public final void setScanningFinishedHandler(final IScanningEvent scanningFinishedHandler) {
305+
this.scanningFinishedHandler = scanningFinishedHandler;
306306
}
307307

308-
public final IErrorEvent getErrorReceived() {
309-
return errorReceived;
308+
public final IErrorEvent getErrorHandler() {
309+
return errorHandler;
310310
}
311311

312-
public final void setErrorReceived(final IErrorEvent errorReceivedHandler) {
313-
this.errorReceived = errorReceivedHandler;
312+
public final void setErrorHandler(final IErrorEvent errorReceivedHandler) {
313+
this.errorHandler = errorReceivedHandler;
314314
}
315315

316-
public final ISensorReadingEvent getSensorReadingReceived() {
317-
return sensorReadingReceived;
316+
public final IInputEvent getInputHandler() {
317+
return inputHandler;
318318
}
319319

320-
public final void setSensorReadingReceived(final ISensorReadingEvent sensorReadingReceivedHandler) {
321-
this.sensorReadingReceived = sensorReadingReceivedHandler;
320+
public final void setInputHandler(final IInputEvent inputHandler) {
321+
this.inputHandler = inputHandler;
322322
}
323323

324-
public final IConnectedEvent getOnConnected() {
325-
return onConnected;
324+
public final IConnectedEvent getOnConnectedHandler() {
325+
return onConnectedHandler;
326326
}
327327

328328
public final void setOnConnected(final IConnectedEvent onConnected) {
329-
this.onConnected = onConnected;
329+
this.onConnectedHandler = onConnected;
330330
}
331331

332332
protected abstract void cleanup();

0 commit comments

Comments
 (0)