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

Fix for Port Binding Conflicts in ArtNetClient and ArtNetServer #27

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

tylerstraub
Copy link

@tylerstraub tylerstraub commented May 31, 2024

This PR addresses an issue (#26) where the ArtNetClient and ArtNetServer classes were causing port binding conflicts when used as a sender and receiver simultaneously on the same machine. By introducing a new parameter to control whether the client/server should bind to a port or use an ephemeral port, we resolve the conflicts and improve the flexibility of the library.

Changes Made:

  1. ArtNetClient.java:

    • Added a new start(boolean isReceiver) method to allow specifying the mode (sender or receiver) directly.
    • Modified existing start methods to preserve backward compatibility and default to using the input buffer to determine the receiver mode.
  2. ArtNetServer.java:

    • Updated the start method to accept a boolean isReceiver parameter to control whether to bind to the specified port or use an ephemeral port.

Testing:

  • Tested with Processing 4 application to ensure the ArtNetClient can be started as a sender using artnet.start(false).
  • Verified that no port binding conflicts occur when running multiple instances as both sender and receiver on the same machine.

Documentation:

Usage Example:

To start an ArtNetClient as a sender (without port binding):

import ch.bildspur.artnet.*;

ArtNetClient artnet;
byte[] dmxData = new byte[512];

void setup() {
  size(500, 250);
  
  colorMode(HSB, 360, 100, 100);
  textAlign(CENTER, CENTER);
  textSize(20);

  // create artnet client without buffer (no receiving needed)
  artnet = new ArtNetClient(null);
  artnet.start(false); // false indicates it is a sender
}

void draw() {
  // create color
  int c = color(frameCount % 360, 80, 100);

  background(c);

  // fill dmx array
  dmxData[0] = (byte) red(c);
  dmxData[1] = (byte) green(c);
  dmxData[2] = (byte) blue(c);

  // send dmx to localhost
  artnet.unicastDmx("127.0.0.1", 0, 0, dmxData);

  // show values
  text("R: " + (int)red(c) + " Green: " + (int)green(c) + " Blue: " + (int)blue(c), width / 2, height / 2);
}

Backward Compatibility:

  • The existing methods are preserved to maintain backward compatibility.
  • The new parameter defaults to true for receiver mode when not specified.

Conclusion:

These changes improve the flexibility and usability of the artnet4j library by resolving port binding conflicts and allowing users to specify the mode of the client/server.

I really needed this behavior for my own application so I didn't go too deeply into an optimal fix. A better solution may be to infer if the connection is a reciever or sender somehow, but this was beyond the limits of what I had time for and to be honest my Java is not that great.

If the maintainers feel like this solution is adequate though, great!

All the best,

-T

Modify the artnet4j library to avoid static binding its port when acting solely as a Sender. Instead, it should use an ephemeral port for sending data.
Modify the artnet4j library to avoid static binding its port when acting solely as a Sender. Instead, it should use an ephemeral port for sending data.
Modify the artnet4j library to avoid static binding its port when acting solely as a Sender. Instead, it should use an ephemeral port for sending data.
@tylerstraub tylerstraub changed the title Fix for Port Binding Conflicts in ArtNetClient and ArtNetServer #26 Fix for Port Binding Conflicts in ArtNetClient and ArtNetServer May 31, 2024
@tylerstraub
Copy link
Author

You know what... this commit is not suitable for main branch after testing and thinking through further.

This commit would cause a breaking change and require updating existing code beacuse passing null into .start() results in the server launching in ephemeral mode.

Sorry I didn't catch this before sending the PR. I will close for now and push another one that retains the pre-existing behavior when start() is passed with a null value.

@tylerstraub
Copy link
Author

Fixing the issue now it's a one line change.

@tylerstraub tylerstraub reopened this May 31, 2024
@tylerstraub
Copy link
Author

Alright, it's safe now.

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.

1 participant