Store the source instead of only the format.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / MultiSourceFilter.java
index 57c313e..a6566ed 100644 (file)
@@ -34,6 +34,10 @@ import net.pterodactylus.sonitus.data.Filter;
 import net.pterodactylus.sonitus.data.Format;
 import net.pterodactylus.sonitus.data.ReusableSink;
 import net.pterodactylus.sonitus.data.Source;
+import net.pterodactylus.sonitus.data.event.SourceFinishedEvent;
+
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
 
 /**
  * {@link ReusableSink} implementation that supports changing the source without
@@ -49,15 +53,22 @@ public class MultiSourceFilter implements Filter, ReusableSink {
        /** Object used for synchronization. */
        private final Object syncObject = new Object();
 
+       /** The event bus. */
+       private final EventBus eventBus;
+
        /** The connection. */
        private Connection connection;
 
-       /** The format. */
-       private Format format;
+       @Inject
+       public MultiSourceFilter(EventBus eventBus) {
+               this.eventBus = eventBus;
+       }
 
        @Override
        public Format format() {
-               return format;
+               synchronized (syncObject) {
+                       return connection.source.format();
+               }
        }
 
        @Override
@@ -74,10 +85,8 @@ public class MultiSourceFilter implements Filter, ReusableSink {
        @Override
        public void connect(Source source) throws ConnectException {
                checkNotNull(source, "source must not be null");
-               if (format != null) {
-                       checkArgument(format.equals(source.format()), "source’s format must equal this sink’s format");
-               } else {
-                       format = source.format();
+               if ((connection != null) && (connection.source != null)) {
+                       checkArgument(connection.source.format().equals(source.format()), "source’s format must equal this sink’s format");
                }
 
                if (connection == null) {
@@ -122,6 +131,9 @@ public class MultiSourceFilter implements Filter, ReusableSink {
                 */
                public Connection source(Source source) throws IOException {
                        synchronized (syncObject) {
+                               if (this.source != null) {
+                                       eventBus.post(new SourceFinishedEvent(this.source));
+                               }
                                this.source = source;
                                pipedInputStream = new PipedInputStream();
                                pipedOutputStream = new PipedOutputStream(pipedInputStream);
@@ -135,9 +147,11 @@ public class MultiSourceFilter implements Filter, ReusableSink {
                        while (true) {
                                /* wait for source to be set. */
                                OutputStream outputStream;
+                               Source source;
                                logger.finest("Entering synchronized block...");
                                synchronized (syncObject) {
                                        logger.finest("Entered synchronized block.");
+                                       source = this.source;
                                        while (source == null) {
                                                try {
                                                        logger.finest("Waiting for source to connect...");
@@ -145,6 +159,7 @@ public class MultiSourceFilter implements Filter, ReusableSink {
                                                } catch (InterruptedException ie1) {
                                                        /* ignore, keep waiting. */
                                                }
+                                               source = this.source;
                                        }
                                        outputStream = pipedOutputStream;
                                }