Search in sources :

Example 11 with DatagramSocketOptions

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

the class DatagramTest method testSendReceive.

@Test
public void testSendReceive() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        assertTrue(ar.succeeded());
        Buffer buffer = TestUtils.randomBuffer(128);
        peer2.handler(packet -> {
            Buffer data = packet.data();
            ByteBuf buff = data.getByteBuf();
            while (buff != buff.unwrap() && buff.unwrap() != null) {
                buff = buff.unwrap();
            }
            assertTrue("Was expecting an unpooled buffer instead of " + buff.getClass().getSimpleName(), buff.getClass().getSimpleName().contains("Unpooled"));
            assertEquals(buffer, data);
            testComplete();
        });
        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 12 with DatagramSocketOptions

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

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

Example 14 with DatagramSocketOptions

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

the class DatagramExamples method example3.

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

Example 15 with DatagramSocketOptions

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

the class DatagramExamples method example6.

public void example6(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 -> {
                if (asyncResult2.succeeded()) {
                    socket.unlistenMulticastGroup("230.0.0.1", asyncResult3 -> {
                        System.out.println("Unlisten succeeded? " + asyncResult3.succeeded());
                    });
                } else {
                    System.out.println("Listen failed" + asyncResult2.cause());
                }
            });
        } else {
            System.out.println("Listen failed" + asyncResult.cause());
        }
    });
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

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