FileDocCategorySizeDatePackage
CometTask.javaAPI DocGlassfish v2 API13863Tue Jul 10 13:20:24 BST 2007com.sun.enterprise.web.connector.grizzly.comet

CometTask

public class CometTask extends com.sun.enterprise.web.connector.grizzly.TaskBase
A Task implementation that allow Grizzly ARP to notify CometHandler when new data (bytes) are available from the CometSelector.
author
Jeanfrancois Arcand

Fields Summary
protected OP_EVENT
upcoming_op
The current non blocking operation.
private CometContext
cometContext
The CometContext associated with this instance.
private SelectionKey
key
The SelectionKey.
private CometSelector
cometSelector
The CometSelector .
private long
expireTime
The time in milliseconds before this object was registered the SelectionKey on the CometSelector
private long
expirationDelay
The delay before interrupting the polled request and cancelling the SelectionKey.
private CometInputStream
cometInputStream
The InputStream used to read bytes from the CometSelector
private SelectionKey
cometKey
The CometSelector registered key.
private boolean
connectionClosed
true if socketChannel.read no longer produces data (-1) or throw an IOException.
private CometEvent
event
The CometEvent associated with this task.
private CometWriter
writer
The CometWriter associated with this task.
private CometReader
reader
The CometReader associated with this task.
private boolean
asyncReadSupported
true if the CometHandler has been registered for OP_READ events.
Constructors Summary
public CometTask()
New CometTask.

    
    
           
        
    
Methods Summary
public voiddoTask()
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 CometContextgetCometContext()
Return the CometContext associated with this instance.

return
CometContext the CometContext associated with this instance.

        return cometContext;
    
public java.nio.channels.SelectionKeygetCometKey()
Return the CometSelector's SelectionKey.

        return cometKey;
    
public CometSelectorgetCometSelector()
Return the CometSelector

return
CometSelector the CometSelector

        return cometSelector;
    
public longgetExpirationDelay()
Return the delay before interrupting the polled request and cancelling the SelectionKey.

return
long Return the delay before interrupting the polled request and cancelling the SelectionKey.

        return expirationDelay;
    
public longgetExpireTime()
Return the time in milliseconds before this object was registered the SelectionKey on the CometSelector

return
long Return the time in milliseconds before this object was registered the SelectionKey on the CometSelector

        return expireTime;
    
public java.nio.channels.SelectionKeygetSelectionKey()
Return the SelectionKey

return
SelectionKey SelectionKey

        return key;
    
public booleanisAsyncReadSupported()

        return asyncReadSupported;
    
public voidrecycle()
Recycle this object.

        super.recycle();
        key = null;
        cometContext = null;
        asyncReadSupported = false;
        if(cometInputStream != null) {
            cometInputStream.recycle();
        }
    
public voidsetAsyncReadSupported(boolean asyncReadSupported)

        this.asyncReadSupported = asyncReadSupported;
    
public voidsetCometContext(CometContext cometContext)
Set the CometContext used to notify CometHandler.

param
cometContext the CometContext used to notify CometHandler

        this.cometContext = cometContext;
    
public voidsetCometKey(java.nio.channels.SelectionKey cometKey)
Set the CometSelector's SelectionKey.

        this.cometKey = cometKey;
    
public voidsetCometSelector(CometSelector cometSelector)
Set the CometSelector

param
cometSelector the CometSelector

        this.cometSelector = cometSelector;
    
public voidsetExpirationDelay(long expirationDelay)
Set the delay before interrupting the polled request and cancelling the SelectionKey.

param
expirationDelay Return the delay before interrupting the polled request and cancelling the SelectionKey.

        this.expirationDelay = expirationDelay;
    
public voidsetExpireTime(long expireTime)
Set the time in milliseconds before this object was registered the SelectionKey on the CometSelector

param
expireTime Return the time in milliseconds before this object was registered the SelectionKey on the CometSelector

        this.expireTime = expireTime;
    
public voidsetSelectionKey(java.nio.channels.SelectionKey key)
Set the SelectionKey

param
SelectionKey SelectionKey

        this.key = key;
    
public voidtaskEvent(com.sun.enterprise.web.connector.grizzly.TaskEvent event)
Not used.