Added guard to make sure we do not exceed the maximum number of open channels

git-svn-id: https://hyracks.googlecode.com/svn/branches/hyracks_dev_next@996 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/ChannelSet.java b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/ChannelSet.java
index 48f008b..b13fc94 100644
--- a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/ChannelSet.java
+++ b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/ChannelSet.java
@@ -17,7 +17,11 @@
 import java.util.Arrays;
 import java.util.BitSet;
 
+import edu.uci.ics.hyracks.net.exceptions.NetException;
+
 public class ChannelSet {
+    private static final int MAX_OPEN_CHANNELS = 1024;
+
     private static final int INITIAL_SIZE = 16;
 
     private final MultiplexedConnection mConn;
@@ -47,7 +51,7 @@
         openChannelCount = 0;
     }
 
-    ChannelControlBlock allocateChannel() {
+    ChannelControlBlock allocateChannel() throws NetException {
         synchronized (mConn) {
             int idx = allocationBitmap.nextClearBit(0);
             if (idx < 0) {
@@ -72,7 +76,7 @@
         }
     }
 
-    ChannelControlBlock registerChannel(int channelId) {
+    ChannelControlBlock registerChannel(int channelId) throws NetException {
         return createChannel(channelId);
     }
 
@@ -136,10 +140,13 @@
         }
     }
 
-    private ChannelControlBlock createChannel(int idx) {
+    private ChannelControlBlock createChannel(int idx) throws NetException {
         if (idx >= ccbArray.length) {
             expand(idx);
         }
+        if (idx > MAX_OPEN_CHANNELS) {
+            throw new NetException("More than " + MAX_OPEN_CHANNELS + " opened concurrently");
+        }
         assert idx < ccbArray.length;
         assert !allocationBitmap.get(idx);
         ChannelControlBlock channel = new ChannelControlBlock(this, idx);
diff --git a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/MultiplexedConnection.java b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/MultiplexedConnection.java
index 3075809..e7e65e7 100644
--- a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/MultiplexedConnection.java
+++ b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/muxdemux/MultiplexedConnection.java
@@ -86,7 +86,7 @@
     }
 
     @Override
-    public void notifyIOReady(TCPConnection connection, boolean readable, boolean writable) throws IOException {
+    public void notifyIOReady(TCPConnection connection, boolean readable, boolean writable) throws IOException, NetException {
         if (readable) {
             driveReaderStateMachine();
         }
@@ -250,7 +250,7 @@
         }
     }
 
-    void driveReaderStateMachine() throws IOException {
+    void driveReaderStateMachine() throws IOException, NetException {
         SocketChannel sc = tcpConnection.getSocketChannel();
         if (readerState.readBuffer.remaining() > 0) {
             sc.read(readerState.readBuffer);
diff --git a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/ITCPConnectionEventListener.java b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/ITCPConnectionEventListener.java
index 4d894bd..607bf31 100644
--- a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/ITCPConnectionEventListener.java
+++ b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/ITCPConnectionEventListener.java
@@ -16,6 +16,9 @@
 
 import java.io.IOException;
 
+import edu.uci.ics.hyracks.net.exceptions.NetException;
+
 public interface ITCPConnectionEventListener {
-    public void notifyIOReady(TCPConnection connection, boolean readable, boolean writable) throws IOException;
+    public void notifyIOReady(TCPConnection connection, boolean readable, boolean writable) throws IOException,
+            NetException;
 }
\ No newline at end of file
diff --git a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/TCPEndpoint.java b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/TCPEndpoint.java
index 5f92a11..3892b83 100644
--- a/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/TCPEndpoint.java
+++ b/hyracks-net/src/main/java/edu/uci/ics/hyracks/net/protocols/tcp/TCPEndpoint.java
@@ -130,7 +130,7 @@
                             }
                         }
                     }
-                } catch (IOException e) {
+                } catch (Exception e) {
                     e.printStackTrace();
                 }
             }