Don’t connect sources and sinks directly, use a pipeline to move data around.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / source / MultiSource.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.source;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.io.EOFException;
23 import java.io.IOException;
24 import java.util.concurrent.atomic.AtomicReference;
25 import java.util.logging.Logger;
26
27 import net.pterodactylus.sonitus.data.Metadata;
28 import net.pterodactylus.sonitus.data.Source;
29 import net.pterodactylus.sonitus.data.event.SourceFinishedEvent;
30
31 import com.google.common.eventbus.EventBus;
32 import com.google.inject.Inject;
33
34 /**
35  * {@link Source} implementation that simply forwards another source and
36  * supports changing the source without letting the {@link
37  * net.pterodactylus.sonitus.data.Sink} know.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class MultiSource implements Source {
42
43         /** The logger. */
44         private static final Logger logger = Logger.getLogger(MultiSource.class.getName());
45
46         /** The event bus. */
47         private final EventBus eventBus;
48
49         /** The current source. */
50         private final AtomicReference<Source> source = new AtomicReference<Source>();
51
52         /** Whether the source was changed. */
53         private boolean sourceChanged;
54
55         @Inject
56         public MultiSource(EventBus eventBus) {
57                 this.eventBus = eventBus;
58         }
59
60         //
61         // ACTIONS
62         //
63
64         /**
65          * Sets the new source to use.
66          *
67          * @param source
68          *              The new source to use
69          */
70         public void setSource(Source source) {
71                 checkNotNull(source, "source must not be null");
72
73                 Source oldSource = this.source.getAndSet(source);
74                 if (oldSource != null) {
75                         synchronized (this.source) {
76                                 sourceChanged = true;
77                                 this.source.notifyAll();
78                         }
79                         logger.info(String.format("Next Source set: %s", source));
80                 }
81         }
82
83         //
84         // SOURCE METHODS
85         //
86
87         @Override
88         public Metadata metadata() {
89                 return source.get().metadata();
90         }
91
92         @Override
93         public byte[] get(int bufferSize) throws EOFException, IOException {
94                 while (true) {
95                         try {
96                                 return source.get().get(bufferSize);
97                         } catch (EOFException eofe1) {
98                                 eventBus.post(new SourceFinishedEvent(source.get()));
99                                 synchronized (source) {
100                                         while (!sourceChanged) {
101                                                 try {
102                                                         logger.info("Waiting for next Source...");
103                                                         source.wait();
104                                                         logger.info("Was notified.");
105                                                 } catch (InterruptedException ioe1) {
106                                                         /* ignore: we’ll end up here again if we were interrupted. */
107                                                 }
108                                         }
109                                 }
110                         } finally {
111                                 synchronized (source) {
112                                         sourceChanged = false;
113                                 }
114                         }
115                 }
116         }
117
118 }