Fields Summary |
---|
protected OP_EVENT | upcoming_opThe current non blocking operation. |
private CometContext | cometContextThe CometContext associated with this instance. |
private SelectionKey | keyThe SelectionKey . |
private CometSelector | cometSelectorThe CometSelector . |
private long | expireTimeThe time in milliseconds before this object was registered the
SelectionKey on the CometSelector |
private long | expirationDelayThe delay before interrupting the polled request and cancelling
the SelectionKey . |
private CometInputStream | cometInputStreamThe InputStream used to read bytes from the CometSelector |
private SelectionKey | cometKeyThe CometSelector registered key. |
private boolean | connectionClosedtrue if socketChannel.read no longer produces data (-1) or throw
an IOException. |
private CometEvent | eventThe CometEvent associated with this task. |
private CometWriter | writerThe CometWriter associated with this task. |
private CometReader | readerThe CometReader associated with this task. |
private boolean | asyncReadSupportedtrue if the CometHandler has been registered for OP_READ
events. |
Methods Summary |
---|
public void | doTask()Notify the CometHandler that bytes are available for read.
The notification will invoke all CometContext
/**
* The CometHandler in that case is **always** invoked using this
* thread so we can re-use the Thread attribute safely.
*/
ByteBuffer byteBuffer =
((WorkerThread)Thread.currentThread()).getByteBuffer();
boolean clearBuffer = true;
try{
if (cometKey == null || key == null) return;
if (cometInputStream == null){
cometInputStream = new CometInputStream();
}
if (event == null){
event = new CometEvent();
}
connectionClosed = false;
cometInputStream.setSelectionKey(cometKey);
if (byteBuffer == null){
byteBuffer = ByteBuffer.allocate(selectorThread.getBufferSize());
((WorkerThread)Thread.currentThread()).setByteBuffer(byteBuffer);
} else {
byteBuffer.clear();
}
cometInputStream.setByteBuffer(byteBuffer);
SocketChannel socketChannel = (SocketChannel)cometKey.channel();
if (upcoming_op == OP_EVENT.READ){
event.type = CometEvent.READ;
int nRead = 0;
/*
* We must execute the first read to prevent client abort.
*/
if ( (nRead = socketChannel.read(byteBuffer)) == -1){
connectionClosed = true;
} else {
/*
* This is an HTTP pipelined request. We need to resume
* the continuation and invoke the http parsing
* request code.
*/
if (!asyncReadSupported){
// Don't let the main Selector (SelectorThread) starts
// handling the pipelined request.
selectorThread.addBannedSelectionKey(key);
// Resume the previous request.
CometHandler cometHandler =
cometContext.getCometHandler(key);
/**
* Something when wrong, most probably the CometHandler
* has been resumed or removed by the Comet implementation.
*/
if (cometHandler == null){
return;
}
cometContext.resumeCometHandler(cometHandler, false);
clearBuffer = false;
DefaultReadTask readTask =
(DefaultReadTask)selectorThread.getReadTask(key);
readTask.setByteBuffer(byteBuffer);
readTask.setBytesAvailable(true);
// Re-use the same Thread.
readTask.doTask();
} else {
byteBuffer.flip();
reader = new CometReader();
reader.setNRead(nRead);
reader.setByteBuffer(byteBuffer);
event.attach(reader);
cometContext.notify(event,CometEvent.READ,key);
reader.setByteBuffer(null);
// This Reader is now invalid. Any attempt to use
// it will results in an IllegalStateException.
reader.setReady(false);
}
}
} else if (upcoming_op == OP_EVENT.WRITE){
event.type = CometEvent.WRITE;
writer = new CometWriter();
writer.setChannel(socketChannel);
event.attach(writer);
cometContext.notify(event,CometEvent.WRITE,key);
// This Writer is now invalid. Any attempt to use
// it will results in an IllegalStateException.
writer.setReady(false);
}
} catch (IOException ex){
connectionClosed = true;
// Bug 6403933 & GlassFish 2013
if (selectorThread.logger().isLoggable(Level.FINEST)){
selectorThread.logger().log(Level.FINEST,"Comet exception",ex);
}
} catch (Throwable t){
selectorThread.logger().log(Level.SEVERE,"Comet exception",t);
connectionClosed = true;
} finally {
// Bug 6403933
if (connectionClosed){
cometSelector.cancelKey(cometKey);
}
if (clearBuffer){
byteBuffer.clear();
}
asyncReadSupported = false;
}
|
public CometContext | getCometContext()Return the CometContext associated with this instance.
return cometContext;
|
public java.nio.channels.SelectionKey | getCometKey()Return the CometSelector 's SelectionKey .
return cometKey;
|
public CometSelector | getCometSelector()Return the CometSelector
return cometSelector;
|
public long | getExpirationDelay()Return the delay before interrupting the polled request and cancelling
the SelectionKey .
return expirationDelay;
|
public long | getExpireTime()Return the time in milliseconds before this object was registered the
SelectionKey on the CometSelector
return expireTime;
|
public java.nio.channels.SelectionKey | getSelectionKey()Return the SelectionKey
return key;
|
public boolean | isAsyncReadSupported()
return asyncReadSupported;
|
public void | recycle()Recycle this object.
super.recycle();
key = null;
cometContext = null;
asyncReadSupported = false;
if(cometInputStream != null) {
cometInputStream.recycle();
}
|
public void | setAsyncReadSupported(boolean asyncReadSupported)
this.asyncReadSupported = asyncReadSupported;
|
public void | setCometContext(CometContext cometContext)Set the CometContext used to notify CometHandler .
this.cometContext = cometContext;
|
public void | setCometKey(java.nio.channels.SelectionKey cometKey)Set the CometSelector 's SelectionKey .
this.cometKey = cometKey;
|
public void | setCometSelector(CometSelector cometSelector)Set the CometSelector
this.cometSelector = cometSelector;
|
public void | setExpirationDelay(long expirationDelay)Set the delay before interrupting the polled request and cancelling
the SelectionKey .
this.expirationDelay = expirationDelay;
|
public void | setExpireTime(long expireTime)Set the time in milliseconds before this object was registered the
SelectionKey on the CometSelector
this.expireTime = expireTime;
|
public void | setSelectionKey(java.nio.channels.SelectionKey key)Set the SelectionKey
this.key = key;
|
public void | taskEvent(com.sun.enterprise.web.connector.grizzly.TaskEvent event)Not used.
|