Skip to content

Commit 93661d6

Browse files
committed
synchronize access to SessionContextStore
1 parent 08dd224 commit 93661d6

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/main/java/de/rwth/idsg/steve/ocpp/ws/AbstractWebSocketEndpoint.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public abstract class AbstractWebSocketEndpoint implements WebSocketHandler {
4949
private final List<Consumer<String>> connectedCallbackList = new ArrayList<>();
5050
private final List<Consumer<String>> disconnectedCallbackList = new ArrayList<>();
5151

52+
private final Object sessionContextLock = new Object();
53+
5254
public void init(Pipeline pipeline) {
5355
this.pipeline = pipeline;
5456
sessionContextStore = new SessionContextStoreImpl(wsSessionSelectStrategy);
@@ -108,11 +110,15 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
108110

109111
String chargeBoxId = getChargeBoxId(session);
110112

111-
int sizeBeforeAdd = sessionContextStore.getSize(chargeBoxId);
112-
113-
sessionContextStore.add(chargeBoxId, session, pingSchedule);
114113
futureResponseContextStore.addSession(session);
115114

115+
int sizeBeforeAdd;
116+
117+
synchronized (sessionContextLock) {
118+
sizeBeforeAdd = sessionContextStore.getSize(chargeBoxId);
119+
sessionContextStore.add(chargeBoxId, session, pingSchedule);
120+
}
121+
116122
// Take into account that there might be multiple connections to a charging station.
117123
// Send notification only for the change 0 -> 1.
118124
if (sizeBeforeAdd == 0) {
@@ -125,10 +131,15 @@ public void afterConnectionClosed(WebSocketSession session, CloseStatus closeSta
125131
log.warn("[id={}] Connection was closed, status: {}", session.getId(), closeStatus);
126132

127133
String chargeBoxId = getChargeBoxId(session);
128-
sessionContextStore.remove(chargeBoxId, session);
134+
129135
futureResponseContextStore.removeSession(session);
130136

131-
int sizeAfterRemove = sessionContextStore.getSize(chargeBoxId);
137+
int sizeAfterRemove;
138+
139+
synchronized (sessionContextLock) {
140+
sessionContextStore.remove(chargeBoxId, session);
141+
sizeAfterRemove = sessionContextStore.getSize(chargeBoxId);
142+
}
132143

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

0 commit comments

Comments
 (0)