Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket endpoint #119

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sourceCompatibility = 17
// Set our project variables
project.ext {
dropwizardVersion = '4.0.5'
jettyVersion = '11.0.19'
}

repositories {
Expand Down Expand Up @@ -62,6 +63,8 @@ dependencies {
'io.dropwizard:dropwizard-hibernate:' + dropwizardVersion,
'io.dropwizard:dropwizard-auth:' + dropwizardVersion,
'io.dropwizard:dropwizard-jdbi3:' + dropwizardVersion,
'org.eclipse.jetty.websocket:websocket-jetty-api:' + jettyVersion,
'org.eclipse.jetty.websocket:websocket-jetty-server:' + jettyVersion,
)
// Database
implementation('com.h2database:h2:2.2.220')
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/telraam/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.servlet.DispatcherType;
import jakarta.servlet.FilterRegistration;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.jdbi.v3.core.Jdbi;
import telraam.api.*;
import telraam.database.daos.*;
Expand All @@ -21,6 +22,7 @@
import telraam.logic.robust.RobustLapper;
import telraam.station.Fetcher;
import telraam.util.AcceptedLapsUtil;
import telraam.websocket.WebSocketConnection;

import java.io.IOException;
import java.util.EnumSet;
Expand Down Expand Up @@ -78,6 +80,15 @@ public void run(AppConfiguration configuration, Environment environment) throws
// Initialize AcceptedLapUtil
AcceptedLapsUtil.createInstance(this.database);

// Register websocket endpoint
JettyWebSocketServletContainerInitializer.configure(
environment.getApplicationContext(),
(servletContext, wsContainer) -> {
wsContainer.setMaxTextMessageSize(65535);
wsContainer.addMapping("/ws", (req, res) -> new WebSocketConnection());
}
);

// Add api resources
JerseyEnvironment jersey = environment.jersey();
jersey.register(new BatonResource(database.onDemand(BatonDAO.class)));
Expand All @@ -94,7 +105,6 @@ public void run(AppConfiguration configuration, Environment environment) throws
jersey.register(new MonitoringResource(database));
environment.healthChecks().register("template", new TemplateHealthCheck(configuration.getTemplate()));


// Enable CORS
final FilterRegistration.Dynamic cors = environment.servlets().addFilter("CORS", CrossOriginFilter.class);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/telraam/api/TeamResource.java
Topvennie marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Objects;
import java.util.Optional;

import telraam.websocket.WebSocketMessageSingleton;


@Path("/team")
@Tag(name="Team")
Expand Down Expand Up @@ -48,6 +50,14 @@ public int create(Team team) {
));
}

WebSocketMessageSingleton.getInstance().sendToAll(
"new team (%s, %s, %s)".formatted(
team.getName(),
team.getId(),
team.getBatonId()
)
);

return ret;
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/telraam/websocket/WebSocketConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package telraam.websocket;

import java.io.IOException;
import java.util.logging.Logger;

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;

public class WebSocketConnection extends WebSocketAdapter {
private static final Logger logger = Logger.getLogger(WebSocketConnection.class.getName());

@Override
public void onWebSocketConnect(Session session) {
super.onWebSocketConnect(session);
WebSocketMessageSingleton.getInstance().registerConnection(this);
logger.info("Instance with remote \"%s\" connected".formatted(getRemote().getRemoteAddress()));
}

@Override
public void onWebSocketClose(int statusCode, String reason) {
super.onWebSocketClose(statusCode, reason);
WebSocketMessageSingleton.getInstance().unregisterConnection(this);
logger.info("Instance with remote \"%s\" closed: [%s] %s".formatted(getRemote().getRemoteAddress(), statusCode, reason));
}

@Override
public void onWebSocketError(Throwable cause) {
super.onWebSocketError(cause);
logger.severe("WebSocket error in instance with remote \"%s\": %s".formatted(getRemote().getRemoteAddress(), cause));
}

public void send(String s) {
try {
getRemote().sendString(s);
} catch (IOException e) {
logger.severe("Sending \"%s\" through instance with remote \"%s\" failed with %s".formatted(s, getRemote().getRemoteAddress(), e));
return;
}
logger.finest("Sent \"%s\" through instance with remote \"%s\"".formatted(s, getRemote().getRemoteAddress()));
}
}
37 changes: 37 additions & 0 deletions src/main/java/telraam/websocket/WebSocketMessageSingleton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package telraam.websocket;

import lombok.Getter;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

public class WebSocketMessageSingleton {
private static final Logger logger = Logger.getLogger(WebSocketMessageSingleton.class.getName());

@Getter
private static final WebSocketMessageSingleton instance = new WebSocketMessageSingleton();
private static final Set<WebSocketConnection> registeredConnections = new HashSet<>();

private WebSocketMessageSingleton() {
}

public void registerConnection(WebSocketConnection conn) {
boolean modified = registeredConnections.add(conn);
if (modified) {
logger.info("Registered WebSocketConnection %s".formatted(conn));
}
}

public void unregisterConnection(WebSocketConnection conn) {
boolean modified = registeredConnections.remove(conn);
if (modified) {
logger.info("Unregistered WebSocketConnection %s".formatted(conn));
}
}

public void sendToAll(String s) {
logger.info("Sending \"%s\" to all registered WebSocketConnection instances".formatted(s));
registeredConnections.forEach(conn -> conn.send(s));
}
}
Loading