hoijui / JavaOSC

OSC content format/"protocol" library for JVM languages
http://www.illposed.com/software/javaosc.html
BSD 3-Clause "New" or "Revised" License
156 stars 43 forks source link

Pass sender's InetSocketAddress to OSCListener #34

Closed showlabor closed 5 years ago

showlabor commented 7 years ago

AFAICT, currently it's impossible to tell where a OSCMessage is coming from in a OSCLIstener. That information is vital though for many possible applications. (BTW, that made me switch from JavaOSC to using NetUtil mostly some time ago.) E.g. Behringer X Air mixers respond to a broadcast message such that a control app is able to find a mixer on the network .Moreover, if you have more than one of those mixers on the net it is important to know the source of a message send to the control app.

I'd very much like to move from

public interface OSCListener {
    void acceptMessage(Date time, OSCMessage message);
}

to

public interface OSCListener {
    void acceptMessage(Date time, OSCMessage message, InetSocketAddress sender);
}

But that would probably break too much legacy code. So maybe a second kind of Listeners could be introduced.

What do you think?

Kind regards, Felix

hoijui commented 7 years ago

ahh yep, that makes sense indeed, thank you! :-) i will have a look at it... But first though i have is looking at these code snippets, is that i clearly made a design fault-pas in the beginning already, by not using the scheme (which is standard in Java) of using a single event object, but passing multiple objects as event arguments, which is a bad idea for exactly these kinds of scenarios. ;-) so... best would be, to change that, and thus play safe for the future at least, as we have to break the interface anyway. i think introducing a new listener just to prevent breaking the API is not a good idea. it leads to lots of confusion for people that don't see the history of the code.

hoijui commented 5 years ago

please see this commit: https://github.com/hoijui/JavaOSC/commit/cd36c67d82b32c7826b240208f24f85270d7aed7 it is only on the develop branch so far, and can thus easily be modified. what do you think about this solution?

hoijui commented 5 years ago

now superseeded by b171e65c0c7b800b6351d07ea02d0a833964c83a. To get the receiving address, you would do this:

public interface OSCMessageListener {

    void acceptMessage(OSCMessageEvent event) {

        OSCPortIn portIn = (OSCPortIn) event.getSource();
        InetSocketAddress receivingAddress = (InetSocketAddress) portIn.getLocalAddress();
    }
}

or with a lover level listener (new):

public class MyListener implements OSCPacketListener {
    public void handlePacket(OSCPacketEvent event) {

        OSCPortIn portIn = (OSCPortIn) event.getSource();
        InetSocketAddress receivingAddress = (InetSocketAddress) portIn.getLocalAddress();
    }
    public void handleBadData(OSCBadDataEvent event) { ... }
}

It wil be in the next release, and can until then can be tested in the snaphost (com.illposed.osc:javaosc-core:0.5-SNAPSHOT).