Skip to content

Commit 887b55c

Browse files
committed
extract interface FutureResponseContextStore
reason: to enable multiple implementations
1 parent 3bfcc82 commit 887b55c

File tree

2 files changed

+101
-59
lines changed

2 files changed

+101
-59
lines changed

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

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,20 @@
1919
package de.rwth.idsg.steve.ocpp.ws;
2020

2121
import de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext;
22-
import lombok.AccessLevel;
23-
import lombok.RequiredArgsConstructor;
24-
import lombok.extern.slf4j.Slf4j;
2522
import org.jetbrains.annotations.Nullable;
26-
import org.springframework.stereotype.Service;
2723
import org.springframework.web.socket.WebSocketSession;
2824

29-
import java.util.Map;
30-
import java.util.concurrent.ConcurrentHashMap;
31-
import java.util.function.BiFunction;
32-
3325
/**
34-
* Presumption: The responses must be sent using the same connection as the requests!
35-
*
3626
* @author Sevket Goekay <sevketgokay@gmail.com>
37-
* @since 21.03.2015
27+
* @since 08.02.2025
3828
*/
39-
@Slf4j
40-
@Service
41-
public class FutureResponseContextStore {
42-
43-
// We store for each chargeBox connection, multiple pairs of (messageId, context)
44-
// (session, (messageId, context))
45-
private final Map<WebSocketSession, Map<String, FutureResponseContext>> lookupTable = new ConcurrentHashMap<>();
46-
47-
public void addSession(WebSocketSession session) {
48-
addIfAbsent(session);
49-
}
50-
51-
public void removeSession(WebSocketSession session) {
52-
log.debug("Deleting the store for sessionId '{}'", session.getId());
53-
lookupTable.remove(session);
54-
}
55-
56-
public void add(WebSocketSession session, String messageId, FutureResponseContext context) {
57-
Map<String, FutureResponseContext> map = addIfAbsent(session);
58-
map.put(messageId, context);
59-
log.debug("Store size for sessionId '{}': {}", session.getId(), map.size());
60-
}
61-
62-
@Nullable
63-
public FutureResponseContext get(WebSocketSession session, String messageId) {
64-
RemoveFunction removeFunction = new RemoveFunction(messageId);
65-
lookupTable.computeIfPresent(session, removeFunction);
66-
return removeFunction.removedContext;
67-
}
29+
public interface FutureResponseContextStore {
6830

69-
private Map<String, FutureResponseContext> addIfAbsent(WebSocketSession session) {
70-
return lookupTable.computeIfAbsent(session, innerSession -> {
71-
log.debug("Creating new store for sessionId '{}'", innerSession.getId());
72-
return new ConcurrentHashMap<>();
73-
});
74-
}
31+
void addSession(WebSocketSession session);
7532

76-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
77-
private static class RemoveFunction implements
78-
BiFunction<WebSocketSession, Map<String, FutureResponseContext>, Map<String, FutureResponseContext>> {
33+
void removeSession(WebSocketSession session);
7934

80-
private final String messageId;
81-
@Nullable private FutureResponseContext removedContext;
35+
void add(WebSocketSession session, String messageId, FutureResponseContext context);
8236

83-
@Override
84-
public Map<String, FutureResponseContext> apply(WebSocketSession session,
85-
Map<String, FutureResponseContext> map) {
86-
removedContext = map.remove(messageId);
87-
log.debug("Store size for sessionId '{}': {}", session.getId(), map.size());
88-
return map;
89-
}
90-
}
37+
@Nullable FutureResponseContext get(WebSocketSession session, String messageId);
9138
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve
3+
* Copyright (C) 2013-2025 SteVe Community Team
4+
* All Rights Reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package de.rwth.idsg.steve.ocpp.ws;
20+
21+
import de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext;
22+
import lombok.AccessLevel;
23+
import lombok.RequiredArgsConstructor;
24+
import lombok.extern.slf4j.Slf4j;
25+
import org.jetbrains.annotations.Nullable;
26+
import org.springframework.stereotype.Service;
27+
import org.springframework.web.socket.WebSocketSession;
28+
29+
import java.util.Map;
30+
import java.util.concurrent.ConcurrentHashMap;
31+
import java.util.function.BiFunction;
32+
33+
/**
34+
* Presumption: The responses must be sent using the same connection as the requests!
35+
*
36+
* @author Sevket Goekay <sevketgokay@gmail.com>
37+
* @since 21.03.2015
38+
*/
39+
@Slf4j
40+
@Service
41+
public class FutureResponseContextStoreImpl implements FutureResponseContextStore {
42+
43+
// We store for each chargeBox connection, multiple pairs of (messageId, context)
44+
// (session, (messageId, context))
45+
private final Map<WebSocketSession, Map<String, FutureResponseContext>> lookupTable = new ConcurrentHashMap<>();
46+
47+
@Override
48+
public void addSession(WebSocketSession session) {
49+
addIfAbsent(session);
50+
}
51+
52+
@Override
53+
public void removeSession(WebSocketSession session) {
54+
log.debug("Deleting the store for sessionId '{}'", session.getId());
55+
lookupTable.remove(session);
56+
}
57+
58+
@Override
59+
public void add(WebSocketSession session, String messageId, FutureResponseContext context) {
60+
Map<String, FutureResponseContext> map = addIfAbsent(session);
61+
map.put(messageId, context);
62+
log.debug("Store size for sessionId '{}': {}", session.getId(), map.size());
63+
}
64+
65+
@Nullable
66+
@Override
67+
public FutureResponseContext get(WebSocketSession session, String messageId) {
68+
RemoveFunction removeFunction = new RemoveFunction(messageId);
69+
lookupTable.computeIfPresent(session, removeFunction);
70+
return removeFunction.removedContext;
71+
}
72+
73+
private Map<String, FutureResponseContext> addIfAbsent(WebSocketSession session) {
74+
return lookupTable.computeIfAbsent(session, innerSession -> {
75+
log.debug("Creating new store for sessionId '{}'", innerSession.getId());
76+
return new ConcurrentHashMap<>();
77+
});
78+
}
79+
80+
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
81+
private static class RemoveFunction implements
82+
BiFunction<WebSocketSession, Map<String, FutureResponseContext>, Map<String, FutureResponseContext>> {
83+
84+
private final String messageId;
85+
@Nullable private FutureResponseContext removedContext;
86+
87+
@Override
88+
public Map<String, FutureResponseContext> apply(WebSocketSession session,
89+
Map<String, FutureResponseContext> map) {
90+
removedContext = map.remove(messageId);
91+
log.debug("Store size for sessionId '{}': {}", session.getId(), map.size());
92+
return map;
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)