Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web.connector.grizzly.comet;
 
A Task implementation that allow Grizzly ARP to notify CometHandler when new data (bytes) are available from the CometSelector.

Author(s):
Jeanfrancois Arcand
 
 public class CometTask extends TaskBase{
     
     public enum OP_EVENT { READ, WRITE }
    
   
    
The current non blocking operation.
 
     protected OP_EVENT upcoming_op = .;
    
    
    
The CometContext associated with this instance.
 
     private CometContext cometContext;
        
    
    
The CometSelector .
 
     private CometSelector cometSelector;
    
    
    
The time in milliseconds before this object was registered the SelectionKey on the CometSelector
 
     private long expireTime = 0L;


    
The delay before interrupting the polled request and cancelling the SelectionKey.
 
     private long expirationDelay = 30 * 1000;
    
    
    
The InputStream used to read bytes from the CometSelector
    private ByteBufferInputStream cometInputStream;
    
    
    
The CometSelector registered key.
    private SelectionKey cometKey;
    

    
true if socketChannel.read no longer produces data (-1) or throw an IOException.
    private boolean connectionClosed;
    
    
    
The CometEvent associated with this task.
    private CometEvent event;
        
    
    
The CometWriter associated with this task.
    private CometWriter writer;
    
    
    
The CometReader associated with this task.
    private CometReader reader;
    
    
    
true if the CometHandler has been registered for OP_READ events.
    private boolean asyncReadSupported = false;
    
    
    
New CometTask.
    public CometTask() {  
    }
    
    
    
Notify the CometHandler that bytes are available for read. The notification will invoke all CometContext
    public void doTask() throws IOException{     
         // The CometHandler has been resumed.
        CometHandler cometHandler = .getCometHandler();
        if (.getCometHandler() == null){
            .cancel();
            .attach(null);
            CometEngine.getEngine().returnTask(this);
            return;
        }
        
        if (!.isActive(cometHandler)){
            return;
        }
                
        
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 ( == null ||  == nullreturn;
            
            if ( == null){
                 = new  ByteBufferInputStream();
            }
            
            if ( == null){
                 = new CometEvent();
            }
            
             = false;
            .setSelectionKey();  
            if (byteBuffer == null){
                byteBuffer = ByteBuffer.allocate(.getBufferSize());
                ((WorkerThread)Thread.currentThread()).setByteBuffer(byteBuffer);
            } else {
                byteBuffer.clear();
            }
            .setByteBuffer(byteBuffer); 
            SocketChannel socketChannel = (SocketChannel).channel();
            if ( == .){      
                . = .;
                
                /*
                 * We must execute the first read to prevent client abort.
                 */               
                int nRead = socketChannel.read(byteBuffer);   
                if (nRead == -1 || !socketChannel.isOpen()){
                     = true;
                } else {
                    /* 
                     * This is an HTTP pipelined request. We need to resume
                     * the continuation and invoke the http parsing 
                     * request code.
                     */
                    if (!){
                        // Don't let the main Selector (SelectorThread) starts
                        // handling the pipelined request.
                        .addBannedSelectionKey();
                        
                        
Something when wrong, most probably the CometHandler has been resumed or removed by the Comet implementation.
                        if (!.isActive(cometHandler)){
                            return;
                        }
                        
                        // Before executing, make sure the connection is still
                        // alive. This situation happens with SSL and there 
                        // is not a cleaner way fo handling the browser closing
                        // the connection.
                        nRead = socketChannel.read(byteBuffer);                         
                        if (nRead == -1){
                            = true;
                           return;
                        }
                        
                        .resumeCometHandler(cometHandlerfalse);
                        clearBuffer = false;
                        
                        DefaultReadTask readTask = 
                                (DefaultReadTask).getReadTask();
                        readTask.setByteBuffer(byteBuffer);
                        readTask.setBytesAvailable(true);
                        // Re-use the same Thread.
                        readTask.doTask();
                        
                    } else {
                        byteBuffer.flip(); 
                         = new CometReader();
                        .setNRead(nRead);
                        .setByteBuffer(byteBuffer);
                        .attach();
                        .invokeCometHandler(,.,); 
                        .setByteBuffer(null);
                        
                        // This Reader is now invalid. Any attempt to use
                        // it will results in an IllegalStateException.
                        .setReady(false);
                    }
                }
            } else if ( == .){  
                . = .;
                 = new CometWriter();
                .setChannel(socketChannel);
                .attach();
                .invokeCometHandler(,.,);  
                        
                // This Writer is now invalid. Any attempt to use
                // it will results in an IllegalStateException.                
                .setReady(false);
           }
        } catch (IOException ex){
             = true;
            // Bug 6403933 & GlassFish 2013
            if (SelectorThread.logger().isLoggable(.)){
                SelectorThread.logger().log(.,"Comet exception",ex);
            }
        } catch (Throwable t){
            SelectorThread.logger().log(.,"Comet exception",t);
             = true;
        } finally {   
            // Bug 6403933
            if (){
                .cancelKey();
            }
            
            if (clearBuffer){
                byteBuffer.clear();
            }
             = false;
        }
    }
    
    
    
Not used.
    @Override
    public void taskEvent(TaskEvent event) {
    }

    
    
Return the CometContext associated with this instance.

Returns:
CometContext the CometContext associated with this instance.
    public CometContext getCometContext() {
        return ;
    }
    
    
    
Set the CometContext used to notify CometHandler.

Parameters:
cometContext the CometContext used to notify CometHandler
    public void setCometContext(CometContext cometContext) {
        this. = cometContext;
    }
  
    
Recycle this object.
    @Override
    public void recycle(){
        super.recycle();
         = null;
         = null;
         = false;
        if( != null) {
            .recycle();
        }
    }

    
    
Return the CometSelector

Returns:
CometSelector the CometSelector
    public CometSelector getCometSelector() {
        return ;
    }

    
    
Set the CometSelector

Parameters:
cometSelector the CometSelector
   
    public void setCometSelector(CometSelector cometSelector) {
        this. = cometSelector;
    }
    
    
    
Return the time in milliseconds before this object was registered the SelectionKey on the CometSelector

Returns:
long Return the time in milliseconds before this object was registered the SelectionKey on the CometSelector
    public long getExpireTime() {
        return ;
    }

    
    
Set the time in milliseconds before this object was registered the SelectionKey on the CometSelector

Parameters:
expireTime Return the time in milliseconds before this object was registered the SelectionKey on the CometSelector
   
    public void setExpireTime(long expireTime) {
        this. = expireTime;
    }
    
    
    
Return the delay before interrupting the polled request and cancelling the SelectionKey.

Returns:
long Return the delay before interrupting the polled request and cancelling the SelectionKey.
    public long getExpirationDelay() {
        return ;
    }

    
    
Set the delay before interrupting the polled request and cancelling the SelectionKey.

Parameters:
expirationDelay Return the delay before interrupting the polled request and cancelling the SelectionKey.
    
    public void setExpirationDelay(long expirationDelay) {
        this. = expirationDelay;
    }

    
    
Return the CometSelector's SelectionKey.
    public SelectionKey getCometKey() {
        return ;
    }

    
    
Set the CometSelector's SelectionKey.
    public void setCometKey(SelectionKey cometKey) {
        this. = cometKey;
    }
    
    public boolean isAsyncReadSupported() {
        return ;
    }
    
    public void setAsyncReadSupported(boolean asyncReadSupported) {
        this. = asyncReadSupported;
    }
New to GrepCode? Check out our FAQ X