2 * Sonitus - MultiSource.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.data.filter;
20 import static com.google.common.base.Preconditions.*;
22 import java.io.EOFException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.PipedInputStream;
27 import java.io.PipedOutputStream;
28 import java.util.Arrays;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
32 import net.pterodactylus.sonitus.data.ConnectException;
33 import net.pterodactylus.sonitus.data.Filter;
34 import net.pterodactylus.sonitus.data.Metadata;
35 import net.pterodactylus.sonitus.data.ReusableSink;
36 import net.pterodactylus.sonitus.data.Source;
37 import net.pterodactylus.sonitus.data.event.SourceFinishedEvent;
39 import com.google.common.eventbus.EventBus;
40 import com.google.inject.Inject;
43 * {@link ReusableSink} implementation that supports changing the source without
44 * letting the {@link net.pterodactylus.sonitus.data.Sink} know.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class MultiSourceFilter implements Filter, ReusableSink {
51 private static final Logger logger = Logger.getLogger(MultiSourceFilter.class.getName());
53 /** Object used for synchronization. */
54 private final Object syncObject = new Object();
57 private final EventBus eventBus;
59 /** The connection. */
60 private Connection connection;
63 public MultiSourceFilter(EventBus eventBus) {
64 this.eventBus = eventBus;
68 public Metadata metadata() {
69 synchronized (syncObject) {
70 return connection.source.metadata();
75 public byte[] get(int bufferSize) throws EOFException, IOException {
76 byte[] buffer = new byte[bufferSize];
77 InputStream inputStream;
78 synchronized (syncObject) {
79 inputStream = connection.pipedInputStream;
81 int read = inputStream.read(buffer);
82 return Arrays.copyOf(buffer, read);
86 public void connect(Source source) throws ConnectException {
87 checkNotNull(source, "source must not be null");
88 if ((connection != null) && (connection.source != null)) {
89 checkArgument(connection.source.metadata().channels() == source.metadata().channels(), "source’s channel count must equal existing source’s channel count");
90 checkArgument(connection.source.metadata().frequency() == source.metadata().frequency(), "source’s frequency must equal existing source’s frequency");
91 checkArgument(connection.source.metadata().encoding().equalsIgnoreCase(source.metadata().encoding()), "source’s encoding must equal existing source’s encoding");
94 if (connection == null) {
95 connection = new Connection();
96 new Thread(connection).start();
99 connection.source(source);
100 } catch (IOException ioe1) {
101 throw new ConnectException(ioe1);
106 public void metadataUpdated() {
111 * The connection feeds the input from the currently connected source to the
112 * input stream that {@link #get(int)} will get its data from.
114 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
116 private class Connection implements Runnable {
118 /** The currently connected source. */
119 /* synchronized by syncObject. */
120 private Source source;
122 /** The input stream that {@link #get(int)} will read from. */
123 /* synchronized by syncObject. */
124 private PipedInputStream pipedInputStream;
126 /** The output stream that the source will be fed into. */
127 /* synchronized by syncObject. */
128 private PipedOutputStream pipedOutputStream;
131 * Changes the source of the connection.
134 * The new source of the connection
135 * @return This connection
136 * @throws IOException
137 * if an I/O error occurs
139 public Connection source(Source source) throws IOException {
140 synchronized (syncObject) {
141 Source oldSource = this.source;
142 this.source = source;
143 if (oldSource != null) {
144 eventBus.post(new SourceFinishedEvent(oldSource));
146 pipedInputStream = new PipedInputStream();
147 pipedOutputStream = new PipedOutputStream(pipedInputStream);
148 syncObject.notifyAll();
156 /* wait for source to be set. */
157 OutputStream outputStream;
159 logger.finest("Entering synchronized block...");
160 synchronized (syncObject) {
161 logger.finest("Entered synchronized block.");
162 source = this.source;
163 while (source == null) {
165 logger.finest("Waiting for source to connect...");
167 } catch (InterruptedException ie1) {
168 /* ignore, keep waiting. */
170 source = this.source;
172 outputStream = pipedOutputStream;
174 logger.finest("Exited synchronized block.");
176 byte[] buffer = null;
177 boolean readSuccessful = false;
178 while (!readSuccessful) {
180 buffer = source.get(4096);
181 logger.finest(String.format("Read %d Bytes.", buffer.length));
182 if (buffer.length > 0) {
183 readSuccessful = true;
185 } catch (IOException e) {
186 /* TODO - notify & wait */
191 outputStream.write(buffer);
192 logger.finest(String.format("Wrote %d Bytes.", buffer.length));
193 } catch (IOException ioe1) {
194 /* okay, the sink has died, exit. */
195 logger.log(Level.WARNING, "Could not write to pipe!", ioe1);
200 logger.info("Exiting.");