1 /*
   2  * Copyright (c) 2015, 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 jdk.incubator.http.internal.common.Utils;
  29 import jdk.incubator.http.internal.websocket.RawChannel;
  30 
  31 import java.io.IOException;
  32 import java.nio.ByteBuffer;
  33 import java.nio.channels.SelectableChannel;
  34 import java.nio.channels.SocketChannel;
  35 
  36 /*
  37  * Each RawChannel corresponds to a TCP connection (SocketChannel) but is
  38  * connected to a Selector and an ExecutorService for invoking the send and
  39  * receive callbacks. Also includes SSL processing.
  40  */
  41 final class RawChannelImpl implements RawChannel {
  42 
  43     private final HttpClientImpl client;
  44     private final HttpConnection connection;
  45     private final Object         initialLock = new Object();
  46     private ByteBuffer           initial;
  47 
  48     RawChannelImpl(HttpClientImpl client,
  49                    HttpConnection connection,
  50                    ByteBuffer initial)
  51             throws IOException
  52     {
  53         this.client = client;
  54         this.connection = connection;
  55         SocketChannel chan = connection.channel();
  56         client.cancelRegistration(chan);
  57         // Constructing a RawChannel is supposed to have a "hand over"
  58         // semantics, in other words if construction fails, the channel won't be
  59         // needed by anyone, in which case someone still needs to close it
  60         try {
  61             chan.configureBlocking(false);
  62         } catch (IOException e) {
  63             try {
  64                 chan.close();
  65             } catch (IOException e1) {
  66                 e.addSuppressed(e1);
  67             }
  68             throw e;
  69         }
  70         // empty the initial buffer into our own copy.
  71         synchronized (initialLock) {
  72             this.initial = initial.hasRemaining()
  73                     ? Utils.copy(initial)
  74                     : Utils.EMPTY_BYTEBUFFER;
  75         }
  76     }
  77 
  78     private class NonBlockingRawAsyncEvent extends AsyncEvent {
  79 
  80         private final RawEvent re;
  81 
  82         NonBlockingRawAsyncEvent(RawEvent re) {
  83             super(0); // !BLOCKING & !REPEATING
  84             this.re = re;
  85         }
  86 
  87         @Override
  88         public SelectableChannel channel() {
  89             return connection.channel();
  90         }
  91 
  92         @Override
  93         public int interestOps() {
  94             return re.interestOps();
  95         }
  96 
  97         @Override
  98         public void handle() {
  99             re.handle();
 100         }
 101 
 102         @Override
 103         public void abort() { }
 104     }
 105 
 106     @Override
 107     public void registerEvent(RawEvent event) throws IOException {
 108         client.registerEvent(new NonBlockingRawAsyncEvent(event));
 109     }
 110 
 111     @Override
 112     public ByteBuffer read() throws IOException {
 113         assert !connection.channel().isBlocking();
 114         return connection.read();
 115     }
 116 
 117     @Override
 118     public ByteBuffer initialByteBuffer() {
 119         synchronized (initialLock) {
 120             if (initial == null) {
 121                 throw new IllegalStateException();
 122             }
 123             ByteBuffer ref = initial;
 124             initial = null;
 125             return ref;
 126         }
 127     }
 128 
 129     @Override
 130     public long write(ByteBuffer[] src, int offset, int len) throws IOException {
 131         return connection.write(src, offset, len);
 132     }
 133 
 134     @Override
 135     public void shutdownInput() throws IOException {
 136         connection.shutdownInput();
 137     }
 138 
 139     @Override
 140     public void shutdownOutput() throws IOException {
 141         connection.shutdownOutput();
 142     }
 143 
 144     @Override
 145     public void close() throws IOException {
 146         connection.close();
 147     }
 148 }