Skip to content

Commit

Permalink
Creates thread safe reconnection, reduces unnecessary polling on setu…
Browse files Browse the repository at this point in the history
…p, removes unused variables.

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
  • Loading branch information
digitaldan committed Jan 27, 2023
1 parent 07a3deb commit 38ad2d8
Showing 1 changed file with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
Expand All @@ -52,6 +54,7 @@
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.Transport;
import io.socket.engineio.client.transports.WebSocket;
import io.socket.parser.Packet;
import io.socket.parser.Parser;
import okhttp3.OkHttpClient.Builder;
Expand Down Expand Up @@ -109,11 +112,6 @@ public class CloudClient {
*/
private boolean isConnected;

/*
* This variable holds version of local openHAB
*/
private String openHABVersion;

/*
* This variable holds instance of Socket.IO client class which provides communication
* with the openHAB Cloud
Expand Down Expand Up @@ -145,7 +143,9 @@ public class CloudClient {
protected final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);

private ScheduledFuture<?> reconnectFuture;
@SuppressWarnings("null")
private final AtomicReference<Optional<ScheduledFuture<?>>> reconnectFuture = new AtomicReference<>(
Optional.empty());

/**
* Constructor of CloudClient
Expand Down Expand Up @@ -176,6 +176,9 @@ public CloudClient(HttpClient httpClient, String uuid, String secret, String bas
public void connect() {
try {
Options options = new Options();
// we always use websockets, this prevents unnecessary polling requests
options.transports = new String[] { WebSocket.NAME };

if (logger.isTraceEnabled()) {
// When trace level logging is enabled, we activate further logging of HTTP calls
// of the Socket.IO library
Expand Down Expand Up @@ -328,13 +331,12 @@ public void call(Object... args) {
logger.warn("Error connecting to the openHAB Cloud instance. Reconnecting.");
}
socket.close();
stopReconnectFuture();
reconnectFuture = scheduler.schedule(new Runnable() {
reconnectFuture.getAndSet(Optional.of(scheduler.schedule(new Runnable() {
@Override
public void run() {
socket.connect();
}
}, delay, TimeUnit.MILLISECONDS);
}, delay, TimeUnit.MILLISECONDS))).ifPresent(future -> future.cancel(true));
}
})//

Expand Down Expand Up @@ -675,30 +677,14 @@ public boolean isConnected() {
*/
public void shutdown() {
logger.info("Shutting down openHAB Cloud service connection");
stopReconnectFuture();
reconnectFuture.get().ifPresent(future -> future.cancel(true));
socket.disconnect();
}

public String getOpenHABVersion() {
return openHABVersion;
}

public void setOpenHABVersion(String openHABVersion) {
this.openHABVersion = openHABVersion;
}

public void setListener(CloudClientListener listener) {
this.listener = listener;
}

private void stopReconnectFuture() {
ScheduledFuture<?> reconnectFuture = this.reconnectFuture;
if (reconnectFuture != null) {
reconnectFuture.cancel(true);
this.reconnectFuture = null;
}
}

private JSONObject getJSONHeaders(HttpFields httpFields) {
JSONObject headersJSON = new JSONObject();
try {
Expand Down

0 comments on commit 38ad2d8

Please sign in to comment.