|
5 | 5 | import edu.wpi.grip.core.operations.network.Manager;
|
6 | 6 | import edu.wpi.grip.core.operations.network.MapNetworkPublisher;
|
7 | 7 | import edu.wpi.grip.core.operations.network.MapNetworkPublisherFactory;
|
| 8 | +import edu.wpi.grip.core.operations.network.MapNetworkReceiverFactory; |
| 9 | +import edu.wpi.grip.core.operations.network.NetworkReceiver; |
8 | 10 | import edu.wpi.grip.core.settings.ProjectSettings;
|
9 | 11 | import edu.wpi.grip.core.util.GripMode;
|
10 | 12 |
|
|
19 | 21 | import edu.wpi.first.wpilibj.tables.ITable;
|
20 | 22 |
|
21 | 23 | import java.io.File;
|
| 24 | +import java.util.LinkedList; |
| 25 | +import java.util.List; |
22 | 26 | import java.util.Map;
|
23 | 27 | import java.util.Optional;
|
24 | 28 | import java.util.Set;
|
25 | 29 | import java.util.concurrent.atomic.AtomicInteger;
|
| 30 | +import java.util.function.Consumer; |
26 | 31 | import java.util.logging.Level;
|
27 | 32 | import java.util.logging.Logger;
|
28 |
| - |
29 | 33 | import javax.inject.Inject;
|
30 | 34 |
|
31 | 35 | import static com.google.common.base.Preconditions.checkNotNull;
|
|
34 | 38 | * This class encapsulates the way we map various settings to the global NetworkTables state.
|
35 | 39 | */
|
36 | 40 | @Singleton
|
37 |
| -public class NTManager implements Manager, MapNetworkPublisherFactory { |
| 41 | +public class NTManager implements Manager, MapNetworkPublisherFactory, MapNetworkReceiverFactory { |
38 | 42 | /*
|
39 | 43 | * Nasty hack that is unavoidable because of how NetworkTables works.
|
40 | 44 | */
|
41 |
| - private static final AtomicInteger publisherCount = new AtomicInteger(0); |
| 45 | + private static final AtomicInteger count = new AtomicInteger(0); |
42 | 46 |
|
43 | 47 | /**
|
44 | 48 | * Information from: https://github.com/PeterJohnson/ntcore/blob/master/src/Log.h and
|
45 | 49 | * https://github.com/PeterJohnson/ntcore/blob/e6054f543a6ab10aa27af6cace855da66d67ee44
|
46 | 50 | * /include/ntcore_c.h#L39
|
47 | 51 | */
|
48 |
| - private static final Map<Integer, Level> ntLogLevels = ImmutableMap.<Integer, Level>builder() |
| 52 | + protected static final Map<Integer, Level> ntLogLevels = ImmutableMap.<Integer, Level>builder() |
49 | 53 | .put(40, Level.SEVERE)
|
50 | 54 | .put(30, Level.WARNING)
|
51 | 55 | .put(20, Level.INFO)
|
@@ -120,11 +124,69 @@ public void updateSettings(ProjectSettingsChangedEvent event) {
|
120 | 124 |
|
121 | 125 | @Override
|
122 | 126 | public <P> MapNetworkPublisher<P> create(Set<String> keys) {
|
123 |
| - // Keep track of ever publisher created. |
124 |
| - publisherCount.getAndAdd(1); |
| 127 | + // Keep track of every publisher created. |
| 128 | + count.getAndAdd(1); |
125 | 129 | return new NTPublisher<>(keys);
|
126 | 130 | }
|
127 | 131 |
|
| 132 | + @Override |
| 133 | + public NetworkReceiver create(String path) { |
| 134 | + count.getAndAdd(1); |
| 135 | + return new NTReceiver(path); |
| 136 | + } |
| 137 | + |
| 138 | + private static final class NTReceiver extends NetworkReceiver { |
| 139 | + |
| 140 | + private int entryListenerFunctionUid; |
| 141 | + private Object object = false; |
| 142 | + private final List<Consumer<Object>> listeners = new LinkedList<>(); |
| 143 | + |
| 144 | + protected NTReceiver(String path) { |
| 145 | + super(path); |
| 146 | + addListener(); |
| 147 | + |
| 148 | + synchronized (NetworkTable.class) { |
| 149 | + NetworkTable.initialize(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private void addListener() { |
| 154 | + entryListenerFunctionUid = NetworkTablesJNI.addEntryListener(path, |
| 155 | + (uid, key, value, flags) -> { |
| 156 | + object = value; |
| 157 | + listeners.forEach(c -> c.accept(object)); |
| 158 | + }, |
| 159 | + ITable.NOTIFY_IMMEDIATE |
| 160 | + | ITable.NOTIFY_NEW |
| 161 | + | ITable.NOTIFY_UPDATE |
| 162 | + | ITable.NOTIFY_DELETE |
| 163 | + | ITable.NOTIFY_LOCAL); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void addListener(Consumer<Object> consumer) { |
| 168 | + listeners.add(consumer); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public Object getValue() { |
| 173 | + return object; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void close() { |
| 178 | + NetworkTablesJNI.removeEntryListener(entryListenerFunctionUid); |
| 179 | + |
| 180 | + synchronized (NetworkTable.class) { |
| 181 | + // This receiver is no longer used. |
| 182 | + if (NTManager.count.addAndGet(-1) == 0) { |
| 183 | + // We are the last resource using NetworkTables so shut it down |
| 184 | + NetworkTable.shutdown(); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
128 | 190 | private static final class NTPublisher<P> extends MapNetworkPublisher<P> {
|
129 | 191 | private final ImmutableSet<String> keys;
|
130 | 192 | private Optional<String> name = Optional.empty();
|
@@ -184,8 +246,8 @@ public void close() {
|
184 | 246 | }
|
185 | 247 | synchronized (NetworkTable.class) {
|
186 | 248 | // This publisher is no longer used.
|
187 |
| - if (NTManager.publisherCount.addAndGet(-1) == 0) { |
188 |
| - // We are the last publisher so shut it down |
| 249 | + if (NTManager.count.addAndGet(-1) == 0) { |
| 250 | + // We are the last resource using NetworkTables so shut it down |
189 | 251 | NetworkTable.shutdown();
|
190 | 252 | }
|
191 | 253 | }
|
|
0 commit comments