Add method to notify sink when a source has updated its metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / ExternalFilter.java
1 /*
2  * Sonitus - ExternalFilter.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 java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.PipedInputStream;
25 import java.io.PipedOutputStream;
26 import java.util.Arrays;
27 import java.util.logging.Logger;
28
29 import net.pterodactylus.sonitus.data.ConnectException;
30 import net.pterodactylus.sonitus.data.Connection;
31 import net.pterodactylus.sonitus.data.Filter;
32 import net.pterodactylus.sonitus.data.Format;
33 import net.pterodactylus.sonitus.data.Metadata;
34 import net.pterodactylus.sonitus.data.Source;
35 import net.pterodactylus.sonitus.io.InputStreamDrainer;
36
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.Iterables;
40 import com.google.common.io.ByteStreams;
41
42 /**
43  * {@link Filter} implementation that runs its {@link Source} through an
44  * external program.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public abstract class ExternalFilter implements Filter {
49
50         /** The logger. */
51         private final Logger logger = Logger.getLogger(getClass().getName());
52
53         /** The source. */
54         private Source source;
55
56         /** The input stream that will hold the converted source. */
57         private PipedInputStream pipedInputStream;
58
59         //
60         // FILTER METHODS
61         //
62
63         @Override
64         public Format format() {
65                 return source.format();
66         }
67
68         @Override
69         public Metadata metadata() {
70                 return source.metadata();
71         }
72
73         @Override
74         public byte[] get(int bufferSize) throws EOFException, IOException {
75                 byte[] buffer = new byte[bufferSize];
76                 int read = pipedInputStream.read(buffer);
77                 if (read == -1) {
78                         throw new EOFException();
79                 }
80                 return Arrays.copyOf(buffer, read);
81         }
82
83         @Override
84         public void connect(Source source) throws ConnectException {
85                 Preconditions.checkNotNull(source, "source must not be null");
86
87                 this.source = source;
88                 try {
89                         final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(source.format())).addAll(parameters(source.format())).build(), String.class));
90                         final InputStream processOutput = process.getInputStream();
91                         final OutputStream processInput = process.getOutputStream();
92                         final InputStream processError = process.getErrorStream();
93                         final PipedOutputStream pipedOutputStream = new PipedOutputStream();
94                         pipedInputStream = new PipedInputStream(pipedOutputStream);
95                         new Thread(new InputStreamDrainer(processError)).start();
96                         new Thread(new Runnable() {
97
98                                 @Override
99                                 public void run() {
100                                         try {
101                                                 ByteStreams.copy(processOutput, pipedOutputStream);
102                                         } catch (IOException ioe1) {
103                                                 /* okay, just exit. */
104                                         }
105                                         logger.finest("Reading stdout finished.");
106                                 }
107                         }).start();
108                         new Thread(new Connection(source) {
109
110                                 @Override
111                                 protected int bufferSize() {
112                                         return 4096;
113                                 }
114
115                                 @Override
116                                 protected void feed(byte[] buffer) throws IOException {
117                                         processInput.write(buffer);
118                                         processInput.flush();
119                                 }
120
121                                 @Override
122                                 protected void finish() throws IOException {
123                                         processInput.close();
124                                         processOutput.close();
125                                         processError.close();
126                                 }
127                         }).start();
128                 } catch (IOException ioe1) {
129
130                 }
131         }
132
133         @Override
134         public void metadataUpdated() {
135                 /* ignore. */
136         }
137
138         //
139         // SUBCLASS METHODS
140         //
141
142         /**
143          * Returns the location of the binary to execute.
144          *
145          * @param format
146          *              The format being processed
147          * @return The location of the binary to execute
148          */
149         protected abstract String binary(Format format);
150
151         /**
152          * Returns the parameters for the binary.
153          *
154          * @param format
155          *              The format being processed
156          * @return The parameters for the binary
157          */
158         protected abstract Iterable<String> parameters(Format format);
159
160 }