Skip to content

Commit

Permalink
synchronize access to SessionContextStore
Browse files Browse the repository at this point in the history
  • Loading branch information
goekay committed Jul 27, 2016
1 parent 08dd224 commit 93661d6
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public abstract class AbstractWebSocketEndpoint implements WebSocketHandler {
private final List<Consumer<String>> connectedCallbackList = new ArrayList<>();
private final List<Consumer<String>> disconnectedCallbackList = new ArrayList<>();

private final Object sessionContextLock = new Object();

public void init(Pipeline pipeline) {
this.pipeline = pipeline;
sessionContextStore = new SessionContextStoreImpl(wsSessionSelectStrategy);
Expand Down Expand Up @@ -108,11 +110,15 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio

String chargeBoxId = getChargeBoxId(session);

int sizeBeforeAdd = sessionContextStore.getSize(chargeBoxId);

sessionContextStore.add(chargeBoxId, session, pingSchedule);
futureResponseContextStore.addSession(session);

int sizeBeforeAdd;

synchronized (sessionContextLock) {
sizeBeforeAdd = sessionContextStore.getSize(chargeBoxId);
sessionContextStore.add(chargeBoxId, session, pingSchedule);
}

// Take into account that there might be multiple connections to a charging station.
// Send notification only for the change 0 -> 1.
if (sizeBeforeAdd == 0) {
Expand All @@ -125,10 +131,15 @@ public void afterConnectionClosed(WebSocketSession session, CloseStatus closeSta
log.warn("[id={}] Connection was closed, status: {}", session.getId(), closeStatus);

String chargeBoxId = getChargeBoxId(session);
sessionContextStore.remove(chargeBoxId, session);

futureResponseContextStore.removeSession(session);

int sizeAfterRemove = sessionContextStore.getSize(chargeBoxId);
int sizeAfterRemove;

synchronized (sessionContextLock) {
sessionContextStore.remove(chargeBoxId, session);
sizeAfterRemove = sessionContextStore.getSize(chargeBoxId);
}

// Take into account that there might be multiple connections to a charging station.
// Send notification only for the change 1 -> 0.
Expand Down

0 comments on commit 93661d6

Please sign in to comment.