Class NioSender
- java.lang.Object
-
- org.apache.catalina.tribes.transport.AbstractSender
-
- org.apache.catalina.tribes.transport.nio.NioSender
-
- All Implemented Interfaces:
DataSender
public class NioSender extends AbstractSender
This class is NOT thread safe and should never be used with more than one thread at a time. This is a state machine, handled by the process method. States are:- NOT_CONNECTED -> connect() -> CONNECTED
- CONNECTED -> setMessage() -> READY TO WRITE
- READY_TO_WRITE -> write() -> READY TO WRITE | READY TO READ
- READY_TO_READ -> read() -> READY_TO_READ | TRANSFER_COMPLETE
- TRANSFER_COMPLETE -> CONNECTED
-
-
Field Summary
Fields Modifier and Type Field Description protected XByteBufferackbufAcknowledgment buffer.protected booleancompleteFlag indicating send completion.protected booleanconnectingFlag indicating connection in progress.protected byte[]currentCurrent message being processed.protected java.nio.channels.DatagramChanneldataChannelThe UDP datagram channel.protected java.nio.ByteBufferreadbufBuffer for reading from the channel.protected intremainingNumber of remaining bytes to send.protected java.nio.channels.SelectorselectorThe NIO selector for multiplexing channels.protected static StringManagersmString manager for this class.protected java.nio.channels.SocketChannelsocketChannelThe TCP socket channel.protected java.nio.ByteBufferwritebufBuffer for writing to the channel.
-
Constructor Summary
Constructors Constructor Description NioSender()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconnect()Connect.voiddisconnect()Disconnect.byte[]getMessage()Returns the current message being sent.java.nio.channels.SelectorgetSelector()Returns the NIO selector.booleanisComplete()Checks if the send operation is complete.booleanprocess(java.nio.channels.SelectionKey key, boolean waitForAck)State machine to send data.protected booleanread()Reads acknowledgment data from the channel.voidreset()Resets the sender state for reuse.voidsetComplete(boolean complete)Sets the completion flag.voidsetMessage(byte[] data)Send message.voidsetMessage(byte[] data, int offset, int length)Sets the message to be sent.voidsetSelector(java.nio.channels.Selector selector)Sets the NIO selector.protected booleanwrite()Writes the current message to the channel.-
Methods inherited from class org.apache.catalina.tribes.transport.AbstractSender
getAddress, getAttempt, getConnectTime, getDestination, getDirectBuffer, getKeepAliveCount, getKeepAliveTime, getMaxRetryAttempts, getOoBInline, getPort, getRequestCount, getRxBufSize, getSoKeepAlive, getSoLingerOn, getSoLingerTime, getSoReuseAddress, getSoTrafficClass, getTcpNoDelay, getThrowOnFailedAck, getTimeout, getTxBufSize, getUdpPort, getUdpRxBufSize, getUdpTxBufSize, isConnected, isUdpBased, keepalive, setAddress, setAttempt, setConnected, setConnectTime, setDestination, setDirectBuffer, setKeepAliveCount, setKeepAliveTime, setMaxRetryAttempts, setOoBInline, setPort, setRequestCount, setRxBufSize, setSoKeepAlive, setSoLingerOn, setSoLingerTime, setSoReuseAddress, setSoTrafficClass, setTcpNoDelay, setThrowOnFailedAck, setTimeout, setTxBufSize, setUdpBased, setUdpPort, setUdpRxBufSize, setUdpTxBufSize, transferProperties
-
-
-
-
Field Detail
-
sm
protected static final StringManager sm
String manager for this class.
-
selector
protected java.nio.channels.Selector selector
The NIO selector for multiplexing channels.
-
socketChannel
protected java.nio.channels.SocketChannel socketChannel
The TCP socket channel.
-
dataChannel
protected java.nio.channels.DatagramChannel dataChannel
The UDP datagram channel.
-
readbuf
protected java.nio.ByteBuffer readbuf
Buffer for reading from the channel.
-
writebuf
protected java.nio.ByteBuffer writebuf
Buffer for writing to the channel.
-
current
protected volatile byte[] current
Current message being processed.
-
ackbuf
protected final XByteBuffer ackbuf
Acknowledgment buffer.
-
remaining
protected int remaining
Number of remaining bytes to send.
-
complete
protected boolean complete
Flag indicating send completion.
-
connecting
protected boolean connecting
Flag indicating connection in progress.
-
-
Method Detail
-
process
public boolean process(java.nio.channels.SelectionKey key, boolean waitForAck) throws java.io.IOExceptionState machine to send data.- Parameters:
key- The key to usewaitForAck- Wait for an ack- Returns:
trueif the processing was successful- Throws:
java.io.IOException- An IO error occurred
-
read
protected boolean read() throws java.io.IOExceptionReads acknowledgment data from the channel.- Returns:
trueif there is more data to read- Throws:
java.io.IOException- If an I/O error occurs
-
write
protected boolean write() throws java.io.IOExceptionWrites the current message to the channel.- Returns:
trueif the message was fully written- Throws:
java.io.IOException- If an I/O error occurs
-
connect
public void connect() throws java.io.IOExceptionDescription copied from interface:DataSenderConnect.- Throws:
java.io.IOException- when an error occurs
-
disconnect
public void disconnect()
Description copied from interface:DataSenderDisconnect.
-
reset
public void reset()
Resets the sender state for reuse.
-
setMessage
public void setMessage(byte[] data) throws java.io.IOExceptionSend message.- Parameters:
data- ChannelMessage- Throws:
java.io.IOException- if an error occurs
-
setMessage
public void setMessage(byte[] data, int offset, int length) throws java.io.IOExceptionSets the message to be sent.- Parameters:
data- The message dataoffset- The offset in the data arraylength- The length of the data to send- Throws:
java.io.IOException- If an I/O error occurs
-
getMessage
public byte[] getMessage()
Returns the current message being sent.- Returns:
- the current message byte array
-
isComplete
public boolean isComplete()
Checks if the send operation is complete.- Returns:
trueif the send is complete
-
getSelector
public java.nio.channels.Selector getSelector()
Returns the NIO selector.- Returns:
- the selector
-
setSelector
public void setSelector(java.nio.channels.Selector selector)
Sets the NIO selector.- Parameters:
selector- the selector
-
setComplete
public void setComplete(boolean complete)
Sets the completion flag.- Parameters:
complete- the completion status
-
-