From 68c8d5b882e81bc1c4825c81abb595ad447e88cf Mon Sep 17 00:00:00 2001 From: markt Date: Wed, 6 Jul 2011 10:51:18 +0000 Subject: [PATCH] Refactor the bind call to reduce the length of the stack traces when the unit tests fail. It probably won't fix the issue but it will make the logs easier to read. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1143334 13f79535-47bb-0310-9956-ffa450edef68 --- .../catalina/tribes/transport/ReceiverBase.java | 38 ++++++++++------------ 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/java/org/apache/catalina/tribes/transport/ReceiverBase.java b/java/org/apache/catalina/tribes/transport/ReceiverBase.java index 1dbc38229..a5f4438b1 100644 --- a/java/org/apache/catalina/tribes/transport/ReceiverBase.java +++ b/java/org/apache/catalina/tribes/transport/ReceiverBase.java @@ -209,38 +209,36 @@ public abstract class ReceiverBase implements ChannelReceiver, ListenCallback, R } /** - * recursive bind to find the next available port - * @param socket ServerSocket - * @param portstart int - * @param retries int - * @return int + * Attempts to bind using the provided port and if that fails attempts to + * bind to each of the ports from portstart to (portstart + retries -1) + * until either there are no more ports or the bind is successful. The + * address to bind to is obtained via a call to {link {@link #getBind()}. + * @param socket The socket to bind + * @param portstart Starting port for bind attempts + * @param retries Number of times to attempt to bind (port incremented + * between attempts) * @throws IOException */ - protected int bind(ServerSocket socket, int portstart, int retries) throws IOException { + protected void bind(ServerSocket socket, int portstart, int retries) throws IOException { InetSocketAddress addr = null; - while ( retries > 0 ) { + int port = portstart; + while (retries > 0) { try { - addr = new InetSocketAddress(getBind(), portstart); + addr = new InetSocketAddress(getBind(), port); socket.bind(addr); - setPort(portstart); + setPort(port); log.info("Receiver Server Socket bound to:"+addr); - return 0; - }catch ( IOException x) { + retries = 0; + } catch ( IOException x) { retries--; if ( retries <= 0 ) { - log.info("Unable to bind server socket to:"+addr+" throwing error."); + log.info("Unable to bind server socket to:" + addr + + " throwing error."); throw x; } - portstart++; - try { - Thread.sleep(25); - } catch (InterruptedException ti) { - Thread.currentThread().interrupt(); - } - retries = bind(socket,portstart,retries); + port++; } } - return retries; } /** -- 2.11.0