com.mindbright.nio
Class NetworkConnection

java.lang.Object
  extended by com.mindbright.nio.NetworkConnection

public class NetworkConnection
extends java.lang.Object

Holds a non-blocking internet socket.


Constructor Summary
NetworkConnection(java.nio.channels.SocketChannel channel)
          Creates a new NetworkConnection based on the provided SocketChannel.
NetworkConnection(Switchboard s, java.nio.channels.SocketChannel channel)
           
 
Method Summary
 void close()
          Close the connection.
 java.nio.channels.SocketChannel getChannel()
          Get the underlying SocketChannel
 java.net.InetAddress getInetAddress()
          Returns the address to which the socket is connected.
 NonBlockingInput getInput()
          Get the input stream from which one can read data which arrives on this socket.
 java.net.InetAddress getLocalAddress()
          Gets the local address to which the socket is bound.
 NonBlockingOutput getOutput()
          Get the output stream to which one can write data to the remote end of this socket.
 int getPort()
          Returns the remote port to which this socket is connected.
 java.net.Socket getSocket()
          Get the underlying socket.
 int getSoTimeout()
          Returns setting for SO_TIMEOUT.
 Switchboard getSwitchboard()
          Returns the switchboard implementation which regulates this NetworkConnection instance.
static NetworkConnection open(java.net.InetAddress address, int port)
          Creates a network connection to the specified host and port.
static NetworkConnection open(java.net.InetAddress address, int port, boolean block)
          Creates a network connection to the specified address and port.
static NetworkConnection open(java.lang.String host, int port)
          Creates a network connection to the specified host and port.
static NetworkConnection open(java.lang.String host, int port, boolean block)
          Creates a network connection to the specified host and port.
static NetworkConnection open(Switchboard s, java.net.InetAddress address, int port, boolean block)
          Creates a network connection to the specified switchboard, address and port.
 void setSoTimeout(int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 void setTcpNoDelay(boolean delay)
          Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
 void shutdownOutput()
          Disables the output stream for this socket.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NetworkConnection

public NetworkConnection(java.nio.channels.SocketChannel channel)
                  throws java.io.IOException
Creates a new NetworkConnection based on the provided SocketChannel. The SocketChannel must be connected or at least connecting.

Throws:
java.io.IOException

NetworkConnection

public NetworkConnection(Switchboard s,
                         java.nio.channels.SocketChannel channel)
                  throws java.io.IOException
Throws:
java.io.IOException
Method Detail

open

public static NetworkConnection open(java.lang.String host,
                                     int port)
                              throws java.io.IOException
Creates a network connection to the specified host and port. This call will return directly and the progress can be monitored via the Switchboard.notifyWhenConnected method.

Parameters:
host - the host name
port - the port to connect to
Throws:
java.io.IOException
See Also:
Switchboard

open

public static NetworkConnection open(java.lang.String host,
                                     int port,
                                     boolean block)
                              throws java.io.IOException
Creates a network connection to the specified host and port. If block> is false then the call will return directly and the progress can be monitored via the Switchboard.notifyWhenConnected method.

Parameters:
host - the host name
port - the port to connect to
block - true if the call should block until the connection is established
Throws:
java.io.IOException
See Also:
Switchboard

open

public static NetworkConnection open(java.net.InetAddress address,
                                     int port)
                              throws java.io.IOException
Creates a network connection to the specified host and port. This call will return directly and the progress can be monitored via the Switchboard.notifyWhenConnected method.

Parameters:
address - IP-address of remote server
port - the port to connect to
Throws:
java.io.IOException
See Also:
Switchboard

open

public static NetworkConnection open(java.net.InetAddress address,
                                     int port,
                                     boolean block)
                              throws java.io.IOException
Creates a network connection to the specified address and port. If block is false then the call will return directly and the progress can be monitored via the Switchboard.notifyWhenConnected method.

Parameters:
address - IP-address of remote server
port - the port to connect to
block - true if the call should block until the connection is established
Throws:
java.io.IOException
See Also:
Switchboard

open

public static NetworkConnection open(Switchboard s,
                                     java.net.InetAddress address,
                                     int port,
                                     boolean block)
                              throws java.io.IOException
Creates a network connection to the specified switchboard, address and port. If block is false then the call will return directly and the progress can be monitored via the Switchboard.notifyWhenConnected method.

Parameters:
address - IP-address of remote server
port - the port to connect to
block - true if the call should block until the connection is established
Throws:
java.io.IOException
See Also:
Switchboard

getSwitchboard

public Switchboard getSwitchboard()
Returns the switchboard implementation which regulates this NetworkConnection instance.


close

public void close()
Close the connection.

See Also:
Socket

getPort

public int getPort()
Returns the remote port to which this socket is connected.

See Also:
Socket

setTcpNoDelay

public void setTcpNoDelay(boolean delay)
                   throws java.net.SocketException
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

Throws:
java.net.SocketException
See Also:
Socket

getSoTimeout

public int getSoTimeout()
Returns setting for SO_TIMEOUT.

See Also:
Socket

setSoTimeout

public void setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

See Also:
Socket

getInetAddress

public java.net.InetAddress getInetAddress()
Returns the address to which the socket is connected.

See Also:
Socket

getLocalAddress

public java.net.InetAddress getLocalAddress()
Gets the local address to which the socket is bound.

See Also:
Socket

shutdownOutput

public void shutdownOutput()
                    throws java.io.IOException
Disables the output stream for this socket.

Throws:
java.io.IOException
See Also:
Socket

getInput

public NonBlockingInput getInput()
Get the input stream from which one can read data which arrives on this socket.


getOutput

public NonBlockingOutput getOutput()
Get the output stream to which one can write data to the remote end of this socket.


getChannel

public java.nio.channels.SocketChannel getChannel()
Get the underlying SocketChannel


getSocket

public java.net.Socket getSocket()
                          throws java.io.IOException
Get the underlying socket. Note that you should be careful in operating directly on the socket of a NetworkConnection. Great confusion is bound to happen if you mix direct socket operations with nio operations. As a side effect this will configure the channel to be blocking.

Returns:
a socket or null if there is no socket underlying the network connection.
Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object