Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Implemented websocket PING/PONG mechanism for reliable thing status c…
Browse files Browse the repository at this point in the history
…heck.

Signed-off-by: Alexander Kostadinov <alexander.g.kostadinov@gmail.com>
  • Loading branch information
alex-kostadinov committed Jul 30, 2018
1 parent 8943a2f commit 4269087
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Expand Up @@ -18,6 +18,7 @@ Import-Package:
org.eclipse.jetty.util,
org.eclipse.jetty.util.component,
org.eclipse.jetty.websocket.api,
org.eclipse.jetty.websocket.api.extensions,
org.eclipse.jetty.websocket.client,
org.eclipse.jetty.websocket.common,
org.eclipse.smarthome.binding.bosesoundtouch,
Expand Down
Expand Up @@ -24,7 +24,10 @@

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketFrameListener;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.api.extensions.Frame;
import org.eclipse.jetty.websocket.api.extensions.Frame.Type;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.smarthome.binding.bosesoundtouch.BoseSoundTouchConfiguration;
Expand Down Expand Up @@ -61,8 +64,11 @@
* @author Christian Niessner - Initial contribution
* @author Thomas Traunbauer - Initial contribution
* @author Kai Kreuzer - code clean up
* @author Alexander Kostadinov - Handling of websocket ping-pong mechanism for thing status check
*/
public class BoseSoundTouchHandler extends BaseThingHandler implements WebSocketListener {
public class BoseSoundTouchHandler extends BaseThingHandler implements WebSocketListener, WebSocketFrameListener {

private static final int MAX_MISSED_PONGS_COUNT = 2;

private static final int RETRY_INTERVAL_IN_SECS = 30;

Expand All @@ -76,6 +82,8 @@ public class BoseSoundTouchHandler extends BaseThingHandler implements WebSocket
private CommandExecutor commandExecutor;

private PresetContainer presetContainer;

private int missedPongsCount = 0;

/**
* Creates a new instance of this class for the {@link Thing}.
Expand Down Expand Up @@ -362,9 +370,17 @@ public void onWebSocketBinary(byte[] arr, int pos, int len) {
@Override
public void onWebSocketClose(int code, String reason) {
logger.debug("{}: onClose({}, '{}')", getDeviceName(), code, reason);
missedPongsCount = 0;
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, reason);
commandExecutor.postOperationMode(OperationModeType.OFFLINE);
}

@Override
public void onWebSocketFrame(Frame frame) {
if (frame.getType() == Type.PONG) {
missedPongsCount = 0;
}
}

private void openConnection() {
closeConnection();
Expand All @@ -383,7 +399,7 @@ private void openConnection() {
onWebSocketError(e);
}
}

private void closeConnection() {
if (session != null) {
try {
Expand All @@ -410,15 +426,23 @@ private void checkConnection() {
if (getThing().getStatus() != ThingStatus.ONLINE) {
openConnection(); // try to reconnect....
}
if (getThing().getStatus() == ThingStatus.ONLINE) {

if (getThing().getStatus() == ThingStatus.ONLINE && this.session != null && this.session.isOpen()) {
try {
session.getRemote().sendString("HELLO");
this.session.getRemote().sendPing(null);
missedPongsCount++;
} catch (IOException | NullPointerException e) {
onWebSocketError(e);
closeConnection();
openConnection();
}


if (missedPongsCount == MAX_MISSED_PONGS_COUNT) {
missedPongsCount = 0;
closeConnection();
openConnection();
}
}
}

}

0 comments on commit 4269087

Please sign in to comment.