Skip to content

Commit

Permalink
Always use forcePut for BiHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeilmeier committed Sep 22, 2017
1 parent 98a2837 commit 0ca4144
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
}
}

@Override
protected void _onClose(WebSocket websocket, Optional<BrowserSession> sessionOpt) {
// nothing to do. Session is kept open.
}

/**
* Forward message to OpenEMS websocket.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 21 additions & 15 deletions common/src/io/openems/common/websocket/AbstractWebsocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public abstract class AbstractWebsocketServer<S extends Session<D>, D extends Se

protected abstract void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<JsonArray> jMessageIdOpt,
Optional<String> deviceNameOpt);

protected abstract void _onOpen(WebSocket websocket, ClientHandshake handshake);
protected abstract void _onClose(WebSocket websocket, Optional<S> sessionOpt);

public AbstractWebsocketServer(int port, M sessionManager) {
super(new InetSocketAddress(port));
Expand All @@ -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<S> 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);
}

/**
Expand Down Expand Up @@ -125,13 +131,13 @@ protected JsonObject parseCookieFromHandshake(ClientHandshake handshake) {
*/
protected JsonObject handshakeToJsonObject(ClientHandshake handshake) {
JsonObject j = new JsonObject();
for (Iterator<String> iter = handshake.iterateHttpFields(); iter.hasNext(); ) {
String field = iter.next();
j.addProperty(field, handshake.getFieldValue(field));
for (Iterator<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
*/
if (jMessage.has("authenticate")) {
// add to websockets
this.websockets.put(websocket, session);
this.websockets.forcePut(websocket, session);
// send connection successful to browser
JsonObject jReply = DefaultMessages.browserConnectionSuccessfulReply(session.getToken(),
Optional.of(session.getData().getRole()), new ArrayList<>());
Expand All @@ -135,6 +135,11 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
session.getData().getWebsocketHandler().onMessage(jMessage);
}

@Override
protected void _onClose(WebSocket websocket, Optional<WebsocketApiSession> sessionOpt) {
// nothing to do here
}

/**
* Authenticates a user according to the "authenticate" message. Stores the session if valid.
*
Expand Down

0 comments on commit 0ca4144

Please sign in to comment.