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  */
  24 package java.net.http;
  25 
  26 import java.io.IOException;
  27 import java.nio.ByteBuffer;
  28 import java.nio.channels.ByteChannel;
  29 import java.nio.channels.GatheringByteChannel;
  30 import java.nio.channels.SelectableChannel;
  31 import java.nio.channels.SelectionKey;
  32 import java.nio.channels.SocketChannel;
  33 
  34 //
  35 // Used to implement WebSocket. Each RawChannel corresponds to a TCP connection
  36 // (SocketChannel) but is connected to a Selector and an ExecutorService for
  37 // invoking the send and receive callbacks. Also includes SSL processing.
  38 //
  39 final class RawChannel implements ByteChannel, GatheringByteChannel {
  40 
  41     private final HttpClientImpl client;
  42     private final HttpConnection connection;
  43 
  44     private interface RawEvent {
  45 
  46         /**
  47          * must return the selector interest op flags OR'd.
  48          */
  49         int interestOps();
  50 
  51         /**
  52          * called when event occurs.
  53          */
  54         void handle();
  55     }
  56 
  57     interface NonBlockingEvent extends RawEvent {
  58     }
  59 
  60     RawChannel(HttpClientImpl client, HttpConnection connection)
  61                                                 throws IOException {
  62         this.client = client;
  63         this.connection = connection;
  64         SocketChannel chan = connection.channel();
  65         client.cancelRegistration(chan);
  66         chan.configureBlocking(false);
  67     }
  68 
  69     SocketChannel socketChannel() {
  70         return connection.channel();
  71     }
  72 
  73     ByteBuffer getRemaining() {
  74         return connection.getRemaining();
  75     }
  76 
  77     private class RawAsyncEvent extends AsyncEvent {
  78 
  79         private final RawEvent re;
  80 
  81         RawAsyncEvent(RawEvent re) {
  82             super(AsyncEvent.BLOCKING); // BLOCKING & !REPEATING
  83             this.re = re;
  84         }
  85 
  86         RawAsyncEvent(RawEvent re, int flags) {
  87             super(flags);
  88             this.re = re;
  89         }
  90 
  91         @Override
  92         public SelectableChannel channel() {
  93             return connection.channel();
  94         }
  95 
  96         // must return the selector interest op flags OR'd
  97         @Override
  98         public int interestOps() {
  99             return re.interestOps();
 100         }
 101 
 102         // called when event occurs
 103         @Override
 104         public void handle() {
 105             re.handle();
 106         }
 107 
 108         @Override
 109         public void abort() { }
 110     }
 111 
 112     private class NonBlockingRawAsyncEvent extends RawAsyncEvent {
 113 
 114         NonBlockingRawAsyncEvent(RawEvent re) {
 115             super(re, 0); // !BLOCKING & !REPEATING
 116         }
 117     }
 118 
 119     /*
 120      * Register given event whose callback will be called once only.
 121      * (i.e. register new event for each callback)
 122      */
 123     public void registerEvent(RawEvent event) throws IOException {
 124         if (!(event instanceof NonBlockingEvent)) {
 125             throw new InternalError();
 126         }
 127         if ((event.interestOps() & SelectionKey.OP_READ) != 0
 128                 && connection.buffer.hasRemaining()) {
 129             // FIXME: a hack to deal with leftovers from previous reads into an
 130             // internal buffer (works in conjunction with change in
 131             // java.net.http.PlainHttpConnection.readImpl(java.nio.ByteBuffer)
 132             connection.channel().configureBlocking(false);
 133             event.handle();
 134         } else {
 135             client.registerEvent(new NonBlockingRawAsyncEvent(event));
 136         }
 137     }
 138 
 139     @Override
 140     public int read(ByteBuffer dst) throws IOException {
 141         assert !connection.channel().isBlocking();
 142         return connection.read(dst);
 143     }
 144 
 145     @Override
 146     public boolean isOpen() {
 147         return connection.isOpen();
 148     }
 149 
 150     @Override
 151     public void close() throws IOException {
 152         connection.close();
 153     }
 154 
 155     @Override
 156     public long write(ByteBuffer[] src) throws IOException {
 157         return connection.write(src, 0, src.length);
 158     }
 159 
 160     @Override
 161     public long write(ByteBuffer[] src, int offset, int len)
 162             throws IOException {
 163         return connection.write(src, offset, len);
 164     }
 165 
 166     @Override
 167     public int write(ByteBuffer src) throws IOException {
 168         return (int) connection.write(src);
 169     }
 170 }