Repository: mina Updated Branches: refs/heads/2.0 343344034 -> 04607fa39
apply patch for DIRMINA-1076 & DIRMINA-1077 Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/04607fa3 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/04607fa3 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/04607fa3 Branch: refs/heads/2.0 Commit: 04607fa39210ddcd54dceba4a7641d5363b31318 Parents: 3433440 Author: jvalliere <jon.valli...@emoten.com> Authored: Sat Mar 3 11:30:42 2018 -0500 Committer: jvalliere <jon.valli...@emoten.com> Committed: Sat Mar 3 11:30:42 2018 -0500 ---------------------------------------------------------------------- .../polling/AbstractPollingIoProcessor.java | 72 +++++++++++--------- .../mina/transport/socket/nio/NioProcessor.java | 6 ++ .../socket/nio/PollingIoProcessorTest.java | 5 ++ 3 files changed, 49 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/04607fa3/mina-core/src/main/java/org/apache/mina/core/polling/AbstractPollingIoProcessor.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/core/polling/AbstractPollingIoProcessor.java b/mina-core/src/main/java/org/apache/mina/core/polling/AbstractPollingIoProcessor.java index 79885fa..02d3cd4 100644 --- a/mina-core/src/main/java/org/apache/mina/core/polling/AbstractPollingIoProcessor.java +++ b/mina-core/src/main/java/org/apache/mina/core/polling/AbstractPollingIoProcessor.java @@ -240,6 +240,13 @@ public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession> im * @return {@link Iterator} of {@link IoSession} */ protected abstract Iterator<S> allSessions(); + + /** + * Get the number of {@Link IoSession} polled by this {@Link IoProcessor} + * + * @return the number of sessions attached to this {@Link IoProcessor} + */ + protected abstract int allSessionsCount(); /** * Get an {@link Iterator} for the list of {@link IoSession} found selected @@ -596,7 +603,6 @@ public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession> im public void run() { assert processorRef.get() == this; - int nSessions = 0; lastIdleCheckTime = System.currentTimeMillis(); int nbTries = 10; @@ -641,9 +647,31 @@ public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession> im } else { nbTries = 10; } - + // Manage newly created session first - nSessions += handleNewSessions(); + if(handleNewSessions() == 0) { + // Get a chance to exit the infinite loop if there are no + // more sessions on this Processor + if (allSessionsCount() == 0) { + processorRef.set(null); + + if (newSessions.isEmpty() && isSelectorEmpty()) { + // newSessions.add() precedes startupProcessor + assert processorRef.get() != this; + break; + } + + assert processorRef.get() != this; + + if (!processorRef.compareAndSet(null, this)) { + // startupProcessor won race, so must exit processor + assert processorRef.get() != this; + break; + } + + assert processorRef.get() == this; + } + } updateTrafficMask(); @@ -654,39 +682,17 @@ public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession> im // the MDCFilter test... process(); } - + // Write the pending requests long currentTime = System.currentTimeMillis(); flush(currentTime); - - // And manage removed sessions - nSessions -= removeSessions(); - + // Last, not least, send Idle events to the idle sessions notifyIdleSessions(currentTime); - - // Get a chance to exit the infinite loop if there are no - // more sessions on this Processor - if (nSessions == 0) { - processorRef.set(null); - - if (newSessions.isEmpty() && isSelectorEmpty()) { - // newSessions.add() precedes startupProcessor - assert processorRef.get() != this; - break; - } - - assert processorRef.get() != this; - - if (!processorRef.compareAndSet(null, this)) { - // startupProcessor won race, so must exit processor - assert processorRef.get() != this; - break; - } - - assert processorRef.get() == this; - } - + + // And manage removed sessions + removeSessions(); + // Disconnect all sessions immediately if disposal has been // requested so that we exit this loop eventually. if (isDisposing()) { @@ -702,9 +708,7 @@ public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession> im } } - if (hasKeys) { - wakeup(); - } + wakeup(); } } catch (ClosedSelectorException cse) { // If the selector has been closed, we can exit the loop http://git-wip-us.apache.org/repos/asf/mina/blob/04607fa3/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioProcessor.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioProcessor.java b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioProcessor.java index 3b0fa40..e9755aa 100644 --- a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioProcessor.java +++ b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioProcessor.java @@ -159,6 +159,12 @@ public final class NioProcessor extends AbstractPollingIoProcessor<NioSession> { selectorLock.readLock().unlock(); } } + + @Override + protected int allSessionsCount() + { + return selector.keys().size(); + } @SuppressWarnings("synthetic-access") @Override http://git-wip-us.apache.org/repos/asf/mina/blob/04607fa3/mina-core/src/test/java/org/apache/mina/transport/socket/nio/PollingIoProcessorTest.java ---------------------------------------------------------------------- diff --git a/mina-core/src/test/java/org/apache/mina/transport/socket/nio/PollingIoProcessorTest.java b/mina-core/src/test/java/org/apache/mina/transport/socket/nio/PollingIoProcessorTest.java index 379f55b..224b2fe 100644 --- a/mina-core/src/test/java/org/apache/mina/transport/socket/nio/PollingIoProcessorTest.java +++ b/mina-core/src/test/java/org/apache/mina/transport/socket/nio/PollingIoProcessorTest.java @@ -61,6 +61,11 @@ public class PollingIoProcessorTest { protected Iterator<NioSession> allSessions() { return proc.allSessions(); } + + @Override + protected int allSessionsCount() { + return proc.allSessionsCount(); + } @Override protected void destroy(NioSession session) throws Exception {