1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.incubator.http;
  27 
  28 import java.util.Optional;
  29 import java.util.concurrent.Flow;
  30 import jdk.incubator.http.internal.common.Log;
  31 
  32 /**
  33  * A Publisher which is assumed to run in its own thread.
  34  *
  35  * acceptData() may therefore block while waiting for subscriber demand
  36  */
  37 class BlockingPushPublisher<T> extends AbstractPushPublisher<T> {
  38     volatile Subscription subscription;
  39     volatile Flow.Subscriber<? super T> subscriber;
  40     volatile SubscriptionState state;
  41     long demand;
  42 
  43     @Override
  44     public void subscribe(Flow.Subscriber<? super T> subscriber) {
  45         state = SubscriptionState.OPENED;
  46         subscription = new Subscription(subscriber);
  47         subscriber.onSubscribe(subscription);
  48     }
  49 
  50     /**
  51      * Entry point for supplying items to publisher. This call will block
  52      * when no demand available.
  53      */
  54     @Override
  55     public void acceptData(Optional<T> item) throws InterruptedException {
  56         SubscriptionState s = this.state;
  57 
  58         // do not use switch(this.state): this.state could be null.
  59         if (s == SubscriptionState.CANCELLED) return;
  60         if (s == SubscriptionState.DONE) {
  61             throw new IllegalStateException("subscription complete");
  62         }
  63 
  64         if (!item.isPresent()) {
  65             subscriber.onComplete();
  66             this.state = SubscriptionState.DONE;
  67         } else {
  68             obtainPermit();
  69             if (this.state == SubscriptionState.CANCELLED) return;
  70             subscriber.onNext(item.get());
  71         }
  72     }
  73 
  74     /**
  75      * Terminates the publisher with given exception.
  76      */
  77     @Override
  78     public void acceptError(Throwable t) {
  79         if (this.state != SubscriptionState.OPENED) {
  80             Log.logError(t);
  81             return;
  82         }
  83         subscriber.onError(t);
  84         cancel();
  85     }
  86 
  87     private synchronized void obtainPermit() throws InterruptedException {
  88         while (demand == 0) {
  89             wait();
  90         }
  91         if (this.state == SubscriptionState.DONE) {
  92             throw new IllegalStateException("subscription complete");
  93         }
  94         demand --;
  95     }
  96 
  97     synchronized void addPermits(long n) {
  98         long old = demand;
  99         demand += n;
 100         if (old == 0) {
 101             notifyAll();
 102         }
 103     }
 104 
 105     synchronized void cancel() {
 106         this.state = SubscriptionState.CANCELLED;
 107         notifyAll();
 108     }
 109 
 110     private class Subscription implements Flow.Subscription {
 111 
 112         Subscription(Flow.Subscriber<? super T> subscriber) {
 113             BlockingPushPublisher.this.subscriber = subscriber;
 114         }
 115 
 116         @Override
 117         public void request(long n) {
 118             addPermits(n);
 119         }
 120 
 121         @Override
 122         public void cancel() {
 123             BlockingPushPublisher.this.cancel();
 124         }
 125     }
 126 }