io.vertx.core.datagram.DatagramSocketOptions Java Examples

The following examples show how to use io.vertx.core.datagram.DatagramSocketOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VertxPeerDiscoveryAgent.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<InetSocketAddress> listenForConnections() {
  CompletableFuture<InetSocketAddress> future = new CompletableFuture<>();
  vertx
      .createDatagramSocket(new DatagramSocketOptions().setIpV6(NetworkUtility.isIPv6Available()))
      .listen(
          config.getBindPort(), config.getBindHost(), res -> handleListenerSetup(res, future));
  return future;
}
 
Example #2
Source File: VertxMetricsImpl.java    From vertx-micrometer-metrics with Apache License 2.0 5 votes vote down vote up
@Override
public DatagramSocketMetrics createDatagramSocketMetrics(DatagramSocketOptions options) {
  if (datagramSocketMetrics != null) {
    return datagramSocketMetrics;
  }
  return DummyVertxMetrics.DummyDatagramMetrics.INSTANCE;
}
 
Example #3
Source File: UDPMsgService.java    From jgossip with Apache License 2.0 5 votes vote down vote up
@Override
public void listen(String ipAddress, int port) {
    socket = Vertx.vertx().createDatagramSocket(new DatagramSocketOptions());
    socket.listen(port, ipAddress, asyncResult -> {
        if (asyncResult.succeeded()) {
            socket.handler(packet -> handleMsg(packet.data()));
        } else {
            LOGGER.error("Listen failed " + asyncResult.cause());
        }
    });
}
 
Example #4
Source File: MetricsTest.java    From vertx-dropwizard-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testMetricsCleanupedOnVertxClose() throws Exception {
  CountDownLatch latch1 = new CountDownLatch(1);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestHandler(req -> {});
  server.listen(onSuccess(res -> {
    latch1.countDown();
  }));
  awaitLatch(latch1);
  HttpClient client = vertx.createHttpClient(new HttpClientOptions());
  CountDownLatch latch2 = new CountDownLatch(1);
  NetServer nServer = vertx.createNetServer(new NetServerOptions().setPort(1234));
  nServer.connectHandler(conn -> {});
  nServer.listen(res -> {
    latch2.countDown();
  });
  awaitLatch(latch2);
  NetClient nClient = vertx.createNetClient(new NetClientOptions());
  DatagramSocket sock = vertx.createDatagramSocket(new DatagramSocketOptions());
  EventBus eb = vertx.eventBus();
  assertFalse(metricsService.getMetricsSnapshot(vertx).isEmpty());
  assertFalse(metricsService.getMetricsSnapshot(server).isEmpty());
  assertFalse(metricsService.getMetricsSnapshot(client).isEmpty());
  assertFalse(metricsService.getMetricsSnapshot(nServer).isEmpty());
  assertFalse(metricsService.getMetricsSnapshot(nClient).isEmpty());
  assertFalse(metricsService.getMetricsSnapshot(sock).isEmpty());
  assertFalse(metricsService.getMetricsSnapshot(eb).isEmpty());
  vertx.close(res -> {
    assertTrue(metricsService.getMetricsSnapshot(vertx).isEmpty());
    assertTrue(metricsService.getMetricsSnapshot(server).isEmpty());
    assertTrue(metricsService.getMetricsSnapshot(client).isEmpty());
    assertTrue(metricsService.getMetricsSnapshot(nServer).isEmpty());
    assertTrue(metricsService.getMetricsSnapshot(nClient).isEmpty());
    assertTrue(metricsService.getMetricsSnapshot(sock).isEmpty());
    assertTrue(metricsService.getMetricsSnapshot(eb).isEmpty());
    testComplete();
  });
  await();
  vertx = null;
}
 
Example #5
Source File: SfsVertxImpl.java    From sfs with Apache License 2.0 4 votes vote down vote up
@Override
public DatagramSocket createDatagramSocket(DatagramSocketOptions options) {
    return vertx.createDatagramSocket(options);
}
 
Example #6
Source File: VertxMetricsImpl.java    From vertx-dropwizard-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public DatagramSocketMetrics createDatagramSocketMetrics(DatagramSocketOptions options) {
  return new DatagramSocketMetricsImpl(this, nameOf("datagram"));
}
 
Example #7
Source File: MetricsTest.java    From vertx-dropwizard-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void testDatagramMetrics() throws Exception {
  Buffer clientMax = randomBuffer(1823);
  Buffer clientMin = randomBuffer(123);

  AtomicBoolean complete = new AtomicBoolean(false);
  DatagramSocket datagramSocket = vertx.createDatagramSocket(new DatagramSocketOptions()).listen(1236, "localhost", ar -> {
    assertTrue(ar.succeeded());
    DatagramSocket socket = ar.result();
    socket.handler(packet -> {
      if (complete.getAndSet(true)) {
        testComplete();
      }
    });
    socket.send(clientMin, 1236, "localhost", ds -> {
      assertTrue(ar.succeeded());
    });
    socket.send(clientMax, 1236, "localhost", ds -> {
      assertTrue(ar.succeeded());
    });
  });

  await();

  // Test sender/client (bytes-written)
  JsonObject metrics = metricsService.getMetricsSnapshot(datagramSocket);
  assertCount(metrics.getJsonObject("bytes-written"), 2L);
  assertMinMax(metrics.getJsonObject("bytes-written"), (long) clientMin.length(), (long) clientMax.length());

  // Test server (bytes-read)
  assertCount(metrics.getJsonObject("localhost:1236.bytes-read"), 2L);
  assertMinMax(metrics.getJsonObject("localhost:1236.bytes-read"), (long) clientMin.length(), (long) clientMax.length());

  CountDownLatch latch = new CountDownLatch(1);
  datagramSocket.close(ar -> {
    assertTrue(ar.succeeded());
    latch.countDown();
  });
  awaitLatch(latch);
  assertWaitUntil(() -> metricsService.getMetricsSnapshot(datagramSocket).isEmpty());
}