Skip to content

Commit 0b83729

Browse files
committed
Only report unrecognized packets once.
This will make logs for Deep-Symmetry/beat-link-trigger#111 easier to work with.
1 parent 6817f9d commit 0b83729

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/main/java/org/deepsymmetry/beatlink/Util.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Collections;
1010
import java.util.HashMap;
1111
import java.util.Map;
12+
import java.util.Set;
13+
import java.util.concurrent.ConcurrentHashMap;
1214

1315
import org.slf4j.Logger;
1416
import org.slf4j.LoggerFactory;
@@ -249,6 +251,16 @@ public static void setPayloadByte(byte[] payload, int address, byte value) {
249251
payload[address - 0x1f] = value;
250252
}
251253

254+
/**
255+
* Used to keep track of when we report seeing a packet to an unexpected port, so we only do that once.
256+
*/
257+
private static final Set<Integer> unknownPortsReported = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
258+
259+
/**
260+
* Used to keep track of when we report seeing a packet of an unknown type reported to a port, so we do that only once.
261+
*/
262+
private static final Map<Integer, Set<Byte>> unknownPortTypesReported = new ConcurrentHashMap<Integer, Set<Byte>>();
263+
252264
/**
253265
* Check to see whether a packet starts with the standard header bytes, followed by a known byte identifying it.
254266
* If so, return the kind of packet that has been recognized.
@@ -273,15 +285,27 @@ public static PacketType validateHeader(DatagramPacket packet, int port) {
273285
}
274286

275287
final Map<Byte, PacketType> portMap = PACKET_TYPE_MAP.get(port);
276-
if (portMap == null) {
277-
logger.warn("Do not know any Pro DJ Link packets that are received on port " + port + ".");
288+
if (portMap == null) { // Warn about unrecognized port, once, and return null for packet type.
289+
if (!unknownPortsReported.contains(port)) {
290+
logger.warn("Do not know any Pro DJ Link packets that are received on port " + port +
291+
" (this will be reported only once).");
292+
unknownPortsReported.add(port);
293+
}
278294
return null;
279295
}
280296

281297
final PacketType result = portMap.get(data[PACKET_TYPE_OFFSET]);
282-
if (result == null) {
283-
logger.warn("Do not know any Pro DJ Link packets received on port " + port + " with type " +
284-
String.format("0x%02x", data[PACKET_TYPE_OFFSET]) + ".");
298+
if (result == null) { // Warn about unrecognized type, once.
299+
Set<Byte> typesReportedForPort = unknownPortTypesReported.get(port);
300+
if (typesReportedForPort == null) { // First problem we have seen for this port, set up set for it.
301+
typesReportedForPort = Collections.newSetFromMap(new ConcurrentHashMap<Byte, Boolean>());
302+
unknownPortTypesReported.put(port, typesReportedForPort);
303+
}
304+
if (!typesReportedForPort.contains(data[PACKET_TYPE_OFFSET])) {
305+
logger.warn("Do not know any Pro DJ Link packets received on port " + port + " with type " +
306+
String.format("0x%02x", data[PACKET_TYPE_OFFSET]) + " (this will be reported only once).");
307+
typesReportedForPort.add(data[PACKET_TYPE_OFFSET]);
308+
}
285309
}
286310

287311
return result;

0 commit comments

Comments
 (0)