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