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