Skip to content

Commit a41b9ec

Browse files
committed
Makes SettingsProvider an interface
This makes it so that the pipeline doesn't have to be injected into places that doesn't make any sense.
1 parent b0217f2 commit a41b9ec

File tree

6 files changed

+47
-27
lines changed

6 files changed

+47
-27
lines changed

core/src/main/java/edu/wpi/grip/core/GRIPCoreModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.google.inject.spi.TypeListener;
1212
import edu.wpi.grip.core.events.UnexpectedThrowableEvent;
1313
import edu.wpi.grip.core.serialization.Project;
14+
import edu.wpi.grip.core.settings.SettingsProvider;
1415
import edu.wpi.grip.core.sources.CameraSource;
1516
import edu.wpi.grip.core.sources.ImageFileSource;
1617
import edu.wpi.grip.core.sources.MultiImageFileSource;
@@ -100,6 +101,9 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
100101

101102
bind(EventBus.class).toInstance(eventBus);
102103

104+
// Allow for just injecting the settings provider, instead of the whole pipeline
105+
bind(SettingsProvider.class).to(Pipeline.class);
106+
103107
install(new FactoryModuleBuilder().build(new TypeLiteral<Connection.Factory<Object>>() {
104108
}));
105109

core/src/main/java/edu/wpi/grip/core/Pipeline.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
import com.google.common.collect.ImmutableList;
44
import com.google.common.eventbus.EventBus;
55
import com.google.common.eventbus.Subscribe;
6+
import com.google.inject.Inject;
67
import com.google.inject.Singleton;
78
import com.thoughtworks.xstream.annotations.XStreamAlias;
89
import com.thoughtworks.xstream.annotations.XStreamOmitField;
910
import edu.wpi.grip.core.events.*;
1011
import edu.wpi.grip.core.settings.ProjectSettings;
12+
import edu.wpi.grip.core.settings.SettingsProvider;
1113
import edu.wpi.grip.core.sockets.InputSocket;
1214
import edu.wpi.grip.core.sockets.OutputSocket;
1315
import edu.wpi.grip.core.sockets.SocketHint;
1416

15-
import javax.inject.Inject;
1617
import java.util.*;
1718
import java.util.concurrent.locks.Lock;
1819
import java.util.concurrent.locks.ReadWriteLock;
@@ -33,7 +34,7 @@
3334
*/
3435
@Singleton
3536
@XStreamAlias(value = "grip:Pipeline")
36-
public class Pipeline implements ConnectionValidator {
37+
public class Pipeline implements ConnectionValidator, SettingsProvider {
3738

3839
@Inject
3940
@XStreamOmitField
@@ -166,11 +167,8 @@ public Set<Connection> getConnections() {
166167
return Collections.unmodifiableSet(this.connections);
167168
}
168169

169-
/*
170-
* @return The current per-project settings. This object may become out of date if the settings are edited
171-
* by the user, so objects requiring a preference value should also subscribe to {@link ProjectSettingsChangedEvent}
172-
* to get updates.
173-
*/
170+
171+
@Override
174172
public ProjectSettings getProjectSettings() {
175173
return settings;
176174
}

core/src/main/java/edu/wpi/grip/core/operations/network/networktables/NTManager.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import edu.wpi.first.wpilibj.networktables.NetworkTable;
88
import edu.wpi.first.wpilibj.networktables.NetworkTablesJNI;
99
import edu.wpi.first.wpilibj.tables.ITable;
10-
import edu.wpi.grip.core.Pipeline;
1110
import edu.wpi.grip.core.PipelineRunner;
1211
import edu.wpi.grip.core.events.ProjectSettingsChangedEvent;
13-
import edu.wpi.grip.core.events.StepRemovedEvent;
1412
import edu.wpi.grip.core.operations.network.Manager;
1513
import edu.wpi.grip.core.operations.network.MapNetworkPublisher;
1614
import edu.wpi.grip.core.operations.network.MapNetworkPublisherFactory;
@@ -23,6 +21,7 @@
2321
import java.util.Map;
2422
import java.util.Optional;
2523
import java.util.Set;
24+
import java.util.concurrent.atomic.AtomicInteger;
2625
import java.util.logging.Level;
2726
import java.util.logging.Logger;
2827

@@ -33,6 +32,10 @@
3332
*/
3433
@Singleton
3534
public 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
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package edu.wpi.grip.core.settings;
2+
3+
import edu.wpi.grip.core.events.ProjectSettingsChangedEvent;
4+
5+
/**
6+
* Provides access to the project's settings.
7+
* This should be injected to get access to the settings.
8+
*/
9+
public interface SettingsProvider {
10+
/**
11+
* @return The current per-project settings. This object may become out of date if the settings are edited
12+
* by the user, so objects requiring a preference value should also subscribe to {@link ProjectSettingsChangedEvent}
13+
* to get updates.
14+
*/
15+
ProjectSettings getProjectSettings();
16+
}

ui/src/main/java/edu/wpi/grip/ui/DeployController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import com.google.common.hash.Hashing;
66
import com.google.common.io.LineReader;
77
import com.google.common.io.Resources;
8-
import edu.wpi.grip.core.Pipeline;
98
import edu.wpi.grip.core.events.ProjectSettingsChangedEvent;
109
import edu.wpi.grip.core.events.StopPipelineEvent;
1110
import edu.wpi.grip.core.serialization.Project;
1211
import edu.wpi.grip.core.settings.ProjectSettings;
12+
import edu.wpi.grip.core.settings.SettingsProvider;
1313
import edu.wpi.grip.ui.util.StringInMemoryFile;
1414
import javafx.application.Platform;
1515
import javafx.beans.binding.Bindings;
@@ -81,7 +81,7 @@ public class DeployController {
8181
@Inject
8282
private Project project;
8383
@Inject
84-
private Pipeline pipeline;
84+
private SettingsProvider settingsProvider;
8585
@Inject
8686
private Logger logger;
8787

@@ -93,7 +93,7 @@ public void initialize() {
9393
command.bind(Bindings.concat(javaHome.textProperty(), "/bin/java ", jvmArgs.textProperty(), " -jar '",
9494
deployDir.textProperty(), "/", GRIP_JAR, "' '", deployDir.textProperty(), "/", projectFile.textProperty(), "'"));
9595

96-
loadSettings(pipeline.getProjectSettings());
96+
loadSettings(settingsProvider.getProjectSettings());
9797
}
9898

9999
@Subscribe
@@ -115,7 +115,7 @@ private void loadSettings(ProjectSettings settings) {
115115
private void saveSettings() {
116116
// If the settings are updated in the deploy dialog, we still want to save them in the persistent project
117117
// settings, so they don't get reset the next time the project is opened to the settings are edited.
118-
final ProjectSettings settings = pipeline.getProjectSettings();
118+
final ProjectSettings settings = settingsProvider.getProjectSettings();
119119
settings.setDeployAddress(address.getText());
120120
settings.setDeployUser(user.getText());
121121
settings.setDeployJavaHome(javaHome.getText());

ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import edu.wpi.grip.core.events.ProjectSettingsChangedEvent;
1010
import edu.wpi.grip.core.serialization.Project;
1111
import edu.wpi.grip.core.settings.ProjectSettings;
12+
import edu.wpi.grip.core.settings.SettingsProvider;
1213
import edu.wpi.grip.core.util.SafeShutdown;
1314
import edu.wpi.grip.core.util.service.SingleActionListener;
1415
import edu.wpi.grip.ui.components.StartStoppableButton;
@@ -54,6 +55,8 @@ public class MainWindowController {
5455
@Inject
5556
private Pipeline pipeline;
5657
@Inject
58+
private SettingsProvider settingsProvider;
59+
@Inject
5760
private PipelineRunner pipelineRunner;
5861
@Inject
5962
private StartStoppableButton.Factory startStoppableButtonFactory;
@@ -196,7 +199,7 @@ public boolean saveProjectAs() throws IOException {
196199

197200
@FXML
198201
public void showProjectSettingsEditor() {
199-
final ProjectSettings projectSettings = pipeline.getProjectSettings().clone();
202+
final ProjectSettings projectSettings = settingsProvider.getProjectSettings().clone();
200203

201204
ProjectSettingsEditor projectSettingsEditor = new ProjectSettingsEditor(root, projectSettings);
202205
projectSettingsEditor.showAndWait().ifPresent(buttonType -> {

0 commit comments

Comments
 (0)