You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2014/09/07 14:24:24 UTC

git commit: o Added some info when a IOException occurs (DIRMINA-825) o Close the channel when we get an IOException (DIRMINA-928)

Repository: mina
Updated Branches:
  refs/heads/2.0 faa8e58e1 -> 9d7d823ed


o Added some info when a IOException occurs (DIRMINA-825)
o Close the channel when we get an IOException (DIRMINA-928)

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/9d7d823e
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/9d7d823e
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/9d7d823e

Branch: refs/heads/2.0
Commit: 9d7d823edacede56813674b94cba2ae65977426f
Parents: faa8e58
Author: Emmanuel Lécharny <el...@symas.com>
Authored: Sun Sep 7 14:22:06 2014 +0200
Committer: Emmanuel Lécharny <el...@symas.com>
Committed: Sun Sep 7 14:22:06 2014 +0200

----------------------------------------------------------------------
 .../socket/nio/NioDatagramAcceptor.java         | 35 ++++++++++++----
 .../socket/nio/NioDatagramConnector.java        | 34 ++++++++++++----
 .../transport/socket/nio/NioSocketAcceptor.java |  4 ++
 .../socket/nio/NioSocketConnector.java          | 43 ++++++++++++++------
 4 files changed, 85 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/9d7d823e/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
----------------------------------------------------------------------
diff --git a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
index 1a404a9..a128105 100644
--- a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
+++ b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
@@ -19,6 +19,7 @@
  */
 package org.apache.mina.transport.socket.nio;
 
+import java.io.IOException;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
@@ -65,9 +66,9 @@ import org.apache.mina.util.ExceptionMonitor;
  * @org.apache.xbean.XBean
  */
 public final class NioDatagramAcceptor extends AbstractIoAcceptor implements DatagramAcceptor, IoProcessor<NioSession> {
-    /** 
+    /**
      * A session recycler that is used to retrieve an existing session, unless it's too old.
-     **/  
+     **/
     private static final IoSessionRecycler DEFAULT_RECYCLER = new ExpiringSessionRecycler();
 
     /**
@@ -690,21 +691,37 @@ public final class NioDatagramAcceptor extends AbstractIoAcceptor implements Dat
     }
 
     protected DatagramChannel open(SocketAddress localAddress) throws Exception {
-        final DatagramChannel c = DatagramChannel.open();
+        final DatagramChannel ch = DatagramChannel.open();
         boolean success = false;
         try {
-            new NioDatagramSessionConfig(c).setAll(getSessionConfig());
-            c.configureBlocking(false);
-            c.socket().bind(localAddress);
-            c.register(selector, SelectionKey.OP_READ);
+            new NioDatagramSessionConfig(ch).setAll(getSessionConfig());
+            ch.configureBlocking(false);
+
+            try {
+                ch.socket().bind(localAddress);
+            } catch (IOException ioe) {
+                // Add some info regarding the address we try to bind to the
+                // message
+                String newMessage = "Error while binding on " + localAddress + "\n" + "original message : "
+                        + ioe.getMessage();
+                Exception e = new IOException(newMessage);
+                e.initCause(ioe.getCause());
+
+                // And close the channel
+                ch.close();
+
+                throw e;
+            }
+
+            ch.register(selector, SelectionKey.OP_READ);
             success = true;
         } finally {
             if (!success) {
-                close(c);
+                close(ch);
             }
         }
 
-        return c;
+        return ch;
     }
 
     protected SocketAddress receive(DatagramChannel handle, IoBuffer buffer) throws Exception {

http://git-wip-us.apache.org/repos/asf/mina/blob/9d7d823e/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramConnector.java
----------------------------------------------------------------------
diff --git a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramConnector.java b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramConnector.java
index 1c22975..4dcfd31 100644
--- a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramConnector.java
+++ b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramConnector.java
@@ -19,11 +19,13 @@
  */
 package org.apache.mina.transport.socket.nio;
 
+import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.nio.channels.DatagramChannel;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.concurrent.Executor;
 
 import org.apache.mina.core.polling.AbstractPollingIoConnector;
 import org.apache.mina.core.service.IoConnector;
@@ -39,7 +41,7 @@ import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSession, DatagramChannel> implements
-        DatagramConnector {
+DatagramConnector {
 
     /**
      * Creates a new instance.
@@ -63,9 +65,9 @@ public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSe
     }
 
     /**
-     * Constructor for {@link NioDatagramConnector} with default configuration which will use a built-in 
-     * thread pool executor to manage the given number of processor instances. The processor class must have 
-     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
+     * Constructor for {@link NioDatagramConnector} with default configuration which will use a built-in
+     * thread pool executor to manage the given number of processor instances. The processor class must have
+     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a
      * no-arg constructor.
      * 
      * @param processorClass the processor class.
@@ -78,10 +80,10 @@ public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSe
     }
 
     /**
-     * Constructor for {@link NioDatagramConnector} with default configuration with default configuration which will use a built-in 
-     * thread pool executor to manage the default number of processor instances. The processor class must have 
-     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
-     * no-arg constructor. The default number of instances is equal to the number of processor cores 
+     * Constructor for {@link NioDatagramConnector} with default configuration with default configuration which will use a built-in
+     * thread pool executor to manage the default number of processor instances. The processor class must have
+     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a
+     * no-arg constructor. The default number of instances is equal to the number of processor cores
      * in the system, plus one.
      * 
      * @param processorClass the processor class.
@@ -121,7 +123,21 @@ public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSe
 
         try {
             if (localAddress != null) {
-                ch.socket().bind(localAddress);
+                try {
+                    ch.socket().bind(localAddress);
+                } catch (IOException ioe) {
+                    // Add some info regarding the address we try to bind to the
+                    // message
+                    String newMessage = "Error while binding on " + localAddress + "\n" + "original message : "
+                            + ioe.getMessage();
+                    Exception e = new IOException(newMessage);
+                    e.initCause(ioe.getCause());
+
+                    // and close the channel
+                    ch.close();
+
+                    throw e;
+                }
             }
 
             return ch;

http://git-wip-us.apache.org/repos/asf/mina/blob/9d7d823e/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
----------------------------------------------------------------------
diff --git a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
index 8e9e333..77f218c 100644
--- a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
+++ b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
@@ -237,6 +237,10 @@ implements SocketAcceptor {
                         + ioe.getMessage();
                 Exception e = new IOException(newMessage);
                 e.initCause(ioe.getCause());
+
+                // And close the channel
+                channel.close();
+
                 throw e;
             }
 

http://git-wip-us.apache.org/repos/asf/mina/blob/9d7d823e/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
----------------------------------------------------------------------
diff --git a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
index 72a6240..2dc2b40 100644
--- a/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
+++ b/mina-core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
@@ -19,6 +19,7 @@
  */
 package org.apache.mina.transport.socket.nio;
 
+import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.nio.channels.SelectionKey;
@@ -44,7 +45,7 @@ import org.apache.mina.transport.socket.SocketSessionConfig;
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public final class NioSocketConnector extends AbstractPollingIoConnector<NioSession, SocketChannel> implements
-        SocketConnector {
+SocketConnector {
 
     private volatile Selector selector;
 
@@ -57,10 +58,10 @@ public final class NioSocketConnector extends AbstractPollingIoConnector<NioSess
     }
 
     /**
-     * Constructor for {@link NioSocketConnector} with default configuration, and 
+     * Constructor for {@link NioSocketConnector} with default configuration, and
      * given number of {@link NioProcessor} for multithreading I/O operations
      * @param processorCount the number of processor to create and place in a
-     * {@link SimpleIoProcessorPool} 
+     * {@link SimpleIoProcessorPool}
      */
     public NioSocketConnector(int processorCount) {
         super(new DefaultSocketSessionConfig(), NioProcessor.class, processorCount);
@@ -79,8 +80,8 @@ public final class NioSocketConnector extends AbstractPollingIoConnector<NioSess
     }
 
     /**
-     *  Constructor for {@link NioSocketConnector} with a given {@link Executor} for handling 
-     *  connection events and a given {@link IoProcessor} for handling I/O events, useful for sharing 
+     *  Constructor for {@link NioSocketConnector} with a given {@link Executor} for handling
+     *  connection events and a given {@link IoProcessor} for handling I/O events, useful for sharing
      *  the same processor and executor over multiple {@link IoService} of the same type.
      * @param executor the executor for connection
      * @param processor the processor for I/O operations
@@ -91,9 +92,9 @@ public final class NioSocketConnector extends AbstractPollingIoConnector<NioSess
     }
 
     /**
-     * Constructor for {@link NioSocketConnector} with default configuration which will use a built-in 
-     * thread pool executor to manage the given number of processor instances. The processor class must have 
-     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
+     * Constructor for {@link NioSocketConnector} with default configuration which will use a built-in
+     * thread pool executor to manage the given number of processor instances. The processor class must have
+     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a
      * no-arg constructor.
      * 
      * @param processorClass the processor class.
@@ -106,10 +107,10 @@ public final class NioSocketConnector extends AbstractPollingIoConnector<NioSess
     }
 
     /**
-     * Constructor for {@link NioSocketConnector} with default configuration with default configuration which will use a built-in 
-     * thread pool executor to manage the default number of processor instances. The processor class must have 
-     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
-     * no-arg constructor. The default number of instances is equal to the number of processor cores 
+     * Constructor for {@link NioSocketConnector} with default configuration with default configuration which will use a built-in
+     * thread pool executor to manage the default number of processor instances. The processor class must have
+     * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a
+     * no-arg constructor. The default number of instances is equal to the number of processor cores
      * in the system, plus one.
      * 
      * @param processorClass the processor class.
@@ -238,14 +239,30 @@ public final class NioSocketConnector extends AbstractPollingIoConnector<NioSess
         SocketChannel ch = SocketChannel.open();
 
         int receiveBufferSize = (getSessionConfig()).getReceiveBufferSize();
+
         if (receiveBufferSize > 65535) {
             ch.socket().setReceiveBufferSize(receiveBufferSize);
         }
 
         if (localAddress != null) {
-            ch.socket().bind(localAddress);
+            try {
+                ch.socket().bind(localAddress);
+            } catch (IOException ioe) {
+                // Add some info regarding the address we try to bind to the
+                // message
+                String newMessage = "Error while binding on " + localAddress + "\n" + "original message : "
+                        + ioe.getMessage();
+                Exception e = new IOException(newMessage);
+                e.initCause(ioe.getCause());
+
+                // Preemptively close the channel
+                ch.close();
+                throw ioe;
+            }
         }
+
         ch.configureBlocking(false);
+
         return ch;
     }