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.Format;
35 import net.pterodactylus.sonitus.data.ReusableSink;
36 import net.pterodactylus.sonitus.data.Source;
38 import com.google.common.eventbus.EventBus;
39 import com.google.inject.Inject;
42 * {@link ReusableSink} implementation that supports changing the source without
43 * letting the {@link net.pterodactylus.sonitus.data.Sink} know.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class MultiSourceFilter implements Filter, ReusableSink {
50 private static final Logger logger = Logger.getLogger(MultiSourceFilter.class.getName());
52 /** Object used for synchronization. */
53 private final Object syncObject = new Object();
56 private final EventBus eventBus;
58 /** The connection. */
59 private Connection connection;
62 private Format format;
65 public MultiSourceFilter(EventBus eventBus) {
66 this.eventBus = eventBus;
70 public Format format() {
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");
89 checkArgument(format.equals(source.format()), "source’s format must equal this sink’s format");
91 format = source.format();
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 * The connection feeds the input from the currently connected source to the
107 * input stream that {@link #get(int)} will get its data from.
109 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
111 private class Connection implements Runnable {
113 /** The currently connected source. */
114 /* synchronized by syncObject. */
115 private Source source;
117 /** The input stream that {@link #get(int)} will read from. */
118 /* synchronized by syncObject. */
119 private PipedInputStream pipedInputStream;
121 /** The output stream that the source will be fed into. */
122 /* synchronized by syncObject. */
123 private PipedOutputStream pipedOutputStream;
126 * Changes the source of the connection.
129 * The new source of the connection
130 * @return This connection
131 * @throws IOException
132 * if an I/O error occurs
134 public Connection source(Source source) throws IOException {
135 synchronized (syncObject) {
136 this.source = source;
137 pipedInputStream = new PipedInputStream();
138 pipedOutputStream = new PipedOutputStream(pipedInputStream);
139 syncObject.notifyAll();
147 /* wait for source to be set. */
148 OutputStream outputStream;
150 logger.finest("Entering synchronized block...");
151 synchronized (syncObject) {
152 logger.finest("Entered synchronized block.");
153 source = this.source;
154 while (source == null) {
156 logger.finest("Waiting for source to connect...");
158 } catch (InterruptedException ie1) {
159 /* ignore, keep waiting. */
161 source = this.source;
163 outputStream = pipedOutputStream;
165 logger.finest("Exited synchronized block.");
167 byte[] buffer = null;
168 boolean readSuccessful = false;
169 while (!readSuccessful) {
171 buffer = source.get(4096);
172 logger.finest(String.format("Read %d Bytes.", buffer.length));
173 if (buffer.length > 0) {
174 readSuccessful = true;
176 } catch (IOException e) {
177 /* TODO - notify & wait */
182 outputStream.write(buffer);
183 logger.finest(String.format("Wrote %d Bytes.", buffer.length));
184 } catch (IOException ioe1) {
185 /* okay, the sink has died, exit. */
186 logger.log(Level.WARNING, "Could not write to pipe!", ioe1);
191 logger.info("Exiting.");