Skip to content

Commit 43a64e8

Browse files
committed
improve WsSessionSelectStrategy impl
reason: the instances do not have to be Spring beans
1 parent 6388dbc commit 43a64e8

6 files changed

Lines changed: 34 additions & 85 deletions

File tree

src/main/java/de/rwth/idsg/steve/config/OcppConfiguration.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import de.rwth.idsg.steve.ocpp.soap.LoggingFeatureProxy;
44
import de.rwth.idsg.steve.ocpp.soap.MediatorInInterceptor;
55
import de.rwth.idsg.steve.ocpp.soap.MessageIdInterceptor;
6-
import de.rwth.idsg.steve.ocpp.ws.custom.AlwaysLastStrategy;
7-
import de.rwth.idsg.steve.ocpp.ws.custom.RoundRobinStrategy;
8-
import de.rwth.idsg.steve.ocpp.ws.custom.WsSessionSelectStrategy;
96
import org.apache.cxf.Bus;
107
import org.apache.cxf.bus.spring.SpringBus;
118
import org.apache.cxf.common.logging.LogUtils;
@@ -71,18 +68,6 @@ public SpringBus springBus() {
7168
return new SpringBus();
7269
}
7370

74-
@Bean
75-
public WsSessionSelectStrategy sessionSelectStrategy() {
76-
switch (CONFIG.getOcpp().getWsSessionSelectStrategy()) {
77-
case ALWAYS_LAST:
78-
return new AlwaysLastStrategy();
79-
case ROUND_ROBIN:
80-
return new RoundRobinStrategy();
81-
default:
82-
throw new RuntimeException("Could not find a valid WsSessionSelectStrategy");
83-
}
84-
}
85-
8671
private void createOcppService(Object serviceBean, String address,
8772
List<Interceptor<? extends Message>> interceptors,
8873
Collection<? extends Feature> features) {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import de.rwth.idsg.steve.config.WebSocketConfiguration;
44
import de.rwth.idsg.steve.ocpp.OcppVersion;
5-
import de.rwth.idsg.steve.ocpp.ws.custom.WsSessionSelectStrategy;
65
import de.rwth.idsg.steve.ocpp.ws.data.CommunicationContext;
76
import de.rwth.idsg.steve.ocpp.ws.data.SessionContext;
87
import de.rwth.idsg.steve.ocpp.ws.pipeline.IncomingPipeline;
@@ -36,24 +35,21 @@ public abstract class AbstractWebSocketEndpoint implements WebSocketHandler {
3635
@Autowired private ScheduledExecutorService service;
3736
@Autowired private OcppServerRepository ocppServerRepository;
3837
@Autowired private FutureResponseContextStore futureResponseContextStore;
39-
@Autowired private WsSessionSelectStrategy wsSessionSelectStrategy;
4038
@Autowired private NotificationService notificationService;
4139

4240
public static final String CHARGEBOX_ID_KEY = "CHARGEBOX_ID_KEY";
4341

44-
private IncomingPipeline pipeline;
45-
private SessionContextStoreImpl sessionContextStore;
46-
42+
private final SessionContextStoreImpl sessionContextStore = new SessionContextStoreImpl();
4743
private final List<Consumer<String>> connectedCallbackList = new ArrayList<>();
4844
private final List<Consumer<String>> disconnectedCallbackList = new ArrayList<>();
49-
5045
private final Object sessionContextLock = new Object();
5146

47+
private IncomingPipeline pipeline;
48+
5249
public abstract OcppVersion getVersion();
5350

5451
public void init(IncomingPipeline pipeline) {
5552
this.pipeline = pipeline;
56-
sessionContextStore = new SessionContextStoreImpl(wsSessionSelectStrategy);
5753

5854
connectedCallbackList.add((chargeBoxId) -> notificationService.ocppStationWebSocketConnected(chargeBoxId));
5955
disconnectedCallbackList.add((chargeBoxId) -> notificationService.ocppStationWebSocketDisconnected(chargeBoxId));

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.concurrent.ScheduledFuture;
2020
import java.util.concurrent.locks.Lock;
2121

22+
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
23+
2224
/**
2325
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
2426
* @since 17.03.2015
@@ -34,11 +36,7 @@ public class SessionContextStoreImpl implements SessionContextStore {
3436

3537
private final Striped<Lock> locks = Striped.lock(16);
3638

37-
private final WsSessionSelectStrategy wsSessionSelectStrategy;
38-
39-
public SessionContextStoreImpl(WsSessionSelectStrategy wsSessionSelectStrategy) {
40-
this.wsSessionSelectStrategy = wsSessionSelectStrategy;
41-
}
39+
private final WsSessionSelectStrategy wsSessionSelectStrategy = CONFIG.getOcpp().getWsSessionSelectStrategy();
4240

4341
@Override
4442
public void add(String chargeBoxId, WebSocketSession session, ScheduledFuture pingSchedule) {

src/main/java/de/rwth/idsg/steve/ocpp/ws/custom/AlwaysLastStrategy.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/de/rwth/idsg/steve/ocpp/ws/custom/RoundRobinStrategy.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
package de.rwth.idsg.steve.ocpp.ws.custom;
22

3+
import de.rwth.idsg.steve.ocpp.ws.data.SessionContext;
34
import lombok.Getter;
5+
import org.springframework.web.socket.WebSocketSession;
6+
7+
import java.util.Deque;
48

59
/**
610
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
711
* @since 30.04.2015
812
*/
913
@Getter
10-
public enum WsSessionSelectStrategyEnum {
14+
public enum WsSessionSelectStrategyEnum implements WsSessionSelectStrategy {
1115

12-
// Always use the last opened session/connection.
13-
ALWAYS_LAST,
16+
ALWAYS_LAST {
17+
/**
18+
* Always use the last opened session/connection.
19+
*/
20+
@Override
21+
public WebSocketSession getSession(Deque<SessionContext> sessionContexts) {
22+
return sessionContexts.getLast().getSession();
23+
}
24+
},
1425

15-
// The sessions/connections are chosen in a round robin fashion.
16-
// This would allow to distribute load to different connections.
17-
ROUND_ROBIN;
26+
ROUND_ROBIN {
27+
/**
28+
* The sessions/connections are chosen in a round robin fashion.
29+
* This would allow to distribute load to different connections.
30+
*/
31+
@Override
32+
public WebSocketSession getSession(Deque<SessionContext> sessionContexts) {
33+
// Remove the first item, and add at the end
34+
SessionContext s = sessionContexts.removeFirst();
35+
sessionContexts.addLast(s);
36+
return s.getSession();
37+
}
38+
};
1839

1940
public static WsSessionSelectStrategyEnum fromName(String v) {
2041
for (WsSessionSelectStrategyEnum s: WsSessionSelectStrategyEnum.values()) {
2142
if (s.name().equals(v)) {
2243
return s;
2344
}
2445
}
25-
throw new IllegalArgumentException(v);
46+
throw new IllegalArgumentException("Could not find a valid WsSessionSelectStrategy for name: " + v);
2647
}
2748
}

0 commit comments

Comments
 (0)