Measure and expose the bandwidth of the download.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 10 Apr 2013 20:32:58 +0000 (22:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 10 Apr 2013 20:32:58 +0000 (22:32 +0200)
src/main/java/net/pterodactylus/irc/DccReceiver.java

index 39f236c..88d3b64 100644 (file)
@@ -22,11 +22,14 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.irc.event.DccSendReceived;
+import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream;
 
+import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.AbstractExecutionThreadService;
 
 /**
@@ -57,6 +60,9 @@ public class DccReceiver extends AbstractExecutionThreadService {
        /** The number of bytes already written. */
        private long progress;
 
+       /** The bandwidth-measuring input stream. */
+       private BandwidthCountingInputStream inputStream;
+
        /**
         * Creates a new DCC receiver.
         *
@@ -114,6 +120,24 @@ public class DccReceiver extends AbstractExecutionThreadService {
                return progress;
        }
 
+       /**
+        * Returns the current rate of the download.
+        *
+        * @return The current rate of the download, in bytes/second
+        */
+       public long getCurrentRate() {
+               return inputStream.getCurrentRate();
+       }
+
+       /**
+        * Returns the overall rate of the download.
+        *
+        * @return The overall rate of the download, in bytes/second
+        */
+       public long getOverallRate() {
+               return inputStream.getOverallRate();
+       }
+
        //
        // ABSTRACTEXECUTIONTHREADSERVICE METHODS
        //
@@ -124,9 +148,10 @@ public class DccReceiver extends AbstractExecutionThreadService {
                try {
                        socket = new Socket(inetAddress, port);
                        InputStream socketInputStream = socket.getInputStream();
+                       inputStream = new BandwidthCountingInputStream(socketInputStream, 5, TimeUnit.SECONDS);
                        byte[] buffer = new byte[65536];
                        while (true) {
-                               int r = socketInputStream.read(buffer);
+                               int r = inputStream.read(buffer);
                                if (r == -1) {
                                        /* yay, eof! */
                                        break;
@@ -137,6 +162,7 @@ public class DccReceiver extends AbstractExecutionThreadService {
                } catch (IOException ioe1) {
                        logger.log(Level.WARNING, "I/O error while receiving DCC!", ioe1);
                } finally {
+                       Closeables.close(inputStream, true);
                        socket.close();
                }
        }