X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsource%2FMultiSource.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsource%2FMultiSource.java;h=b5705d5a76e6d144a6bcb4862e4dabc7d155d1f9;hb=7188da95cfb6dc2bf140eb8ac7e4dc99a0761a97;hp=0000000000000000000000000000000000000000;hpb=3d073631242b121378ff9c60104170d13cab52d7;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 new file mode 100644 index 0000000..b5705d5 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java @@ -0,0 +1,118 @@ +/* + * Sonitus - MultiSource.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.data.source; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.io.EOFException; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicReference; +import java.util.logging.Logger; + +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; + +/** + * {@link Source} implementation that simply forwards another source and + * supports changing the source without letting the {@link + * net.pterodactylus.sonitus.data.Sink} know. + * + * @author David ‘Bombe’ Roden + */ +public class MultiSource implements Source { + + /** The logger. */ + private static final Logger logger = Logger.getLogger(MultiSource.class.getName()); + + /** The event bus. */ + private final EventBus eventBus; + + /** The current source. */ + private final AtomicReference source = new AtomicReference(); + + /** Whether the source was changed. */ + private boolean sourceChanged; + + @Inject + public MultiSource(EventBus eventBus) { + this.eventBus = eventBus; + } + + // + // ACTIONS + // + + /** + * Sets the new source to use. + * + * @param source + * The new source to use + */ + public void setSource(Source source) { + checkNotNull(source, "source must not be null"); + + Source oldSource = this.source.getAndSet(source); + if (oldSource != null) { + synchronized (this.source) { + sourceChanged = true; + this.source.notifyAll(); + } + logger.info(String.format("Next Source set: %s", source)); + } + } + + // + // SOURCE METHODS + // + + @Override + public Metadata metadata() { + return source.get().metadata(); + } + + @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. */ + } + } + } + } finally { + synchronized (source) { + sourceChanged = false; + } + } + } + } + +}