Skip to content

Commit

Permalink
Slightly more generic socket factory
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacchella committed May 20, 2014
1 parent 9dff5eb commit 628e9f3
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions src/jrds/starter/SocketFactory.java
@@ -1,22 +1,14 @@
package jrds.starter;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

import jrds.PropertiesManager;
import java.net.SocketException;

public class SocketFactory extends Starter {

/* (non-Javadoc)
* @see jrds.starter.Starter#configure(jrds.PropertiesManager)
*/
@Override
public void configure(PropertiesManager pm) {
super.configure(pm);
}

public ServerSocket createServerSocket(int port) throws IOException {
if(! isStarted())
return null;
Expand All @@ -42,20 +34,8 @@ public Socket createSocket(String host, int port) throws IOException {
if(! isStarted())
return null;

Socket s = new Socket(host, port) {
public void connect(SocketAddress endpoint) throws IOException {
super.connect(endpoint, getTimeout() * 1000);
}

/* (non-Javadoc)
* @see java.net.Socket#connect(java.net.SocketAddress, int)
*/
public void connect(SocketAddress endpoint, int timeout) throws IOException {
super.connect(endpoint, timeout);
}
};
s.setSoTimeout(getTimeout() * 1000);
s.setTcpNoDelay(true);
Socket s = getSocket();
s.connect(new InetSocketAddress(host, port), getTimeout());
return s;
}

Expand All @@ -67,16 +47,27 @@ public Socket createSocket(StarterNode host, int port) throws IOException {
if(r == null || ! r.isStarted())
return null;

Socket s = new Socket(r.getInetAddress(), port) {
Socket s = getSocket();
s.connect(new InetSocketAddress(r.getInetAddress(), port), getTimeout());
return s;
}

public Socket createSocket() throws IOException {
if(! isStarted())
return null;
return getSocket();
}

private Socket getSocket() throws SocketException {
Socket s = new Socket() {
public void connect(SocketAddress endpoint) throws IOException {
super.connect(endpoint, getTimeout() * 1000);
}

/* (non-Javadoc)
* @see java.net.Socket#connect(java.net.SocketAddress, int)
*/
public void connect(SocketAddress endpoint, int timeout) throws IOException {
super.connect(endpoint, timeout);
@Override
public void connect(SocketAddress endpoint, int timeout)
throws IOException {
super.connect(endpoint, getTimeout() * 1000);
}
};
s.setSoTimeout(getTimeout() * 1000);
Expand Down

0 comments on commit 628e9f3

Please sign in to comment.