Search in sources :

Example 21 with NetServer

use of io.vertx.core.net.NetServer in project vertx-micrometer-metrics by vert-x3.

the class VertxNetClientServerMetricsTest method setUp.

@Before
public void setUp(TestContext ctx) {
    vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MicrometerMetricsOptions().setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true)).setRegistryName(registryName).setEnabled(true))).exceptionHandler(ctx.exceptionHandler());
    // Setup server
    Async serverReady = ctx.async();
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start(Future<Void> future) throws Exception {
            netServer = vertx.createNetServer();
            netServer.connectHandler(socket -> socket.handler(buffer -> socket.write(SERVER_RESPONSE))).listen(9194, "localhost", r -> {
                if (r.failed()) {
                    ctx.fail(r.cause());
                } else {
                    serverReady.complete();
                }
            });
        }
    });
    serverReady.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) Future(io.vertx.core.Future) RegistryInspector.dp(io.vertx.micrometer.RegistryInspector.dp) List(java.util.List) NetServer(io.vertx.core.net.NetServer) ForkJoinPool(java.util.concurrent.ForkJoinPool) AbstractVerticle(io.vertx.core.AbstractVerticle) After(org.junit.After) NetClient(io.vertx.core.net.NetClient) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Async(io.vertx.ext.unit.Async) VertxOptions(io.vertx.core.VertxOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) Before(org.junit.Before)

Example 22 with NetServer

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

the class ConnectionBaseTest method testQueueFlushFromEventLoop.

@Test
public void testQueueFlushFromEventLoop() {
    NetClient client = vertx.createNetClient();
    NetServer server = vertx.createNetServer();
    try {
        server.connectHandler(so -> {
            ConnectionBase conn = (ConnectionBase) so;
            ChannelHandlerContext ctx = conn.channelHandlerContext();
            ChannelPipeline pipeline = ctx.pipeline();
            List<String> order = new ArrayList<>();
            Runnable checkOrder = () -> {
                if (order.size() == 3) {
                    vertx.runOnContext(v -> {
                        assertEquals(Arrays.asList("msg1", "msg2", "flush"), order);
                        testComplete();
                    });
                }
            };
            pipeline.addBefore("handler", "myhandler", new ChannelDuplexHandler() {

                @Override
                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                    if (msg instanceof String) {
                        String s = (String) msg;
                        order.add(s);
                        if ("msg1".equals(s)) {
                            // Flush a message why there are two messages queued on the connection
                            conn.flush();
                        }
                        checkOrder.run();
                    } else {
                        super.write(ctx, msg, promise);
                    }
                }

                @Override
                public void flush(ChannelHandlerContext ctx) throws Exception {
                    order.add("flush");
                    checkOrder.run();
                    super.flush(ctx);
                }
            });
            executeAsyncTask(() -> {
                conn.writeToChannel("msg1");
                conn.writeToChannel("msg2");
            });
        });
        server.listen(1234, "localhost", onSuccess(s -> {
            client.connect(1234, "localhost", onSuccess(so -> {
            }));
        }));
        await();
    } finally {
        server.close();
        client.close();
    }
}
Also used : ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Arrays(java.util.Arrays) Test(org.junit.Test) ChannelPipeline(io.netty.channel.ChannelPipeline) Context(io.vertx.core.Context) VertxTestBase(io.vertx.test.core.VertxTestBase) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) NetServer(io.vertx.core.net.NetServer) NetClient(io.vertx.core.net.NetClient) ArrayList(java.util.ArrayList) NetServer(io.vertx.core.net.NetServer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ChannelPipeline(io.netty.channel.ChannelPipeline) NetClient(io.vertx.core.net.NetClient) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Test(org.junit.Test)

Example 23 with NetServer

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

the class ConnectionBaseTest method testQueueMessageFromEvent.

@Test
public void testQueueMessageFromEvent() {
    NetClient client = vertx.createNetClient();
    NetServer server = vertx.createNetServer();
    try {
        server.connectHandler(so -> {
            NetSocketInternal conn = (NetSocketInternal) so;
            ChannelHandlerContext ctx = conn.channelHandlerContext();
            ChannelPipeline pipeline = ctx.pipeline();
            List<String> order = new ArrayList<>();
            pipeline.addBefore("handler", "myhandler", new ChannelDuplexHandler() {

                @Override
                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                    if (msg instanceof String) {
                        String s = (String) msg;
                        order.add(s);
                        if ("msg1".equals(s)) {
                            // Write a message why there are two messages queued on the connection
                            conn.writeMessage("msg3");
                        }
                        if (order.size() == 3) {
                            vertx.runOnContext(v -> {
                                assertEquals(Arrays.asList("msg1", "msg2", "msg3"), order);
                                testComplete();
                            });
                        }
                    } else {
                        super.write(ctx, msg, promise);
                    }
                }
            });
            executeAsyncTask(() -> {
                conn.writeMessage("msg1");
                conn.writeMessage("msg2");
            });
        });
        server.listen(1234, "localhost", onSuccess(s -> {
            client.connect(1234, "localhost", onSuccess(so -> {
            }));
        }));
        await();
    } finally {
        server.close();
        client.close();
    }
}
Also used : ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Arrays(java.util.Arrays) Test(org.junit.Test) ChannelPipeline(io.netty.channel.ChannelPipeline) Context(io.vertx.core.Context) VertxTestBase(io.vertx.test.core.VertxTestBase) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) NetServer(io.vertx.core.net.NetServer) NetClient(io.vertx.core.net.NetClient) ArrayList(java.util.ArrayList) NetServer(io.vertx.core.net.NetServer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ChannelPipeline(io.netty.channel.ChannelPipeline) NetClient(io.vertx.core.net.NetClient) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Test(org.junit.Test)

Example 24 with NetServer

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

the class StreamsExamples method pipe5.

public void pipe5(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.pipeTo(sock);
    }).listen();
}
Also used : AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) NetServerOptions(io.vertx.core.net.NetServerOptions) Pipe(io.vertx.core.streams.Pipe) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) FileSystem(io.vertx.core.file.FileSystem) ReadStream(io.vertx.core.streams.ReadStream) HttpServerOptions(io.vertx.core.http.HttpServerOptions) 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 25 with NetServer

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

the class StreamsExamples method pipe2.

public void pipe2(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 : AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) NetServerOptions(io.vertx.core.net.NetServerOptions) Pipe(io.vertx.core.streams.Pipe) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) FileSystem(io.vertx.core.file.FileSystem) ReadStream(io.vertx.core.streams.ReadStream) HttpServerOptions(io.vertx.core.http.HttpServerOptions) 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)

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