Search in sources :

Example 1 with Transformer

use of rx.Observable.Transformer in project jocean-http by isdom.

the class StreamUtil method src2dwb.

public static <SRC, STATE> Transformer<SRC, DisposableWrapper<ByteBuf>> src2dwb(final Func0<DisposableWrapper<ByteBuf>> newdwb, final Func1<SRC, byte[]> src2bytes, final Func1<SRC, STATE> newstate, final Action2<SRC, STATE> updatestate) {
    final AtomicReference<DisposableWrapper<ByteBuf>> ref = new AtomicReference<>();
    return new Transformer<SRC, DisposableWrapper<ByteBuf>>() {

        @Override
        public Observable<DisposableWrapper<ByteBuf>> call(final Observable<SRC> upstream) {
            return upstream.flatMap(new Func1<SRC, Observable<DisposableWrapper<ByteBuf>>>() {

                @Override
                public Observable<DisposableWrapper<ByteBuf>> call(final SRC src) {
                    if (null == ref.get()) {
                        ref.set(newdwb.call());
                        StateableUtil.setStateTo(newstate.call(src), ref.get());
                    }
                    final byte[] bytes = src2bytes.call(src);
                    if (bytes.length <= ref.get().unwrap().maxWritableBytes()) {
                        ref.get().unwrap().writeBytes(bytes);
                        updatestate.call(src, StateableUtil.<STATE>stateOf(ref.get()));
                        return Observable.empty();
                    } else {
                        final DisposableWrapper<ByteBuf> newbuf = newdwb.call();
                        newbuf.unwrap().writeBytes(bytes);
                        StateableUtil.setStateTo(newstate.call(src), newbuf);
                        return Observable.just(ref.getAndSet(newbuf));
                    }
                }
            }, new Func1<Throwable, Observable<DisposableWrapper<ByteBuf>>>() {

                @Override
                public Observable<DisposableWrapper<ByteBuf>> call(final Throwable e) {
                    return Observable.error(e);
                }
            }, new Func0<Observable<DisposableWrapper<ByteBuf>>>() {

                @Override
                public Observable<DisposableWrapper<ByteBuf>> call() {
                    if (null == ref.get()) {
                        LOG.debug("src2dwb onCompleted with ref is null");
                        return Observable.empty();
                    } else {
                        final DisposableWrapper<ByteBuf> last = ref.getAndSet(null);
                        if (last.unwrap().readableBytes() > 0) {
                            LOG.debug("src2dwb onCompleted with last as content");
                            return Observable.just(last);
                        } else {
                            last.dispose();
                            LOG.debug("src2dwb onCompleted with last NO content");
                            return Observable.empty();
                        }
                    }
                }
            }).doOnUnsubscribe(new Action0() {

                @Override
                public void call() {
                    final DisposableWrapper<ByteBuf> last = ref.getAndSet(null);
                    if (null != last) {
                        LOG.debug("src2dwb doOnUnsubscribe with last disposed.");
                        last.dispose();
                    }
                }
            });
        }
    };
}
Also used : Action0(rx.functions.Action0) Transformer(rx.Observable.Transformer) DisposableWrapper(org.jocean.idiom.DisposableWrapper) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) Func1(rx.functions.Func1) Func0(rx.functions.Func0)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DisposableWrapper (org.jocean.idiom.DisposableWrapper)1 Observable (rx.Observable)1 Transformer (rx.Observable.Transformer)1 Action0 (rx.functions.Action0)1 Func0 (rx.functions.Func0)1 Func1 (rx.functions.Func1)1