use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class StreamsExamples method pipe3.
public void pipe3(Vertx vertx) {
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
server.connectHandler(sock -> {
sock.handler(buffer -> {
sock.write(buffer);
if (sock.writeQueueFull()) {
sock.pause();
}
});
}).listen();
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class StreamsExamples method pipe1.
public void pipe1(Vertx vertx) {
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
server.connectHandler(sock -> {
sock.handler(buffer -> {
// Write the data straight back
sock.write(buffer);
});
}).listen();
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class WebSocketTest method doTestClientWebSocketConnectionCloseOnBadResponse.
private void doTestClientWebSocketConnectionCloseOnBadResponse(boolean keepAliveInOptions) throws Throwable {
final Exception serverGotCloseException = new Exception();
netServer = vertx.createNetServer().connectHandler(sock -> {
final Buffer fullReq = Buffer.buffer(230);
sock.handler(b -> {
fullReq.appendBuffer(b);
String reqPart = b.toString();
if (fullReq.toString().contains("\r\n\r\n")) {
try {
String content = "0123456789";
content = content + content;
content = content + content + content + content + content;
String resp = "HTTP/1.1 200 OK\r\n";
if (keepAliveInOptions) {
resp += "Connection: close\r\n";
}
resp += "Content-Length: 100\r\n\r\n" + content;
sock.write(Buffer.buffer(resp.getBytes("ASCII")));
} catch (UnsupportedEncodingException e) {
addResult(e);
}
}
});
sock.closeHandler(v -> {
addResult(serverGotCloseException);
});
}).listen(ar -> {
if (ar.failed()) {
addResult(ar.cause());
return;
}
NetServer server = ar.result();
int port = server.actualPort();
HttpClientOptions opts = new HttpClientOptions().setKeepAlive(keepAliveInOptions);
client = vertx.createHttpClient(opts);
client.webSocket(port, "localhost", "/", ar2 -> {
if (ar2.succeeded()) {
addResult(new AssertionError("WebSocket unexpectedly connected"));
ar2.result().close();
} else {
addResult(ar2.cause());
}
});
});
boolean serverGotClose = false;
boolean clientGotCorrectException = false;
while (!serverGotClose || !clientGotCorrectException) {
Throwable result = resultQueue.poll(20, TimeUnit.SECONDS);
if (result == null) {
throw new AssertionError("Timed out waiting for expected state, current: serverGotClose = " + serverGotClose + ", clientGotCorrectException = " + clientGotCorrectException);
} else if (result == serverGotCloseException) {
serverGotClose = true;
} else if (result instanceof UpgradeRejectedException && ((UpgradeRejectedException) result).getStatus() == 200) {
clientGotCorrectException = true;
} else {
throw result;
}
}
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class HostnameResolutionTest method testResolveMissingLocalhost.
@Test
public void testResolveMissingLocalhost() throws Exception {
InetAddress localhost = InetAddress.getByName("localhost");
// Set a dns resolver that won't resolve localhost
dnsServer.testResolveASameServer("127.0.0.1");
// Test using the resolver API
VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false)));
CompletableFuture<Void> test1 = new CompletableFuture<>();
vertx.resolveAddress("localhost", ar -> {
if (ar.succeeded()) {
InetAddress resolved = ar.result();
if (resolved.equals(localhost)) {
test1.complete(null);
} else {
test1.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
}
} else {
test1.completeExceptionally(ar.cause());
}
});
test1.get(10, TimeUnit.SECONDS);
CompletableFuture<Void> test2 = new CompletableFuture<>();
vertx.resolveAddress("LOCALHOST", ar -> {
if (ar.succeeded()) {
InetAddress resolved = ar.result();
if (resolved.equals(localhost)) {
test2.complete(null);
} else {
test2.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
}
} else {
test2.completeExceptionally(ar.cause());
}
});
test2.get(10, TimeUnit.SECONDS);
// Test using bootstrap
CompletableFuture<Void> test3 = new CompletableFuture<>();
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost(localhost.getHostAddress()));
try {
server.connectHandler(so -> {
so.end(Buffer.buffer("hello"));
});
server.listen(ar -> {
if (ar.succeeded()) {
test3.complete(null);
} else {
test3.completeExceptionally(ar.cause());
}
});
test3.get(10, TimeUnit.SECONDS);
CompletableFuture<Void> test4 = new CompletableFuture<>();
NetClient client = vertx.createNetClient();
client.connect(1234, "localhost", ar -> {
if (ar.succeeded()) {
test4.complete(null);
} else {
test4.completeExceptionally(ar.cause());
}
});
test4.get(10, TimeUnit.SECONDS);
} finally {
server.close();
}
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class HostnameResolutionTest method testAsyncResolveConnectIsNotifiedOnChannelEventLoop.
@Test
public void testAsyncResolveConnectIsNotifiedOnChannelEventLoop() throws Exception {
CountDownLatch listenLatch = new CountDownLatch(1);
NetServer server = vertx.createNetServer().connectHandler(so -> {
});
try {
server.listen(1234, "localhost", onSuccess(v -> listenLatch.countDown()));
awaitLatch(listenLatch);
AtomicReference<Thread> channelThread = new AtomicReference<>();
CountDownLatch connectLatch = new CountDownLatch(1);
Bootstrap bootstrap = new Bootstrap();
bootstrap.channelFactory(((VertxInternal) vertx).transport().channelFactory(false));
bootstrap.group(vertx.nettyEventLoopGroup());
bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup());
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
channelThread.set(Thread.currentThread());
connectLatch.countDown();
}
});
ChannelFuture channelFut = bootstrap.connect("localhost", 1234);
awaitLatch(connectLatch);
channelFut.addListener(v -> {
assertTrue(v.isSuccess());
assertEquals(channelThread.get(), Thread.currentThread());
testComplete();
});
await();
} finally {
server.close();
}
}
Aggregations