Skip to content

Commit 40da3f9

Browse files
mz1999dmatej
authored andcommitted
Enhance socket connection with configurable timeout
1 parent f3300cf commit 40da3f9

File tree

6 files changed

+141
-32
lines changed

6 files changed

+141
-32
lines changed

orbmain/src/main/java/com/sun/corba/ee/impl/misc/ORBUtility.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.sun.corba.ee.impl.misc;
2222

23+
import java.net.Socket;
2324
import java.security.AccessController;
2425
import java.security.PermissionCollection;
2526
import java.security.Policy;
@@ -72,24 +73,33 @@
7273
import com.sun.corba.ee.impl.ior.iiop.JavaSerializationComponent;
7374
import com.sun.corba.ee.impl.javax.rmi.CORBA.Util;
7475

76+
import static com.sun.corba.ee.spi.misc.ORBConstants.TRANSPORT_TCP_CONNECT_MAX_TIME_TO_WAIT;
77+
7578
/**
7679
* Handy class full of static functions that don't belong in util.Utility for pure ORB reasons.
7780
*/
7881
public final class ORBUtility {
82+
83+
public static SocketChannel openSocketChannel(SocketAddress sa) throws IOException {
84+
return openSocketChannel(sa, TRANSPORT_TCP_CONNECT_MAX_TIME_TO_WAIT);
85+
}
86+
7987
/** Utility method for working around leak in SocketChannel.open( SocketAddress )
8088
* method.
8189
* @param sa address to connect to
90+
* @param timeout – the timeout value to be used in milliseconds.
8291
* @return The opened channel
8392
* @throws java.io.IOException If an I/O error occurs
8493
* @see SocketChannel#connect(java.net.SocketAddress)
8594
*/
86-
public static SocketChannel openSocketChannel( SocketAddress sa )
95+
public static SocketChannel openSocketChannel(SocketAddress sa, int timeout)
8796
throws IOException {
8897

8998
SocketChannel sc = SocketChannel.open() ;
9099

91100
try {
92-
sc.connect( sa ) ;
101+
Socket socket = sc.socket();
102+
socket.connect(sa, timeout);
93103
return sc ;
94104
} catch (RuntimeException | IOException exc ) {
95105
try {

orbmain/src/main/java/com/sun/corba/ee/impl/transport/ConnectionImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,13 @@ private ConnectionImpl(ORB orb,
225225
this.contactInfo = contactInfo;
226226

227227
try {
228+
TcpTimeouts tcpConnectTimeouts = orb.getORBData().getTransportTcpConnectTimeouts();
228229
defineSocket(useSelectThreadToWait,
229-
orb.getORBData().getSocketFactory().createSocket(socketType, new InetSocketAddress(hostname, port)));
230+
orb.getORBData().getSocketFactory().createSocket(
231+
orb.getORBData().connectionSocketType(),
232+
new InetSocketAddress(hostname, port),
233+
tcpConnectTimeouts.get_max_time_to_wait())
234+
);
230235
} catch (Throwable t) {
231236
throw wrapper.connectFailure(t, socketType, hostname,
232237
Integer.toString(port));

orbmain/src/main/java/com/sun/corba/ee/impl/transport/DefaultSocketFactoryImpl.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public class DefaultSocketFactoryImpl
3939
implements ORBSocketFactory
4040
{
41-
private ORB orb;
41+
protected ORB orb;
4242

4343
public void setORB(ORB orb)
4444
{
@@ -62,27 +62,6 @@ public ServerSocket createServerSocket(String type,
6262
return serverSocket;
6363
}
6464

65-
public Socket createSocket(String type,
66-
InetSocketAddress inetSocketAddress)
67-
throws IOException
68-
{
69-
SocketChannel socketChannel = null;
70-
Socket socket = null;
71-
72-
if (orb.getORBData().connectionSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
73-
socketChannel = ORBUtility.openSocketChannel(inetSocketAddress);
74-
socket = socketChannel.socket();
75-
} else {
76-
socket = new Socket(inetSocketAddress.getHostName(),
77-
inetSocketAddress.getPort());
78-
}
79-
80-
// Disable Nagle's algorithm (i.e., always send immediately).
81-
socket.setTcpNoDelay(true);
82-
83-
return socket;
84-
}
85-
8665
public void setAcceptedSocketOptions(Acceptor acceptor,
8766
ServerSocket serverSocket,
8867
Socket socket)

orbmain/src/main/java/com/sun/corba/ee/spi/transport/ORBSocketFactory.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
import java.net.Socket;
2525
import java.net.SocketException;
2626
import java.net.ServerSocket;
27+
import java.nio.channels.SocketChannel;
2728

29+
import com.sun.corba.ee.impl.misc.ORBUtility;
30+
import com.sun.corba.ee.spi.misc.ORBConstants;
2831
import com.sun.corba.ee.spi.orb.ORB;
2932

33+
import static com.sun.corba.ee.spi.misc.ORBConstants.TRANSPORT_TCP_CONNECT_MAX_TIME_TO_WAIT;
34+
3035
/**
3136
* @author Harold Carr
3237
*/
@@ -38,9 +43,31 @@ public ServerSocket createServerSocket(String type,
3843
InetSocketAddress inetSocketAddress)
3944
throws IOException;
4045

41-
public Socket createSocket(String type,
42-
InetSocketAddress inetSocketAddress)
43-
throws IOException;
46+
public default Socket createSocket(String type,
47+
InetSocketAddress inetSocketAddress) throws IOException {
48+
return createSocket(type, inetSocketAddress, TRANSPORT_TCP_CONNECT_MAX_TIME_TO_WAIT);
49+
}
50+
51+
public default Socket createSocket(String type,
52+
InetSocketAddress inetSocketAddress,
53+
int timeout)
54+
throws IOException {
55+
SocketChannel socketChannel = null;
56+
Socket socket = null;
57+
58+
if (type.equals(ORBConstants.SOCKETCHANNEL)) {
59+
socketChannel = ORBUtility.openSocketChannel(inetSocketAddress, timeout);
60+
socket = socketChannel.socket();
61+
} else {
62+
socket = new Socket();
63+
socket.connect(inetSocketAddress, timeout);
64+
}
65+
66+
// Disable Nagle's algorithm (i.e., always send immediately).
67+
socket.setTcpNoDelay(true);
68+
69+
return socket;
70+
}
4471

4572
public void setAcceptedSocketOptions(Acceptor acceptor,
4673
ServerSocket serverSocket,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.sun.corba.ee.impl.transport;
2+
3+
import com.sun.corba.ee.spi.misc.ORBConstants;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.net.InetSocketAddress;
10+
import java.net.ServerSocket;
11+
import java.net.Socket;
12+
import java.net.SocketTimeoutException;
13+
import java.util.UUID;
14+
15+
import static org.junit.Assert.*;
16+
17+
public class DefaultSocketFactoryImplTest {
18+
private static final String TEST_TYPE = ORBConstants.SOCKETCHANNEL;
19+
private static final int TEST_TIMEOUT = 1000;
20+
21+
@Test
22+
public void testCreateSocketWithSocketChannelType() throws IOException {
23+
DefaultSocketFactoryImpl sf = new DefaultSocketFactoryImpl();
24+
String testMessage = UUID.randomUUID().toString();
25+
try (ServerSocket serverSocket = createServerSocket(testMessage)) {
26+
final InetSocketAddress address = new InetSocketAddress("localhost", serverSocket.getLocalPort());
27+
Socket socket = sf.createSocket(TEST_TYPE, address, TEST_TIMEOUT);
28+
assertNotNull(socket);
29+
assertTrue(socket.getTcpNoDelay());
30+
validateSocket(socket, testMessage);
31+
}
32+
}
33+
34+
@Test
35+
public void testCreateSocketWithOtherType() throws IOException {
36+
DefaultSocketFactoryImpl sf = new DefaultSocketFactoryImpl();
37+
String testMessage = UUID.randomUUID().toString();
38+
try (ServerSocket serverSocket = createServerSocket(testMessage)) {
39+
final InetSocketAddress address = new InetSocketAddress("localhost", serverSocket.getLocalPort());
40+
Socket socket = sf.createSocket("otherType", address, TEST_TIMEOUT);
41+
assertNotNull(socket);
42+
assertTrue(socket.getTcpNoDelay());
43+
validateSocket(socket, testMessage);
44+
}
45+
}
46+
47+
@Test(expected = SocketTimeoutException.class)
48+
public void testCreateSocketWithTimeoutSocketChannelType() throws IOException {
49+
DefaultSocketFactoryImpl sf = new DefaultSocketFactoryImpl();
50+
InetSocketAddress unreachableAddress = new InetSocketAddress("10.0.0.0", 8080);
51+
sf.createSocket(TEST_TYPE, unreachableAddress, TEST_TIMEOUT);
52+
}
53+
54+
@Test(expected = SocketTimeoutException.class)
55+
public void testCreateSocketWithTimeoutOtherType() throws IOException {
56+
DefaultSocketFactoryImpl sf = new DefaultSocketFactoryImpl();
57+
InetSocketAddress unreachableAddress = new InetSocketAddress("10.0.0.0", 8080);
58+
sf.createSocket("otherType", unreachableAddress, TEST_TIMEOUT);
59+
}
60+
61+
private ServerSocket createServerSocket(String message) throws IOException {
62+
ServerSocket serverSocket = new ServerSocket();
63+
serverSocket.bind(null);
64+
65+
new Thread(() -> {
66+
try {
67+
Socket clientSocket = serverSocket.accept();
68+
OutputStream out = clientSocket.getOutputStream();
69+
out.write(message.getBytes());
70+
out.flush();
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
}
74+
}).start();
75+
76+
return serverSocket;
77+
}
78+
79+
private void validateSocket(Socket socket, String expectedMessage) throws IOException {
80+
InputStream in = socket.getInputStream();
81+
byte[] buffer = new byte[expectedMessage.length()];
82+
int read = in.read(buffer);
83+
assertEquals(expectedMessage, new String(buffer, 0, read));
84+
}
85+
}

test/src/share/classes/corba/nortel/NortelSocketFactory.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package corba.nortel ;
2121

2222
import com.sun.corba.ee.impl.transport.DefaultSocketFactoryImpl;
23+
import com.sun.corba.ee.spi.transport.TcpTimeouts;
2324

2425
import java.io.IOException;
2526

@@ -62,13 +63,15 @@ public Socket createSocket(String type, InetSocketAddress in) throws IOException
6263
}
6364

6465
Socket socket = null;
66+
TcpTimeouts tcpConnectTimeouts = orb.getORBData().getTransportTcpConnectTimeouts();
6567
if (useNio) {
66-
socket = super.createSocket(type, in);
68+
socket = super.createSocket(orb.getORBData().connectionSocketType(), in, tcpConnectTimeouts.get_max_time_to_wait());
6769
} else {
68-
socket = new Socket(in.getHostName(), in.getPort());
69-
socket.setTcpNoDelay(true);
70+
socket = new Socket();
71+
socket.connect(in, tcpConnectTimeouts.get_max_time_to_wait());
7072
}
71-
73+
74+
socket.setTcpNoDelay(true);
7275
savedSocket = socket;
7376
return socket;
7477
}

0 commit comments

Comments
 (0)