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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/main/java/ch/bildspur/artnet/ArtNetClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public ArtNetClient(ArtNetBuffer inputBuffer, int serverPort, int clientPort) {
* Start client with default arguments (listen on broadcast).
*/
public void start() {
// use default network interface
this.start((InetAddress)null);
this.start(null, true); // default to static binding (receiver mode)
}

/**
Expand All @@ -60,7 +59,7 @@ public void start() {
public void start(String networkInterfaceAddress)
{
try {
this.start(InetAddress.getByName(networkInterfaceAddress));
this.start(InetAddress.getByName(networkInterfaceAddress), inputBuffer != null);
} catch (UnknownHostException e) {
e.printStackTrace();
}
Expand All @@ -71,6 +70,15 @@ public void start(String networkInterfaceAddress)
* @param networkInterfaceAddress Network interface address to listen to.
*/
public void start(InetAddress networkInterfaceAddress) {
this.start(networkInterfaceAddress, inputBuffer != null);
}

/**
* Start client with specific network interface address and receiver mode.
* @param networkInterfaceAddress Network interface address to listen to.
* @param isReceiver If true, the client will bind to the specified port to receive data.
*/
public void start(InetAddress networkInterfaceAddress, boolean isReceiver) {
if (isRunning)
return;

Expand All @@ -87,14 +95,22 @@ public void artNetPacketReceived(ArtNetPacket packet) {
}
});

server.start(networkInterfaceAddress);
server.start(networkInterfaceAddress, isReceiver);

isRunning = true;
} catch (SocketException | ArtNetException e) {
e.printStackTrace();
}
}

/**
* Start client with receiver mode.
* @param isReceiver If true, the client will bind to the specified port to receive data.
*/
public void start(boolean isReceiver) {
this.start((InetAddress) null, isReceiver);
}

/**
* Stops the client and udp server.
*/
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/ch/bildspur/artnet/ArtNetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,14 @@ private void setBufferSize(int size) {
}

public void start() throws SocketException, ArtNetException {
start(null);
start(null, true);
}

public void start(InetAddress networkAddress) throws SocketException, ArtNetException {
start(networkAddress, true);
}

public void start(InetAddress networkAddress, boolean isReceiver) throws SocketException, ArtNetException {
if (broadCastAddress == null) {
setBroadcastAddress(DEFAULT_BROADCAST_IP);
}
Expand All @@ -170,12 +174,16 @@ public void start(InetAddress networkAddress) throws SocketException, ArtNetExce
socket.setReuseAddress(true);
socket.setBroadcast(true);

if (networkAddress == null)
networkAddress = socket.getLocalAddress();
if (isReceiver) {
if (networkAddress == null)
networkAddress = socket.getLocalAddress();

socket.bind(new InetSocketAddress(networkAddress, port));
socket.bind(new InetSocketAddress(networkAddress, port));
logger.info("Art-Net server started at: " + networkAddress.getHostAddress() + ":" + port);
} else {
logger.info("Art-Net server started as sender using ephemeral port.");
}

logger.info("Art-Net server started at: " + networkAddress.getHostAddress() + ":" + port);
for (ArtNetServerListener l : listeners) {
l.artNetServerStarted(this);
}
Expand Down