private boolean changeState(State oldState, State newState) { boolean result = this.state.compareAndSet(oldState, newState); if (result && rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + oldState + " -> " + newState); } return result; }
/** * Sub-classes can call this to delegate container error notifications. */ public final void onError(Throwable ex) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Connection error: " + ex); } this.state.get().onError(this, ex); }
/** * Invoked when reading is possible, either in the same thread after a check * via {@link #checkOnDataAvailable()}, or as a callback from the underlying * container. */ public final void onDataAvailable() { rsReadLogger.trace(getLogPrefix() + "onDataAvailable"); this.state.get().onDataAvailable(this); }
/** * Sub-classes can call this method to delegate a contain notification when * all data has been read. */ public void onAllDataRead() { rsReadLogger.trace(getLogPrefix() + "onAllDataRead"); this.state.get().onAllDataRead(this); }
private boolean changeState(State oldState, State newState) { boolean result = this.state.compareAndSet(oldState, newState); if (result && rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + oldState + " -> " + newState); } return result; }
/** * Sub-classes can call this method to delegate a contain notification when * all data has been read. */ public void onAllDataRead() { rsReadLogger.trace(getLogPrefix() + "onAllDataRead"); this.state.get().onAllDataRead(this); }
/** * Invoked when reading is possible, either in the same thread after a check * via {@link #checkOnDataAvailable()}, or as a callback from the underlying * container. */ public final void onDataAvailable() { rsReadLogger.trace(getLogPrefix() + "onDataAvailable"); this.state.get().onDataAvailable(this); }
/** * Sub-classes can call this to delegate container error notifications. */ public final void onError(Throwable ex) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Connection error: " + ex); } this.state.get().onError(this, ex); }
@Override <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? super T> subscriber) { Assert.notNull(publisher, "Publisher must not be null"); Assert.notNull(subscriber, "Subscriber must not be null"); if (publisher.changeState(this, SUBSCRIBING)) { Subscription subscription = publisher.createSubscription(); publisher.subscriber = subscriber; subscriber.onSubscribe(subscription); publisher.changeState(SUBSCRIBING, NO_DEMAND); // Now safe to check "beforeDemand" flags, they won't change once in NO_DEMAND String logPrefix = publisher.getLogPrefix(); if (publisher.completionBeforeDemand) { rsReadLogger.trace(logPrefix + "Completed before demand"); publisher.state.get().onAllDataRead(publisher); } Throwable ex = publisher.errorBeforeDemand; if (ex != null) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(logPrefix + "Completed with error before demand: " + ex); } publisher.state.get().onError(publisher, ex); } } else { throw new IllegalStateException("Failed to transition to SUBSCRIBING, " + "subscriber: " + subscriber); } }
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Publishing data read"); } subscriber.onNext(data); } else { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "No more data to read"); } return true; } } return false; }
@Override <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? super T> subscriber) { Assert.notNull(publisher, "Publisher must not be null"); Assert.notNull(subscriber, "Subscriber must not be null"); if (publisher.changeState(this, SUBSCRIBING)) { Subscription subscription = publisher.createSubscription(); publisher.subscriber = subscriber; subscriber.onSubscribe(subscription); publisher.changeState(SUBSCRIBING, NO_DEMAND); // Now safe to check "beforeDemand" flags, they won't change once in NO_DEMAND String logPrefix = publisher.getLogPrefix(); if (publisher.completionBeforeDemand) { rsReadLogger.trace(logPrefix + "Completed before demand"); publisher.state.get().onAllDataRead(publisher); } Throwable ex = publisher.errorBeforeDemand; if (ex != null) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(logPrefix + "Completed with error before demand: " + ex); } publisher.state.get().onError(publisher, ex); } } else { throw new IllegalStateException("Failed to transition to SUBSCRIBING, " + "subscriber: " + subscriber); } }
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Publishing data read"); } subscriber.onNext(data); } else { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "No more data to read"); } return true; } } return false; }
private boolean changeState(State oldState, State newState) { boolean result = this.state.compareAndSet(oldState, newState); if (result && rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + oldState + " -> " + newState); } return result; }
/** * Sub-classes can call this to delegate container error notifications. */ public final void onError(Throwable ex) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Connection error: " + ex); } this.state.get().onError(this, ex); }
/** * Invoked when reading is possible, either in the same thread after a check * via {@link #checkOnDataAvailable()}, or as a callback from the underlying * container. */ public final void onDataAvailable() { rsReadLogger.trace(getLogPrefix() + "onDataAvailable"); this.state.get().onDataAvailable(this); }
/** * Sub-classes can call this method to delegate a contain notification when * all data has been read. */ public void onAllDataRead() { rsReadLogger.trace(getLogPrefix() + "onAllDataRead"); this.state.get().onAllDataRead(this); }
@Override <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? super T> subscriber) { Assert.notNull(publisher, "Publisher must not be null"); Assert.notNull(subscriber, "Subscriber must not be null"); if (publisher.changeState(this, SUBSCRIBING)) { Subscription subscription = publisher.createSubscription(); publisher.subscriber = subscriber; subscriber.onSubscribe(subscription); publisher.changeState(SUBSCRIBING, NO_DEMAND); // Now safe to check "beforeDemand" flags, they won't change once in NO_DEMAND String logPrefix = publisher.getLogPrefix(); if (publisher.completionBeforeDemand) { rsReadLogger.trace(logPrefix + "Completed before demand"); publisher.state.get().onAllDataRead(publisher); } Throwable ex = publisher.errorBeforeDemand; if (ex != null) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(logPrefix + "Completed with error before demand: " + ex); } publisher.state.get().onError(publisher, ex); } } else { throw new IllegalStateException("Failed to transition to SUBSCRIBING, " + "subscriber: " + subscriber); } }
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Publishing data read"); } subscriber.onNext(data); } else { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "No more data to read"); } return true; } } return false; }