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 } | 1 /* 2 * Copyright (c) 2015, 2017, 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 import java.util.function.Supplier; 36 37 /* 38 * Each RawChannel corresponds to a TCP connection (SocketChannel) but is 39 * connected to a Selector and an ExecutorService for invoking the send and 40 * receive callbacks. Also includes SSL processing. 41 */ 42 final class RawChannelImpl implements RawChannel { 43 44 private final HttpClientImpl client; 45 private final HttpConnection.DetachedConnectionChannel detachedChannel; 46 private final Object initialLock = new Object(); 47 private Supplier<ByteBuffer> initial; 48 49 RawChannelImpl(HttpClientImpl client, 50 HttpConnection connection, 51 Supplier<ByteBuffer> initial) 52 throws IOException 53 { 54 this.client = client; 55 this.detachedChannel = connection.detachChannel(); 56 this.initial = initial; 57 58 SocketChannel chan = connection.channel(); 59 client.cancelRegistration(chan); 60 // Constructing a RawChannel is supposed to have a "hand over" 61 // semantics, in other words if construction fails, the channel won't be 62 // needed by anyone, in which case someone still needs to close it 63 try { 64 chan.configureBlocking(false); 65 } catch (IOException e) { 66 try { 67 chan.close(); 68 } catch (IOException e1) { 69 e.addSuppressed(e1); 70 } finally { 71 detachedChannel.close(); 72 } 73 throw e; 74 } 75 } 76 77 private class NonBlockingRawAsyncEvent extends AsyncEvent { 78 79 private final RawEvent re; 80 81 NonBlockingRawAsyncEvent(RawEvent re) { 82 // !BLOCKING & !REPEATING 83 this.re = re; 84 } 85 86 @Override 87 public SelectableChannel channel() { 88 return detachedChannel.channel(); 89 } 90 91 @Override 92 public int interestOps() { 93 return re.interestOps(); 94 } 95 96 @Override 97 public void handle() { 98 re.handle(); 99 } 100 101 @Override 102 public void abort(IOException ioe) { } 103 } 104 105 @Override 106 public void registerEvent(RawEvent event) throws IOException { 107 client.registerEvent(new NonBlockingRawAsyncEvent(event)); 108 } 109 110 @Override 111 public ByteBuffer read() throws IOException { 112 assert !detachedChannel.channel().isBlocking(); 113 // connection.read() will no longer be available. 114 return detachedChannel.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.get(); 124 ref = ref.hasRemaining() ? Utils.copy(ref) 125 : Utils.EMPTY_BYTEBUFFER; 126 initial = null; 127 return ref; 128 } 129 } 130 131 @Override 132 public long write(ByteBuffer[] src, int offset, int len) throws IOException { 133 // this makes the whitebox driver test fail. 134 return detachedChannel.write(src, offset, len); 135 } 136 137 @Override 138 public void shutdownInput() throws IOException { 139 detachedChannel.shutdownInput(); 140 } 141 142 @Override 143 public void shutdownOutput() throws IOException { 144 detachedChannel.shutdownOutput(); 145 } 146 147 @Override 148 public void close() throws IOException { 149 detachedChannel.close(); 150 } 151 152 @Override 153 public String toString() { 154 return super.toString()+"("+ detachedChannel.toString() + ")"; 155 } 156 157 158 } |