Skip to content

Commit

Permalink
Propagate code and reason from disconnect from server on client
Browse files Browse the repository at this point in the history
  • Loading branch information
emilm committed Sep 21, 2022
1 parent 44def2e commit f041a38
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public WebSocketReceiver(WebSocketReceiverEvents handler) {
@Override
public void disconnect() {
receiverEvents.close();
handler.disconnected();
handler.disconnected(0, "disconnect() method called");
}

void relay(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onClose(int code, String reason, boolean remote) {
logger.debug(
"On connection close (code: {}, reason: {}, remote: {})", code, reason, remote);

events.disconnected();
events.disconnected(code, reason);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public void handleError(
}

@Override
public void handleConnectionClosed() {
if (events != null) events.connectionClosed();
public void handleConnectionClosed(int code, String reason) {
if (events != null) events.connectionClosed(code, reason);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ of this software and associated documentation files (the "Software"), to deal
public interface ClientEvents {
void connectionOpened();

void connectionClosed();
void connectionClosed(int code, String reason);
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ public void receivedMessage(Object input) {
}

@Override
public void disconnected() {
events.onDisconnected();
public void disconnected(int code, String reason) {
events.onDisconnected(code, reason);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ public interface CommunicatorEvents {
*/
void onError(String id, String errorCode, String errorDescription, Object payload);

/** The connection was disconnected. */
void onDisconnected();
/** The connection was disconnected.
* @param code
* @param reason*/
void onDisconnected(int code, String reason);

/** A connection was established. */
void onConnected();
Expand Down
6 changes: 4 additions & 2 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/RadioEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface RadioEvents {
*/
void receivedMessage(Object message);

/** Disconnected from node. */
void disconnected();
/** Disconnected from node.
* @param code
* @param reason*/
void disconnected(int code, String reason);
}
2 changes: 1 addition & 1 deletion ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void handleError(
}

@Override
public void handleConnectionClosed() {
public void handleConnectionClosed(int code, String reason) {
Optional<UUID> sessionIdOptional = getSessionID(session);
if (sessionIdOptional.isPresent()) {
serverEvents.lostSession(sessionIdOptional.get());
Expand Down
4 changes: 2 additions & 2 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ public void onError(String id, String errorCode, String errorDescription, Object
}

@Override
public void onDisconnected() {
events.handleConnectionClosed();
public void onDisconnected(int code, String reason) {
events.handleConnectionClosed(code, reason);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public interface SessionEvents {
*/
void handleError(String uniqueId, String errorCode, String errorDescription, Object payload);

/** Handle a closed connection. */
void handleConnectionClosed();
/** Handle a closed connection.
* @param code
* @param reason*/
void handleConnectionClosed(int code, String reason);

/** Handle a opened connection. */
void handleConnectionOpened();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void connect_connectionOpenedEvent() {

// Then
verify(events, times(1)).connectionOpened();
verify(events, never()).connectionClosed();
verify(events, never()).connectionClosed(0, null);
}

@Test
Expand All @@ -98,10 +98,10 @@ public void connect_connectionClosedEvent() {
client.connect(null, events);

// When
this.eventHandler.handleConnectionClosed();
this.eventHandler.handleConnectionClosed(0, null);

// Then
verify(events, times(1)).connectionClosed();
verify(events, times(1)).connectionClosed(0, null);
verify(events, never()).connectionOpened();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void connect() {
public void connectionOpened() {}

@Override
public void connectionClosed() {}
public void connectionClosed(int code, String reason) {}
});
} catch (Exception ex) {
ex.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public void handleError(
}

@Override
public void handleConnectionClosed() {
eventHandler.handleConnectionClosed();
public void handleConnectionClosed(int code, String reason) {
eventHandler.handleConnectionClosed(code, reason);
stopTimer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void disconnect() {
logger.info("disconnect() failed", e);
}
}
events.disconnected();
events.disconnected(0, "disconnect() method called");
receiverEvents.disconnect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void disconnect() {
logger.info("disconnect() failed", e);
}
}
events.disconnected();
events.disconnected(0, "disconnect() method called");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void handleConnectionClosed_opened_endTimeout() throws Exception {
session.open(null, sessionEvents);

// When
sessionEvents.handleConnectionClosed();
sessionEvents.handleConnectionClosed(0, null);

// Then
verify(timeoutTimer, times(1)).end();
Expand All @@ -100,7 +100,7 @@ public void handleConnectionClosed_accepted_endTimeout() throws Exception {
session.accept(sessionEvents);

// When
sessionEvents.handleConnectionClosed();
sessionEvents.handleConnectionClosed(0, null);

// Then
verify(timeoutTimer, times(1)).end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void connect() {
public void connectionOpened() {}

@Override
public void connectionClosed() {}
public void connectionClosed(int code, String reason) {}
});
}

Expand Down

0 comments on commit f041a38

Please sign in to comment.