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

Conversation

IsakTjernberg
Copy link
Contributor

@IsakTjernberg IsakTjernberg commented May 15, 2020

Need this method as public to be able to see if the connection is broken or not. Access outside package was only available through an iterator in SBPHandler which swallows the exception.

Copy link
Contributor

@jbangelo jbangelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me, but I'm less versed on Java. @silverjam think this is a problem?

@silverjam
Copy link
Contributor

Why is this needed? Can we add more description to the PR?

@IsakTjernberg
Copy link
Contributor Author

@silverjam I updated the description with some more details.

@silverjam
Copy link
Contributor

@IsakTjernberg @jbangelo Could we expose this state in the iterator instead? E.g. store the last exception that occurs, or record a flag and expose a new interface for that?

@IsakTjernberg
Copy link
Contributor Author

@silverjam well, wouldn't that complicate things unnecessarily? The sendMessage() method is already public.
How would someone with a SBPHandler while looping over the iterator check for that exception/flag?

@jbangelo
Copy link
Contributor

Can Java Iterable objects not throw exceptions from their getNext() implementations?

@silverjam
Copy link
Contributor

silverjam commented May 15, 2020

@IsakTjernberg I’m thinking we should maintain the existing abstractions if possible, how does making this public fix the problem? Don’t you need to create your own reader / iterator logic in order to detect the error?

I was thinking SBPHandler would just gain a lastException() method that would return null or the last exception so you could detect why the iterator broke.

Or, we could create a variant of SBPHandler that doesn’t swallow the error.

@IsakTjernberg
Copy link
Contributor Author

@silverjam Ok, fair point. Does the iterator() in SBPHandler ever break though? What happens in ReceiveThread in SBPHandler if source throws a NoSuchElementException? Does the thread just end and then the SBPQueueIterator will be waiting forever for a new item to be put in its BlockingQueue?

@silverjam
Copy link
Contributor

@IsakTjernberg SBPIterable.hasNext should return false in that case and terminate the iterator, I believe? The pattern we have in Python is for the iterator to record it's exception, then the termination of the threaded stuff propagates the exception when the iterator stops (so SBPQueueIterator.hasNext would need to look for an exception recorded by the source in ReceiveThread and propagate/throw it.

@silverjam
Copy link
Contributor

silverjam commented May 15, 2020

@silverjam
Copy link
Contributor

silverjam commented May 15, 2020

In other words, we had this exact problem with the original design of the Python sbp client library (with it swallowing exceptions, so you couldn't detect connection drops)-- and it looks like the Java sbp client library mirrors Python in terms of trying to put the blocking receive logic on a thread, so it's not surprising that it has the same issue.

@IsakTjernberg
Copy link
Contributor Author

@silverjam Ok, thanks! I'll try to replicate it similarly in the Java parts.

@IsakTjernberg
Copy link
Contributor Author

@silverjam The new commit stops the iterator from forever waiting for new messages, but unfortunately does not expose the underlying exception. In SBPHandler the source is abstracted as a Iterable<SBPMessage>, whose iterator can only throw NoSuchElementException. So it is difficult to get the exception back up the chain without changing the type of source, which I did not want to do.
I'm happy to keep discussing if you see a better way.

Copy link
Contributor

@silverjam silverjam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, maybe we can tackle the exception bubbling later

One suggest on naming, otherwise looks fine

@@ -16,4 +16,5 @@
/** Interface for SBP message handlers. */
public interface SBPCallback {
void receiveCallback(SBPMessage msg);
void allCallbacksDone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just “callbackDone” seems sufficient

@IsakTjernberg IsakTjernberg merged commit 4b4425d into master May 18, 2020
@IsakTjernberg IsakTjernberg deleted the isak/sbpframer_public branch May 18, 2020 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants