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 2007/09/17 15:46:35 UTC

svn commit: r576437 - in /mina/trunk/core/src/main/java/org/apache/mina: common/AbstractIoProcessor.java transport/socket/nio/NIOProcessor.java

Author: jvermillard
Date: Mon Sep 17 06:46:34 2007
New Revision: 576437

URL: http://svn.apache.org/viewvc?rev=576437&view=rev
Log:
isReadyOps & interestOps splitted in something less NIO dependent

Modified:
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NIOProcessor.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java?rev=576437&r1=576436&r2=576437&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java Mon Sep 17 06:46:34 2007
@@ -67,12 +67,54 @@
     protected abstract Iterator<AbstractIoSession> selectedSessions() throws Exception;
 
     protected abstract SessionState state(IoSession session);
-
-    protected abstract int readyOps(IoSession session) throws Exception;
-
-    protected abstract int interestOps(IoSession session) throws Exception;
-
-    protected abstract void interestOps(IoSession session, int interestOps) throws Exception;
+    
+    
+    /**
+     * Is the session ready for writing
+     * @param session the session queried
+     * @return true is ready, false if not ready
+     * @throws Exception if some low level IO error occurs
+     */
+    protected abstract boolean isWritable(IoSession session) throws Exception;
+    
+    /**
+     * Is the session ready for reading
+     * @param session the session queried
+     * @return true is ready, false if not ready
+     * @throws Exception if some low level IO error occurs
+     */
+    protected abstract boolean isReadable(IoSession session) throws Exception;
+    /**
+     * register a session for writing 
+     * @param session the session registered
+     * @param value true for registering, false for removing
+     * @throws Exception if some low level IO error occurs
+     */
+    protected abstract void setOpWrite(IoSession session,boolean value) throws Exception;
+    
+    /**
+     * register a session for reading 
+     * @param session the session registered
+     * @param value true for registering, false for removing
+     * @throws Exception if some low level IO error occurs
+     */
+    protected abstract void setOpRead(IoSession session,boolean value) throws Exception;
+    
+    /**
+     * is this session registered for reading
+     * @param session the session queried
+     * @return true is registered for reading 
+     * @throws Exception if some low level IO error occurs
+     */
+    protected abstract boolean isOpRead(IoSession session) throws Exception;
+    
+    /**
+     * is this session registered for writing
+     * @param session the session queried
+     * @return true is registered for writing 
+     * @throws Exception if some low level IO error occurs
+     */
+    protected abstract boolean isOpWrite(IoSession session) throws Exception;
 
     protected abstract void doAdd(IoSession session) throws Exception;
 
@@ -221,12 +263,12 @@
     }
 
     private void process(AbstractIoSession session) throws Exception {
-        int readyOps = readyOps(session);
-        if ((readyOps & SelectionKey.OP_READ) != 0 && session.getTrafficMask().isReadable()) {
+        
+        if (isReadable(session) && session.getTrafficMask().isReadable()) {
             read(session);
         }
 
-        if ((readyOps & SelectionKey.OP_WRITE) != 0 && session.getTrafficMask().isWritable()) {
+        if (isWritable(session) && session.getTrafficMask().isWritable()) {
             scheduleFlush(session);
         }
     }
@@ -330,7 +372,7 @@
     private void notifyWriteTimeout(AbstractIoSession session,
                                     long currentTime, long writeTimeout, long lastIoTime) throws Exception {
         if (writeTimeout > 0 && currentTime - lastIoTime >= writeTimeout
-                && (interestOps(session) & SelectionKey.OP_WRITE) != 0) {
+                && isOpWrite(session)) {
             session.getFilterChain().fireExceptionCaught(new WriteTimeoutException());
         }
     }
@@ -410,7 +452,7 @@
 
     private boolean flush(AbstractIoSession session) throws Exception {
         // Clear OP_WRITE
-        interestOps(session, interestOps(session) & ~SelectionKey.OP_WRITE);
+        setOpWrite(session,false);
 
         Queue<WriteRequest> writeRequestQueue = session.getWriteRequestQueue();
 
@@ -438,7 +480,7 @@
                     continue;
                 }
 
-                if ((readyOps(session) & SelectionKey.OP_WRITE) != 0) {
+                if (isWritable(session)) {
                     long localWrittenBytes = transferFile(session, region);
                     region.setPosition(region.getPosition() + localWrittenBytes);
                     writtenBytes += localWrittenBytes;
@@ -446,7 +488,7 @@
 
                 if (region.getCount() > 0 || writtenBytes >= maxWrittenBytes) {
                     // Kernel buffer is full or wrote too much.
-                    interestOps(session, interestOps(session) | SelectionKey.OP_WRITE);
+                    setOpWrite(session, true);
                     return false;
                 }
 
@@ -460,13 +502,13 @@
                     continue;
                 }
 
-                if ((readyOps(session) & SelectionKey.OP_WRITE) != 0) {
+                if (isWritable(session)) {
                     writtenBytes += write(session, buf);
                 }
 
                 if (buf.hasRemaining() || writtenBytes >= maxWrittenBytes) {
                     // Kernel buffer is full or wrote too much.
-                    interestOps(session, interestOps(session) | SelectionKey.OP_WRITE);
+                    setOpWrite(session, true);
                     return false;
                 }
             }
@@ -496,7 +538,12 @@
                 // Now mask the preferred ops with the mask of the current session
                 int mask = session.getTrafficMask().getInterestOps();
                 try {
-                    interestOps(session, ops & mask);
+                    setOpRead(session, isOpRead(session) || ( (mask &SelectionKey.OP_READ) >0) );
+                } catch (Exception e) {
+                    session.getFilterChain().fireExceptionCaught(e);
+                }
+                try {
+                    setOpWrite(session, isOpWrite(session) || ( (mask &SelectionKey.OP_WRITE) >0) );
                 } catch (Exception e) {
                     session.getFilterChain().fireExceptionCaught(e);
                 }

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NIOProcessor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NIOProcessor.java?rev=576437&r1=576436&r2=576437&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NIOProcessor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NIOProcessor.java Mon Sep 17 06:46:34 2007
@@ -115,21 +115,38 @@
     }
 
     @Override
-    protected int readyOps(IoSession session) throws Exception {
-        return getSelectionKey(session).readyOps();
+    protected boolean isReadable(IoSession session) throws Exception {
+        return  (getSelectionKey(session).readyOps() & SelectionKey.OP_READ)>0;
     }
 
     @Override
-    protected int interestOps(IoSession session) throws Exception {
-        return getSelectionKey(session).interestOps();
+    protected boolean isWritable(IoSession session) throws Exception {
+        return  (getSelectionKey(session).readyOps() & SelectionKey.OP_WRITE)>0;
     }
 
     @Override
-    protected void interestOps(IoSession session, int interestOps) throws Exception {
-        getSelectionKey(session).interestOps(interestOps);
+    protected boolean isOpRead(IoSession session) throws Exception {
+        return  (getSelectionKey(session).interestOps() & SelectionKey.OP_READ)>0;
     }
 
     @Override
+    protected boolean isOpWrite(IoSession session) throws Exception {
+        return  (getSelectionKey(session).interestOps() & SelectionKey.OP_WRITE)>0;
+    }
+
+    @Override
+    protected void setOpRead(IoSession session, boolean value) throws Exception {
+        getSelectionKey(session).interestOps( getSelectionKey(session).interestOps() | SelectionKey.OP_READ);
+    }
+
+    @Override
+    protected void setOpWrite(IoSession session, boolean value)
+            throws Exception {
+        getSelectionKey(session).interestOps( getSelectionKey(session).interestOps() | SelectionKey.OP_WRITE);
+    }
+    
+
+    @Override
     protected int read(IoSession session, ByteBuffer buf) throws Exception {
         return getChannel(session).read(buf.buf());
     }
@@ -175,4 +192,5 @@
             i.remove();
         }
     }
+
 }