|
| 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