Skip to content

Commit

Permalink
Handle non-normalized topic names
Browse files Browse the repository at this point in the history
Shuffleboard historically relied on the assumption that a leading slash was optional for paths in networktables. NT4 changed that by making topic names exact (so two topic names only differing by the presence or absence of a leading slash would be two unique topics), which broke NT data sources for topics without leading slashes
  • Loading branch information
SamCarlberg committed Oct 26, 2024
1 parent b636afc commit de08b0d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CompositeNetworkTableSource(String tableName, ComplexDataType<D> dataType
setData(dataType.getDefaultValue());

setTableListener((key, event) -> {
String relativeKey = NetworkTable.normalizeKey(key.substring(path.length() + 1), false);
String relativeKey = NetworkTable.basenameKey(key);
if (event.is(NetworkTableEvent.Kind.kUnpublish)) {
backingMap.remove(relativeKey);
} else if (event.valueData != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.PubSubOption;

import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Map;
Expand Down Expand Up @@ -73,7 +74,19 @@ protected final void setTableListener(TableListener listener) {
}
setConnected(true);
if (isSingular()) {
singleSub = inst.getTopic(fullTableKey).genericSubscribe(PubSubOption.hidden(false), PubSubOption.sendAll(true));
// Handle leading slashes. Topic names are exact and do no normalization
String topicName;
if (Arrays.stream(inst.getTopicInfo()).anyMatch(t -> t.name.equals(fullTableKey))) {
topicName = fullTableKey;
} else {
if (fullTableKey.startsWith("/")) {
topicName = NetworkTable.normalizeKey(fullTableKey, false);
} else {
topicName = NetworkTable.normalizeKey(fullTableKey, true);
}
}

singleSub = inst.getTopic(topicName).genericSubscribe(PubSubOption.hidden(false), PubSubOption.sendAll(true));
listenerUid = inst.addListener(
singleSub,
EnumSet.of(
Expand Down Expand Up @@ -197,7 +210,7 @@ public static void removeAllCachedSources() {
*/
@SuppressWarnings("unchecked")
public static DataSource<?> forKey(String fullTableKey) {
String key = NetworkTable.normalizeKey(fullTableKey, false);
String key = fullTableKey;
final String uri = NetworkTableSourceType.getInstance().toUri(key);
return sources.computeIfAbsent(uri, __ -> {
DataType<?> lookup = NetworkTableUtils.dataTypeForEntry(key);
Expand Down

0 comments on commit de08b0d

Please sign in to comment.