Fix concurrent modification in DataFeedHandler

This commit is contained in:
Ryan Butler committed 2022-05-03 00:41:25 -04:00
1 parent 6d940503af
commit a800749b87
4 files changed
+22 -29

No files matched your search

@@ -13,60 +13,59 @@ import org.java_websocket.server.WebSocketServer;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class WebsocketAPI extends WebSocketServer implements ProtocolAPIServer {
public final VRServer server;
public final ProtocolAPI protocolAPI;
private final Map<Integer, GenericConnection> websocketConnections = new HashMap<>();
public WebsocketAPI(VRServer server, ProtocolAPI protocolAPI) {
super(new InetSocketAddress(21110), Collections.singletonList(new Draft_6455()));
this.server = server;
this.protocolAPI = protocolAPI;
this.protocolAPI.registerAPIServer(this);
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
LogManager.log.info("[WebSocketAPI] New connection from: " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
this.websocketConnections.put(conn.hashCode(), new WebsocketConnection(conn));
conn.setAttachment(new WebsocketConnection(conn));
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
LogManager.log.info("[WebSocketAPI] Disconnected: " + conn.getRemoteSocketAddress().getAddress().getHostAddress() + ", (" + code + ") " + reason + ". Remote: " + remote);
this.websocketConnections.remove(conn.hashCode());
}
@Override
public void onMessage(WebSocket conn, String message) {
}
@Override
public void onMessage(WebSocket conn, ByteBuffer message) {
GenericConnection connection = this.websocketConnections.get(conn.hashCode());
if (connection != null)
var connection = conn.<WebsocketConnection> getAttachment();
if(connection != null)
this.protocolAPI.onMessage(connection, message);
}
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}
@Override
public void onStart() {
LogManager.log.info("[WebSocketAPI] Web Socket API started on port " + getPort());
setConnectionLostTimeout(0);
}
@Override
public Map<Integer, GenericConnection> getAPIConnections() {
return this.websocketConnections;
public Stream<GenericConnection> getAPIConnections() {
return this.getConnections().stream().map(conn -> {
var c = conn.<WebsocketConnection> getAttachment();
return (GenericConnection) c;
}).filter(c -> c != null);
}
}