diff --git a/src/main/java/ch/bildspur/artnet/ArtNetClient.java b/src/main/java/ch/bildspur/artnet/ArtNetClient.java index 2019324..2765800 100644 --- a/src/main/java/ch/bildspur/artnet/ArtNetClient.java +++ b/src/main/java/ch/bildspur/artnet/ArtNetClient.java @@ -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) } /** @@ -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(); } @@ -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; @@ -87,7 +95,7 @@ public void artNetPacketReceived(ArtNetPacket packet) { } }); - server.start(networkInterfaceAddress); + server.start(networkInterfaceAddress, isReceiver); isRunning = true; } catch (SocketException | ArtNetException e) { @@ -95,6 +103,14 @@ public void artNetPacketReceived(ArtNetPacket packet) { } } + /** + * 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. */ diff --git a/src/main/java/ch/bildspur/artnet/ArtNetServer.java b/src/main/java/ch/bildspur/artnet/ArtNetServer.java index c198550..a0d4ec9 100644 --- a/src/main/java/ch/bildspur/artnet/ArtNetServer.java +++ b/src/main/java/ch/bildspur/artnet/ArtNetServer.java @@ -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); } @@ -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); }