use of io.vertx.core.Handler in project vert.x by eclipse.
the class WebsocketTest method testUpgrade.
private void testUpgrade(boolean delayed) {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT));
server.requestHandler(request -> {
Runnable runner = () -> {
ServerWebSocket ws = request.upgrade();
ws.handler(buff -> {
ws.write(Buffer.buffer("helloworld"));
ws.close();
});
};
if (delayed) {
vertx.runOnContext(v -> {
runner.run();
});
} else {
runner.run();
}
});
server.listen(ar -> {
assertTrue(ar.succeeded());
client.websocketStream(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null).handler(ws -> {
Buffer buff = Buffer.buffer();
ws.handler(b -> {
buff.appendBuffer(b);
});
ws.endHandler(v -> {
assertEquals("helloworld", buff.toString());
testComplete();
});
ws.write(Buffer.buffer("foo"));
});
});
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class VertxTest method testCloseHookFailure2.
@Test
public void testCloseHookFailure2() throws Exception {
AtomicInteger closedCount = new AtomicInteger();
class Hook implements Closeable {
@Override
public void close(Handler<AsyncResult<Void>> completionHandler) {
if (closedCount.incrementAndGet() == 1) {
completionHandler.handle(Future.succeededFuture());
throw new RuntimeException();
} else {
completionHandler.handle(Future.succeededFuture());
}
}
}
VertxInternal vertx = (VertxInternal) Vertx.vertx();
vertx.addCloseHook(new Hook());
vertx.addCloseHook(new Hook());
// Now undeploy
vertx.close(ar -> {
assertTrue(ar.succeeded());
assertEquals(2, closedCount.get());
testComplete();
});
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class SocksProxy method start.
/**
* Start the server.
*
* @param vertx
* Vertx instance to use for creating the server and client
* @param finishedHandler
* will be called when the start has started
*/
@Override
public void start(Vertx vertx, Handler<Void> finishedHandler) {
NetServerOptions options = new NetServerOptions();
options.setHost("localhost").setPort(PORT);
server = vertx.createNetServer(options);
server.connectHandler(socket -> {
socket.handler(buffer -> {
Buffer expectedInit = username == null ? clientInit : clientInitAuth;
if (!buffer.equals(expectedInit)) {
throw new IllegalStateException("expected " + toHex(expectedInit) + ", got " + toHex(buffer));
}
boolean useAuth = buffer.equals(clientInitAuth);
log.debug("got request: " + toHex(buffer));
final Handler<Buffer> handler = buffer2 -> {
if (!buffer2.getBuffer(0, clientRequest.length()).equals(clientRequest)) {
throw new IllegalStateException("expected " + toHex(clientRequest) + ", got " + toHex(buffer2));
}
int stringLen = buffer2.getUnsignedByte(4);
log.debug("string len " + stringLen);
if (buffer2.length() != 7 + stringLen) {
throw new IllegalStateException("format error in client request, got " + toHex(buffer2));
}
String host = buffer2.getString(5, 5 + stringLen);
int port = buffer2.getUnsignedShort(5 + stringLen);
log.debug("got request: " + toHex(buffer2));
log.debug("connect: " + host + ":" + port);
socket.handler(null);
lastUri = host + ":" + port;
if (forceUri != null) {
host = forceUri.substring(0, forceUri.indexOf(':'));
port = Integer.valueOf(forceUri.substring(forceUri.indexOf(':') + 1));
}
log.debug("connecting to " + host + ":" + port);
NetClient netClient = vertx.createNetClient(new NetClientOptions());
netClient.connect(port, host, result -> {
if (result.succeeded()) {
log.debug("writing: " + toHex(connectResponse));
socket.write(connectResponse);
log.debug("connected, starting pump");
NetSocket clientSocket = result.result();
socket.closeHandler(v -> clientSocket.close());
clientSocket.closeHandler(v -> socket.close());
Pump.pump(socket, clientSocket).start();
Pump.pump(clientSocket, socket).start();
} else {
log.error("exception", result.cause());
socket.handler(null);
log.debug("writing: " + toHex(errorResponse));
socket.write(errorResponse);
socket.close();
}
});
};
if (useAuth) {
socket.handler(buffer3 -> {
log.debug("auth handler");
log.debug("got request: " + toHex(buffer3));
Buffer authReply = Buffer.buffer(new byte[] { 1, (byte) username.length() });
authReply.appendString(username);
authReply.appendByte((byte) username.length());
authReply.appendString(username);
if (!buffer3.equals(authReply)) {
log.debug("expected " + toHex(authReply) + ", got " + toHex(buffer3));
socket.handler(null);
log.debug("writing: " + toHex(authFailed));
socket.write(authFailed);
socket.close();
} else {
socket.handler(handler);
log.debug("writing: " + toHex(authSuccess));
socket.write(authSuccess);
}
});
log.debug("writing: " + toHex(serverReplyAuth));
socket.write(serverReplyAuth);
} else {
socket.handler(handler);
log.debug("writing: " + toHex(serverReply));
socket.write(serverReply);
}
});
});
server.listen(result -> {
log.debug("socks5 server started");
finishedHandler.handle(null);
});
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class ProxyErrorTest method proxyTest.
private void proxyTest(int error, String username, String url, Handler<HttpClientResponse> assertResponse, boolean completeOnException) throws Exception {
startProxy(error, username);
final HttpClientOptions options = new HttpClientOptions().setSsl(url.startsWith("https")).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()));
HttpClient client = vertx.createHttpClient(options);
client.getAbs(url, assertResponse).exceptionHandler(e -> {
if (completeOnException) {
testComplete();
} else {
fail(e);
}
}).end();
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class RecordParserTest method doTestDelimited.
private void doTestDelimited(final Buffer input, Buffer delim, Integer[] chunkSizes, final Buffer... expected) {
final Buffer[] results = new Buffer[expected.length];
Handler<Buffer> out = new Handler<Buffer>() {
int pos;
public void handle(Buffer buff) {
results[pos++] = buff;
}
};
RecordParser parser = RecordParser.newDelimited(delim, out);
feedChunks(input, parser, chunkSizes);
checkResults(expected, results);
}
Aggregations