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());
});
}
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());
}
});
}
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();
}
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();
}
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();
}
Aggregations