Source for gnu.java.nio.KqueueSelectionKeyImpl

   1: /* KqueueSelectionKeyImpl.java -- selection key for kqueue/kevent.
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.nio;
  40: 
  41: 
  42: import java.nio.channels.SelectableChannel;
  43: import java.nio.channels.SelectionKey;
  44: import java.nio.channels.Selector;
  45: import java.nio.channels.spi.AbstractSelectionKey;
  46: 
  47: /**
  48:  * @author Casey Marshall (csm@gnu.org)
  49:  */
  50: public class KqueueSelectionKeyImpl extends AbstractSelectionKey
  51: {
  52:   int interestOps;
  53:   int readyOps;
  54:   int activeOps = 0;
  55:   int key;
  56:   int fd;
  57: 
  58:   /** The selector we were created for. */
  59:   private final KqueueSelectorImpl selector;
  60: 
  61:   /** The channel we are attached to. */
  62:   private final SelectableChannel channel;
  63: 
  64:   private final VMChannelOwner natChannel;
  65: 
  66:   public KqueueSelectionKeyImpl(KqueueSelectorImpl selector,
  67:                                 SelectableChannel channel)
  68:   {
  69:     this.selector = selector;
  70:     this.channel = channel;
  71:     natChannel = (VMChannelOwner) channel;
  72:     interestOps = 0;
  73:     readyOps = 0;
  74:   }
  75: 
  76:   /* (non-Javadoc)
  77:    * @see java.nio.channels.SelectionKey#channel()
  78:    */
  79:   //@Override
  80:   public SelectableChannel channel()
  81:   {
  82:     return channel;
  83:   }
  84: 
  85:   /* (non-Javadoc)
  86:    * @see java.nio.channels.SelectionKey#interestOps()
  87:    */
  88:   //@Override
  89:   public int interestOps()
  90:   {
  91:     return interestOps;
  92:   }
  93: 
  94:   /* (non-Javadoc)
  95:    * @see java.nio.channels.SelectionKey#interestOps(int)
  96:    */
  97:   //@Override
  98:   public SelectionKey interestOps(int ops)
  99:   {
 100:     if (!isValid())
 101:       throw new IllegalStateException("key is invalid");
 102:     if ((ops & ~channel.validOps()) != 0)
 103:       throw new IllegalArgumentException("channel does not support all operations");
 104: 
 105:     selector.setInterestOps(this, ops);
 106:     return this;
 107:   }
 108: 
 109:   /* (non-Javadoc)
 110:    * @see java.nio.channels.SelectionKey#readyOps()
 111:    */
 112:   //@Override
 113:   public int readyOps()
 114:   {
 115:     return readyOps;
 116:   }
 117: 
 118:   /* (non-Javadoc)
 119:    * @see java.nio.channels.SelectionKey#selector()
 120:    */
 121:   //@Override
 122:   public Selector selector()
 123:   {
 124:     return selector;
 125:   }
 126: 
 127:   public String toString()
 128:   {
 129:     if (!isValid())
 130:       return super.toString() + " [ fd: " + fd + " <<invalid>> ]";
 131:     return super.toString() + " [ fd: " + fd + " interest ops: {"
 132:       + ((interestOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "")
 133:       + ((interestOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "")
 134:       + ((interestOps & OP_READ) != 0 ? " OP_READ" : "")
 135:       + ((interestOps & OP_WRITE) != 0 ? " OP_WRITE" : "")
 136:       + " }; ready ops: {"
 137:       + ((readyOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "")
 138:       + ((readyOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "")
 139:       + ((readyOps & OP_READ) != 0 ? " OP_READ" : "")
 140:       + ((readyOps & OP_WRITE) != 0 ? " OP_WRITE" : "")
 141:       + " } ]";
 142:   }
 143: 
 144:   public int hashCode()
 145:   {
 146:     return fd;
 147:   }
 148: 
 149:   public boolean equals(Object o)
 150:   {
 151:     if (!(o instanceof KqueueSelectionKeyImpl))
 152:       return false;
 153:     KqueueSelectionKeyImpl that = (KqueueSelectionKeyImpl) o;
 154:     return that.fd == this.fd && that.channel.equals(this.channel);
 155:   }
 156: 
 157: 
 158:   boolean isReadActive()
 159:   {
 160:     return (activeOps & (OP_READ | OP_ACCEPT)) != 0;
 161:   }
 162: 
 163:   boolean isReadInterested()
 164:   {
 165:     return (interestOps & (OP_READ | OP_ACCEPT)) != 0;
 166:   }
 167: 
 168:   boolean isWriteActive()
 169:   {
 170:     return (activeOps & (OP_WRITE | OP_CONNECT)) != 0;
 171:   }
 172: 
 173:   boolean isWriteInterested()
 174:   {
 175:     return (interestOps & (OP_WRITE | OP_CONNECT)) != 0;
 176:   }
 177: 
 178:   boolean needCommitRead()
 179:   {
 180:     return isReadActive() == (!isReadInterested());
 181:   }
 182: 
 183:   boolean needCommitWrite()
 184:   {
 185:     return isWriteActive() == (!isWriteInterested());
 186:   }
 187: }