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