From 52a33fecbe37ccf6b97f9637745bb03107349283 Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Thu, 15 Nov 2012 16:28:43 +0000
Subject: [PATCH] Fix OPENDJ-637 - Connection handler bytesRead and bytesWritten statistics are wrong

---
 opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java |   30 +++++++++++++++++++++++-------
 1 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java b/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java
index 6c9dc22..1b62eb1 100644
--- a/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java
+++ b/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java
@@ -161,7 +161,12 @@
 
     public int read(ByteBuffer byteBuffer) throws IOException
     {
-      return clientChannel.read(byteBuffer);
+      int bytesRead = clientChannel.read(byteBuffer);
+      if (bytesRead > 0 && keepStats)
+      {
+        statTracker.updateBytesRead(bytesRead);
+      }
+      return bytesRead;
     }
 
     public boolean isOpen()
@@ -182,7 +187,11 @@
       try
       {
         int bytesToWrite = byteBuffer.remaining();
-        clientChannel.write(byteBuffer);
+        int bytesWritten = clientChannel.write(byteBuffer);
+        if (bytesWritten > 0 && keepStats)
+        {
+          statTracker.updateBytesWritten(bytesWritten);
+        }
         if (!byteBuffer.hasRemaining())
         {
           return bytesToWrite;
@@ -208,11 +217,16 @@
           while (byteBuffer.hasRemaining()
               && (System.currentTimeMillis() < stopTime))
           {
-            if (clientChannel.write(byteBuffer) < 0)
+            bytesWritten = clientChannel.write(byteBuffer);
+            if (bytesWritten < 0)
             {
               // The client connection has been closed.
               throw new ClosedChannelException();
             }
+            if (bytesWritten > 0 && keepStats)
+            {
+              statTracker.updateBytesWritten(bytesWritten);
+            }
           }
 
           if (byteBuffer.hasRemaining())
@@ -250,12 +264,16 @@
               SelectionKey k = iterator.next();
               if (k.isWritable())
               {
-                int bytesWritten = clientChannel.write(byteBuffer);
+                bytesWritten = clientChannel.write(byteBuffer);
                 if (bytesWritten < 0)
                 {
                   // The client connection has been closed.
                   throw new ClosedChannelException();
                 }
+                if (bytesWritten > 0 && keepStats)
+                {
+                  statTracker.updateBytesWritten(bytesWritten);
+                }
 
                 iterator.remove();
               }
@@ -960,9 +978,7 @@
 
       if (keepStats)
       {
-        // TODO hard-coded for now, flush probably needs to
-        // return how many bytes were flushed.
-        statTracker.updateMessageWritten(message, 4096);
+        statTracker.updateMessageWritten(message);
       }
     }
     catch (Exception e)

--
Gitblit v1.10.0