opensensorhub / osh-core

OpenSensorHub Core Modules
http://docs.opensensorhub.org
Mozilla Public License 2.0
32 stars 16 forks source link

Implement TCPServerCommProvider #241

Closed MichaelElmore1 closed 11 months ago

MichaelElmore1 commented 1 year ago

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:

commProvider.onConnection(args -> {
//do stuff with args.getInputStream() and getOutputStream()
})

or (alt syntax)

commProvider.onConnection(this::handleConnection)

...

handleConnection(ConnectionEventArgs args) {
//do stuff with args.getInputStream() and getOutputStream()
}

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:

var moduleReg = getParentHub().getModuleRegistry();
commProvider = (ICommProvider<?>) moduleReg.loadSubModule(config.commSettings, true);
commProvider.onConnection(this::handleConnection);
commProvider.start();

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.

alexrobin commented 11 months ago

Sorry I didn't really have time to look at this in details. Are you guys happy with the current version or does this PR need more work? Thanks

MichaelElmore1 commented 11 months ago

It does need more work

MichaelElmore1 commented 11 months ago

https://github.com/opensensorhub/osh-core/pull/245

This PR is obsolete, and the above is the new version.