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