Send events from the DCC receiver.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 05:28:25 +0000 (07:28 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 05:28:25 +0000 (07:28 +0200)
src/main/java/net/pterodactylus/irc/DccReceiver.java
src/main/java/net/pterodactylus/xdcc/core/Core.java

index e58fed3..56a3853 100644 (file)
@@ -26,9 +26,12 @@ import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.irc.event.DccDownloadFailed;
+import net.pterodactylus.irc.event.DccDownloadFinished;
 import net.pterodactylus.irc.event.DccSendReceived;
 import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream;
 
+import com.google.common.eventbus.EventBus;
 import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.AbstractExecutionThreadService;
 
@@ -42,6 +45,9 @@ public class DccReceiver extends AbstractExecutionThreadService {
        /** The logger. */
        private static final Logger logger = Logger.getLogger(DccReceiver.class.getName());
 
+       /** The event bus. */
+       private final EventBus eventBus;
+
        /** The address to connect to. */
        private final InetAddress inetAddress;
 
@@ -78,7 +84,8 @@ public class DccReceiver extends AbstractExecutionThreadService {
         * @param outputStream
         *              The output stream to write the file to
         */
-       public DccReceiver(InetAddress inetAddress, int port, String filename, long size, OutputStream outputStream) {
+       public DccReceiver(EventBus eventBus, InetAddress inetAddress, int port, String filename, long size, OutputStream outputStream) {
+               this.eventBus = eventBus;
                this.inetAddress = inetAddress;
                this.port = port;
                this.filename = filename;
@@ -160,8 +167,10 @@ public class DccReceiver extends AbstractExecutionThreadService {
                                progress += r;
                        }
                        outputStream.flush();
+                       eventBus.post(new DccDownloadFinished(this));
                } catch (IOException ioe1) {
                        logger.log(Level.WARNING, "I/O error while receiving DCC!", ioe1);
+                       eventBus.post(new DccDownloadFailed(this, ioe1));
                } finally {
                        Closeables.close(inputStream, true);
                        socket.close();
index d253148..36768a4 100644 (file)
@@ -328,7 +328,7 @@ public class Core extends AbstractIdleService {
                logger.info(String.format("Starting download of %s.", dccSendReceived.filename()));
                try {
                        OutputStream fileOutputStream = new FileOutputStream(new File("/home/bombe/Temp", dccSendReceived.filename()));
-                       DccReceiver dccReceiver = new DccReceiver(dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
+                       DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
                        dccReceivers.add(dccReceiver);
                        dccReceiver.start();
                } catch (FileNotFoundException fnfe1) {