feat: Enable ephemeral port usage (#231)
* Port 0 allows to request ephemeral port
This commit is contained in:
parent
924f40c4e0
commit
10346acade
@ -195,30 +195,29 @@ public abstract class AbstractUdpListener
|
||||
/**
|
||||
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
|
||||
* bind on the specified local address.
|
||||
* @param localAddress the address to bind to.
|
||||
* @param localAddress the address to bind to. If the port is 0 an ephemeral port will be chosen by the OS and the
|
||||
* AbstractUdpListener.localAddress will reflect the allocated port.
|
||||
* @throws IOException if initialization fails.
|
||||
*/
|
||||
protected AbstractUdpListener(TransportAddress localAddress)
|
||||
throws IOException
|
||||
{
|
||||
TransportAddress tempAddress = localAddress;
|
||||
|
||||
boolean bindWildcard = StackProperties.getBoolean(
|
||||
StackProperties.BIND_WILDCARD,
|
||||
false);
|
||||
|
||||
if (bindWildcard)
|
||||
{
|
||||
this.localAddress = new TransportAddress(
|
||||
tempAddress = new TransportAddress(
|
||||
(InetAddress) null,
|
||||
localAddress.getPort(),
|
||||
localAddress.getTransport()
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.localAddress = localAddress;
|
||||
}
|
||||
|
||||
socket = new DatagramSocket( this.localAddress );
|
||||
socket = new DatagramSocket( tempAddress );
|
||||
|
||||
Integer receiveBufferSize = config.udpReceiveBufferSize();
|
||||
if (receiveBufferSize != null)
|
||||
@ -226,6 +225,17 @@ public abstract class AbstractUdpListener
|
||||
socket.setReceiveBufferSize(receiveBufferSize);
|
||||
}
|
||||
|
||||
/* Update the port number if needed. */
|
||||
if (localAddress.getPort() == 0)
|
||||
{
|
||||
tempAddress = new TransportAddress(
|
||||
tempAddress.getAddress(),
|
||||
socket.getLocalPort(),
|
||||
tempAddress.getTransport()
|
||||
);
|
||||
}
|
||||
this.localAddress = tempAddress;
|
||||
|
||||
String logMessage
|
||||
= "Initialized AbstractUdpListener with address " + this.localAddress;
|
||||
logMessage += ". Receive buffer size " + socket.getReceiveBufferSize();
|
||||
|
||||
@ -101,7 +101,8 @@ public class SinglePortUdpHarvester
|
||||
/**
|
||||
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
|
||||
* bind on the specified local address.
|
||||
* @param localAddress the address to bind to.
|
||||
* @param localAddress the address to bind to. If the port is 0 an ephemeral port is chosen by the OS and the
|
||||
* SinglePortUdpHarvester.localAddress will reflect the allocated port number.
|
||||
* @throws IOException if initialization fails.
|
||||
*/
|
||||
public SinglePortUdpHarvester(TransportAddress localAddress)
|
||||
@ -109,7 +110,7 @@ public class SinglePortUdpHarvester
|
||||
{
|
||||
super(localAddress);
|
||||
logger.info("Initialized SinglePortUdpHarvester with address "
|
||||
+ localAddress);
|
||||
+ this.localAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -101,7 +101,7 @@ public class SinglePortUdpHarvesterTest
|
||||
// Verify results.
|
||||
catch ( BindException ex )
|
||||
{
|
||||
fail( "A bind exception should not have been thrown, as the original harvester was propertly closed.");
|
||||
fail( "A bind exception should not have been thrown, as the original harvester was properly closed.");
|
||||
}
|
||||
|
||||
// Tear down.
|
||||
@ -113,4 +113,29 @@ public class SinglePortUdpHarvesterTest
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindWithPortZero() throws Exception
|
||||
{
|
||||
// Setup test fixture.
|
||||
final TransportAddress address = new TransportAddress("127.0.0.1", 0, Transport.UDP);
|
||||
SinglePortUdpHarvester harvester;
|
||||
try
|
||||
{
|
||||
harvester = new SinglePortUdpHarvester(address);
|
||||
}
|
||||
catch (BindException ex)
|
||||
{
|
||||
// This is not expected at this stage.
|
||||
// Rethrow as a different exception than the BindException, that is expected to be thrown later in
|
||||
// this test.
|
||||
throw new Exception("Test fixture is invalid.", ex);
|
||||
}
|
||||
|
||||
assertFalse(harvester.localAddress.getPort() == 0,
|
||||
"A random port number not equal to zero should have been chosen by the OS");
|
||||
|
||||
// Tear down
|
||||
harvester.close();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user