< prev index next >

src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/websocket/Transmitter.java

Print this page




  23  * questions.
  24  */
  25 
  26 package jdk.incubator.http.internal.websocket;
  27 
  28 import java.io.IOException;
  29 import java.nio.channels.SelectionKey;
  30 import java.util.concurrent.atomic.AtomicBoolean;
  31 import java.util.function.Consumer;
  32 
  33 import static java.util.Objects.requireNonNull;
  34 
  35 /*
  36  * Sends messages one at a time, in an asynchronous and non-blocking fashion.
  37  *
  38  * No matter whether the message has been fully sent or an error has occurred,
  39  * the transmitter reports the outcome to the supplied handler and becomes ready
  40  * to accept a new message. Until then, the transmitter is considered "busy" and
  41  * an IllegalStateException will be thrown on each attempt to invoke send.
  42  */
  43 final class Transmitter {
  44 
  45     /* This flag is used solely for assertions */
  46     private final AtomicBoolean busy = new AtomicBoolean();
  47     private OutgoingMessage message;
  48     private Consumer<Exception> completionHandler;
  49     private final RawChannel channel;
  50     private final RawChannel.RawEvent event;
  51 
  52     Transmitter(RawChannel channel) {
  53         this.channel = requireNonNull(channel);
  54         this.event = createHandler();
  55     }
  56 
  57     /**
  58      * The supplied handler may be invoked in the calling thread.
  59      * A {@code StackOverflowError} may thus occur if there's a possibility
  60      * that this method is called again by the supplied handler.
  61      */
  62     void send(OutgoingMessage message, Consumer<Exception> completionHandler) {


  63         requireNonNull(message);
  64         requireNonNull(completionHandler);
  65         if (!busy.compareAndSet(false, true)) {
  66             throw new IllegalStateException();
  67         }
  68         send0(message, completionHandler);




  69     }
  70 
  71     private RawChannel.RawEvent createHandler() {
  72         return new RawChannel.RawEvent() {
  73 
  74             @Override
  75             public int interestOps() {
  76                 return SelectionKey.OP_WRITE;
  77             }
  78 
  79             @Override
  80             public void handle() {
  81                 // registerEvent(e) happens-before subsequent e.handle(), so
  82                 // we're fine reading the stored message and the completionHandler
  83                 send0(message, completionHandler);
  84             }
  85         };
  86     }
  87 
  88     private void send0(OutgoingMessage message, Consumer<Exception> handler) {




  23  * questions.
  24  */
  25 
  26 package jdk.incubator.http.internal.websocket;
  27 
  28 import java.io.IOException;
  29 import java.nio.channels.SelectionKey;
  30 import java.util.concurrent.atomic.AtomicBoolean;
  31 import java.util.function.Consumer;
  32 
  33 import static java.util.Objects.requireNonNull;
  34 
  35 /*
  36  * Sends messages one at a time, in an asynchronous and non-blocking fashion.
  37  *
  38  * No matter whether the message has been fully sent or an error has occurred,
  39  * the transmitter reports the outcome to the supplied handler and becomes ready
  40  * to accept a new message. Until then, the transmitter is considered "busy" and
  41  * an IllegalStateException will be thrown on each attempt to invoke send.
  42  */
  43 public class Transmitter {
  44 
  45     /* This flag is used solely for assertions */
  46     private final AtomicBoolean busy = new AtomicBoolean();
  47     private OutgoingMessage message;
  48     private Consumer<Exception> completionHandler;
  49     private final RawChannel channel;
  50     private final RawChannel.RawEvent event;
  51 
  52     public Transmitter(RawChannel channel) {
  53         this.channel = channel;
  54         this.event = createHandler();
  55     }
  56 
  57     /**
  58      * The supplied handler may be invoked in the calling thread.
  59      * A {@code StackOverflowError} may thus occur if there's a possibility
  60      * that this method is called again by the supplied handler.
  61      */
  62     public void send(OutgoingMessage message,
  63                      Consumer<Exception> completionHandler)
  64     {
  65         requireNonNull(message);
  66         requireNonNull(completionHandler);
  67         if (!busy.compareAndSet(false, true)) {
  68             throw new IllegalStateException();
  69         }
  70         send0(message, completionHandler);
  71     }
  72 
  73     public void close() throws IOException {
  74         channel.shutdownOutput();
  75     }
  76 
  77     private RawChannel.RawEvent createHandler() {
  78         return new RawChannel.RawEvent() {
  79 
  80             @Override
  81             public int interestOps() {
  82                 return SelectionKey.OP_WRITE;
  83             }
  84 
  85             @Override
  86             public void handle() {
  87                 // registerEvent(e) happens-before subsequent e.handle(), so
  88                 // we're fine reading the stored message and the completionHandler
  89                 send0(message, completionHandler);
  90             }
  91         };
  92     }
  93 
  94     private void send0(OutgoingMessage message, Consumer<Exception> handler) {


< prev index next >