|
17 | 17 | import co.paralleluniverse.fibers.SuspendExecution;
|
18 | 18 | import co.paralleluniverse.fibers.Suspendable;
|
19 | 19 | import co.paralleluniverse.strands.Strand;
|
| 20 | +import co.paralleluniverse.strands.Timeout; |
| 21 | +import co.paralleluniverse.strands.channels.Channel; |
| 22 | +import co.paralleluniverse.strands.channels.Channels; |
20 | 23 | import co.paralleluniverse.strands.channels.ReceivePort;
|
21 | 24 | import co.paralleluniverse.strands.channels.SendPort;
|
| 25 | +import java.util.concurrent.TimeUnit; |
22 | 26 | import rx.Observable;
|
23 | 27 | import rx.Observer;
|
24 | 28 | import rx.Scheduler;
|
| 29 | +import rx.util.Exceptions; |
25 | 30 | import rx.util.OnErrorNotImplementedException;
|
26 | 31 |
|
27 | 32 | /**
|
28 |
| - * |
| 33 | + * This class contains static methods that connect {@link Observable}s and {@link Channel}s. |
29 | 34 | */
|
30 |
| -public class ChannelObservable { |
| 35 | +public final class ChannelObservable { |
| 36 | + private ChannelObservable() { |
| 37 | + } |
31 | 38 |
|
32 | 39 | /**
|
33 | 40 | * Converts an {@link Iterable} sequence into an Observable that emits each message received on the channel.
|
@@ -102,4 +109,152 @@ public void onError(Throwable e) {
|
102 | 109 | }
|
103 | 110 | };
|
104 | 111 | }
|
| 112 | + |
| 113 | + /** |
| 114 | + * Creates a {@link ReceivePort} subscribed to an {@link Observable}. |
| 115 | + * <p> |
| 116 | + * @param <T> the type of messages emitted by the observable and received on the channel. |
| 117 | + * @param bufferSize the channel's buffer size |
| 118 | + * @param policy the channel's {@link Channels.OverflowPolicy OverflowPolicy} |
| 119 | + * @param o the observable |
| 120 | + * @return A new channel with the given buffer size and overflow policy that will receive all events emitted by the observable. |
| 121 | + */ |
| 122 | + public final static <T> ReceivePort<T> subscribe(int bufferSize, Channels.OverflowPolicy policy, Observable<T> o) { |
| 123 | + final ChannelWithErrors<T> channel = new ChannelWithErrors<T>(Channels.newChannel(bufferSize, policy)); |
| 124 | + |
| 125 | + o.subscribe(new Observer<T>() { |
| 126 | + @Override |
| 127 | + @Suspendable |
| 128 | + public void onNext(T t) { |
| 129 | + try { |
| 130 | + channel.sendPort().send(t); |
| 131 | + } catch (InterruptedException ex) { |
| 132 | + Strand.interrupted(); |
| 133 | + } catch (SuspendExecution ex) { |
| 134 | + throw new AssertionError(ex); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onCompleted() { |
| 140 | + channel.sendPort().close(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void onError(Throwable e) { |
| 145 | + channel.error(e); |
| 146 | + } |
| 147 | + }); |
| 148 | + return channel.receivePort(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Creates a {@link ReceivePort} subscribed to an {@link Observable}. |
| 153 | + * <p> |
| 154 | + * @param <T> the type of messages emitted by the observable and received on the channel. |
| 155 | + * @param bufferSize the channel's buffer size |
| 156 | + * @param policy the channel's {@link Channels.OverflowPolicy OverflowPolicy} |
| 157 | + * @param o the observable |
| 158 | + * @param scheduler the scheduler used to emit the observable's events |
| 159 | + * @return A new channel with the given buffer size and overflow policy that will receive all events emitted by the observable. |
| 160 | + */ |
| 161 | + public final static <T> ReceivePort<T> subscribe(int bufferSize, Channels.OverflowPolicy policy, Observable<T> o, Scheduler scheduler) { |
| 162 | + final ChannelWithErrors<T> channel = new ChannelWithErrors<T>(Channels.newChannel(bufferSize, policy)); |
| 163 | + |
| 164 | + o.subscribe(new Observer<T>() { |
| 165 | + @Override |
| 166 | + @Suspendable |
| 167 | + public void onNext(T t) { |
| 168 | + try { |
| 169 | + channel.sendPort().send(t); |
| 170 | + } catch (InterruptedException ex) { |
| 171 | + Strand.interrupted(); |
| 172 | + } catch (SuspendExecution ex) { |
| 173 | + throw new AssertionError(ex); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void onCompleted() { |
| 179 | + channel.sendPort().close(); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void onError(Throwable e) { |
| 184 | + channel.error(e); |
| 185 | + } |
| 186 | + }, scheduler); |
| 187 | + return channel.receivePort(); |
| 188 | + } |
| 189 | + |
| 190 | + private static class ChannelWithErrors<T> { |
| 191 | + private final Channel<Object> ch; |
| 192 | + |
| 193 | + public ChannelWithErrors(Channel<Object> ch) { |
| 194 | + this.ch = ch; |
| 195 | + } |
| 196 | + |
| 197 | + @Suspendable |
| 198 | + public void error(Throwable t) { |
| 199 | + try { |
| 200 | + ch.send(new ThrowableWrapper(t)); |
| 201 | + ch.close(); |
| 202 | + } catch (InterruptedException e) { |
| 203 | + } catch (SuspendExecution e) { |
| 204 | + throw new AssertionError(e); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public ReceivePort<T> receivePort() { |
| 209 | + return new ReceivePort<T>() { |
| 210 | + @Override |
| 211 | + public T receive() throws SuspendExecution, InterruptedException { |
| 212 | + return get(ch.receive()); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public T receive(long timeout, TimeUnit unit) throws SuspendExecution, InterruptedException { |
| 217 | + return get(ch.receive(timeout, unit)); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public T receive(Timeout timeout) throws SuspendExecution, InterruptedException { |
| 222 | + return get(ch.receive(timeout)); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public T tryReceive() { |
| 227 | + return get(ch.tryReceive()); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public void close() { |
| 232 | + ch.close(); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public boolean isClosed() { |
| 237 | + return ch.isClosed(); |
| 238 | + } |
| 239 | + }; |
| 240 | + } |
| 241 | + |
| 242 | + public SendPort<T> sendPort() { |
| 243 | + return (SendPort<T>) ch; |
| 244 | + } |
| 245 | + |
| 246 | + private T get(Object m) { |
| 247 | + if (m instanceof ThrowableWrapper) |
| 248 | + throw Exceptions.propagate(((ThrowableWrapper) m).t); |
| 249 | + return (T) m; |
| 250 | + } |
| 251 | + |
| 252 | + private static class ThrowableWrapper { |
| 253 | + final Throwable t; |
| 254 | + |
| 255 | + public ThrowableWrapper(Throwable t) { |
| 256 | + this.t = t; |
| 257 | + } |
| 258 | + } |
| 259 | + } |
105 | 260 | }
|
0 commit comments