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.io.IOException;
  43: import java.nio.ByteBuffer;
  44: import java.nio.channels.SelectableChannel;
  45: import java.nio.channels.SelectionKey;
  46: import java.nio.channels.Selector;
  47: import java.nio.channels.spi.AbstractSelectionKey;
  48: 
  49: /**
  50:  * @author Casey Marshall (csm@gnu.org)
  51:  */
  52: public class KqueueSelectionKeyImpl extends AbstractSelectionKey
  53: {
  54:   int interestOps;
  55:   int readyOps;
  56:   int activeOps = 0;
  57:   int key;
  58:   int fd;
  59: 
  60:   /** The selector we were created for. */
  61:   private final KqueueSelectorImpl selector;
  62:   
  63:   /** The channel we are attached to. */
  64:   private final SelectableChannel channel;
  65:   
  66:   private final VMChannelOwner natChannel;
  67:   
  68:   public KqueueSelectionKeyImpl(KqueueSelectorImpl selector,
  69:                                 SelectableChannel channel)
  70:   {
  71:     this.selector = selector;
  72:     this.channel = channel;
  73:     natChannel = (VMChannelOwner) channel;
  74:     interestOps = 0;
  75:     readyOps = 0;
  76:   }
  77: 
  78:   /* (non-Javadoc)
  79:    * @see java.nio.channels.SelectionKey#channel()
  80:    */
  81:   //@Override
  82:   public SelectableChannel channel()
  83:   {
  84:     return channel;
  85:   }
  86: 
  87:   /* (non-Javadoc)
  88:    * @see java.nio.channels.SelectionKey#interestOps()
  89:    */
  90:   //@Override
  91:   public int interestOps()
  92:   {
  93:     return interestOps;
  94:   }
  95: 
  96:   /* (non-Javadoc)
  97:    * @see java.nio.channels.SelectionKey#interestOps(int)
  98:    */
  99:   //@Override
 100:   public SelectionKey interestOps(int ops)
 101:   {
 102:     if (!isValid())
 103:       throw new IllegalStateException("key is invalid");
 104:     if ((ops & ~channel.validOps()) != 0)
 105:       throw new IllegalArgumentException("channel does not support all operations");
 106:     
 107:     selector.setInterestOps(this, ops);
 108:     return this;
 109:   }
 110: 
 111:   /* (non-Javadoc)
 112:    * @see java.nio.channels.SelectionKey#readyOps()
 113:    */
 114:   //@Override
 115:   public int readyOps()
 116:   {
 117:     return readyOps;
 118:   }
 119: 
 120:   /* (non-Javadoc)
 121:    * @see java.nio.channels.SelectionKey#selector()
 122:    */
 123:   //@Override
 124:   public Selector selector()
 125:   {
 126:     return selector;
 127:   }
 128:   
 129:   public String toString()
 130:   {
 131:     if (!isValid())
 132:       return super.toString() + " [ fd: " + fd + " <<invalid>> ]";
 133:     return super.toString() + " [ fd: " + fd + " interest ops: {"
 134:       + ((interestOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "")
 135:       + ((interestOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "")
 136:       + ((interestOps & OP_READ) != 0 ? " OP_READ" : "")
 137:       + ((interestOps & OP_WRITE) != 0 ? " OP_WRITE" : "")
 138:       + " }; ready ops: {"
 139:       + ((readyOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "")
 140:       + ((readyOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "")
 141:       + ((readyOps & OP_READ) != 0 ? " OP_READ" : "")
 142:       + ((readyOps & OP_WRITE) != 0 ? " OP_WRITE" : "")
 143:       + " } ]";
 144:   }
 145:   
 146:   public int hashCode()
 147:   {
 148:     return fd;
 149:   }
 150:   
 151:   public boolean equals(Object o)
 152:   {
 153:     if (!(o instanceof KqueueSelectionKeyImpl))
 154:       return false;
 155:     KqueueSelectionKeyImpl that = (KqueueSelectionKeyImpl) o;
 156:     return that.fd == this.fd && that.channel.equals(this.channel);
 157:   }
 158:   
 159:   
 160:   boolean isReadActive()
 161:   {
 162:     return (activeOps & (OP_READ | OP_ACCEPT)) != 0;
 163:   }
 164: 
 165:   boolean isReadInterested()
 166:   {
 167:     return (interestOps & (OP_READ | OP_ACCEPT)) != 0;
 168:   }
 169:   
 170:   boolean isWriteActive()
 171:   {
 172:     return (activeOps & (OP_WRITE | OP_CONNECT)) != 0;
 173:   }
 174:   
 175:   boolean isWriteInterested()
 176:   {
 177:     return (interestOps & (OP_WRITE | OP_CONNECT)) != 0;
 178:   }
 179:   
 180:   boolean needCommitRead()
 181:   {
 182:     return isReadActive() == (!isReadInterested());
 183:   }
 184: 
 185:   boolean needCommitWrite()
 186:   {
 187:     return isWriteActive() == (!isWriteInterested());
 188:   }
 189: }