Search in sources :

Example 1 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class HostnameResolutionTest method testNet.

private void testNet(String hostname) throws Exception {
    NetClient client = vertx.createNetClient();
    NetServer server = vertx.createNetServer().connectHandler(so -> {
        so.handler(buff -> {
            so.write(buff);
            so.close();
        });
    });
    try {
        CountDownLatch listenLatch = new CountDownLatch(1);
        server.listen(1234, hostname, onSuccess(s -> {
            listenLatch.countDown();
        }));
        awaitLatch(listenLatch);
        client.connect(1234, hostname, onSuccess(so -> {
            Buffer buffer = Buffer.buffer();
            so.handler(buffer::appendBuffer);
            so.closeHandler(v -> {
                assertEquals(Buffer.buffer("foo"), buffer);
                testComplete();
            });
            so.write(Buffer.buffer("foo"));
        }));
        await();
    } finally {
        client.close();
        server.close();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) Buffer(io.vertx.core.buffer.Buffer) NetClient(io.vertx.core.net.NetClient) NetServer(io.vertx.core.net.NetServer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with NetServer

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 s = vertx.createNetServer().connectHandler(so -> {
    });
    s.listen(1234, "localhost", onSuccess(v -> listenLatch.countDown()));
    awaitLatch(listenLatch);
    AtomicReference<Thread> channelThread = new AtomicReference<>();
    CountDownLatch connectLatch = new CountDownLatch(1);
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(NioSocketChannel.class);
    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();
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) NetServer(io.vertx.core.net.NetServer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) VertxException(io.vertx.core.VertxException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 3 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class StreamsExamples method pump2.

public void pump2(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            if (!sock.writeQueueFull()) {
                sock.write(buffer);
            }
        });
    }).listen();
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Vertx(io.vertx.core.Vertx) Pump(io.vertx.core.streams.Pump) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer)

Example 4 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class StreamsExamples method pump1.

public void pump1(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            sock.write(buffer);
        });
    }).listen();
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Vertx(io.vertx.core.Vertx) Pump(io.vertx.core.streams.Pump) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer)

Example 5 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class DummyMetricsTest method testDummyNetServerMetrics.

@Test
public void testDummyNetServerMetrics() {
    NetServer server = vertx.createNetServer(new NetServerOptions());
    assertFalse(server.isMetricsEnabled());
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer) Test(org.junit.Test)

Aggregations

NetServer (io.vertx.core.net.NetServer)28 NetServerOptions (io.vertx.core.net.NetServerOptions)20 Buffer (io.vertx.core.buffer.Buffer)16 Vertx (io.vertx.core.Vertx)15 Test (org.junit.Test)15 NetSocket (io.vertx.core.net.NetSocket)13 Handler (io.vertx.core.Handler)12 NetClient (io.vertx.core.net.NetClient)11 VertxOptions (io.vertx.core.VertxOptions)10 Pump (io.vertx.core.streams.Pump)10 HttpServer (io.vertx.core.http.HttpServer)9 ReadStream (io.vertx.core.streams.ReadStream)7 InetSocketAddress (java.net.InetSocketAddress)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeUnit (java.util.concurrent.TimeUnit)7 TestUtils (io.vertx.test.core.TestUtils)6 File (java.io.File)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AsyncFile (io.vertx.core.file.AsyncFile)5 FileSystem (io.vertx.core.file.FileSystem)5