Add ExceptionHandler to the Netty Channel Pipeline 44/8744/2
authorDave Tucker <djt@redhat.com>
Mon, 7 Jul 2014 18:21:38 +0000 (19:21 +0100)
committerDave Tucker <djt@redhat.com>
Mon, 7 Jul 2014 18:32:23 +0000 (18:32 +0000)
- Handle cases where an connection is quickly openend and closed
- Fix bug 1175

Change-Id: Ib0dc2f5c39e5418263222af7b50f82fa1bf31c88
Signed-off-by: Dave Tucker <djt@redhat.com>
library/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java
library/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/ExceptionHandler.java

index b08933af7ce3828ffebe7a5f499b39cda02dc3a8..cf647d34978151a75727e00d719b7bc62c1f492d 100644 (file)
@@ -37,6 +37,7 @@ import org.opendaylight.ovsdb.lib.OvsdbClient;
 import org.opendaylight.ovsdb.lib.OvsdbConnection;
 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo.ConnectionType;
 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
+import org.opendaylight.ovsdb.lib.jsonrpc.ExceptionHandler;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
@@ -99,7 +100,8 @@ public class OvsdbConnectionService implements OvsdbConnection {
                     channel.pipeline().addLast(
                             //new LoggingHandler(LogLevel.INFO),
                             new JsonRpcDecoder(100000),
-                            new StringEncoder(CharsetUtil.UTF_8));
+                            new StringEncoder(CharsetUtil.UTF_8),
+                            new ExceptionHandler());
                 }
             });
 
@@ -186,7 +188,8 @@ public class OvsdbConnectionService implements OvsdbConnection {
                      logger.debug("New Passive channel created : "+ channel.toString());
                      channel.pipeline().addLast(
                              new JsonRpcDecoder(100000),
-                             new StringEncoder(CharsetUtil.UTF_8));
+                             new StringEncoder(CharsetUtil.UTF_8),
+                             new ExceptionHandler());
 
                      handleNewPassiveConnection(channel);
                  }
index 9dad2f2f834d94ffbced288779ea8b86e68a5196..1a708f92c15b5fc63fb8f06e8d123a5ed81867d0 100644 (file)
@@ -14,14 +14,21 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.TooLongFrameException;
 import org.opendaylight.ovsdb.lib.error.InvalidEncodingException;
 
+import java.io.IOException;
+
 public class ExceptionHandler extends ChannelHandlerAdapter {
 
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
         if ((cause instanceof InvalidEncodingException)
-                || (cause instanceof TooLongFrameException)) {
-
+                || (cause instanceof TooLongFrameException)){
             ctx.channel().disconnect();
         }
+        /* In cases where a connection is quickly established and the closed
+        Catch the IOException and close the channel
+         */
+        if (cause instanceof IOException){
+            ctx.channel().close();
+        }
     }
 }