From fb0272637c7f6109556eac048f90efbef71a1398 Mon Sep 17 00:00:00 2001 From: Isak Tjernberg <5121240+IsakTjernberg@users.noreply.github.com> Date: Fri, 15 May 2020 20:59:33 +0200 Subject: [PATCH 1/4] Public receive method in SBPFramer Java class --- java/src/com/swiftnav/sbp/client/SBPFramer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/com/swiftnav/sbp/client/SBPFramer.java b/java/src/com/swiftnav/sbp/client/SBPFramer.java index 1866a45550..3b8ff316b4 100644 --- a/java/src/com/swiftnav/sbp/client/SBPFramer.java +++ b/java/src/com/swiftnav/sbp/client/SBPFramer.java @@ -33,7 +33,7 @@ protected SBPMessage getNext() { } } - private SBPMessage receive() throws IOException { + public SBPMessage receive() throws IOException { /* Wait for a preamble to be received */ byte[] preamble; do { From e4c88fc4559150da8078565cea594d7cc61f3e33 Mon Sep 17 00:00:00 2001 From: Isak Tjernberg <5121240+IsakTjernberg@users.noreply.github.com> Date: Mon, 18 May 2020 10:03:51 +0200 Subject: [PATCH 2/4] Revert "Public receive method in SBPFramer Java class" This reverts commit fb0272637c7f6109556eac048f90efbef71a1398. --- java/src/com/swiftnav/sbp/client/SBPFramer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/com/swiftnav/sbp/client/SBPFramer.java b/java/src/com/swiftnav/sbp/client/SBPFramer.java index 3b8ff316b4..1866a45550 100644 --- a/java/src/com/swiftnav/sbp/client/SBPFramer.java +++ b/java/src/com/swiftnav/sbp/client/SBPFramer.java @@ -33,7 +33,7 @@ protected SBPMessage getNext() { } } - public SBPMessage receive() throws IOException { + private SBPMessage receive() throws IOException { /* Wait for a preamble to be received */ byte[] preamble; do { From 87fce9c641b41d9f795cd32f2498476552585af0 Mon Sep 17 00:00:00 2001 From: Isak Tjernberg <5121240+IsakTjernberg@users.noreply.github.com> Date: Mon, 18 May 2020 10:04:26 +0200 Subject: [PATCH 3/4] Make SBPHandler return from iterating when no more messages are available --- .../com/swiftnav/sbp/client/SBPCallback.java | 1 + .../com/swiftnav/sbp/client/SBPHandler.java | 36 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/java/src/com/swiftnav/sbp/client/SBPCallback.java b/java/src/com/swiftnav/sbp/client/SBPCallback.java index e6acaf9c66..4f2886ad7b 100644 --- a/java/src/com/swiftnav/sbp/client/SBPCallback.java +++ b/java/src/com/swiftnav/sbp/client/SBPCallback.java @@ -16,4 +16,5 @@ /** Interface for SBP message handlers. */ public interface SBPCallback { void receiveCallback(SBPMessage msg); + void allCallbacksDone(); } diff --git a/java/src/com/swiftnav/sbp/client/SBPHandler.java b/java/src/com/swiftnav/sbp/client/SBPHandler.java index b322accfa4..25a46a4559 100644 --- a/java/src/com/swiftnav/sbp/client/SBPHandler.java +++ b/java/src/com/swiftnav/sbp/client/SBPHandler.java @@ -23,6 +23,8 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; /** Provide an interface for queueing and filtering messages. */ @@ -148,6 +150,17 @@ public void run() { dispatch(null, msg); } } + + // Inform all callbacks that there are no more messages coming from the source + for (List> cb_list : callbacks.values()) { + for (Reference wr : cb_list) { + SBPCallback cb = wr.get(); + if (cb != null) { + cb.allCallbacksDone(); + } + } + } + } void finish() { @@ -169,6 +182,7 @@ private void dispatch(Integer type, SBPMessage msg) { class SBPQueueIterator extends SBPIterable implements SBPCallback { private BlockingQueue queue; + private AtomicBoolean finished; private SBPQueueIterator(int queue_size) { if (queue_size <= 0) { @@ -176,6 +190,7 @@ private SBPQueueIterator(int queue_size) { } else { queue = new ArrayBlockingQueue<>(queue_size); } + finished = new AtomicBoolean(false); } @Override @@ -183,13 +198,26 @@ public void receiveCallback(SBPMessage msg) { queue.add(msg); } + @Override + public void allCallbacksDone() { + finished.set(true); + } + @Override protected SBPMessage getNext() { - try { - return queue.take(); - } catch (InterruptedException e) { - throw new NoSuchElementException(); + while (!finished.get()) { + try { + SBPMessage msg = queue.poll(100, TimeUnit.MILLISECONDS); + if (msg != null) { + return msg; + } + continue; + } catch (InterruptedException e) { + continue; + } } + // If we get here finished is set to true so there are no more messages available + throw new NoSuchElementException(); } } } From 880031dfb0a9ffd0c483be8903a56f4a683d876b Mon Sep 17 00:00:00 2001 From: Isak Tjernberg <5121240+IsakTjernberg@users.noreply.github.com> Date: Mon, 18 May 2020 20:27:52 +0200 Subject: [PATCH 4/4] Review comment --- java/src/com/swiftnav/sbp/client/SBPCallback.java | 2 +- java/src/com/swiftnav/sbp/client/SBPHandler.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/java/src/com/swiftnav/sbp/client/SBPCallback.java b/java/src/com/swiftnav/sbp/client/SBPCallback.java index 4f2886ad7b..ff87e680dd 100644 --- a/java/src/com/swiftnav/sbp/client/SBPCallback.java +++ b/java/src/com/swiftnav/sbp/client/SBPCallback.java @@ -16,5 +16,5 @@ /** Interface for SBP message handlers. */ public interface SBPCallback { void receiveCallback(SBPMessage msg); - void allCallbacksDone(); + void callbacksDone(); } diff --git a/java/src/com/swiftnav/sbp/client/SBPHandler.java b/java/src/com/swiftnav/sbp/client/SBPHandler.java index 25a46a4559..30b289c807 100644 --- a/java/src/com/swiftnav/sbp/client/SBPHandler.java +++ b/java/src/com/swiftnav/sbp/client/SBPHandler.java @@ -156,7 +156,7 @@ public void run() { for (Reference wr : cb_list) { SBPCallback cb = wr.get(); if (cb != null) { - cb.allCallbacksDone(); + cb.callbacksDone(); } } } @@ -199,7 +199,7 @@ public void receiveCallback(SBPMessage msg) { } @Override - public void allCallbacksDone() { + public void callbacksDone() { finished.set(true); }