-
Notifications
You must be signed in to change notification settings - Fork 18
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
Implement TCPServerCommProvider #241
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good job! A couple of concerns listed in the review comments.
sensorhub-core/src/main/java/org/sensorhub/api/comm/ICommProvider.java
Outdated
Show resolved
Hide resolved
sensorhub-core/src/main/java/org/sensorhub/impl/comm/TCPCommProvider.java
Outdated
Show resolved
Hide resolved
static final Logger log = LoggerFactory.getLogger(TCPCommProvider.class.getSimpleName()); | ||
|
||
private final Set<Consumer<ConnectionEventArgs>> listeners = new HashSet<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 Questions:
- Why a Set<> and why not just a simple ArrayList<>?
- There should be a single commProvider per "driver" and a single "Consumer" pre comm provider shouldn't there? Maybe I am not thinking about this right but what is the use case for 1 CommProvider to multiple listeners?
if (config.enableTLS) { | ||
var sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); | ||
try (var sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(config.localPort)) { | ||
while (true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not use a loop like this, I would create a variable that is synchronized like an AtomicBoolean to control execution of the loop(s) (here and line 64). In the doStop method I would set that AtomicBoolean false so that the active loop in the thread knows when to exit.
Interrupt causes an InterruptedException to be thrown by the thread. As a general rule using interrupts are expensive computationally and when possible, should be avoided to control program logic and flow. Exceptions should be exceptional.
sensorhub-core/src/main/java/org/sensorhub/impl/comm/TCPServerCommProvider.java
Outdated
Show resolved
Hide resolved
static final Logger log = LoggerFactory.getLogger(UDPCommProvider.class.getSimpleName()); | ||
|
||
private final Set<Consumer<ConnectionEventArgs>> listeners = new HashSet<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as in TCP Comm Provider regarding need for or understanding need for more than one listener as well as choice of Set over ArrayList
Sorry I didn't really have time to look at this in details. |
It does need more work |
This PR is obsolete, and the above is the new version. |
I added a TCPServerCommProvider, which allows OSH to act as a server and send/receive data from a number of clients.
The TCP and UDP comm providers only allow a single connection, and so have been using getInputStream and getOutputStream. Since a server can have many connections, I had to implement a way to serve up the input and output stream, and do so in a way that all the comm providers can get access to the input and output streams regardless of whether server or client.
Now, rather than using getInputStream and getOutputStream, you can register code to call in onConnection:
or (alt syntax)
onConnection is called whenever a connection is established. For TCP and UDP comm providers, this connection always happens in start(), so the onConnection event should be registered before the comm module is started:
In your onConnection method, you can handle getting the input and output streams the same way, regardless of whether OSH is acting as a client or server.
Please let me know if I need to make any changes, write unit tests, or anything else.