1 /* |
1 /* |
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
40 import java.util.HashMap; |
40 import java.util.HashMap; |
41 import java.util.Iterator; |
41 import java.util.Iterator; |
42 import java.util.LinkedList; |
42 import java.util.LinkedList; |
43 import java.util.List; |
43 import java.util.List; |
44 import java.util.Map; |
44 import java.util.Map; |
|
45 import java.util.concurrent.CountDownLatch; |
45 import java.util.concurrent.atomic.AtomicBoolean; |
46 import java.util.concurrent.atomic.AtomicBoolean; |
46 import java.util.function.Function; |
47 import java.util.function.Function; |
47 import java.util.regex.Pattern; |
48 import java.util.regex.Pattern; |
48 import java.util.stream.Collectors; |
49 import java.util.stream.Collectors; |
49 |
50 |
84 |
85 |
85 private final AtomicBoolean started = new AtomicBoolean(); |
86 private final AtomicBoolean started = new AtomicBoolean(); |
86 private final Thread thread; |
87 private final Thread thread; |
87 private volatile ServerSocketChannel ssc; |
88 private volatile ServerSocketChannel ssc; |
88 private volatile InetSocketAddress address; |
89 private volatile InetSocketAddress address; |
|
90 private ByteBuffer read = ByteBuffer.allocate(1024); |
|
91 private final CountDownLatch readReady = new CountDownLatch(1); |
89 |
92 |
90 public DummyWebSocketServer() { |
93 public DummyWebSocketServer() { |
91 this(defaultMapping()); |
94 this(defaultMapping()); |
92 } |
95 } |
93 |
96 |
112 } catch (IOException e) { |
115 } catch (IOException e) { |
113 err.println("Error in connection: " + channel + ", " + e); |
116 err.println("Error in connection: " + channel + ", " + e); |
114 } finally { |
117 } finally { |
115 err.println("Closed: " + channel); |
118 err.println("Closed: " + channel); |
116 close(channel); |
119 close(channel); |
|
120 readReady.countDown(); |
117 } |
121 } |
118 } |
122 } |
119 } catch (ClosedByInterruptException ignored) { |
123 } catch (ClosedByInterruptException ignored) { |
120 } catch (IOException e) { |
124 } catch (IOException e) { |
121 err.println(e); |
125 err.println(e); |
131 protected void serve(SocketChannel channel) throws IOException { |
135 protected void serve(SocketChannel channel) throws IOException { |
132 // Read until the thread is interrupted or an error occurred |
136 // Read until the thread is interrupted or an error occurred |
133 // or the input is shutdown |
137 // or the input is shutdown |
134 ByteBuffer b = ByteBuffer.allocate(1024); |
138 ByteBuffer b = ByteBuffer.allocate(1024); |
135 while (channel.read(b) != -1) { |
139 while (channel.read(b) != -1) { |
|
140 b.flip(); |
|
141 if (read.remaining() < b.remaining()) { |
|
142 int required = read.capacity() - read.remaining() + b.remaining(); |
|
143 int log2required = 32 - Integer.numberOfLeadingZeros(required - 1); |
|
144 ByteBuffer newBuffer = ByteBuffer.allocate(1 << log2required); |
|
145 newBuffer.put(read.flip()); |
|
146 read = newBuffer; |
|
147 } |
|
148 read.put(b); |
136 b.clear(); |
149 b.clear(); |
137 } |
150 } |
|
151 ByteBuffer close = ByteBuffer.wrap(new byte[]{(byte) 0x88, 0x00}); |
|
152 while (close.hasRemaining()) { |
|
153 channel.write(close); |
|
154 } |
|
155 } |
|
156 |
|
157 public ByteBuffer read() throws InterruptedException { |
|
158 readReady.await(); |
|
159 return read.duplicate().asReadOnlyBuffer().flip(); |
138 } |
160 } |
139 |
161 |
140 public void open() throws IOException { |
162 public void open() throws IOException { |
141 err.println("Starting"); |
163 err.println("Starting"); |
142 if (!started.compareAndSet(false, true)) { |
164 if (!started.compareAndSet(false, true)) { |