From 0ca414475f20bc90c4b3500d10f0e68610c16dca Mon Sep 17 00:00:00 2001 From: Stefan Feilmeier Date: Fri, 22 Sep 2017 23:41:07 +0200 Subject: [PATCH] Always use forcePut for BiHashMap --- .../BrowserWebsocketSingleton.java | 5 +++ .../core/ConnectionManagerSingleton.java | 4 +-- .../session/OpenemsSessionManager.java | 2 +- .../backend/utilities/MultiKeyMap.java | 2 +- .../websocket/AbstractWebsocketServer.java | 36 +++++++++++-------- .../api/websocket/WebsocketApiServer.java | 7 +++- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/backend/src/main/java/io/openems/backend/browserwebsocket/BrowserWebsocketSingleton.java b/backend/src/main/java/io/openems/backend/browserwebsocket/BrowserWebsocketSingleton.java index 903a384e496..a1fcfc646d7 100644 --- a/backend/src/main/java/io/openems/backend/browserwebsocket/BrowserWebsocketSingleton.java +++ b/backend/src/main/java/io/openems/backend/browserwebsocket/BrowserWebsocketSingleton.java @@ -166,6 +166,11 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional sessionOpt) { + // nothing to do. Session is kept open. + } + /** * Forward message to OpenEMS websocket. * diff --git a/backend/src/main/java/io/openems/backend/core/ConnectionManagerSingleton.java b/backend/src/main/java/io/openems/backend/core/ConnectionManagerSingleton.java index 6b0478716ab..2ecfc7137c4 100644 --- a/backend/src/main/java/io/openems/backend/core/ConnectionManagerSingleton.java +++ b/backend/src/main/java/io/openems/backend/core/ConnectionManagerSingleton.java @@ -45,7 +45,7 @@ public void removeWebsocketInterconnection(WebSocket browserWebsocket, WebSocket * Helper methods for Browser websockets */ public void addBrowserWebsocket(WebSocket websocket, BrowserSession session) { - this.browserWebsockets.put(websocket, session); + this.browserWebsockets.forcePut(websocket, session); } public void removeBrowserWebsocket(WebSocket websocket) { @@ -57,7 +57,7 @@ public void removeBrowserWebsocket(WebSocket websocket) { * Helper methods for OpenEMS websockets */ public void addOpenemsWebsocket(WebSocket websocket, String deviceName) { - this.openemsWebsockets.put(websocket, deviceName); + this.openemsWebsockets.forcePut(websocket, deviceName); } public void removeOpenemsWebsocket(WebSocket websocket) { diff --git a/backend/src/main/java/io/openems/backend/openemswebsocket/session/OpenemsSessionManager.java b/backend/src/main/java/io/openems/backend/openemswebsocket/session/OpenemsSessionManager.java index 9f528d4010d..27183d71376 100644 --- a/backend/src/main/java/io/openems/backend/openemswebsocket/session/OpenemsSessionManager.java +++ b/backend/src/main/java/io/openems/backend/openemswebsocket/session/OpenemsSessionManager.java @@ -21,7 +21,7 @@ public OpenemsSession _createNewSession(String token, OpenemsSessionData data) { @Override protected void _putSession(String token, OpenemsSession session) { super._putSession(token, session); - this.token2name.put(token, session.getData().getDevice().getName()); + this.token2name.forcePut(token, session.getData().getDevice().getName()); } @Override diff --git a/backend/src/main/java/io/openems/backend/utilities/MultiKeyMap.java b/backend/src/main/java/io/openems/backend/utilities/MultiKeyMap.java index f174c88b26f..752283aa345 100644 --- a/backend/src/main/java/io/openems/backend/utilities/MultiKeyMap.java +++ b/backend/src/main/java/io/openems/backend/utilities/MultiKeyMap.java @@ -15,7 +15,7 @@ public MultiKeyMap() {} public void put(K1 key1, K2 key2, V value) { k1Map.put(key1, value); - k2Map.put(key2, key1); + k2Map.forcePut(key2, key1); } public void put(K1 key1, V value) { diff --git a/common/src/io/openems/common/websocket/AbstractWebsocketServer.java b/common/src/io/openems/common/websocket/AbstractWebsocketServer.java index ee660c11f58..5b34311e9d4 100644 --- a/common/src/io/openems/common/websocket/AbstractWebsocketServer.java +++ b/common/src/io/openems/common/websocket/AbstractWebsocketServer.java @@ -30,8 +30,9 @@ public abstract class AbstractWebsocketServer, D extends Se protected abstract void _onMessage(WebSocket websocket, JsonObject jMessage, Optional jMessageIdOpt, Optional deviceNameOpt); - + protected abstract void _onOpen(WebSocket websocket, ClientHandshake handshake); + protected abstract void _onClose(WebSocket websocket, Optional sessionOpt); public AbstractWebsocketServer(int port, M sessionManager) { super(new InetSocketAddress(port)); @@ -52,19 +53,24 @@ public final void onOpen(WebSocket websocket, ClientHandshake handshake) { } /** - * Close event of websocket. Removes the websocket. Keeps the session + * Close event of websocket. Removes the websocket. Keeps the session. Calls _onClose() */ @Override - public void onClose(WebSocket websocket, int code, String reason, boolean remote) { - S session = this.websockets.get(websocket); - String sessionString; - if (session == null) { - sessionString = ""; - } else { - sessionString = session.toString(); + public final void onClose(WebSocket websocket, int code, String reason, boolean remote) { + try { + Optional sessionOpt = Optional.ofNullable(this.websockets.get(websocket)); + String sessionString; + if (sessionOpt.isPresent()) { + sessionString = sessionOpt.get().toString() + " "; + } else { + sessionString = ""; + } + log.info(sessionString + "Websocket closed. Code [" + code + "] Reason [" + reason + "]"); + this.websockets.remove(websocket); + this._onClose(websocket, sessionOpt); + } catch (Throwable e) { + log.error("onClose-Error. Code [" + code + "] Reason [" + reason + "]: " + e.getMessage()); } - log.info(sessionString + " Websocket closed. Code [" + code + "] Reason [" + reason + "]. Total websockets [" + this.websockets.size() + "]"); - this.websockets.remove(websocket); } /** @@ -125,13 +131,13 @@ protected JsonObject parseCookieFromHandshake(ClientHandshake handshake) { */ protected JsonObject handshakeToJsonObject(ClientHandshake handshake) { JsonObject j = new JsonObject(); - for (Iterator iter = handshake.iterateHttpFields(); iter.hasNext(); ) { - String field = iter.next(); - j.addProperty(field, handshake.getFieldValue(field)); + for (Iterator iter = handshake.iterateHttpFields(); iter.hasNext();) { + String field = iter.next(); + j.addProperty(field, handshake.getFieldValue(field)); } return j; } - + @Override public final void onStart() { // nothing to do diff --git a/edge/src/io/openems/impl/controller/api/websocket/WebsocketApiServer.java b/edge/src/io/openems/impl/controller/api/websocket/WebsocketApiServer.java index a3277571212..4431995a3c8 100644 --- a/edge/src/io/openems/impl/controller/api/websocket/WebsocketApiServer.java +++ b/edge/src/io/openems/impl/controller/api/websocket/WebsocketApiServer.java @@ -120,7 +120,7 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional()); @@ -135,6 +135,11 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional sessionOpt) { + // nothing to do here + } + /** * Authenticates a user according to the "authenticate" message. Stores the session if valid. *