{
try
{
IIOPAddress address = (IIOPAddress)addressIterator.next();
final SocketFactory factory =
(use_ssl) ? transportManager.getSSLSocketFactory() :
transportManager.getSocketFactory();
final String ipAddress = address.getIP();
final int port = (use_ssl) ? ssl_port : address.getPort();
if (ipAddress.indexOf(':') == -1)
connection_info = ipAddress + ":" + port;
else
connection_info = "[" + ipAddress + "]:" + port;
if (logger.isDebugEnabled())
{
logger.debug("Trying to connect to " + connection_info + " with timeout=" + time_out);
}
exception = null;
socket = null;
if( time_out > 0 )
{
//set up connect with an extra thread
//if thread returns within time_out it notifies current thread
//if not this thread will cancel the connect-thread
//this is necessary since earlier JDKs didnt support connect()
//with time_out
final ClientIIOPConnection self = this;
Thread thread = new Thread ( new Runnable()
{
public void run()
{
try
{
socket = factory.createSocket(ipAddress, port);
}
catch (Exception e)
{
exception = e;
}
finally
{
synchronized (self)
{
self.notify();
}
}
}
} );
thread.setName("SocketConnectorThread");
thread.setDaemon(true);
try
{
synchronized (self)
{
thread.start();
self.wait(time_out);
}
}
catch (InterruptedException _ex)
{ }
if (socket == null)
{
if (exception == null)
{
if (logger.isDebugEnabled())
{
logger.debug("connect to " + connection_info +
" with timeout=" + time_out + " timed out");
}
thread.interrupt();
exception =
new TIMEOUT("connection timeout of " + time_out + " milliseconds expired");
}
else
{
if (logger.isDebugEnabled())
{
logger.debug("connect to " + connection_info + " with timeout="
+ time_out + " raised exception: " + exception.toString());
}
}
}
}
else
{
//no timeout --> may hang forever!
socket = factory.createSocket(ipAddress, port);
}
}
catch (Exception e)
{
exception = e;