<T> void cancel(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); } else { publisher.state.get().cancel(publisher); } }
/** * 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; }
@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 if (publisher.completionBeforeDemand) { publisher.logger.trace("Completed before demand"); publisher.state.get().onAllDataRead(publisher); } Throwable ex = publisher.errorBeforeDemand; if (ex != null) { if (publisher.logger.isTraceEnabled()) { publisher.logger.trace("Completed with error before demand: " + ex); } publisher.state.get().onError(publisher, ex); } } else { throw new IllegalStateException("Failed to transition to SUBSCRIBING, " + "subscriber: " + subscriber); } }
@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); } }
<T> void request(AbstractListenerReadPublisher<T> publisher, long n) { throw new IllegalStateException(toString()); }
@Override public void subscribe(Subscriber<? super T> subscriber) { this.state.get().subscribe(this, subscriber); }
<T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? super T> subscriber) { throw new IllegalStateException(toString()); }
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable t) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); Subscriber<? super T> s = publisher.subscriber; if (s != null) { s.onError(t); } } else { publisher.state.get().onError(publisher, t); } } }
<T> void cancel(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); } else { publisher.state.get().cancel(publisher); } }
@Override public void subscribe(Subscriber<? super T> subscriber) { this.state.get().subscribe(this, subscriber); }
private void changeToDemandState(State oldState) { if (changeState(oldState, State.DEMAND)) { // Protect from infinite recursion in Undertow, where we can't check if data // is available, so all we can do is to try to read. // Generally, no need to check if we just came out of readAndPublish()... if (!oldState.equals(State.READING)) { checkOnDataAvailable(); } } }
<T> void onAllDataRead(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, COMPLETED)) { Subscriber<? super T> s = publisher.subscriber; if (s != null) { s.onComplete(); } } else { publisher.state.get().onAllDataRead(publisher); } }
/** * Sub-classes can call this to delegate container error notifications. */ public final void onError(Throwable ex) { if (this.logger.isTraceEnabled()) { this.logger.trace("I/O event onError: " + ex); } this.state.get().onError(this, ex); }
/** * 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); }
<T> void onAllDataRead(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, COMPLETED)) { Subscriber<? super T> s = publisher.subscriber; if (s != null) { s.onComplete(); } } else { publisher.state.get().onAllDataRead(publisher); } }
/** * Sub-classes can call this method to delegate a contain notification when * all data has been read. */ public void onAllDataRead() { this.logger.trace("I/O event onAllDataRead"); this.state.get().onAllDataRead(this); }