77import edu .wpi .first .wpilibj .networktables .NetworkTable ;
88import edu .wpi .first .wpilibj .networktables .NetworkTablesJNI ;
99import edu .wpi .first .wpilibj .tables .ITable ;
10- import edu .wpi .grip .core .Pipeline ;
1110import edu .wpi .grip .core .PipelineRunner ;
1211import edu .wpi .grip .core .events .ProjectSettingsChangedEvent ;
13- import edu .wpi .grip .core .events .StepRemovedEvent ;
1412import edu .wpi .grip .core .operations .network .Manager ;
1513import edu .wpi .grip .core .operations .network .MapNetworkPublisher ;
1614import edu .wpi .grip .core .operations .network .MapNetworkPublisherFactory ;
2321import java .util .Map ;
2422import java .util .Optional ;
2523import java .util .Set ;
24+ import java .util .concurrent .atomic .AtomicInteger ;
2625import java .util .logging .Level ;
2726import java .util .logging .Logger ;
2827
3332 */
3433@ Singleton
3534public class NTManager implements Manager , MapNetworkPublisherFactory {
35+ /*
36+ * Nasty hack that is unavoidable because of how NetworkTables works.
37+ */
38+ private static final AtomicInteger publisherCount = new AtomicInteger (0 );
3639
3740 /**
3841 * Information from:
@@ -52,7 +55,6 @@ public class NTManager implements Manager, MapNetworkPublisherFactory {
5255 }};
5356 private final Logger logger = Logger .getLogger (getClass ().getName ());
5457
55- @ Inject private Pipeline pipeline ;
5658 @ Inject private PipelineRunner pipelineRunner ;
5759 @ Inject private GRIPMode gripMode ;
5860
@@ -108,18 +110,6 @@ public void updateSettings(ProjectSettingsChangedEvent event) {
108110 }
109111 }
110112
111- /**
112- * If there are no NTPublishAnnotatedOperation steps, we can shut down NetworkTables
113- */
114- @ Subscribe
115- public void disableNetworkTables (StepRemovedEvent event ) {
116- if (!pipeline .getSteps ().stream ().anyMatch (step -> step .getOperation () instanceof NTPublishAnnotatedOperation )) {
117- synchronized (NetworkTable .class ) {
118- NetworkTable .shutdown ();
119- }
120- }
121- }
122-
123113 private static final class NTPublisher <P > extends MapNetworkPublisher <P > {
124114 private final ImmutableSet <String > keys ;
125115 private Optional <String > name = Optional .empty ();
@@ -170,6 +160,13 @@ public void close() {
170160 if (name .isPresent ()) {
171161 deleteOldTable (name .get ());
172162 }
163+ synchronized (NetworkTable .class ) {
164+ // This publisher is no longer used.
165+ if (NTManager .publisherCount .addAndGet (-1 ) == 0 ) {
166+ // We are the last publisher so shut it down
167+ NetworkTable .shutdown ();
168+ }
169+ }
173170 }
174171
175172 private ITable getTable () {
@@ -188,6 +185,8 @@ private static ITable getRootTable() {
188185
189186 @ Override
190187 public <P >MapNetworkPublisher <P > create (Set <String > keys ) {
188+ // Keep track of ever publisher created.
189+ publisherCount .getAndAdd (1 );
191190 return new NTPublisher <>(keys );
192191 }
193192}
0 commit comments