org.restlet.engine.connector
Class Way

java.lang.Object
  extended by org.restlet.engine.connector.Way
All Implemented Interfaces:
BufferProcessor, CompletionListener, SelectionListener
Direct Known Subclasses:
InboundWay, OutboundWay

public abstract class Way
extends Object
implements SelectionListener, CompletionListener, BufferProcessor

A network connection way though which messages are exchanged. Messages can be either sent or received, requests or responses.

Author:
Jerome Louvel

Constructor Summary
Way(Connection<?> connection, int bufferSize)
          Constructor.
 
Method Summary
 boolean canLoop(Buffer buffer, Object... args)
          Indicates if the processing loop can continue.
 void clear()
          Recycles the way so it can be reused.
protected  void clearLineBuilder()
          Clears the line builder and adjust its state.
 boolean couldDrain(Buffer buffer, Object... args)
          Indicates if the buffer could be drained again.
 boolean couldFill(Buffer buffer, Object... args)
          Indicates if the buffer could be filled again.
abstract  Message getActualMessage()
          Returns the actual message, request or response.
 Buffer getBuffer()
          Returns the IO buffer.
 Connection<?> getConnection()
          Returns the parent connection.
 Series<Header> getHeaders()
          Returns the response headers.
 ConnectionHelper<?> getHelper()
          Returns the parent connector helper.
abstract  int getInterestOperations()
          Returns the operations of interest.
 IoState getIoState()
          Returns the IO state.
 StringBuilder getLineBuilder()
          Returns the line builder.
 BufferState getLineBuilderState()
          Returns the line builder state.
 int getLoadScore()
          Returns a score representing the way load and that could be compared with other ways of the same parent connection.
protected  Logger getLogger()
          Returns the logger.
 Response getMessage()
          Returns the current message processed.
 MessageState getMessageState()
          Returns the message state.
 SelectionRegistration getRegistration()
          Returns the socket's NIO registration holding the link between the channel and the connection.
protected  boolean hasIoInterest()
          Indicates if we want to be selected for IO processing when the socket related socket is prepared.
 boolean isAvailable()
          Indicates if the way is available to handle new messages.
 boolean isEmpty()
          Indicates if the way is empty.
 void onClosed()
          Callback method invoked when the parent connection is ready to be closed.
abstract  int onDrain(Buffer buffer, int maxDrained, Object... args)
          Drains the byte buffer by writing available bytes to the socket channel.
abstract  void onError(Status status)
          Called on error.
abstract  int onFill(Buffer buffer, Object... args)
          Fills the byte buffer by writing the current message.
protected abstract  void onHeadersCompleted()
          Callback method invoked when the headers of the current message have been completely received or sent.
 void onMessageCompleted(boolean endDetected)
          Callback method invoked when the current message has been completely received or sent.
protected abstract  void onPostProcessing()
          Called back after the IO processing to indicate if there is further IO interest.
 void onSelected(SelectionRegistration selectionRegistration)
          Callback method invoked when the way has been selected for IO operations it registered interest in.
abstract  void onTimeOut()
          Called back by the controller when an IO time out has been detected.
 void postProcess(int drained)
          Does nothing by default.
 int preProcess(int maxDrained, Object... args)
          Does nothing by default.
protected  int processIoBuffer()
          Processes the IO buffer by filling and draining it.
protected  void setHeaders(Series<Header> headers)
          Sets the response headers to be written.
 void setIoState(IoState ioState)
          Sets the IO state.
protected  void setLineBuilderState(BufferState lineBuilderState)
          Sets the line builder state.
protected  void setMessage(Response message)
          Sets the current message processed.
protected  void setMessageState(MessageState messageState)
          Sets the message state.
protected  void setRegistration(SelectionRegistration registration)
          Sets the NIO selection registration holding the link between the connection and the way.
 String toString()
           
 void updateState()
          Updates the way IO and message states.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.restlet.engine.io.BufferProcessor
onFillEof
 

Constructor Detail

Way

public Way(Connection<?> connection,
           int bufferSize)
Constructor.

Parameters:
connection - The parent connection.
bufferSize - The byte buffer size.
Method Detail

canLoop

public boolean canLoop(Buffer buffer,
                       Object... args)
Indicates if the processing loop can continue.

Specified by:
canLoop in interface BufferProcessor
Parameters:
buffer - The IO buffer to drain.
args - The optional arguments to pass back to the callbacks.
Returns:
True if the processing loop can continue.

clear

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


clearLineBuilder

protected void clearLineBuilder()
Clears the line builder and adjust its state.


couldDrain

public boolean couldDrain(Buffer buffer,
                          Object... args)
Indicates if the buffer could be drained again.

Parameters:
buffer - The IO buffer to drain.
args - The optional arguments to pass back to the callbacks.
Returns:
True if the buffer could be drained again.

couldFill

public boolean couldFill(Buffer buffer,
                         Object... args)
Indicates if the buffer could be filled again.

Specified by:
couldFill in interface BufferProcessor
Parameters:
buffer - The IO buffer to fill.
args - The optional arguments to pass back to the callbacks.
Returns:
True if the buffer could be filled again.

getActualMessage

public abstract Message getActualMessage()
Returns the actual message, request or response.

Returns:
The actual message, request or response.

getBuffer

public Buffer getBuffer()
Returns the IO buffer.

Returns:
The IO buffer.

getConnection

public Connection<?> getConnection()
Returns the parent connection.

Returns:
The parent connection.

getHeaders

public Series<Header> getHeaders()
Returns the response headers.

Returns:
The response headers to be written.

getHelper

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

Returns:
The parent connector helper.

getInterestOperations

public abstract int getInterestOperations()
Returns the operations of interest.

Returns:
The operations of interest.

getIoState

public IoState getIoState()
Returns the IO state.

Returns:
The IO state.

getLineBuilder

public StringBuilder getLineBuilder()
Returns the line builder.

Returns:
The line builder.

getLineBuilderState

public BufferState getLineBuilderState()
Returns the line builder state.

Returns:
The line builder state.

getLoadScore

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

Returns:
A score representing the way load.

getLogger

protected Logger getLogger()
Returns the logger.

Returns:
The logger.

getMessage

public Response getMessage()
Returns the current message processed.

Returns:
The current message processed.

getMessageState

public MessageState getMessageState()
Returns the message state.

Returns:
The message state.

getRegistration

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

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

hasIoInterest

protected boolean hasIoInterest()
Indicates if we want to be selected for IO processing when the socket related socket is prepared.

Returns:
True if we want to be selected for IO processing when the socket is ready.

isAvailable

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

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

isEmpty

public boolean isEmpty()
Indicates if the way is empty.

Returns:
True if the way is empty.

onClosed

public void onClosed()
Callback method invoked when the parent connection is ready to be closed.


onDrain

public abstract int onDrain(Buffer buffer,
                            int maxDrained,
                            Object... args)
                     throws IOException
Drains the byte buffer by writing available bytes to the socket channel.

Specified by:
onDrain in interface BufferProcessor
Parameters:
buffer - The IO buffer to drain.
maxDrained - The maximum number of bytes drained by this call.
args - The optional arguments to pass back to the callbacks.
Returns:
The number of bytes drained.
Throws:
IOException

onError

public abstract void onError(Status status)
Called on error.

Parameters:
status - The error status.

onFill

public abstract int onFill(Buffer buffer,
                           Object... args)
                    throws IOException
Fills the byte buffer by writing the current message.

Specified by:
onFill in interface BufferProcessor
Parameters:
buffer - The IO buffer to drain.
args - The optional arguments to pass back to the callbacks.
Returns:
The number of bytes filled.
Throws:
IOException

onHeadersCompleted

protected abstract void onHeadersCompleted()
                                    throws IOException
Callback method invoked when the headers of the current message have been completely received or sent.

Throws:
IOException

onMessageCompleted

public void onMessageCompleted(boolean endDetected)
                        throws IOException
Callback method invoked when the current message has been completely received or sent.

Specified by:
onMessageCompleted in interface CompletionListener
Parameters:
endDetected - Indicates if the end of the socket channel was detected.
Throws:
IOException

onPostProcessing

protected abstract void onPostProcessing()
Called back after the IO processing to indicate if there is further IO interest. By default, it sets the IO state to IoState.INTEREST.


onSelected

public void onSelected(SelectionRegistration selectionRegistration)
Callback method invoked when the way has been selected for IO operations it registered interest in.

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

onTimeOut

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


postProcess

public void postProcess(int drained)
                 throws IOException
Does nothing by default.

Specified by:
postProcess in interface BufferProcessor
Parameters:
drained - The number of bytes drained or -1 if the filling source has ended.
Throws:
IOException

preProcess

public int preProcess(int maxDrained,
                      Object... args)
               throws IOException
Does nothing by default.

Specified by:
preProcess in interface BufferProcessor
Parameters:
maxDrained - The maximum number of bytes drained by this call or 0 for unlimited length.
args - The optional arguments to pass back to the callbacks.
Returns:
The number of bytes drained or -1 if the filling source has ended.
Throws:
IOException

processIoBuffer

protected int processIoBuffer()
                       throws IOException
Processes the IO buffer by filling and draining it.

Throws:
IOException

setHeaders

protected void setHeaders(Series<Header> headers)
Sets the response headers to be written.

Parameters:
headers - The response headers.

setIoState

public void setIoState(IoState ioState)
Sets the IO state.

Parameters:
ioState - The IO state.

setLineBuilderState

protected void setLineBuilderState(BufferState lineBuilderState)
Sets the line builder state.

Parameters:
lineBuilderState - The line builder state.

setMessage

protected void setMessage(Response message)
Sets the current message processed.

Parameters:
message - The current message processed.

setMessageState

protected void setMessageState(MessageState messageState)
Sets the message state.

Parameters:
messageState - The message state.

setRegistration

protected void setRegistration(SelectionRegistration registration)
Sets the NIO selection registration holding the link between the connection and the way.

Parameters:
registration - The NIO selection registration holding the link between the connection and the way.

toString

public String toString()
Overrides:
toString in class Object

updateState

public void updateState()
Updates the way IO and message states.



Copyright © 2005-2013 Restlet.