| Method from org.apache.catalina.cluster.tcp.TcpReplicationThread Detail: |
protected void drainChannel(SelectionKey key) throws Exception {
boolean packetReceived=false;
SocketChannel channel = (SocketChannel) key.channel();
int count;
buffer.clear(); // make buffer empty
ObjectReader reader = (ObjectReader)key.attachment();
// loop while data available, channel is non-blocking
while ((count = channel.read (buffer)) > 0) {
buffer.flip(); // make buffer readable
reader.append(buffer.array(),0,count);
buffer.clear(); // make buffer empty
}
//check to see if any data is available
int pkgcnt = reader.execute();
if (log.isTraceEnabled()) {
log.trace("sending " + pkgcnt + " ack packages to " + channel.socket().getLocalPort() );
}
if (sendAck) {
while ( pkgcnt > 0 ) {
sendAck(key,channel);
pkgcnt--;
}
}
if (count < 0) {
// close channel on EOF, invalidates the key
channel.close();
return;
}
//acquire the interestOps mutex
Object mutex = this.getPool().getInterestOpsMutex();
synchronized (mutex) {
// cycle the selector so this key is active again
key.selector().wakeup();
// resume interest in OP_READ, OP_WRITE
int resumeOps = key.interestOps() | SelectionKey.OP_READ;
key.interestOps(resumeOps);
}
}
The actual code which drains the channel associated with
the given key. This method assumes the key has been
modified prior to invocation to turn off selection
interest in OP_READ. When this method completes it
re-enables OP_READ and calls wakeup() on the selector
so the selector will resume watching this channel. |
public synchronized void run() {
while (doRun) {
try {
// sleep and release object lock
this.wait();
} catch (InterruptedException e) {
if(log.isInfoEnabled())
log.info("TCP worker thread interrupted in cluster",e);
// clear interrupt status
Thread.interrupted();
}
if (key == null) {
continue; // just in case
}
try {
drainChannel (key);
} catch (Exception e) {
log.error ("TCP Worker thread in cluster caught '"
+ e + "' closing channel", e);
// close channel and nudge selector
try {
key.channel().close();
} catch (IOException ex) {
log.error("Unable to close channel.",ex);
}
key.selector().wakeup();
}
key = null;
// done, ready for more, return to pool
this.pool.returnWorker (this);
}
}
|
protected void sendAck(SelectionKey key,
SocketChannel channel) {
try {
channel.write(ByteBuffer.wrap(ACK_COMMAND));
if (log.isTraceEnabled()) {
log.trace("ACK sent to " + channel.socket().getPort());
}
} catch ( java.io.IOException x ) {
log.warn("Unable to send ACK back through channel, channel disconnected?: "+x.getMessage());
}
}
send a reply-acknowledgement (6,2,3) |
synchronized void serviceChannel(SelectionKey key,
boolean sendAck) {
this.key = key;
this.sendAck=sendAck;
key.interestOps (key.interestOps() & (~SelectionKey.OP_READ));
key.interestOps (key.interestOps() & (~SelectionKey.OP_WRITE));
this.notify(); // awaken the thread
}
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. |