Skip to content

Commit 9da6bc9

Browse files
authored
Merge pull request #362 from code-dot-org/molly-ignore-unused-messages
Ignore unused output messages in basic output adapters
2 parents 5bfd51f + 039ca01 commit 9da6bc9

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

org-code-javabuilder/lib/src/main/java/dev/javabuilder/WebSocketOutputAdapter.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,26 @@ public WebSocketOutputAdapter(Session session) {
2020

2121
@Override
2222
public void sendMessage(ClientMessage message) {
23-
try {
24-
endpoint.sendText(message.getFormattedMessage());
25-
} catch (IOException e) {
26-
e.printStackTrace();
27-
} catch (IllegalStateException e) {
28-
throw new InternalServerRuntimeException(InternalExceptionKey.CONNECTION_TERMINATED, e);
23+
if (message.shouldAlwaysSend()) {
24+
try {
25+
endpoint.sendText(message.getFormattedMessage());
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
} catch (IllegalStateException e) {
29+
throw new InternalServerRuntimeException(InternalExceptionKey.CONNECTION_TERMINATED, e);
30+
}
2931
}
3032
}
3133

3234
public void sendDebuggingMessage(ClientMessage message) {
33-
try {
34-
endpoint.sendText(message.getFormattedMessage());
35-
} catch (IOException e) {
36-
e.printStackTrace();
37-
} catch (IllegalStateException e) {
38-
throw new InternalServerRuntimeException(InternalExceptionKey.CONNECTION_TERMINATED, e);
35+
if (message.shouldAlwaysSend()) {
36+
try {
37+
endpoint.sendText(message.getFormattedMessage());
38+
} catch (IOException e) {
39+
e.printStackTrace();
40+
} catch (IllegalStateException e) {
41+
throw new InternalServerRuntimeException(InternalExceptionKey.CONNECTION_TERMINATED, e);
42+
}
3943
}
4044
}
4145
}

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/AWSOutputAdapter.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ public AWSOutputAdapter(String connectionId, AmazonApiGatewayManagementApi api)
2525
*/
2626
@Override
2727
public void sendMessage(ClientMessage message) {
28-
PostToConnectionRequest post = new PostToConnectionRequest();
29-
post.setConnectionId(connectionId);
30-
post.setData(ByteBuffer.wrap((message.getFormattedMessage()).getBytes()));
31-
this.sendMessageHelper(post);
28+
if (message.shouldAlwaysSend()) {
29+
PostToConnectionRequest post = new PostToConnectionRequest();
30+
post.setConnectionId(connectionId);
31+
post.setData(ByteBuffer.wrap((message.getFormattedMessage()).getBytes()));
32+
this.sendMessageHelper(post);
33+
}
3234
}
3335

3436
public void sendDebuggingMessage(ClientMessage message) {
35-
String time = String.valueOf(java.time.Clock.systemUTC().instant());
36-
PostToConnectionRequest post = new PostToConnectionRequest();
37-
post.setConnectionId(connectionId);
38-
post.setData(ByteBuffer.wrap((message + " " + time).getBytes()));
39-
this.sendMessageHelper(post);
37+
if (message.shouldAlwaysSend()) {
38+
String time = String.valueOf(java.time.Clock.systemUTC().instant());
39+
PostToConnectionRequest post = new PostToConnectionRequest();
40+
post.setConnectionId(connectionId);
41+
post.setData(ByteBuffer.wrap((message + " " + time).getBytes()));
42+
this.sendMessageHelper(post);
43+
}
4044
}
4145

4246
private void sendMessageHelper(PostToConnectionRequest post) {
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
package org.code.neighborhood.support;
22

33
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Set;
46
import org.code.protocol.ClientMessage;
57
import org.code.protocol.ClientMessageType;
68

79
public class NeighborhoodSignalMessage extends ClientMessage {
810
public NeighborhoodSignalMessage(NeighborhoodSignalKey key, HashMap<String, String> detail) {
911
super(ClientMessageType.NEIGHBORHOOD, key.toString(), detail);
1012
}
13+
14+
@Override
15+
public boolean shouldAlwaysSend() {
16+
String signalKey = this.getValue();
17+
Set<String> ignoredSignalKeys = new HashSet<>();
18+
// These keys are only used for validation testing, by default don't send them.
19+
ignoredSignalKeys.add(NeighborhoodSignalKey.CAN_MOVE.toString());
20+
ignoredSignalKeys.add(NeighborhoodSignalKey.IS_ON_BUCKET.toString());
21+
ignoredSignalKeys.add(NeighborhoodSignalKey.IS_ON_PAINT.toString());
22+
if (ignoredSignalKeys.contains(signalKey)) {
23+
return false;
24+
}
25+
return true;
26+
}
1127
}

org-code-javabuilder/protocol/src/main/java/org/code/protocol/ClientMessage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ public String getFormattedMessage() {
6767
}
6868
return formattedMessage.toString();
6969
}
70+
71+
/**
72+
* @return whether or not this message should always be sent by any output adapter. Some messages
73+
* are only relevant for the test output adapter
74+
*/
75+
public boolean shouldAlwaysSend() {
76+
return true;
77+
}
7078
}

0 commit comments

Comments
 (0)