Messages sent on SSL sessions will generate a single sent event when the last message...
authorJeff MAURY <jeffmaury@apache.org>
Sun, 14 Dec 2014 21:23:56 +0000 (22:23 +0100)
committerJeff MAURY <jeffmaury@apache.org>
Sun, 14 Dec 2014 21:23:56 +0000 (22:23 +0100)
core/src/main/java/org/apache/mina/session/DefaultWriteRequest.java
core/src/main/java/org/apache/mina/session/WriteRequest.java
core/src/main/java/org/apache/mina/transport/bio/BioUdpSession.java
core/src/main/java/org/apache/mina/transport/nio/AbstractNioSession.java
core/src/main/java/org/apache/mina/transport/nio/SslHelper.java
core/src/test/java/org/apache/mina/transport/nio/SslTest.java

index 2e0b5a8..39bf895 100644 (file)
@@ -45,6 +45,8 @@ public class DefaultWriteRequest implements WriteRequest {
      */
     private boolean secureInternal = false;
 
+    private boolean confirmRequested = true;
+    
     /**
      * Creates a new instance of a WriteRequest, storing the message as it was
      * when the IoSession.write() has been called.
@@ -57,6 +59,20 @@ public class DefaultWriteRequest implements WriteRequest {
     }
 
     /**
+     * Creates a new instance of a WriteRequest, storing the message as it was
+     * when the IoSession.write() has been called.
+     * 
+     * @param message The message to write
+     * @param originalMessage the original message
+     * @param confirmRequested whether to send an event or not
+     */
+    public DefaultWriteRequest(Object message, Object originalMessage, boolean confirmRequested) {
+        this.message = message;
+        this.originalMessage = originalMessage;
+        this.confirmRequested = confirmRequested;
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -158,4 +174,9 @@ public class DefaultWriteRequest implements WriteRequest {
     public void setSecureInternal(boolean secureInternal) {
         this.secureInternal = secureInternal;        
     }
+
+    @Override
+    public boolean isConfirmRequested() {
+        return confirmRequested;
+    }
 }
\ No newline at end of file
index cb43354..c054482 100644 (file)
@@ -84,4 +84,11 @@ public interface WriteRequest {
      * @param secureInternal the secure internal flag
      */
     void setSecureInternal(boolean secureInternal);
+    
+    /**
+     * When this message has been set, should we send a corresponding send event
+     * or not.
+     * @return the send confirm flag
+     */
+    boolean isConfirmRequested();
 }
\ No newline at end of file
index a2ec97c..83f91c2 100644 (file)
@@ -157,7 +157,7 @@ public class BioUdpSession extends AbstractIoSession {
 
                 final Object highLevel = ((DefaultWriteRequest) writeRequest).getOriginalMessage();
 
-                if (highLevel != null) {
+                if ((highLevel != null) && writeRequest.isConfirmRequested()) {
                     processMessageSent(highLevel);
                 }
 
index 41a97b0..69b0250 100644 (file)
@@ -231,7 +231,7 @@ public abstract class AbstractNioSession extends AbstractIoSession {
 
                     final Object highLevel = ((DefaultWriteRequest) writeRequest).getOriginalMessage();
 
-                    if (highLevel != null) {
+                    if ((highLevel != null) && writeRequest.isConfirmRequested()) {
                         processMessageSent(highLevel);
                     }
                 }
@@ -329,7 +329,7 @@ public abstract class AbstractNioSession extends AbstractIoSession {
                     // generate the message sent event
                     final Object highLevel = ((DefaultWriteRequest) writeRequest).getOriginalMessage();
 
-                    if (highLevel != null) {
+                    if ((highLevel != null) && writeRequest.isConfirmRequested()) {
                         processMessageSent(highLevel);
                     }
                 } else {
index 83cce1b..aff0beb 100644 (file)
@@ -420,9 +420,9 @@ public class SslHelper {
                         appBuffer.flip();
                         done = buf.remaining() == 0;
                         if (done) {
-                            request = new DefaultWriteRequest(appBuffer);
+                            request = new DefaultWriteRequest(appBuffer, buf, done);
                         } else {
-                            writeQueue.offer(new DefaultWriteRequest(appBuffer));
+                            writeQueue.offer(new DefaultWriteRequest(appBuffer, buf, done));
                             appBuffer = ByteBuffer.allocateDirect(appBuffer.capacity());
                         }
                         break;
index a376ad5..0c9d8f2 100644 (file)
@@ -473,4 +473,31 @@ public class SslTest {
     public void test1MMessageWithMINAClientBeforeHandshake() throws IOException, GeneralSecurityException, InterruptedException {
         testMessage(1024 * 1024, Client.MINA_BEFORE_HANDSHAKE);
     }
+    
+    @Test
+    public void checkThatASingleMessageSentEventIsSent() throws IOException, GeneralSecurityException, InterruptedException {
+        final CountDownLatch counter = new CountDownLatch(1);
+        final byte[] message = new byte[1024 * 1024];
+        new Random().nextBytes(message);
+        final AtomicInteger sentCounter = new AtomicInteger();
+
+        NioTcpServer server = createReceivingServer(1024 * 1024, counter, null);
+        NioTcpClient client = new NioTcpClient();
+        client.getSessionConfig().setSslContext(createSSLContext());
+        client.setIoHandler(new AbstractIoHandler() {
+
+            @Override
+            public void handshakeCompleted(IoSession session) {
+                session.write(ByteBuffer.wrap(message));
+            }
+
+            @Override
+            public void messageSent(IoSession session, Object message) {
+                sentCounter.incrementAndGet();
+            }
+        });
+        client.connect(new InetSocketAddress(server.getServerSocketChannel().socket().getLocalPort()));
+        assertTrue(counter.await(10, TimeUnit.SECONDS));
+        assertEquals(5, sentCounter.get());
+    }
 }