|
| 1 | +// GPars - Groovy Parallel Systems |
| 2 | +// |
| 3 | +// Copyright © 2008-10 The original author or authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package groovyx.gpars.dataflow; |
| 18 | + |
| 19 | +import groovyx.gpars.dataflow.stream.DataFlowStream; |
| 20 | +import groovyx.gpars.dataflow.stream.DataFlowStreamReadAdapter; |
| 21 | +import groovyx.gpars.dataflow.stream.DataFlowStreamWriteAdapter; |
| 22 | + |
| 23 | +/** |
| 24 | + * Offers a deterministic one-to-many and many-to-many messaging alternative to DataFlowQueue. |
| 25 | + * Internally it wraps a DataFlowStream class with a DataFlowStreamWriteAdapter and so |
| 26 | + * synchronizes all writes to the underlying stream allowing multiple threads accessing the stream concurrently. |
| 27 | + * On demand through the createReadChannel() method it will return an DataFlowReadChannel through which the reader will receive |
| 28 | + * all messages written to the channel since then. |
| 29 | + * <p/> |
| 30 | + * Typical use: |
| 31 | + * <p/> |
| 32 | + * DataFlowWriteChannel broadcastStream = new DataFlowBroadcast() |
| 33 | + * DataFlowReadChannel stream1 = broadcastStream.createReadChannel() |
| 34 | + * DataFlowReadChannel stream2 = broadcastStream.createReadChannel() |
| 35 | + * broadcastStream << 'Message' |
| 36 | + * assert stream1.val == stream2.val |
| 37 | + * |
| 38 | + * @param <T> The type of messages to pass through the stream |
| 39 | + * @author Vaclav Pech |
| 40 | + */ |
| 41 | +public final class DataFlowBroadcast<T> extends DataFlowStreamWriteAdapter<T> { |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a new adapter |
| 45 | + */ |
| 46 | + public DataFlowBroadcast() { |
| 47 | + super(new DataFlowStream<T>()); |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings({"SynchronizedMethod"}) |
| 51 | + @Override |
| 52 | + public synchronized String toString() { |
| 53 | + return "DataFlowBroadcast around " + super.toString(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Retrieves an implementation of DataFlowReadChannel to read all messages submitted to the broadcast chanel. |
| 58 | + * Since multiple parties (threads/tasks/actors/...) may ask for read channels independently, the submitted messages are effectively |
| 59 | + * broadcast to all the subscribers. |
| 60 | + * |
| 61 | + * @return A read channel to receive messages submitted to the broadcast channel from now on. |
| 62 | + */ |
| 63 | + public DataFlowReadChannel<T> createReadChannel() { |
| 64 | + return new DataFlowStreamReadAdapter<T>(getHead()); |
| 65 | + } |
| 66 | +} |
| 67 | + |
0 commit comments