mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-19 00:41:41 +00:00
Make NT client name the same as hostname (#2107)
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
package org.photonvision.common.dataflow.networktables;
|
||||
|
||||
import edu.wpi.first.apriltag.AprilTagFieldLayout;
|
||||
import edu.wpi.first.cscore.CameraServerJNI;
|
||||
import edu.wpi.first.networktables.LogMessage;
|
||||
import edu.wpi.first.networktables.MultiSubscriber;
|
||||
import edu.wpi.first.networktables.NetworkTable;
|
||||
@@ -325,7 +326,7 @@ public class NetworkTablesManager {
|
||||
if (config.runNTServer) {
|
||||
setServerMode();
|
||||
} else {
|
||||
setClientMode(config.ntServerAddress);
|
||||
setClientMode(config);
|
||||
}
|
||||
|
||||
m_timeSync.setConfig(config);
|
||||
@@ -337,17 +338,20 @@ public class NetworkTablesManager {
|
||||
return m_timeSync.getOffset();
|
||||
}
|
||||
|
||||
private void setClientMode(String ntServerAddress) {
|
||||
private void setClientMode(NetworkConfig config) {
|
||||
ntInstance.stopServer();
|
||||
ntInstance.startClient4("photonvision");
|
||||
ntInstance.stopClient();
|
||||
String hostname = config.shouldManage ? config.hostname : CameraServerJNI.getHostname();
|
||||
logger.debug("Starting NT Client with hostname: " + hostname);
|
||||
ntInstance.startClient4(hostname);
|
||||
try {
|
||||
int t = Integer.parseInt(ntServerAddress);
|
||||
int t = Integer.parseInt(config.ntServerAddress);
|
||||
if (!m_isRetryingConnection) logger.info("Starting NT Client, server team is " + t);
|
||||
ntInstance.setServerTeam(t);
|
||||
} catch (NumberFormatException e) {
|
||||
if (!m_isRetryingConnection)
|
||||
logger.info("Starting NT Client, server IP is \"" + ntServerAddress + "\"");
|
||||
ntInstance.setServer(ntServerAddress);
|
||||
logger.info("Starting NT Client, server IP is \"" + config.ntServerAddress + "\"");
|
||||
ntInstance.setServer(config.ntServerAddress);
|
||||
}
|
||||
ntInstance.startDSClient();
|
||||
broadcastVersion();
|
||||
|
||||
@@ -29,6 +29,7 @@ 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.dataflow.networktables.NetworkTablesManager;
|
||||
import org.photonvision.common.hardware.Platform;
|
||||
import org.photonvision.common.hardware.PlatformUtils;
|
||||
import org.photonvision.common.logging.LogGroup;
|
||||
@@ -121,7 +122,7 @@ public class NetworkManager {
|
||||
|
||||
// always set hostname (unless it's blank)
|
||||
if (!config.hostname.isBlank()) {
|
||||
setHostname(config.hostname);
|
||||
setHostname(config);
|
||||
} else {
|
||||
logger.warn("Got empty hostname?");
|
||||
}
|
||||
@@ -145,29 +146,33 @@ public class NetworkManager {
|
||||
true));
|
||||
}
|
||||
|
||||
private void setHostname(String hostname) {
|
||||
private void setHostname(NetworkConfig config) {
|
||||
try {
|
||||
var shell = new ShellExec(true, false);
|
||||
shell.executeBashCommand("cat /etc/hostname | tr -d \" \\t\\n\\r\"");
|
||||
var oldHostname = shell.getOutput().replace("\n", "");
|
||||
logger.debug("Old host name: \"" + oldHostname + "\"");
|
||||
logger.debug("New host name: \"" + hostname + "\"");
|
||||
logger.debug("New host name: \"" + config.hostname + "\"");
|
||||
|
||||
if (!oldHostname.equals(hostname)) {
|
||||
if (!oldHostname.equals(config.hostname)) {
|
||||
var setHostnameRetCode =
|
||||
shell.executeBashCommand(
|
||||
"echo $NEW_HOSTNAME > /etc/hostname".replace("$NEW_HOSTNAME", hostname));
|
||||
setHostnameRetCode = shell.executeBashCommand("hostnamectl set-hostname " + hostname);
|
||||
"echo $NEW_HOSTNAME > /etc/hostname".replace("$NEW_HOSTNAME", config.hostname));
|
||||
setHostnameRetCode =
|
||||
shell.executeBashCommand("hostnamectl set-hostname " + config.hostname);
|
||||
|
||||
// Add to /etc/hosts
|
||||
var addHostRetCode =
|
||||
shell.executeBashCommand(
|
||||
String.format(
|
||||
"sed -i \"s/127.0.1.1.*%s/127.0.1.1\\t%s/g\" /etc/hosts",
|
||||
oldHostname, hostname));
|
||||
oldHostname, config.hostname));
|
||||
|
||||
shell.executeBashCommand("systemctl restart avahi-daemon.service");
|
||||
|
||||
// This resets the NetworkTables config to use the new hostname as the client ID
|
||||
NetworkTablesManager.getInstance().setConfig(config);
|
||||
|
||||
var success = setHostnameRetCode == 0 && addHostRetCode == 0;
|
||||
if (!success) {
|
||||
logger.error(
|
||||
@@ -177,7 +182,7 @@ public class NetworkManager {
|
||||
+ addHostRetCode
|
||||
+ "!");
|
||||
} else {
|
||||
logger.info("Set hostname to " + hostname);
|
||||
logger.info("Set hostname to " + config.hostname);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user