Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public receive method in SBPFramer Java class #804

Merged
merged 4 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions java/src/com/swiftnav/sbp/client/SBPCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
/** Interface for SBP message handlers. */
public interface SBPCallback {
void receiveCallback(SBPMessage msg);
void callbacksDone();
}
36 changes: 32 additions & 4 deletions java/src/com/swiftnav/sbp/client/SBPHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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<Reference<SBPCallback>> cb_list : callbacks.values()) {
for (Reference<SBPCallback> wr : cb_list) {
SBPCallback cb = wr.get();
if (cb != null) {
cb.callbacksDone();
}
}
}

}

void finish() {
Expand All @@ -169,27 +182,42 @@ private void dispatch(Integer type, SBPMessage msg) {

class SBPQueueIterator extends SBPIterable implements SBPCallback {
private BlockingQueue<SBPMessage> queue;
private AtomicBoolean finished;

private SBPQueueIterator(int queue_size) {
if (queue_size <= 0) {
queue = new LinkedBlockingQueue<>();
} else {
queue = new ArrayBlockingQueue<>(queue_size);
}
finished = new AtomicBoolean(false);
}

@Override
public void receiveCallback(SBPMessage msg) {
queue.add(msg);
}

@Override
public void callbacksDone() {
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();
}
}
}