X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsource%2FMultiSource.java;h=80c3f562467a4890f225dc18637e527ecab1203a;hb=6b199b3a9929aa9e0fb9314c82f395d8c9b245fe;hp=0779b1a64550a600cb17cf2299d48b40e6ef7b93;hpb=9a5bbdb694cda8f4bc0794aea136b993399961dd;p=sonitus.git diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java index 0779b1a..80c3f56 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java @@ -25,13 +25,13 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Logger; +import javax.swing.event.EventListenerList; +import net.pterodactylus.sonitus.data.AbstractControlledComponent; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; -import net.pterodactylus.sonitus.data.event.SourceFinishedEvent; -import com.google.common.eventbus.EventBus; import com.google.inject.Inject; /** @@ -41,13 +41,13 @@ import com.google.inject.Inject; * * @author David ‘Bombe’ Roden */ -public class MultiSource implements Source { +public class MultiSource extends AbstractControlledComponent implements Source { /** The logger. */ private static final Logger logger = Logger.getLogger(MultiSource.class.getName()); - /** The event bus. */ - private final EventBus eventBus; + /** The source finished listeners. */ + private final EventListenerList sourceFinishedListeners = new EventListenerList(); /** The current source. */ private final AtomicReference source = new AtomicReference(); @@ -55,9 +55,34 @@ public class MultiSource implements Source { /** Whether the source was changed. */ private boolean sourceChanged; + /** Creates a new multi source. */ @Inject - public MultiSource(EventBus eventBus) { - this.eventBus = eventBus; + public MultiSource() { + super("Multisource"); + } + + // + // LISTENER MANAGEMENT + // + + /** + * Adds a source finished listener to the list of registered listeners. + * + * @param sourceFinishedListener + * The source finished listener to add + */ + public void addSourceFinishedListener(SourceFinishedListener sourceFinishedListener) { + sourceFinishedListeners.add(SourceFinishedListener.class, sourceFinishedListener); + } + + /** + * Removes a source finished listener from the list of registered listeners. + * + * @param sourceFinishedListener + * The source finished listener to remove + */ + public void removeSourceFinishedListener(SourceFinishedListener sourceFinishedListener) { + sourceFinishedListeners.remove(SourceFinishedListener.class, sourceFinishedListener); } // @@ -74,51 +99,62 @@ public class MultiSource implements Source { checkNotNull(source, "source must not be null"); Source oldSource = this.source.getAndSet(source); - if (oldSource != null) { + if (!source.equals(oldSource)) { synchronized (this.source) { sourceChanged = true; this.source.notifyAll(); } + metadataUpdated(source.metadata()); logger.info(String.format("Next Source set: %s", source)); } } // - // CONTROLLED METHODS + // EVENT METHODS // - @Override - public List controllers() { - return Collections.emptyList(); + /** + * Notifies all registered listeners that the current source finished playing + * and that a new source should be {@link #setSource(Source) set}. + * + * @see SourceFinishedListener + */ + private void fireSourceFinished() { + for (SourceFinishedListener sourceFinishedListener : sourceFinishedListeners.getListeners(SourceFinishedListener.class)) { + sourceFinishedListener.sourceFinished(this); + } } // - // SOURCE METHODS + // CONTROLLED METHODS // @Override + public List> controllers() { + return Collections.emptyList(); + } + + @Override public Metadata metadata() { - return source.get().metadata(); + if (super.metadata() == null) { + /* no metadata yet, wait for it. */ + waitForNewSource(); + sourceChanged = false; + } + return super.metadata(); } + // + // SOURCE METHODS + // + @Override public byte[] get(int bufferSize) throws EOFException, IOException { while (true) { try { return source.get().get(bufferSize); } catch (EOFException eofe1) { - eventBus.post(new SourceFinishedEvent(source.get())); - synchronized (source) { - while (!sourceChanged) { - try { - logger.info("Waiting for next Source..."); - source.wait(); - logger.info("Was notified."); - } catch (InterruptedException ioe1) { - /* ignore: we’ll end up here again if we were interrupted. */ - } - } - } + waitForNewSource(); } finally { synchronized (source) { sourceChanged = false; @@ -127,4 +163,24 @@ public class MultiSource implements Source { } } + // + // PRIVATE METHODS + // + + /** Waits for a new source to be {@link #setSource(Source) set}. */ + private void waitForNewSource() { + fireSourceFinished(); + synchronized (source) { + while (!sourceChanged) { + try { + logger.info("Waiting for next Source..."); + source.wait(); + logger.info("Was notified."); + } catch (InterruptedException ioe1) { + /* ignore: we’ll end up here again if we were interrupted. */ + } + } + } + } + }