Skip to content

Commit 1ee2ecb

Browse files
authored
Make NT client name the same as hostname (#2107)
1 parent 82d6b6b commit 1ee2ecb

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

photon-core/src/main/java/org/photonvision/common/dataflow/networktables/NetworkTablesManager.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.photonvision.common.dataflow.networktables;
1919

2020
import edu.wpi.first.apriltag.AprilTagFieldLayout;
21+
import edu.wpi.first.cscore.CameraServerJNI;
2122
import edu.wpi.first.networktables.LogMessage;
2223
import edu.wpi.first.networktables.MultiSubscriber;
2324
import edu.wpi.first.networktables.NetworkTable;
@@ -325,7 +326,7 @@ public void setConfig(NetworkConfig config) {
325326
if (config.runNTServer) {
326327
setServerMode();
327328
} else {
328-
setClientMode(config.ntServerAddress);
329+
setClientMode(config);
329330
}
330331

331332
m_timeSync.setConfig(config);
@@ -337,17 +338,20 @@ public long getOffset() {
337338
return m_timeSync.getOffset();
338339
}
339340

340-
private void setClientMode(String ntServerAddress) {
341+
private void setClientMode(NetworkConfig config) {
341342
ntInstance.stopServer();
342-
ntInstance.startClient4("photonvision");
343+
ntInstance.stopClient();
344+
String hostname = config.shouldManage ? config.hostname : CameraServerJNI.getHostname();
345+
logger.debug("Starting NT Client with hostname: " + hostname);
346+
ntInstance.startClient4(hostname);
343347
try {
344-
int t = Integer.parseInt(ntServerAddress);
348+
int t = Integer.parseInt(config.ntServerAddress);
345349
if (!m_isRetryingConnection) logger.info("Starting NT Client, server team is " + t);
346350
ntInstance.setServerTeam(t);
347351
} catch (NumberFormatException e) {
348352
if (!m_isRetryingConnection)
349-
logger.info("Starting NT Client, server IP is \"" + ntServerAddress + "\"");
350-
ntInstance.setServer(ntServerAddress);
353+
logger.info("Starting NT Client, server IP is \"" + config.ntServerAddress + "\"");
354+
ntInstance.setServer(config.ntServerAddress);
351355
}
352356
ntInstance.startDSClient();
353357
broadcastVersion();

photon-core/src/main/java/org/photonvision/common/networking/NetworkManager.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.photonvision.common.dataflow.DataChangeService;
3030
import org.photonvision.common.dataflow.DataChangeSource;
3131
import org.photonvision.common.dataflow.events.DataChangeEvent;
32+
import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
3233
import org.photonvision.common.hardware.Platform;
3334
import org.photonvision.common.hardware.PlatformUtils;
3435
import org.photonvision.common.logging.LogGroup;
@@ -121,7 +122,7 @@ public void initialize(boolean shouldManage) {
121122

122123
// always set hostname (unless it's blank)
123124
if (!config.hostname.isBlank()) {
124-
setHostname(config.hostname);
125+
setHostname(config);
125126
} else {
126127
logger.warn("Got empty hostname?");
127128
}
@@ -145,29 +146,33 @@ public void reinitialize() {
145146
true));
146147
}
147148

148-
private void setHostname(String hostname) {
149+
private void setHostname(NetworkConfig config) {
149150
try {
150151
var shell = new ShellExec(true, false);
151152
shell.executeBashCommand("cat /etc/hostname | tr -d \" \\t\\n\\r\"");
152153
var oldHostname = shell.getOutput().replace("\n", "");
153154
logger.debug("Old host name: \"" + oldHostname + "\"");
154-
logger.debug("New host name: \"" + hostname + "\"");
155+
logger.debug("New host name: \"" + config.hostname + "\"");
155156

156-
if (!oldHostname.equals(hostname)) {
157+
if (!oldHostname.equals(config.hostname)) {
157158
var setHostnameRetCode =
158159
shell.executeBashCommand(
159-
"echo $NEW_HOSTNAME > /etc/hostname".replace("$NEW_HOSTNAME", hostname));
160-
setHostnameRetCode = shell.executeBashCommand("hostnamectl set-hostname " + hostname);
160+
"echo $NEW_HOSTNAME > /etc/hostname".replace("$NEW_HOSTNAME", config.hostname));
161+
setHostnameRetCode =
162+
shell.executeBashCommand("hostnamectl set-hostname " + config.hostname);
161163

162164
// Add to /etc/hosts
163165
var addHostRetCode =
164166
shell.executeBashCommand(
165167
String.format(
166168
"sed -i \"s/127.0.1.1.*%s/127.0.1.1\\t%s/g\" /etc/hosts",
167-
oldHostname, hostname));
169+
oldHostname, config.hostname));
168170

169171
shell.executeBashCommand("systemctl restart avahi-daemon.service");
170172

173+
// This resets the NetworkTables config to use the new hostname as the client ID
174+
NetworkTablesManager.getInstance().setConfig(config);
175+
171176
var success = setHostnameRetCode == 0 && addHostRetCode == 0;
172177
if (!success) {
173178
logger.error(
@@ -177,7 +182,7 @@ private void setHostname(String hostname) {
177182
+ addHostRetCode
178183
+ "!");
179184
} else {
180-
logger.info("Set hostname to " + hostname);
185+
logger.info("Set hostname to " + config.hostname);
181186
}
182187
}
183188
} catch (Exception e) {

0 commit comments

Comments
 (0)