Skip to content

Commit 1fa9ee4

Browse files
committed
Merge pull request #285 from AustinShalit/AustinShalit-SourcesSocketHandles
Sources Socket Handles
2 parents e9a3717 + 6fb5b1e commit 1fa9ee4

File tree

6 files changed

+42
-35
lines changed

6 files changed

+42
-35
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public InputSocket<T> getInputSocket() {
4848
@Subscribe
4949
public void onConnectionAdded(ConnectionAddedEvent event) {
5050
if (event.getConnection().equals(this)) {
51+
inputSocket.addConnection(this);
52+
outputSocket.addConnection(this);
5153
inputSocket.setValueOptional(outputSocket.getValue());
5254
}
5355
}
@@ -59,6 +61,14 @@ public void onOutputChanged(SocketChangedEvent e) {
5961
}
6062
}
6163

64+
@Subscribe
65+
public void onConnectionRemoved(ConnectionRemovedEvent e) {
66+
if (e.getConnection() == this) {
67+
inputSocket.removeConnection(this);
68+
outputSocket.removeConnection(this);
69+
}
70+
}
71+
6272
@Subscribe
6373
public void removeConnection(StepRemovedEvent e) {
6474
// Remove this connection if one of the steps it was connected to was removed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ public InputSocket(EventBus eventBus, SocketHint<T> socketHint) {
2323
}
2424

2525
/**
26-
* Reset the socket to its default value when it's no longer connected to anything. This prevents removed
27-
* connections from continuing to have an effect on steps because they still hold references to the values they
28-
* were connected to.
26+
* {@inheritDoc}
2927
*/
30-
@Subscribe
31-
public void onDisconnected(SocketConnectedChangedEvent event) {
32-
if (event.getSocket() == this && this.getConnections().isEmpty()) {
28+
@Override
29+
protected void onDisconnected() {
30+
super.onDisconnected();
31+
if (this.getConnections().isEmpty()) {
3332
this.setValue(this.getSocketHint().createInitialValue().orElse(null));
3433
}
3534
}

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import java.util.Optional;
1414
import java.util.Set;
1515

16-
import static com.google.common.base.Preconditions.checkNotNull;
1716
import static com.google.common.base.Preconditions.checkState;
17+
import static com.google.common.base.Preconditions.checkNotNull;
1818

1919
/**
2020
* A Socket is an abstract wrapper for a value that can be updated and passed around operations. Sockets contain a set of hints
@@ -134,29 +134,38 @@ public Set<Connection> getConnections() {
134134
return ImmutableSet.copyOf(this.connections);
135135
}
136136

137+
/**
138+
* @param connection The connection to add to this socket.
139+
*/
140+
public void addConnection(Connection connection) {
141+
checkNotNull(connection, "Can not remove null connection");
142+
this.connections.add(connection);
137143

138-
@Subscribe
139-
public void onConnectionAdded(ConnectionAddedEvent event) {
140-
if (event.getConnection().getInputSocket() == this || event.getConnection().getOutputSocket() == this) {
141-
this.connections.add(event.getConnection());
142-
143-
if (this.connections.size() == 1) {
144-
this.eventBus.post(new SocketConnectedChangedEvent(this));
145-
}
144+
if (this.connections.size() == 1) {
145+
this.eventBus.post(new SocketConnectedChangedEvent(this));
146146
}
147147
}
148148

149-
@Subscribe
150-
public void onConnectionRemoved(ConnectionRemovedEvent event) {
151-
if (event.getConnection().getInputSocket() == this || event.getConnection().getOutputSocket() == this) {
152-
this.connections.remove(event.getConnection());
149+
/**
150+
* @param connection The connection to remove from this socket.
151+
*/
152+
public void removeConnection(Connection connection) {
153+
checkNotNull(connection, "Can not remove null connection");
154+
onDisconnected();
155+
this.connections.remove(connection);
153156

154-
if (this.connections.isEmpty()) {
155-
this.eventBus.post(new SocketConnectedChangedEvent(this));
156-
}
157+
if (this.connections.isEmpty()) {
158+
this.eventBus.post(new SocketConnectedChangedEvent(this));
157159
}
158160
}
159161

162+
/**
163+
* Reset the socket to its default value when it's no longer connected to anything. This prevents removed
164+
* connections from continuing to have an effect on steps because they still hold references to the values they
165+
* were connected to.
166+
*/
167+
protected void onDisconnected() {}
168+
160169
@Override
161170
public String toString() {
162171
return MoreObjects.toStringHelper(this)

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ public Step create(Operation operation) {
4646
checkNotNull(operation, "The operation can not be null");
4747
// Create the list of input and output sockets, and mark this step as their owner.
4848
final InputSocket<?>[] inputSockets = operation.createInputSockets(eventBus);
49-
50-
for (Socket<?> socket : inputSockets) {
51-
eventBus.register(socket);
52-
}
53-
5449
final OutputSocket<?>[] outputSockets = operation.createOutputSockets(eventBus);
55-
for (Socket<?> socket : outputSockets) {
56-
eventBus.register(socket);
57-
}
5850

5951
final Step step = new Step(
6052
operation,

core/src/main/java/edu/wpi/grip/core/sources/ImageFileSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public String getName() {
9494

9595
@Override
9696
public OutputSocket[] createOutputSockets() {
97-
return new OutputSocket[]{this.outputSocket};
97+
return new OutputSocket[]{outputSocket};
9898
}
9999

100100
@Override

core/src/main/java/edu/wpi/grip/core/sources/MultiImageFileSource.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package edu.wpi.grip.core.sources;
22

3-
43
import com.google.common.eventbus.EventBus;
54
import com.google.common.math.IntMath;
65
import com.google.inject.assistedinject.Assisted;
@@ -106,9 +105,7 @@ public String getName() {
106105

107106
@Override
108107
protected OutputSocket[] createOutputSockets() {
109-
return new OutputSocket[]{
110-
outputSocket
111-
};
108+
return new OutputSocket[]{outputSocket};
112109
}
113110

114111
@Override

0 commit comments

Comments
 (0)