Search in sources :

Example 16 with DatagramSocketOptions

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

the class DatagramExamples method example7.

public void example7(Vertx vertx) {
    DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions());
    // Some code
    // This would block packets which are send from 10.0.0.2
    socket.blockMulticastGroup("230.0.0.1", "10.0.0.2", asyncResult -> {
        System.out.println("block succeeded? " + asyncResult.succeeded());
    });
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Example 17 with DatagramSocketOptions

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

the class DatagramExamples method example5.

public void example5(Vertx vertx) {
    DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions());
    socket.listen(1234, "0.0.0.0", asyncResult -> {
        if (asyncResult.succeeded()) {
            socket.handler(packet -> {
            });
            socket.listenMulticastGroup("230.0.0.1", asyncResult2 -> {
                System.out.println("Listen succeeded? " + asyncResult2.succeeded());
            });
        } else {
            System.out.println("Listen failed" + asyncResult.cause());
        }
    });
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Example 18 with DatagramSocketOptions

use of io.vertx.core.datagram.DatagramSocketOptions 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 19 with DatagramSocketOptions

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

the class DatagramTest method testOptions.

@Test
public void testOptions() {
    DatagramSocketOptions options = new DatagramSocketOptions();
    assertEquals(NetworkOptions.DEFAULT_SEND_BUFFER_SIZE, options.getSendBufferSize());
    int rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setSendBufferSize(rand));
    assertEquals(rand, options.getSendBufferSize());
    assertIllegalArgumentException(() -> options.setSendBufferSize(0));
    assertIllegalArgumentException(() -> options.setSendBufferSize(-123));
    assertEquals(NetworkOptions.DEFAULT_RECEIVE_BUFFER_SIZE, options.getReceiveBufferSize());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setReceiveBufferSize(rand));
    assertEquals(rand, options.getReceiveBufferSize());
    assertIllegalArgumentException(() -> options.setReceiveBufferSize(0));
    assertIllegalArgumentException(() -> options.setReceiveBufferSize(-123));
    assertFalse(options.isReuseAddress());
    assertEquals(options, options.setReuseAddress(true));
    assertTrue(options.isReuseAddress());
    assertEquals(NetworkOptions.DEFAULT_TRAFFIC_CLASS, options.getTrafficClass());
    rand = 23;
    assertEquals(options, options.setTrafficClass(rand));
    assertEquals(rand, options.getTrafficClass());
    assertIllegalArgumentException(() -> options.setTrafficClass(-2));
    assertIllegalArgumentException(() -> options.setTrafficClass(256));
    assertFalse(options.isBroadcast());
    assertEquals(options, options.setBroadcast(true));
    assertTrue(options.isBroadcast());
    assertTrue(options.isLoopbackModeDisabled());
    assertEquals(options, options.setLoopbackModeDisabled(false));
    assertFalse(options.isLoopbackModeDisabled());
    assertEquals(-1, options.getMulticastTimeToLive());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setMulticastTimeToLive(rand));
    assertEquals(rand, options.getMulticastTimeToLive());
    assertIllegalArgumentException(() -> options.setMulticastTimeToLive(-1));
    assertNull(options.getMulticastNetworkInterface());
    String randString = TestUtils.randomUnicodeString(100);
    assertEquals(options, options.setMulticastNetworkInterface(randString));
    assertEquals(randString, options.getMulticastNetworkInterface());
    assertFalse(options.isIpV6());
    assertEquals(options, options.setIpV6(true));
    assertTrue(options.isIpV6());
    testComplete();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Test(org.junit.Test)

Example 20 with DatagramSocketOptions

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

the class DatagramTest method testMulticastJoinLeave.

@Test
public void testMulticastJoinLeave() throws Exception {
    Buffer buffer = TestUtils.randomBuffer(128);
    String groupAddress = "230.0.0.1";
    String iface = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1")).getName();
    AtomicBoolean received = new AtomicBoolean();
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions().setMulticastNetworkInterface(iface));
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions().setMulticastNetworkInterface(iface));
    peer1.handler(packet -> {
        assertEquals(buffer, packet.data());
        received.set(true);
    });
    peer1.listen(1234, "0.0.0.0", ar1 -> {
        assertTrue(ar1.succeeded());
        peer1.listenMulticastGroup(groupAddress, iface, null, ar2 -> {
            assertTrue(ar2.succeeded());
            peer2.send(buffer, 1234, groupAddress, ar3 -> {
                assertTrue(ar3.succeeded());
                vertx.setTimer(1000, id -> {
                    peer1.unlistenMulticastGroup(groupAddress, iface, null, ar4 -> {
                        assertTrue(ar4.succeeded());
                        AtomicBoolean receivedAfter = new AtomicBoolean();
                        peer1.handler(packet -> {
                            receivedAfter.set(true);
                        });
                        peer2.send(buffer, 1234, groupAddress, ar5 -> {
                            assertTrue(ar5.succeeded());
                            vertx.setTimer(1000, id2 -> {
                                assertFalse(receivedAfter.get());
                                assertTrue(received.get());
                                testComplete();
                            });
                        });
                    });
                });
            });
        });
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

DatagramSocketOptions (io.vertx.core.datagram.DatagramSocketOptions)24 Test (org.junit.Test)18 Buffer (io.vertx.core.buffer.Buffer)10 DatagramSocket (io.vertx.core.datagram.DatagramSocket)8 TestLoggerFactory (io.vertx.test.netty.TestLoggerFactory)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 JsonObject (io.vertx.core.json.JsonObject)2 Random (java.util.Random)2 ByteBuf (io.netty.buffer.ByteBuf)1 io.vertx.core (io.vertx.core)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 DeploymentOptions (io.vertx.core.DeploymentOptions)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