Skip to content

Commit

Permalink
Add getLastError() API to MultiProtocolJSONClient
Browse files Browse the repository at this point in the history
To allow determining the cause of a disconnection, add a new API to
MultiProtocolJSONClient to retrieve the Exception passed from the
WebSocket library in the onError() callback.

This is required to allow logging the OCPP 2.0.1 Security Events
"InvalidTLSVersion" and "InvalidTLSCipherSuite", by examining the type
of Exception and its message string.
  • Loading branch information
robert-s-ubi committed Jun 28, 2024
1 parent 63fe226 commit 7904a1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public void disconnect() {
client.disconnect();
}

public Exception getLastError() {
return transmitter.getLastError();
}

@Override
public boolean isClosed() {
return transmitter.isClosed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class MultiProtocolWebSocketTransmitter implements Transmitter {
private final JSONConfiguration configuration;
private final Draft draft;

private volatile Exception lastError = null;
private volatile boolean closed = true;
private volatile WebSocketClient client;
private WssSocketBuilder wssSocketBuilder;
Expand All @@ -71,6 +72,7 @@ public MultiProtocolWebSocketTransmitter(
public void connect(String uri, RadioEvents events) {
final URI resource = URI.create(uri);

lastError = null;
Map<String, String> httpHeaders = new HashMap<>();
String username = configuration.getParameter(JSONConfiguration.USERNAME_PARAMETER);
Object password = configuration.getParameter(JSONConfiguration.PASSWORD_PARAMETER);
Expand Down Expand Up @@ -120,6 +122,7 @@ public void onClose(int code, String reason, boolean remote) {

@Override
public void onError(Exception ex) {
lastError = ex;
if (ex instanceof ConnectException) {
logger.error("On error triggered caused by:", ex);
} else {
Expand Down Expand Up @@ -261,6 +264,10 @@ public void send(Object request) throws NotConnectedException {
}
}

public Exception getLastError() {
return lastError;
}

public boolean isClosed() {
return closed;
}
Expand Down

0 comments on commit 7904a1f

Please sign in to comment.