james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r722245 - /james/mpt/trunk/main/src/main/java/org/apache/james/mpt/ExternalSession.java
Date Mon, 01 Dec 2008 21:33:30 GMT
Author: rdonkin
Date: Mon Dec  1 13:33:30 2008
New Revision: 722245

URL: http://svn.apache.org/viewvc?rev=722245&view=rev
Log:
Ensure socket work copes with more than one line per read OK.

Modified:
    james/mpt/trunk/main/src/main/java/org/apache/james/mpt/ExternalSession.java

Modified: james/mpt/trunk/main/src/main/java/org/apache/james/mpt/ExternalSession.java
URL: http://svn.apache.org/viewvc/james/mpt/trunk/main/src/main/java/org/apache/james/mpt/ExternalSession.java?rev=722245&r1=722244&r2=722245&view=diff
==============================================================================
--- james/mpt/trunk/main/src/main/java/org/apache/james/mpt/ExternalSession.java (original)
+++ james/mpt/trunk/main/src/main/java/org/apache/james/mpt/ExternalSession.java Mon Dec 
1 13:33:30 2008
@@ -25,6 +25,9 @@
 
 final class ExternalSession implements Session {
 
+    /** Number of milliseconds to sleep after empty read */
+    private static final int SHORT_WAIT_FOR_INPUT = 10;
+
     private static final byte[] CRLF = { '\r', '\n' };
 
     private final SocketChannel socket;
@@ -73,32 +76,43 @@
 
     private void readlineInto(StringBuffer buffer) throws Exception {
         monitor.debug("[Reading line]");
-        while (socket.read(readBuffer) == 0)
-            ;
         readBuffer.flip();
-        while (readOneMore(buffer))
+        while (oneFromLine(buffer))
             ;
+//      May have partial read
         readBuffer.compact();
         monitor.debug("[Done]");
     }
 
-    private boolean readOneMore(StringBuffer buffer) throws Exception {
+    private boolean oneFromLine(StringBuffer buffer) throws Exception {
         final boolean result;
         if (readBuffer.hasRemaining()) {
             char next = (char) readBuffer.get();
             if (next == '\n') {
+                monitor.debug("[LF]");
+//              Reached end of the line
                 result = false;
             } else if (next == '\r') {
-                monitor.debug('\r');
+//              CRLF line endings so drop
+                monitor.debug("[CR]");
                 result = true;
             } else {
+//              Load buffer
                 monitor.debug(next);
                 buffer.append(next);
                 result = true;
             }
         } else {
+            monitor.debug("[Reading into buffer]");
             readBuffer.clear();
-            readlineInto(buffer);
+            while (socket.read(readBuffer) == 0) {
+//              No response yet
+//              Wait a little while
+                Thread.sleep(SHORT_WAIT_FOR_INPUT);
+            }
+//          Reset for transfer into string buffer
+            readBuffer.flip();
+            monitor.debug("[Done]");
             result = true;
         }
         return result;



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message