Skip to content

Commit

Permalink
Restart server on general settings change
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Jan 8, 2024
1 parent 02df8aa commit 44a2aff
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum DataChangeDestination {
DCD_ACTIVEPIPELINESETTINGS,
DCD_GENSETTINGS,
DCD_UI,
DCD_WEBSERVER,
DCD_OTHER;

public static final List<DataChangeDestination> AllDestinations =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import org.photonvision.common.configuration.ConfigManager;
import org.photonvision.common.configuration.NetworkConfig;
import org.photonvision.common.dataflow.DataChangeDestination;
import org.photonvision.common.dataflow.DataChangeService;
import org.photonvision.common.dataflow.DataChangeSource;
import org.photonvision.common.dataflow.events.DataChangeEvent;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
Expand Down Expand Up @@ -148,5 +152,13 @@ public void initialize(boolean shouldManage) {

public void reinitialize() {
initialize(ConfigManager.getInstance().getConfig().getNetworkConfig().shouldManage());

DataChangeService.getInstance()
.publishEvent(
new DataChangeEvent<Boolean>(
DataChangeSource.DCS_OTHER,
DataChangeDestination.DCD_WEBSERVER,
"restartServer",
true));
}
}
2 changes: 1 addition & 1 deletion photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,6 @@ public static void main(String[] args) {

logger.info("Starting server...");
HardwareManager.getInstance().setRunning(true);
Server.start(DEFAULT_WEBPORT);
Server.initialize(DEFAULT_WEBPORT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,14 @@ public void onConnect(WsConnectContext context) {
protected void onClose(WsCloseContext context) {
users.remove(context);
var remote = (InetSocketAddress) context.session.getRemoteAddress();
var host = remote.getAddress().toString() + ":" + remote.getPort();
var reason = context.reason() != null ? context.reason() : "Connection closed by client";
logger.info("Closing websocket connection from " + host + " for reason: " + reason);
// Remote can be null if server is being closed for restart
if (remote != null) {
var host = remote.getAddress().toString() + ":" + remote.getPort();
var reason = context.reason() != null ? context.reason() : "Connection closed by client";
logger.info("Closing websocket connection from " + host + " for reason: " + reason);
} else {
logger.info("Closing websockets for user " + context.getSessionId());
}
}

@SuppressWarnings({"unchecked"})
Expand Down
43 changes: 41 additions & 2 deletions photon-server/src/main/java/org/photonvision/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,42 @@
import io.javalin.Javalin;
import io.javalin.plugin.bundled.CorsPluginConfig;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.StringJoiner;
import org.photonvision.common.dataflow.DataChangeDestination;
import org.photonvision.common.dataflow.DataChangeService;
import org.photonvision.common.dataflow.DataChangeSource;
import org.photonvision.common.dataflow.DataChangeSubscriber;
import org.photonvision.common.dataflow.events.DataChangeEvent;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;

public class Server {
private static final Logger logger = new Logger(Server.class, LogGroup.WebServer);

public static void start(int port) {
var app =
private static Javalin app = null;

static class RestartSubscriber extends DataChangeSubscriber {
private RestartSubscriber() {
super(DataChangeSource.AllSources, List.of(DataChangeDestination.DCD_WEBSERVER));
}

@Override
public void onDataChangeEvent(DataChangeEvent<?> event) {
if (event.propertyName.equals("restartServer")) {
Server.restart();
}
}
}

public static void initialize(int port) {
DataChangeService.getInstance().addSubscriber(new RestartSubscriber());

start(port);
}

private static void start(int port) {
app =
Javalin.create(
javalinConfig -> {
javalinConfig.showJavalinBanner = false;
Expand Down Expand Up @@ -111,5 +138,17 @@ public static void start(int port) {
app.post("/api/calibration/importFromData", RequestHandler::onDataCalibrationImportRequest);

app.start(port);
System.out.println("hi");
}

/**
* Seems like if we change the static IP of this device, Javalin refuses to tell us when new
* Websocket clients connect. As a hack, we can restart the server every time we change static IPs
*/
public static void restart() {
logger.info("Web server going down for restart");
int oldPort = app.port();
app.stop();
start(oldPort);
}
}

0 comments on commit 44a2aff

Please sign in to comment.