a505c92c22ea1466befe9542f7337a77b59a7756
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / MultiSourceFilter.java
1 /*
2  * Sonitus - MultiSource.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import static com.google.common.base.Preconditions.*;
21
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;
31
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.Metadata;
36 import net.pterodactylus.sonitus.data.ReusableSink;
37 import net.pterodactylus.sonitus.data.Source;
38 import net.pterodactylus.sonitus.data.event.SourceFinishedEvent;
39
40 import com.google.common.eventbus.EventBus;
41 import com.google.inject.Inject;
42
43 /**
44  * {@link ReusableSink} implementation that supports changing the source without
45  * letting the {@link net.pterodactylus.sonitus.data.Sink} know.
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 public class MultiSourceFilter implements Filter, ReusableSink {
50
51         /** The logger. */
52         private static final Logger logger = Logger.getLogger(MultiSourceFilter.class.getName());
53
54         /** Object used for synchronization. */
55         private final Object syncObject = new Object();
56
57         /** The event bus. */
58         private final EventBus eventBus;
59
60         /** The connection. */
61         private Connection connection;
62
63         @Inject
64         public MultiSourceFilter(EventBus eventBus) {
65                 this.eventBus = eventBus;
66         }
67
68         @Override
69         public Format format() {
70                 synchronized (syncObject) {
71                         return connection.source.format();
72                 }
73         }
74
75         @Override
76         public Metadata metadata() {
77                 synchronized (syncObject) {
78                         return connection.source.metadata();
79                 }
80         }
81
82         @Override
83         public byte[] get(int bufferSize) throws EOFException, IOException {
84                 byte[] buffer = new byte[bufferSize];
85                 InputStream inputStream;
86                 synchronized (syncObject) {
87                         inputStream = connection.pipedInputStream;
88                 }
89                 int read = inputStream.read(buffer);
90                 return Arrays.copyOf(buffer, read);
91         }
92
93         @Override
94         public void connect(Source source) throws ConnectException {
95                 checkNotNull(source, "source must not be null");
96                 if ((connection != null) && (connection.source != null)) {
97                         checkArgument(connection.source.format().equals(source.format()), "source’s format must equal this sink’s format");
98                 }
99
100                 if (connection == null) {
101                         connection = new Connection();
102                         new Thread(connection).start();
103                 }
104                 try {
105                         connection.source(source);
106                 } catch (IOException ioe1) {
107                         throw new ConnectException(ioe1);
108                 }
109         }
110
111         @Override
112         public void metadataUpdated() {
113                 /* ignore. */
114         }
115
116         /**
117          * The connection feeds the input from the currently connected source to the
118          * input stream that {@link #get(int)} will get its data from.
119          *
120          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
121          */
122         private class Connection implements Runnable {
123
124                 /** The currently connected source. */
125                 /* synchronized by syncObject. */
126                 private Source source;
127
128                 /** The input stream that {@link #get(int)} will read from. */
129                 /* synchronized by syncObject. */
130                 private PipedInputStream pipedInputStream;
131
132                 /** The output stream that the source will be fed into. */
133                 /* synchronized by syncObject. */
134                 private PipedOutputStream pipedOutputStream;
135
136                 /**
137                  * Changes the source of the connection.
138                  *
139                  * @param source
140                  *              The new source of the connection
141                  * @return This connection
142                  * @throws IOException
143                  *              if an I/O error occurs
144                  */
145                 public Connection source(Source source) throws IOException {
146                         synchronized (syncObject) {
147                                 if (this.source != null) {
148                                         eventBus.post(new SourceFinishedEvent(this.source));
149                                 }
150                                 this.source = source;
151                                 pipedInputStream = new PipedInputStream();
152                                 pipedOutputStream = new PipedOutputStream(pipedInputStream);
153                                 syncObject.notifyAll();
154                         }
155                         return this;
156                 }
157
158                 @Override
159                 public void run() {
160                         while (true) {
161                                 /* wait for source to be set. */
162                                 OutputStream outputStream;
163                                 Source source;
164                                 logger.finest("Entering synchronized block...");
165                                 synchronized (syncObject) {
166                                         logger.finest("Entered synchronized block.");
167                                         source = this.source;
168                                         while (source == null) {
169                                                 try {
170                                                         logger.finest("Waiting for source to connect...");
171                                                         syncObject.wait();
172                                                 } catch (InterruptedException ie1) {
173                                                         /* ignore, keep waiting. */
174                                                 }
175                                                 source = this.source;
176                                         }
177                                         outputStream = pipedOutputStream;
178                                 }
179                                 logger.finest("Exited synchronized block.");
180
181                                 byte[] buffer = null;
182                                 boolean readSuccessful = false;
183                                 while (!readSuccessful) {
184                                         try {
185                                                 buffer = source.get(4096);
186                                                 logger.finest(String.format("Read %d Bytes.", buffer.length));
187                                                 if (buffer.length > 0) {
188                                                         readSuccessful = true;
189                                                 }
190                                         } catch (IOException e) {
191                                                 /* TODO - notify & wait */
192                                         }
193                                 }
194
195                                 try {
196                                         outputStream.write(buffer);
197                                         logger.finest(String.format("Wrote %d Bytes.", buffer.length));
198                                 } catch (IOException ioe1) {
199                                         /* okay, the sink has died, exit. */
200                                         logger.log(Level.WARNING, "Could not write to pipe!", ioe1);
201                                         break;
202                                 }
203                         }
204
205                         logger.info("Exiting.");
206                 }
207
208         }
209
210 }