Search in sources :

Example 1 with DatagramSocket

use of io.vertx.core.datagram.DatagramSocket in project vert.x by eclipse.

the class DummyMetricsTest method testDummyDatagramSocketMetrics.

@Test
public void testDummyDatagramSocketMetrics() {
    DatagramSocket sock = vertx.createDatagramSocket(new DatagramSocketOptions());
    assertFalse(sock.isMetricsEnabled());
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) DatagramSocket(io.vertx.core.datagram.DatagramSocket) Test(org.junit.Test)

Example 2 with DatagramSocket

use of io.vertx.core.datagram.DatagramSocket in project vert.x by eclipse.

the class DatagramExamples method example4.

public void example4(Vertx vertx) {
    DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions());
    Buffer buffer = Buffer.buffer("content");
    // Send a Buffer to a multicast address
    socket.send(buffer, 1234, "230.0.0.1", asyncResult -> {
        System.out.println("Send succeeded? " + asyncResult.succeeded());
    });
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Example 3 with DatagramSocket

use of io.vertx.core.datagram.DatagramSocket in project vert.x by eclipse.

the class MetricsTest method testDatagram.

private void testDatagram(String host, Consumer<PacketMetric> checker) throws Exception {
    DatagramSocket peer1 = vertx.createDatagramSocket();
    DatagramSocket peer2 = vertx.createDatagramSocket();
    CountDownLatch latch = new CountDownLatch(1);
    peer1.handler(packet -> {
        FakeDatagramSocketMetrics peer1Metrics = FakeMetricsBase.getMetrics(peer1);
        FakeDatagramSocketMetrics peer2Metrics = FakeMetricsBase.getMetrics(peer2);
        assertEquals(host, peer1Metrics.getLocalName());
        assertEquals("127.0.0.1", peer1Metrics.getLocalAddress().host());
        assertNull(peer2Metrics.getLocalAddress());
        assertEquals(1, peer1Metrics.getReads().size());
        PacketMetric read = peer1Metrics.getReads().get(0);
        assertEquals(5, read.numberOfBytes);
        assertEquals(0, peer1Metrics.getWrites().size());
        assertEquals(0, peer2Metrics.getReads().size());
        assertEquals(1, peer2Metrics.getWrites().size());
        checker.accept(peer2Metrics.getWrites().get(0));
        testComplete();
    });
    peer1.listen(1234, host, ar -> {
        assertTrue(ar.succeeded());
        latch.countDown();
    });
    awaitLatch(latch);
    peer2.send("hello", 1234, host, ar -> {
        assertTrue(ar.succeeded());
    });
    await();
}
Also used : DatagramSocket(io.vertx.core.datagram.DatagramSocket) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with DatagramSocket

use of io.vertx.core.datagram.DatagramSocket in project vert.x by eclipse.

the class MetricsContextTest method testDatagram.

private void testDatagram(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) {
    AtomicReference<Thread> expectedThread = new AtomicReference<>();
    AtomicReference<Context> expectedContext = new AtomicReference<>();
    AtomicBoolean listening = new AtomicBoolean();
    AtomicBoolean bytesReadCalled = new AtomicBoolean();
    AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
    AtomicBoolean closeCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public DatagramSocketMetrics createMetrics(DatagramSocket socket, DatagramSocketOptions options) {
            return new DummyDatagramMetrics() {

                @Override
                public void listening(String localName, SocketAddress localAddress) {
                    listening.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesReadCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesWrittenCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void close() {
                    closeCalled.set(true);
                }

                @Override
                public boolean isEnabled() {
                    return true;
                }
            };
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    Context ctx = contextFactory.apply(vertx);
    ctx.runOnContext(v1 -> {
        expectedThread.set(Thread.currentThread());
        expectedContext.set(Vertx.currentContext());
        DatagramSocket socket = vertx.createDatagramSocket();
        socket.listen(1234, "localhost", ar1 -> {
            assertTrue(ar1.succeeded());
            checker.accept(expectedThread.get(), expectedContext.get());
            socket.handler(packet -> {
                assertTrue(listening.get());
                assertTrue(bytesReadCalled.get());
                assertTrue(bytesWrittenCalled.get());
                executeInVanillaThread(() -> {
                    socket.close(ar2 -> {
                        assertTrue(closeCalled.get());
                        assertTrue(ar2.succeeded());
                        testComplete();
                    });
                });
            });
            socket.send(Buffer.buffer("msg"), 1234, "localhost", ar2 -> {
                assertTrue(ar2.succeeded());
            });
        });
    });
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Example 5 with DatagramSocket

use of io.vertx.core.datagram.DatagramSocket in project vert.x by eclipse.

the class DatagramExamples method example2.

public void example2(Vertx vertx) {
    DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions());
    Buffer buffer = Buffer.buffer("content");
    // Send a Buffer
    socket.send(buffer, 1234, "10.0.0.1", asyncResult -> {
        System.out.println("Send succeeded? " + asyncResult.succeeded());
    });
    // Send a String
    socket.send("A string used as content", 1234, "10.0.0.1", asyncResult -> {
        System.out.println("Send succeeded? " + asyncResult.succeeded());
    });
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Aggregations

DatagramSocket (io.vertx.core.datagram.DatagramSocket)9 DatagramSocketOptions (io.vertx.core.datagram.DatagramSocketOptions)8 Buffer (io.vertx.core.buffer.Buffer)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 io.vertx.core (io.vertx.core)1 EventBus (io.vertx.core.eventbus.EventBus)1 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)1 io.vertx.core.http (io.vertx.core.http)1 MetricsOptions (io.vertx.core.metrics.MetricsOptions)1 DummyVertxMetrics (io.vertx.core.metrics.impl.DummyVertxMetrics)1 io.vertx.core.net (io.vertx.core.net)1 VertxMetricsFactory (io.vertx.core.spi.VertxMetricsFactory)1 io.vertx.core.spi.metrics (io.vertx.core.spi.metrics)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 Ignore (org.junit.Ignore)1