use of org.apache.catalina.tribes.io.ObjectReader in project tomcat by apache.
the class NioReceiver method listen.
/**
* Get data from channel and store in byte array
* send it to cluster
* @throws IOException IO error
*/
protected void listen() throws Exception {
if (doListen()) {
log.warn(sm.getString("nioReceiver.alreadyStarted"));
return;
}
setListen(true);
// Avoid NPEs if selector is set to null on stop.
Selector selector = this.selector.get();
if (selector != null && datagramChannel != null) {
//max size for a datagram packet
ObjectReader oreader = new ObjectReader(MAX_UDP_SIZE);
registerChannel(selector, datagramChannel, SelectionKey.OP_READ, oreader);
}
while (doListen() && selector != null) {
// selected set contains keys of the ready channels
try {
events();
socketTimeouts();
int n = selector.select(getSelectorTimeout());
if (n == 0) {
// nothing to do
continue;
}
// get an iterator over the set of selected keys
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
// look at each key in the selected set
while (it != null && it.hasNext()) {
SelectionKey key = it.next();
// Is a new connection coming in?
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();
channel.socket().setReceiveBufferSize(getTxBufSize());
channel.socket().setSendBufferSize(getTxBufSize());
channel.socket().setTcpNoDelay(getTcpNoDelay());
channel.socket().setKeepAlive(getSoKeepAlive());
channel.socket().setOOBInline(getOoBInline());
channel.socket().setReuseAddress(getSoReuseAddress());
channel.socket().setSoLinger(getSoLingerOn(), getSoLingerTime());
channel.socket().setSoTimeout(getTimeout());
Object attach = new ObjectReader(channel);
registerChannel(selector, channel, SelectionKey.OP_READ, attach);
}
// is there data to read on this channel?
if (key.isReadable()) {
readDataFromSocket(key);
} else {
key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
}
// remove key from selected set, it's been handled
it.remove();
}
} catch (java.nio.channels.ClosedSelectorException cse) {
// ignore is normal at shutdown or stop listen socket
} catch (java.nio.channels.CancelledKeyException nx) {
log.warn(sm.getString("nioReceiver.clientDisconnect"));
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error(sm.getString("nioReceiver.requestError"), t);
}
}
serverChannel.close();
if (datagramChannel != null) {
try {
datagramChannel.close();
} catch (Exception iox) {
if (log.isDebugEnabled())
log.debug("Unable to close datagram channel.", iox);
}
datagramChannel = null;
}
closeSelector();
}
use of org.apache.catalina.tribes.io.ObjectReader in project tomcat by apache.
the class NioReplicationTask method serviceChannel.
/**
* Called to initiate a unit of work by this worker thread
* on the provided SelectionKey object. This method is
* synchronized, as is the run() method, so only one key
* can be serviced at a given time.
* Before waking the worker thread, and before returning
* to the main selection loop, this key's interest set is
* updated to remove OP_READ. This will cause the selector
* to ignore read-readiness for this channel while the
* worker thread is servicing it.
* @param key The key to process
*/
public synchronized void serviceChannel(SelectionKey key) {
if (log.isTraceEnabled())
log.trace("About to service key:" + key);
ObjectReader reader = (ObjectReader) key.attachment();
if (reader != null)
reader.setLastAccess(System.currentTimeMillis());
this.key = key;
key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
}
Aggregations