From c145bf0ba7bbcf5a7430ab9381e6bae5e20ee434 Mon Sep 17 00:00:00 2001 From: Casey Marshall Date: Wed, 27 Sep 2006 21:19:31 +0000 Subject: 2006-09-27 Casey Marshall * gnu/java/nio/KqueueSelectionKeyImpl.java: extend AbstractSelectionKey. (nstate, valid, readEverEnabled, writeEverEnabled): removed. (activeOps, fd): new fields. (cancel): removed. (interestOps): just call `selector.setInterestOps.' (isValid): removed. (toString): include native fd in output. (hashCode, equals, isReadActive, isReadInterested, isWriteActive, isWriteInterested, needCommitRead, needCommitWrite): new methods. * gnu/java/nio/KqueueSelectorImpl.java (MAX_DOUBLING_CAPACITY, CAP_INCREMENT, INITIAL_CAPACITY): new constants. (cancelled): removed. (events): new field. (OP_ACCEPT, OP_CONNECT, OP_READ, OP_WRITE): new constants. (toString, equals): new methods. (doSelect): get cancelled keys from superclass; fix synchronization; initialize events that need to be added/deleted only when selecting; ignore keys attached to closed channels. (register): fix key initialization; synchronize adding keys. (setInterestOps): new method. (updateOps, updateOps): removed. (reallocateBuffer): new method. (doCancel): removed. (kevent_set): add index, active ops parameters; remove delete parameter. (kevent): add output space size parameter. * include/gnu_java_nio_KqueueSelectorImpl.h: regenerated. * native/jni/java-nio/gnu_java_nio_KqueueSelectorImpl.c (Java_gnu_java_nio_KqueueSelectorImpl_kevent_1set): only fill in one filter, at the given index. (Java_gnu_java_nio_KqueueSelectorImpl_kevent): separate incoming event count and outgoing event count. --- gnu/java/nio/KqueueSelectionKeyImpl.java | 94 ++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 40 deletions(-) (limited to 'gnu/java/nio/KqueueSelectionKeyImpl.java') diff --git a/gnu/java/nio/KqueueSelectionKeyImpl.java b/gnu/java/nio/KqueueSelectionKeyImpl.java index 9e5035eb4..2f93c50cc 100644 --- a/gnu/java/nio/KqueueSelectionKeyImpl.java +++ b/gnu/java/nio/KqueueSelectionKeyImpl.java @@ -44,19 +44,18 @@ import java.nio.ByteBuffer; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelectionKey; /** * @author Casey Marshall (csm@gnu.org) */ -public class KqueueSelectionKeyImpl extends SelectionKey +public class KqueueSelectionKeyImpl extends AbstractSelectionKey { int interestOps; int readyOps; - ByteBuffer nstate; - boolean valid; + int activeOps = 0; int key; - boolean readEverEnabled = false; - boolean writeEverEnabled = false; + int fd; /** The selector we were created for. */ private final KqueueSelectorImpl selector; @@ -74,17 +73,6 @@ public class KqueueSelectionKeyImpl extends SelectionKey natChannel = (VMChannelOwner) channel; interestOps = 0; readyOps = 0; - valid = true; - } - - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#cancel() - */ - //@Override - public void cancel() - { - selector.doCancel(this); - valid = false; } /* (non-Javadoc) @@ -112,32 +100,14 @@ public class KqueueSelectionKeyImpl extends SelectionKey public SelectionKey interestOps(int ops) { if (!isValid()) - throw new IllegalStateException(); + throw new IllegalStateException("key is invalid"); if ((ops & ~channel.validOps()) != 0) - throw new IllegalArgumentException(); - this.interestOps = ops; - try - { - selector.updateOps(this, - natChannel.getVMChannel().getState().getNativeFD(), - false); - } - catch (IOException ioe) - { - throw new IllegalStateException("channel is invalid"); - } + throw new IllegalArgumentException("channel does not support all operations"); + + selector.setInterestOps(this, ops); return this; } - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#isValid() - */ - //@Override - public boolean isValid() - { - return valid && selector.isOpen(); - } - /* (non-Javadoc) * @see java.nio.channels.SelectionKey#readyOps() */ @@ -159,8 +129,8 @@ public class KqueueSelectionKeyImpl extends SelectionKey public String toString() { if (!isValid()) - return super.toString() + " [ <> ]"; - return super.toString() + " [ interest ops: {" + return super.toString() + " [ fd: " + fd + " <> ]"; + return super.toString() + " [ fd: " + fd + " interest ops: {" + ((interestOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "") + ((interestOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "") + ((interestOps & OP_READ) != 0 ? " OP_READ" : "") @@ -172,4 +142,48 @@ public class KqueueSelectionKeyImpl extends SelectionKey + ((readyOps & OP_WRITE) != 0 ? " OP_WRITE" : "") + " } ]"; } + + public int hashCode() + { + return fd; + } + + public boolean equals(Object o) + { + if (!(o instanceof KqueueSelectionKeyImpl)) + return false; + KqueueSelectionKeyImpl that = (KqueueSelectionKeyImpl) o; + return that.fd == this.fd && that.channel.equals(this.channel); + } + + + boolean isReadActive() + { + return (activeOps & (OP_READ | OP_ACCEPT)) != 0; + } + + boolean isReadInterested() + { + return (interestOps & (OP_READ | OP_ACCEPT)) != 0; + } + + boolean isWriteActive() + { + return (activeOps & (OP_WRITE | OP_CONNECT)) != 0; + } + + boolean isWriteInterested() + { + return (interestOps & (OP_WRITE | OP_CONNECT)) != 0; + } + + boolean needCommitRead() + { + return isReadActive() == (!isReadInterested()); + } + + boolean needCommitWrite() + { + return isWriteActive() == (!isWriteInterested()); + } } -- cgit v1.2.1