/* * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
staticfinal String responses[] = { "Lorem ipsum dolor sit amet consectetur adipiscing elit,", "sed do eiusmod tempor quis nostrud exercitation ullamco laboris nisi ut", "aliquip ex ea commodo consequat."
};
@Override publicvoid onNext(List<ByteBuffer> item) { //if (result.isDone()) for (ByteBuffer b : item) { while (b.hasRemaining() && !result.isDone()) { int i = index.getAndIncrement(); char at = expected.charAt(i); byte[] data = newbyte[b.remaining()];
b.get(data); // we know that the server writes 1 char
String s = new String(data); char c = s.charAt(0); if (c != at) {
Throwable x = new IllegalStateException("char at "
+ i + " is '" + c + "' expected '"
+ at + "' for \"" + expected +"\"");
out.println("unexpected char received, cancelling");
subscription.cancel();
result.completeExceptionally(x); return;
}
}
} if (index.get() > 0 && !result.isDone()) { // we should complete the result here, but let's // see if we get something back...
out.println("Cancelling subscription after reading " + index.get());
cancelled.set(true);
subscription.cancel();
result.completeExceptionally(new CancelException()); return;
} if (!result.isDone()) {
out.println("requesting 1 more");
subscription.request(1);
}
}
// required for cleanup volatile MockServer.Connection conn;
// Sends the response, mostly, one byte at a time with a small delay // between bytes, to encourage that each byte is read in a separate read Thread sendSplitResponse(String s, MockServer server, AtomicBoolean cancelled) {
System.out.println("Sending: "); Thread t = newThread(() -> {
System.out.println("Waiting for server to receive headers");
conn = server.activity();
System.out.println("Start sending response"); int sent = 0; try { int len = s.length();
out.println("sending " + s); for (int i = 0; i < len; i++) {
String onechar = s.substring(i, i + 1);
conn.send(onechar);
sent++; Thread.sleep(10);
}
out.println("sent " + s);
} catch (SSLException | SocketException | RuntimeException x) { // if SSL then we might get a "Broken Pipe", or a // RuntimeException wrapping an InvalidAlgorithmParameterException // (probably if the channel is closed during the handshake), // otherwise we get a "Socket closed". boolean expected = cancelled.get(); if (sent > 0 && expected) {
System.out.println("Connection closed by peer as expected: " + x); return;
} else {
System.out.println("Unexpected exception (sent="
+ sent + ", cancelled=" + expected + "): " + x); if (x instanceof RuntimeException) throw (RuntimeException) x; thrownew RuntimeException(x);
}
} catch (IOException | InterruptedException e) { thrownew RuntimeException(e);
}
});
t.setDaemon(true);
t.start(); return t;
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.24Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.