From 887b55c8f51cb6bc82bb160ce5850b314b6d0b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sevket=20G=C3=B6kay?= Date: Sat, 8 Feb 2025 23:34:28 +0100 Subject: [PATCH] extract interface FutureResponseContextStore reason: to enable multiple implementations --- .../ocpp/ws/FutureResponseContextStore.java | 65 ++----------- .../ws/FutureResponseContextStoreImpl.java | 95 +++++++++++++++++++ 2 files changed, 101 insertions(+), 59 deletions(-) create mode 100644 src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStoreImpl.java diff --git a/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStore.java b/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStore.java index 899f87892..27043e681 100644 --- a/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStore.java +++ b/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStore.java @@ -19,73 +19,20 @@ package de.rwth.idsg.steve.ocpp.ws; import de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext; -import lombok.AccessLevel; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.Nullable; -import org.springframework.stereotype.Service; import org.springframework.web.socket.WebSocketSession; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.BiFunction; - /** - * Presumption: The responses must be sent using the same connection as the requests! - * * @author Sevket Goekay - * @since 21.03.2015 + * @since 08.02.2025 */ -@Slf4j -@Service -public class FutureResponseContextStore { - - // We store for each chargeBox connection, multiple pairs of (messageId, context) - // (session, (messageId, context)) - private final Map> lookupTable = new ConcurrentHashMap<>(); - - public void addSession(WebSocketSession session) { - addIfAbsent(session); - } - - public void removeSession(WebSocketSession session) { - log.debug("Deleting the store for sessionId '{}'", session.getId()); - lookupTable.remove(session); - } - - public void add(WebSocketSession session, String messageId, FutureResponseContext context) { - Map map = addIfAbsent(session); - map.put(messageId, context); - log.debug("Store size for sessionId '{}': {}", session.getId(), map.size()); - } - - @Nullable - public FutureResponseContext get(WebSocketSession session, String messageId) { - RemoveFunction removeFunction = new RemoveFunction(messageId); - lookupTable.computeIfPresent(session, removeFunction); - return removeFunction.removedContext; - } +public interface FutureResponseContextStore { - private Map addIfAbsent(WebSocketSession session) { - return lookupTable.computeIfAbsent(session, innerSession -> { - log.debug("Creating new store for sessionId '{}'", innerSession.getId()); - return new ConcurrentHashMap<>(); - }); - } + void addSession(WebSocketSession session); - @RequiredArgsConstructor(access = AccessLevel.PRIVATE) - private static class RemoveFunction implements - BiFunction, Map> { + void removeSession(WebSocketSession session); - private final String messageId; - @Nullable private FutureResponseContext removedContext; + void add(WebSocketSession session, String messageId, FutureResponseContext context); - @Override - public Map apply(WebSocketSession session, - Map map) { - removedContext = map.remove(messageId); - log.debug("Store size for sessionId '{}': {}", session.getId(), map.size()); - return map; - } - } + @Nullable FutureResponseContext get(WebSocketSession session, String messageId); } diff --git a/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStoreImpl.java b/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStoreImpl.java new file mode 100644 index 000000000..9ecf5233c --- /dev/null +++ b/src/main/java/de/rwth/idsg/steve/ocpp/ws/FutureResponseContextStoreImpl.java @@ -0,0 +1,95 @@ +/* + * SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve + * Copyright (C) 2013-2025 SteVe Community Team + * All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.rwth.idsg.steve.ocpp.ws; + +import de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.Nullable; +import org.springframework.stereotype.Service; +import org.springframework.web.socket.WebSocketSession; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiFunction; + +/** + * Presumption: The responses must be sent using the same connection as the requests! + * + * @author Sevket Goekay + * @since 21.03.2015 + */ +@Slf4j +@Service +public class FutureResponseContextStoreImpl implements FutureResponseContextStore { + + // We store for each chargeBox connection, multiple pairs of (messageId, context) + // (session, (messageId, context)) + private final Map> lookupTable = new ConcurrentHashMap<>(); + + @Override + public void addSession(WebSocketSession session) { + addIfAbsent(session); + } + + @Override + public void removeSession(WebSocketSession session) { + log.debug("Deleting the store for sessionId '{}'", session.getId()); + lookupTable.remove(session); + } + + @Override + public void add(WebSocketSession session, String messageId, FutureResponseContext context) { + Map map = addIfAbsent(session); + map.put(messageId, context); + log.debug("Store size for sessionId '{}': {}", session.getId(), map.size()); + } + + @Nullable + @Override + public FutureResponseContext get(WebSocketSession session, String messageId) { + RemoveFunction removeFunction = new RemoveFunction(messageId); + lookupTable.computeIfPresent(session, removeFunction); + return removeFunction.removedContext; + } + + private Map addIfAbsent(WebSocketSession session) { + return lookupTable.computeIfAbsent(session, innerSession -> { + log.debug("Creating new store for sessionId '{}'", innerSession.getId()); + return new ConcurrentHashMap<>(); + }); + } + + @RequiredArgsConstructor(access = AccessLevel.PRIVATE) + private static class RemoveFunction implements + BiFunction, Map> { + + private final String messageId; + @Nullable private FutureResponseContext removedContext; + + @Override + public Map apply(WebSocketSession session, + Map map) { + removedContext = map.remove(messageId); + log.debug("Store size for sessionId '{}': {}", session.getId(), map.size()); + return map; + } + } +}