org.restlet.engine.connector
Class Connection<T extends Connector>

java.lang.Object
  extended by org.restlet.engine.connector.Connection<T>
Type Parameters:
T - The parent connector type.
All Implemented Interfaces:
SelectionListener

public class Connection<T extends Connector>
extends Object
implements SelectionListener

A network connection though which messages are exchanged by connectors. Messages can be either requests or responses.

Author:
Jerome Louvel

Constructor Summary
Connection(ConnectionHelper<T> helper, SocketChannel socketChannel, ConnectionController controller, InetSocketAddress socketAddress, int inboundBufferSize, int outboundBufferSize)
          Constructor.
 
Method Summary
 void clear()
          Clears the connection so it can be reused.
 void close(boolean graceful)
          Closes the connection.
 void commit(Response response)
          Asks the server connector to immediately commit the given response associated to this request, making it ready to be sent back to the client.
protected  ReadableSelectionChannel createReadableSelectionChannel()
          Creates a new readable channel.
protected  WritableSelectionChannel createWritableSelectionChannel()
          Creates a new writable channel.
 String getAddress()
          Returns the socket IP address.
 ConnectionHelper<T> getHelper()
          Returns the parent connector helper.
 int getInboundBufferSize()
          Returns the size of the content buffer for receiving messages.
 InboundWay getInboundWay()
          Returns the inbound way.
 long getLastActivity()
          Returns the timestamp of the last IO activity.
 int getLoadScore()
          Returns a score representing the connection load and that could be compared with other connections of the same parent connector.
 Logger getLogger()
          Returns the logger.
 int getMaxIoIdleTimeMs()
          Returns the time for an idle IO connection to wait for an operation before being closed.
 int getOutboundBufferSize()
          Returns the size of the content buffer for sending responses.
 OutboundWay getOutboundWay()
          Returns the outbound way.
 int getPort()
          Returns the socket port.
 ReadableSelectionChannel getReadableSelectionChannel()
          Returns the underlying socket channel as a readable selection channel.
 SelectionRegistration getRegistration()
          Returns the socket's NIO registration holding the link between the SocketChannel and the Connection.
 Socket getSocket()
          Returns the underlying socket.
 SocketAddress getSocketAddress()
          Returns the socket address.
 SocketChannel getSocketChannel()
          Returns the underlying NIO socket channel.
 ConnectionState getState()
          Returns the state of the connection.
 WritableSelectionChannel getWritableSelectionChannel()
          Returns the underlying socket channel as a writable selection channel.
 boolean hasTimedOut()
          Indicates if the connection has timed out.
 void init(SocketChannel socketChannel, ConnectionController controller, InetSocketAddress socketAddress)
          Initializes the connection and associates it to the given socket.
 boolean isAvailable()
          Indicates if the connection is available to handle new messages.
 boolean isClientSide()
          Indicates if it is a client-side connection.
 boolean isEmpty()
          Indicates if the connection is empty of messages and bytes.
 boolean isPersistent()
          Indicates if the connection should be persisted across calls.
 boolean isPipelining()
          Indicates if idempotent sequences of requests can be pipelined.
protected  boolean isReady()
          Indicates if at least one way is in the IoState.READY state.
 boolean isServerSide()
          Indicates if it is a server-side connection.
 void onActivity()
          Notifies the connection that a new activity has been detected and that it should be kept alive.
 void onError(String message, Throwable throwable, Status status)
          Called on error.
 void onSelected(SelectionRegistration registration)
          Callback method invoked when the connection has been selected for IO operations it registered interest in.
 void onTimeOut()
          Called back by the controller when an IO time out has been detected.
 void open()
          Opens the connection.
 void reuse(SocketChannel socketChannel, ConnectionController controller, InetSocketAddress socketAddress)
          Reuses the connection and associates it to the given socket.
 void setPersistent(boolean persistent)
          Indicates if the connection should be persisted across calls.
 void setPipelining(boolean pipelining)
          Indicates if idempotent sequences of requests can be pipelined.
 void setRegistration(SelectionRegistration registration)
          Sets the socket's NIO registration holding the link between the channel and the way.
 void setState(ConnectionState state)
          Sets the state of the connection.
protected  void shutdown(Socket socket)
          Shutdowns the socket, first its input then its output.
 String toString()
           
 boolean updateState()
          Updates the connection states.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Connection

public Connection(ConnectionHelper<T> helper,
                  SocketChannel socketChannel,
                  ConnectionController controller,
                  InetSocketAddress socketAddress,
                  int inboundBufferSize,
                  int outboundBufferSize)
           throws IOException
Constructor.

Parameters:
helper - The parent connector helper.
socketChannel - The underlying NIO socket channel.
controller - The IO controller.
socketAddress - The associated IP address.
Throws:
IOException
Method Detail

clear

public void clear()
Clears the connection so it can be reused. Typically invoked by a connection pool.


close

public void close(boolean graceful)
Closes the connection. By default, set the state to ConnectionState.CLOSED.

Parameters:
graceful - Indicates if a graceful close should be attempted.

commit

public void commit(Response response)
Asks the server connector to immediately commit the given response associated to this request, making it ready to be sent back to the client. Note that all server connectors don't necessarily support this feature.

Parameters:
response - The response to commit.

createReadableSelectionChannel

protected ReadableSelectionChannel createReadableSelectionChannel()
Creates a new readable channel.

Returns:
A new readable channel.

createWritableSelectionChannel

protected WritableSelectionChannel createWritableSelectionChannel()
Creates a new writable channel.

Returns:
A new writable channel.

getAddress

public String getAddress()
Returns the socket IP address.

Returns:
The socket IP address.

getHelper

public ConnectionHelper<T> getHelper()
Returns the parent connector helper.

Returns:
The parent connector helper.

getInboundBufferSize

public int getInboundBufferSize()
Returns the size of the content buffer for receiving messages. By default, it calls getInboundBufferSize().

Returns:
The size of the content buffer for receiving messages.

getInboundWay

public InboundWay getInboundWay()
Returns the inbound way.

Returns:
The inbound way.

getLastActivity

public long getLastActivity()
Returns the timestamp of the last IO activity.

Returns:
The timestamp of the last IO activity.

getLoadScore

public int getLoadScore()
Returns a score representing the connection load and that could be compared with other connections of the same parent connector.

Returns:
A score representing the connection load.

getLogger

public Logger getLogger()
Returns the logger.

Returns:
The logger.

getMaxIoIdleTimeMs

public int getMaxIoIdleTimeMs()
Returns the time for an idle IO connection to wait for an operation before being closed.

Returns:
The time for an idle IO connection to wait for an operation before being closed.

getOutboundBufferSize

public int getOutboundBufferSize()
Returns the size of the content buffer for sending responses. By default, it calls getOutboundBufferSize().

Returns:
The size of the content buffer for sending responses.

getOutboundWay

public OutboundWay getOutboundWay()
Returns the outbound way.

Returns:
The outbound way.

getPort

public int getPort()
Returns the socket port.

Returns:
The socket port.

getReadableSelectionChannel

public ReadableSelectionChannel getReadableSelectionChannel()
Returns the underlying socket channel as a readable selection channel.

Returns:
The underlying socket channel as a readable selection channel.

getRegistration

public SelectionRegistration getRegistration()
Returns the socket's NIO registration holding the link between the SocketChannel and the Connection.

Returns:
The socket's NIO registration holding the link between the channel and the connection.

getSocket

public Socket getSocket()
Returns the underlying socket.

Returns:
The underlying socket.

getSocketAddress

public SocketAddress getSocketAddress()
Returns the socket address.

Returns:
The socket address.

getSocketChannel

public SocketChannel getSocketChannel()
Returns the underlying NIO socket channel.

Returns:
The underlying NIO socket channel.

getState

public ConnectionState getState()
Returns the state of the connection.

Returns:
The state of the connection.

getWritableSelectionChannel

public WritableSelectionChannel getWritableSelectionChannel()
Returns the underlying socket channel as a writable selection channel.

Returns:
The underlying socket channel as a writable selection channel.

hasTimedOut

public boolean hasTimedOut()
Indicates if the connection has timed out.

Returns:
True if the connection has timed out.

init

public void init(SocketChannel socketChannel,
                 ConnectionController controller,
                 InetSocketAddress socketAddress)
          throws IOException
Initializes the connection and associates it to the given socket.

Parameters:
socketChannel - The underlying NIO socket channel.
controller - The underlying IO controller.
socketAddress - The associated socket address.
Throws:
IOException

isAvailable

public boolean isAvailable()
Indicates if the connection is available to handle new messages.

Returns:
True if the connection is available to handle new messages.

isClientSide

public boolean isClientSide()
Indicates if it is a client-side connection.

Returns:
True if it is a client-side connection.

isEmpty

public boolean isEmpty()
Indicates if the connection is empty of messages and bytes.

Returns:
True if the connection is empty.

isPersistent

public boolean isPersistent()
Indicates if the connection should be persisted across calls.

Returns:
True if the connection should be persisted across calls.

isPipelining

public boolean isPipelining()
Indicates if idempotent sequences of requests can be pipelined.

Returns:
True requests pipelining is enabled.

isReady

protected boolean isReady()
Indicates if at least one way is in the IoState.READY state.

Returns:
True if at least one way is in the IoState.READY state.

isServerSide

public boolean isServerSide()
Indicates if it is a server-side connection.

Returns:
True if it is a server-side connection.

onActivity

public void onActivity()
Notifies the connection that a new activity has been detected and that it should be kept alive.


onError

public void onError(String message,
                    Throwable throwable,
                    Status status)
Called on error. By default, it calls close(boolean) with a 'false' parameter.

Parameters:
message - The error message.
throwable - The cause of the error.
status - The error status.

onSelected

public void onSelected(SelectionRegistration registration)
                throws IOException
Callback method invoked when the connection has been selected for IO operations it registered interest in. By default it updates the timestamp that allows the detection of expired connections and calls SelectionRegistration.onSelected(int) on the inbound or outbound way.

Specified by:
onSelected in interface SelectionListener
Parameters:
registration - The selected registration.
Throws:
IOException

onTimeOut

public void onTimeOut()
Called back by the controller when an IO time out has been detected.


open

public void open()
Opens the connection. By default, set the IO state of the connection to ConnectionState.OPEN and the IO state of the inbound way to IoState.INTEREST.


reuse

public void reuse(SocketChannel socketChannel,
                  ConnectionController controller,
                  InetSocketAddress socketAddress)
           throws IOException
Reuses the connection and associates it to the given socket.

Parameters:
socketChannel - The underlying NIO socket channel.
controller - The underlying IO controller.
socketAddress - The associated socket address.
Throws:
IOException

setPersistent

public void setPersistent(boolean persistent)
Indicates if the connection should be persisted across calls.

Parameters:
persistent - True if the connection should be persisted across calls.

setPipelining

public void setPipelining(boolean pipelining)
Indicates if idempotent sequences of requests can be pipelined.

Parameters:
pipelining - True requests pipelining is enabled.

setRegistration

public void setRegistration(SelectionRegistration registration)
Sets the socket's NIO registration holding the link between the channel and the way.

Parameters:
registration - The socket's NIO registration holding the link between the channel and the way.

setState

public void setState(ConnectionState state)
Sets the state of the connection.

Parameters:
state - The state of the connection.

shutdown

protected void shutdown(Socket socket)
                 throws IOException
Shutdowns the socket, first its input then its output.

Parameters:
socket - The socket to shutdown.
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object

updateState

public boolean updateState()
Updates the connection states.

Returns:
True if the state was changed.


Copyright © 2005-2013 Restlet.