Coverage Report - com.sun.grizzly.DefaultSelectionKeyHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultSelectionKeyHandler
79 %
72/91
75 %
33/44
0
 
 1  
 /*
 2  
  * 
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 4  
  * 
 5  
  * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
 6  
  * 
 7  
  * The contents of this file are subject to the terms of either the GNU
 8  
  * General Public License Version 2 only ("GPL") or the Common Development
 9  
  * and Distribution License("CDDL") (collectively, the "License").  You
 10  
  * may not use this file except in compliance with the License. You can obtain
 11  
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 12  
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 13  
  * language governing permissions and limitations under the License.
 14  
  * 
 15  
  * When distributing the software, include this License Header Notice in each
 16  
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 17  
  * Sun designates this particular file as subject to the "Classpath" exception
 18  
  * as provided by Sun in the GPL Version 2 section of the License file that
 19  
  * accompanied this code.  If applicable, add the following below the License
 20  
  * Header, with the fields enclosed by brackets [] replaced by your own
 21  
  * identifying information: "Portions Copyrighted [year]
 22  
  * [name of copyright owner]"
 23  
  * 
 24  
  * Contributor(s):
 25  
  * 
 26  
  * If you wish your version of this file to be governed by only the CDDL or
 27  
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
 28  
  * elects to include this software in this distribution under the [CDDL or GPL
 29  
  * Version 2] license."  If you don't indicate a single choice of license, a
 30  
  * recipient has the option to distribute your version of this file under
 31  
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
 32  
  * its licensees as provided above.  However, if you add GPL Version 2 code
 33  
  * and therefore, elected the GPL Version 2 license, then the option applies
 34  
  * only if the new code is made subject to such option by the copyright
 35  
  * holder.
 36  
  *
 37  
  */
 38  
 
 39  
 package com.sun.grizzly;
 40  
 
 41  
 import com.sun.grizzly.util.Copyable;
 42  
 import com.sun.grizzly.util.SelectionKeyAttachment;
 43  
 import java.nio.channels.ClosedChannelException;
 44  
 import java.nio.channels.SelectableChannel;
 45  
 import java.nio.channels.SelectionKey;
 46  
 import java.nio.channels.Selector;
 47  
 import java.util.Iterator;
 48  
 import java.util.logging.Level;
 49  
 
 50  
 
 51  
 /**
 52  
  * Default implementation of a SelectionKey Handler. By default, this
 53  
  * class will attach a Long to a SelectionKey in order to calculate the
 54  
  * time a SelectionKey can stay active. By default, a SelectionKey will be
 55  
  * active for 30 seconds. If during that 30 seconds the client isn't pushing
 56  
  * bytes (or closing the connection). the SelectionKey will be expired and
 57  
  * its channel closed.
 58  
  *
 59  
  * @author Jeanfrancois Arcand
 60  
  */
 61  
 public class DefaultSelectionKeyHandler extends BaseSelectionKeyHandler {
 62  
 
 63  
 
 64  
     /**
 65  
      * Next time the exprireKeys() will delete keys.
 66  
      */
 67  130
     protected long nextKeysExpiration = 0;
 68  
     
 69  
     
 70  
     /*
 71  
      * Number of seconds before idle keep-alive connections expire
 72  
      */
 73  130
     protected long timeout = 30 * 1000L;
 74  
 
 75  
     
 76  127
     public DefaultSelectionKeyHandler() {
 77  127
     }
 78  
    
 79  
     
 80  
     public DefaultSelectionKeyHandler(SelectorHandler selectorHandler) {
 81  3
         super(selectorHandler);
 82  3
     }
 83  
 
 84  
    
 85  
     /**
 86  
      * {@inheritDoc}
 87  
      */
 88  
     @Override
 89  
     public void copyTo(Copyable copy) {
 90  4
         super.copyTo(copy);
 91  4
         DefaultSelectionKeyHandler copyHandler = (DefaultSelectionKeyHandler) copy;
 92  4
         copyHandler.timeout = timeout;
 93  4
     }
 94  
 
 95  
     /**
 96  
      * {@inheritDoc}
 97  
      */
 98  
     @Override
 99  
     public void process(SelectionKey key) {
 100  114227
         super.process(key);
 101  114227
         removeExpirationStamp(key);
 102  114227
     }
 103  
     
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     @Override
 108  
     public void postProcess(SelectionKey key) {
 109  113225
         super.postProcess(key);
 110  113225
         addExpirationStamp(key);
 111  113225
     }
 112  
     
 113  
     
 114  
     /**
 115  
      * {@inheritDoc}
 116  
      */
 117  
     @Override
 118  
     public void register(Iterator<SelectionKey> keyIterator, int selectionKeyOps) {
 119  0
         long currentTime = System.currentTimeMillis();
 120  
         SelectionKey key;
 121  0
         while (keyIterator.hasNext()) {
 122  0
             key = keyIterator.next();
 123  0
             keyIterator.remove();
 124  0
             doRegisterKey(key, selectionKeyOps, currentTime);
 125  
         }
 126  0
     }
 127  
     
 128  
     
 129  
     /**
 130  
      * {@inheritDoc}
 131  
      */
 132  
     @Override
 133  
     public void register(SelectionKey key, int selectionKeyOps) {
 134  376824
         doRegisterKey(key, selectionKeyOps, System.currentTimeMillis());
 135  376824
     }
 136  
     
 137  
     /**
 138  
      * Registers {@link SelectionKey} to handle certain operations
 139  
      */
 140  
     protected void doRegisterKey(SelectionKey key, int selectionKeyOps,
 141  
             long currentTime) {
 142  376826
         if (!key.isValid()) {
 143  0
             return;
 144  
         }
 145  
 
 146  376826
         key.interestOps(key.interestOps() | selectionKeyOps);
 147  376826
         Object attachment = key.attachment();
 148  
         // By default, attachment a null.
 149  376826
         if (attachment == null) {
 150  3525
             key.attach(currentTime);
 151  
         }
 152  376826
     }
 153  
 
 154  
     /**
 155  
      * {@inheritDoc}
 156  
      */
 157  
     @Override
 158  
     public void register(SelectableChannel channel, int ops) 
 159  
             throws ClosedChannelException {
 160  32
         if (!channel.isOpen()) {
 161  0
             return;
 162  
         }
 163  
 
 164  32
         Selector selector = selectorHandler.getSelector();
 165  32
         SelectionKey key = channel.keyFor(selector);
 166  32
         long time = System.currentTimeMillis();
 167  
         
 168  32
         if (key == null) {
 169  30
             key = channel.register(selector, ops, time);
 170  
         } else {
 171  2
             doRegisterKey(key, ops, time);
 172  
         }
 173  32
     }
 174  
 
 175  
     /**
 176  
      * {@inheritDoc}
 177  
      */
 178  
     @Override
 179  
     @SuppressWarnings("empty-statement")
 180  
     public void register(SelectionKey key, long currentTime){
 181  
        ;
 182  0
     }
 183  
     
 184  
     
 185  
     /**
 186  
      * @deprecated
 187  
      */
 188  
     @Override
 189  
     @SuppressWarnings("empty-statement")
 190  
     public void expire(SelectionKey key, long currentTime) {
 191  
         ;
 192  0
     }
 193  
     
 194  
     
 195  
     /**
 196  
      * {@inheritDoc}
 197  
      */
 198  
     @Override
 199  
     public void expire(Iterator<SelectionKey> iterator) {
 200  200267
         if (timeout <= 0) return;
 201  
         
 202  200267
         long currentTime = System.currentTimeMillis();
 203  200267
         if (currentTime < nextKeysExpiration) {
 204  200181
             return;
 205  
         }
 206  86
         nextKeysExpiration = currentTime + timeout;        
 207  
                 
 208  
         
 209  
         SelectionKey key;
 210  194
         while (iterator.hasNext()) {
 211  108
             key = iterator.next();
 212  
             
 213  108
             if (!key.isValid()){
 214  0
                 continue;
 215  
             }
 216  
 
 217  
 
 218  108
             Long expire = getExpirationStamp(key);
 219  108
             if (expire != null){
 220  29
                 if (currentTime - expire >= timeout) {
 221  0
                     cancel(key);
 222  29
                 } else if (expire + timeout < nextKeysExpiration) {
 223  4
                     nextKeysExpiration = expire + timeout;
 224  
                 }
 225  
             }
 226  108
         }
 227  86
     }
 228  
     
 229  
     
 230  
     public long getTimeout() {
 231  0
         return timeout;
 232  
     }
 233  
     
 234  
     
 235  
     public void setTimeout(long timeout) {
 236  0
         this.timeout = timeout;
 237  0
     }
 238  
     
 239  
 
 240  
     @Override
 241  
     protected Object clearKeyAttachment(SelectionKey key) {
 242  400
         Object attachment = super.clearKeyAttachment(key);
 243  400
         if (attachment instanceof SelectionKeyAttachment) {
 244  292
             ((SelectionKeyAttachment) attachment).release(key);
 245  
         }
 246  400
         return attachment;
 247  
     }
 248  
 
 249  
 
 250  
     /**
 251  
      * Removes expiration timeout stamp from the {@link SelectionKey} 
 252  
      * depending on its attachment
 253  
      * 
 254  
      * @param {@link SelectionKey}
 255  
      */
 256  
     private void removeExpirationStamp(SelectionKey key) {
 257  114227
         Object attachment = key.attachment();
 258  114227
         if (attachment != null) {
 259  114223
             if (attachment instanceof Long) {
 260  8723
                 key.attach(null);
 261  105500
             } else if (attachment instanceof SelectionKeyAttachment) {
 262  105500
                 ((SelectionKeyAttachment) attachment).setTimeout(null);
 263  
             }
 264  
         }
 265  114227
     }
 266  
     
 267  
     /**
 268  
      * Adds expiration timeout stamp to the {@link SelectionKey} 
 269  
      * depending on its attachment
 270  
      * 
 271  
      * @param {@link SelectionKey}
 272  
      */
 273  
     private void addExpirationStamp(SelectionKey key) {
 274  113225
         long currentTime = System.currentTimeMillis();
 275  113225
         Object attachment = key.attachment();
 276  113225
         if (attachment == null) {
 277  5263
             key.attach(currentTime);
 278  107962
         } else if (attachment instanceof SelectionKeyAttachment) {
 279  105500
             ((SelectionKeyAttachment) attachment).setTimeout(currentTime);
 280  
         }    
 281  113225
     }
 282  
     
 283  
     /**
 284  
      * Gets expiration timeout stamp from the {@link SelectionKey} 
 285  
      * depending on its attachment
 286  
      * 
 287  
      * @param {@link SelectionKey}
 288  
      */
 289  
     private Long getExpirationStamp(SelectionKey key) {
 290  108
         Object attachment = key.attachment();
 291  108
         if (attachment != null) {
 292  
             try {
 293  
 
 294  
                 // This is extremely bad to invoke instanceof here but 
 295  
                 // since the framework expose the SelectionKey, an application
 296  
                 // can always attach an object on the SelectionKey and we 
 297  
                 // can't predict the type of the attached object.                                
 298  66
                 if (attachment instanceof Long) {
 299  26
                     return (Long) attachment;
 300  40
                 } else if (attachment instanceof SelectionKeyAttachment) {
 301  40
                     return ((SelectionKeyAttachment) attachment).getTimeout();
 302  
                 }
 303  0
             } catch (ClassCastException ex) {
 304  0
                 if (logger.isLoggable(Level.FINEST)) {
 305  0
                     logger.log(Level.FINEST, 
 306  
                             "Invalid SelectionKey attachment", ex);
 307  
                 }
 308  0
             }
 309  
         }
 310  
 
 311  42
         return null;
 312  
     }
 313  
 }