Minimise our usage of SocketUtils.findAvailableTcpPort

Closes gh-9382
This commit is contained in:
Andy Wilkinson
2017-06-08 10:37:40 +01:00
parent 10868519e1
commit f7e9ec5f42
26 changed files with 442 additions and 286 deletions

View File

@@ -112,13 +112,15 @@ public class LiveReloadServer {
/**
* Start the livereload server and accept incoming connections.
* @return the port on which the server is listening
* @throws IOException in case of I/O errors
*/
public void start() throws IOException {
public int start() throws IOException {
synchronized (this.monitor) {
Assert.state(!isStarted(), "Server already started");
logger.debug("Starting live reload server on port " + this.port);
this.serverSocket = new ServerSocket(this.port);
int localPort = this.serverSocket.getLocalPort();
this.listenThread = this.threadFactory.newThread(new Runnable() {
@Override
@@ -130,6 +132,7 @@ public class LiveReloadServer {
this.listenThread.setDaemon(true);
this.listenThread.setName("Live Reload Server");
this.listenThread.start();
return localPort;
}
}

View File

@@ -57,7 +57,7 @@ public class TunnelClient implements SmartInitializingSingleton {
private ServerThread serverThread;
public TunnelClient(int listenPort, TunnelConnection tunnelConnection) {
Assert.isTrue(listenPort > 0, "ListenPort must be positive");
Assert.isTrue(listenPort >= 0, "ListenPort must be greater than or equal to 0");
Assert.notNull(tunnelConnection, "TunnelConnection must not be null");
this.listenPort = listenPort;
this.tunnelConnection = tunnelConnection;
@@ -78,18 +78,20 @@ public class TunnelClient implements SmartInitializingSingleton {
}
/**
* Start the client and accept incoming connections on the port.
* Start the client and accept incoming connections.
* @return the port on which the client is listening
* @throws IOException in case of I/O errors
*/
public void start() throws IOException {
public int start() throws IOException {
synchronized (this.monitor) {
Assert.state(this.serverThread == null, "Server already started");
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort));
logger.trace(
"Listening for TCP traffic to tunnel on port " + this.listenPort);
int port = serverSocketChannel.socket().getLocalPort();
logger.trace("Listening for TCP traffic to tunnel on port " + port);
this.serverThread = new ServerThread(serverSocketChannel);
this.serverThread.start();
return port;
}
}
@@ -100,7 +102,6 @@ public class TunnelClient implements SmartInitializingSingleton {
public void stop() throws IOException {
synchronized (this.monitor) {
if (this.serverThread != null) {
logger.trace("Closing tunnel client on port " + this.listenPort);
this.serverThread.close();
try {
this.serverThread.join(2000);
@@ -143,6 +144,8 @@ public class TunnelClient implements SmartInitializingSingleton {
}
public void close() throws IOException {
logger.trace("Closing tunnel client on port "
+ this.serverSocketChannel.socket().getLocalPort());
this.serverSocketChannel.close();
this.acceptConnections = false;
interrupt();