ibis.util
Class IbisSocketFactory

java.lang.Object
  extended byibis.util.IbisSocketFactory

public abstract class IbisSocketFactory
extends java.lang.Object

Abstract socket factory class for creating client and server sockets. An implementation can be chosen by means of the ibis.util.socketfactory system property. If not set, a default implementation is chosen.


Method Summary
 java.net.Socket accept(java.net.ServerSocket a)
          Accepts a connection to the specified server socket, and returns the resulting socket.
 int allocLocalPort()
          Returns a port number.
 void close(java.io.InputStream in, java.io.OutputStream out, java.net.Socket s)
          Closes a socket and streams that are associated with it.
 java.net.Socket createBrokeredSocket(java.io.InputStream in, java.io.OutputStream out, boolean isServer, ibis.connect.socketFactory.ConnectionPropertiesProvider p)
          Creates a brokered socket, using the specified streams to negotiate and determine addresses.
 java.net.Socket createBrokeredSocket(java.net.Socket s, boolean isServer, ibis.connect.socketFactory.ConnectionPropertiesProvider p)
          Creates a brokered socket, using the specified socket to negotiate.
static IbisSocketFactory createFactory()
          Creates an IbisSocketFactory.
abstract  java.net.ServerSocket createServerSocket(int port, java.net.InetAddress localAddress, boolean retry)
          Simple ServerSocket creator method.
abstract  java.net.ServerSocket createServerSocket(int port, int backlog, java.net.InetAddress addr)
          Simple ServerSocket creator method.
abstract  java.net.Socket createSocket(java.net.InetAddress rAddr, int rPort)
          Simple client Socket creator method.
abstract  java.net.Socket createSocket(java.net.InetAddress dest, int port, java.net.InetAddress localIP, long timeoutMillis)
          client Socket creator method with a timeout.
 void shutdown()
          Hook for shutdown of socket factory.
static void tuneSocket(java.net.Socket s)
          Configures a socket according to user-specified properties.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createServerSocket

public abstract java.net.ServerSocket createServerSocket(int port,
                                                         int backlog,
                                                         java.net.InetAddress addr)
                                                  throws java.io.IOException
Simple ServerSocket creator method. Creates a server socket that will accept connections on the specified port, with the specified listen backlog, on the specified local address.

Parameters:
port - the local TCP port
backlog - the listen backlog
addr - the local Inetaddress the server will bind to
Returns:
the server socket created.
Throws:
java.io.IOException - when the socket could not be created for some reason.

createSocket

public abstract java.net.Socket createSocket(java.net.InetAddress rAddr,
                                             int rPort)
                                      throws java.io.IOException
Simple client Socket creator method. Creates a client socket and connects it to the the specified Inetaddress and port.

Parameters:
rAddr - the IP address
rPort - the port
Throws:
java.io.IOException - when the socket could not be created for some reason.

allocLocalPort

public int allocLocalPort()
Returns a port number. The system property ibis.port.range can be used to specify a port range, for instance 3000-4000. This can be used to choose port numbers that are for instance not protected by a firewall. If such a range is not given, 0 is returned.

Returns:
a port number, or 0, which means that any free port will do.

createSocket

public abstract java.net.Socket createSocket(java.net.InetAddress dest,
                                             int port,
                                             java.net.InetAddress localIP,
                                             long timeoutMillis)
                                      throws java.io.IOException
client Socket creator method with a timeout. Creates a client socket and connects it to the the specified Inetaddress and port. Some hosts have multiple local IP addresses. If the specified localIP address is null, this method tries to bind to the first of this machine's IP addresses. Otherwise, it uses the specified address.

Parameters:
dest - the IP address
port - the port
localIP - the local IP address, or null
timeoutMillis - if < 0, throw exception on failure. If 0, retry until success. if > 0, block at most timeoutMillis milliseconds.
Returns:
the socket created.
Throws:
java.io.IOException - is thrown when the socket was not properly created within this time.

createServerSocket

public abstract java.net.ServerSocket createServerSocket(int port,
                                                         java.net.InetAddress localAddress,
                                                         boolean retry)
                                                  throws java.io.IOException
Simple ServerSocket creator method. Creates a server socket that will accept connections on the specified port, on the specified local address. If the specified address is null, the first of this machine's IP addresses is chosen.

Parameters:
port - the local TCP port, or 0, in which case a free port is chosen.
localAddress - the local Inetaddress the server will bind to, or null.
retry - when true, the method blocks until the socket is successfuly created.
Returns:
the server socket created.
Throws:
java.io.IOException - when the socket could not be created for some reason.

accept

public java.net.Socket accept(java.net.ServerSocket a)
                       throws java.io.IOException
Accepts a connection to the specified server socket, and returns the resulting socket.

Parameters:
a - the server socket
Returns:
the resulting socket
Throws:
java.io.IOException - is thrown when the accept fails for some reason.

createBrokeredSocket

public java.net.Socket createBrokeredSocket(java.net.Socket s,
                                            boolean isServer,
                                            ibis.connect.socketFactory.ConnectionPropertiesProvider p)
                                     throws java.io.IOException
Creates a brokered socket, using the specified socket to negotiate. The default implementation just returns this socket.

Parameters:
s - socket to negotiate on
isServer - must be set to true on one side, false on the other.
p - connection properties, can be used to pass on socket-dependent parameters.
Returns:
the socket created
Throws:
java.io.IOException - is thrown when the socket could not be created for some reason.

createBrokeredSocket

public java.net.Socket createBrokeredSocket(java.io.InputStream in,
                                            java.io.OutputStream out,
                                            boolean isServer,
                                            ibis.connect.socketFactory.ConnectionPropertiesProvider p)
                                     throws java.io.IOException
Creates a brokered socket, using the specified streams to negotiate and determine addresses. The default implementation returns null.

Parameters:
in - input stream
out - output stream
isServer - must be set to true on one side, false on the other.
p - connection properties, can be used to pass on socket-dependent parameters.
Returns:
the socket created
Throws:
java.io.IOException - is thrown when the socket could not be created for some reason.

close

public void close(java.io.InputStream in,
                  java.io.OutputStream out,
                  java.net.Socket s)
Closes a socket and streams that are associated with it. These streams are given as separate parameters, because they may be streams that are built on top of the actual socket streams.

Parameters:
in - the inputstream ot be closed
out - the outputstream to be closed
s - the socket to be closed

shutdown

public void shutdown()
Hook for shutdown of socket factory. The default implementation does nothing.


createFactory

public static IbisSocketFactory createFactory()
Creates an IbisSocketFactory. An implementation can be chosen by means of the ibis.socketfactory system property. If not set, a default implementation is chosen.

Returns:
the created socket factory, or null if it could not be found.

tuneSocket

public static void tuneSocket(java.net.Socket s)
                       throws java.io.IOException
Configures a socket according to user-specified properties. Currently, the input buffer size and output buffer size can be set using the system properties "ibis.util.socketfactory.InputBufferSize" and "ibis.util.socketfactory.OutputBufferSize".

Parameters:
s - the socket to be configured
Throws:
java.io.IOException - when configuring fails for some reason.


The Ibis project