You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2012/11/06 14:35:24 UTC

svn commit: r1406131 - in /mina/mina/trunk: core/src/main/java/org/apache/mina/transport/nio/ examples/src/main/java/org/apache/mina/examples/echoserver/ examples/src/main/java/org/apache/mina/examples/http/ examples/src/main/java/org/apache/mina/examp...

Author: jvermillard
Date: Tue Nov  6 13:35:23 2012
New Revision: 1406131

URL: http://svn.apache.org/viewvc?rev=1406131&view=rev
Log:
pool of selector for read/write events

Added:
    mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/FixedSelectorLoopPool.java
    mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoopPool.java
Modified:
    mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
    mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/echoserver/NioEchoServer.java
    mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java
    mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpsTest.java
    mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/ldap/LdapTest.java

Added: mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/FixedSelectorLoopPool.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/FixedSelectorLoopPool.java?rev=1406131&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/FixedSelectorLoopPool.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/FixedSelectorLoopPool.java Tue Nov  6 13:35:23 2012
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.mina.transport.nio;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * A fixed size pool of {@link SelectorLoop}.
+ * 
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class FixedSelectorLoopPool implements SelectorLoopPool {
+
+    // the pool of selector loop
+    private SelectorLoop[] pool;
+    
+    // the index of the next selector loop to be served
+    private AtomicInteger nextIndex = new AtomicInteger();
+    
+    /**
+     * Create a pool of "size" {@link SelectorLoop}
+     * @param size
+     */
+    public FixedSelectorLoopPool(int size) {
+        
+        pool = new SelectorLoop[size];
+        for(int i=0;i<size;i++) {
+            pool[i] = new NioSelectorLoop();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public SelectorLoop getSelectorLoop() {
+        return pool[Math.abs(nextIndex.incrementAndGet() % pool.length)];
+    }
+}

Modified: mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java?rev=1406131&r1=1406130&r2=1406131&view=diff
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java (original)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java Tue Nov  6 13:35:23 2012
@@ -47,7 +47,7 @@ public class NioTcpServer extends Abstra
 
     private final SelectorLoop acceptSelectorLoop;
 
-    private final SelectorLoop readWriteSelectorLoop;
+    private final SelectorLoopPool readWriteSelectorPool;
 
     // the key used for selecting accept event
     private SelectionKey acceptKey = null;
@@ -57,10 +57,22 @@ public class NioTcpServer extends Abstra
 
     private final IdleChecker idleChecker = new IndexedIdleChecker();
 
-    public NioTcpServer(final SelectorLoop acceptSelectorLoop, SelectorLoop readWriteSelectorLoop) {
+    /**
+     * Create a TCP server with new selector pool of default size.
+     */
+    public NioTcpServer() {
+        this(new NioSelectorLoop(), new FixedSelectorLoopPool(Runtime.getRuntime().availableProcessors()+1));
+    }
+    
+    /**
+     * Create a TCP server with provided selector loops pool
+     * @param acceptSelectorLoop the selector loop for handling accept events (connection of new session)
+     * @param readWriteSelectorLoop the pool of selector loop for handling read/write events of connected sessions
+     */
+    public NioTcpServer(final SelectorLoop acceptSelectorLoop, SelectorLoopPool readWriteSelectorLoop) {
         super();
         this.acceptSelectorLoop = acceptSelectorLoop;
-        this.readWriteSelectorLoop = readWriteSelectorLoop;
+        this.readWriteSelectorPool = readWriteSelectorLoop;
     }
 
     /**
@@ -175,6 +187,7 @@ public class NioTcpServer extends Abstra
         LOG.debug("create session");
         final SocketChannel socketChannel = clientSocket;
         final TcpSessionConfig config = getSessionConfig();
+        final SelectorLoop readWriteSelectorLoop = readWriteSelectorPool.getSelectorLoop();
         final NioTcpSession session = new NioTcpSession(this, socketChannel, readWriteSelectorLoop, idleChecker);
 
         socketChannel.configureBlocking(false);

Added: mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoopPool.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoopPool.java?rev=1406131&view=auto
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoopPool.java (added)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoopPool.java Tue Nov  6 13:35:23 2012
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.mina.transport.nio;
+
+/**
+ * A pool of {@link SelectorLoop}
+ * 
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ * 
+ */
+public interface SelectorLoopPool {
+
+    /**
+     * Get a {@link SelectorLoop} from the pool
+     * @return a SelectorLoop from the pool
+     */
+    SelectorLoop getSelectorLoop();
+
+}
\ No newline at end of file

Modified: mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/echoserver/NioEchoServer.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/echoserver/NioEchoServer.java?rev=1406131&r1=1406130&r2=1406131&view=diff
==============================================================================
--- mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/echoserver/NioEchoServer.java (original)
+++ mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/echoserver/NioEchoServer.java Tue Nov  6 13:35:23 2012
@@ -51,7 +51,7 @@ public class NioEchoServer {
     public static void main(String[] args) {
         LOG.info("starting echo server");
 
-        NioTcpServer acceptor = new NioTcpServer(new NioSelectorLoop(), new NioSelectorLoop());
+        NioTcpServer acceptor = new NioTcpServer();
 
         // create the fitler chain for this service
         acceptor.setFilters(new LoggingFilter("LoggingFilter1"), new IoFilter() {

Modified: mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java?rev=1406131&r1=1406130&r2=1406131&view=diff
==============================================================================
--- mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java (original)
+++ mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java Tue Nov  6 13:35:23 2012
@@ -38,7 +38,6 @@ import org.apache.mina.http.api.HttpMeth
 import org.apache.mina.http.api.HttpRequest;
 import org.apache.mina.http.api.HttpStatus;
 import org.apache.mina.http.api.HttpVersion;
-import org.apache.mina.transport.nio.NioSelectorLoop;
 import org.apache.mina.transport.nio.NioTcpServer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -49,7 +48,7 @@ public class HttpTest {
 
     public static void main(String[] args) throws Exception {
 
-        NioTcpServer acceptor = new NioTcpServer(new NioSelectorLoop(), new NioSelectorLoop());
+        NioTcpServer acceptor = new NioTcpServer();
         acceptor.setFilters(new LoggingFilter("INCOMING"), new HttpServerCodec(), new LoggingFilter("DECODED"),
                 new DummyHttpSever());
 

Modified: mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpsTest.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpsTest.java?rev=1406131&r1=1406130&r2=1406131&view=diff
==============================================================================
--- mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpsTest.java (original)
+++ mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpsTest.java Tue Nov  6 13:35:23 2012
@@ -38,7 +38,6 @@ import org.apache.mina.http.api.HttpMeth
 import org.apache.mina.http.api.HttpRequest;
 import org.apache.mina.http.api.HttpStatus;
 import org.apache.mina.http.api.HttpVersion;
-import org.apache.mina.transport.nio.NioSelectorLoop;
 import org.apache.mina.transport.nio.NioTcpServer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -49,7 +48,7 @@ public class HttpsTest {
 
     public static void main(String[] args) throws Exception {
 
-        NioTcpServer acceptor = new NioTcpServer(new NioSelectorLoop(), new NioSelectorLoop());
+        NioTcpServer acceptor = new NioTcpServer();
 
         acceptor.setFilters(new LoggingFilter("INCOMING"), new HttpServerCodec(), new LoggingFilter("DECODED"),
                 new DummyHttpSever());

Modified: mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/ldap/LdapTest.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/ldap/LdapTest.java?rev=1406131&r1=1406130&r2=1406131&view=diff
==============================================================================
--- mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/ldap/LdapTest.java (original)
+++ mina/mina/trunk/examples/src/main/java/org/apache/mina/examples/ldap/LdapTest.java Tue Nov  6 13:35:23 2012
@@ -39,7 +39,6 @@ import org.apache.mina.api.IoSession;
 import org.apache.mina.filter.logging.LoggingFilter;
 import org.apache.mina.filterchain.ReadFilterChainController;
 import org.apache.mina.ldap.LdapCodec;
-import org.apache.mina.transport.nio.NioSelectorLoop;
 import org.apache.mina.transport.nio.NioTcpServer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,7 +51,7 @@ public class LdapTest {
 
     public static void main(String[] args) throws Exception {
         LdapTest ldapServer = new LdapTest();
-        NioTcpServer acceptor = new NioTcpServer(new NioSelectorLoop(), new NioSelectorLoop());
+        NioTcpServer acceptor = new NioTcpServer();
         acceptor.setFilters(new LoggingFilter("INCOMING"), new LdapCodec(), new LoggingFilter("DECODED"),
                 ldapServer.new DummyLdapServer());