io.vertx.core.datagram.DatagramSocket Java Examples

The following examples show how to use io.vertx.core.datagram.DatagramSocket. 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
protected void handleListenerSetup(
    final AsyncResult<DatagramSocket> listenResult,
    final CompletableFuture<InetSocketAddress> addressFuture) {
  if (listenResult.failed()) {
    Throwable cause = listenResult.cause();
    LOG.error("An exception occurred when starting the peer discovery agent", cause);

    if (cause instanceof BindException || cause instanceof SocketException) {
      cause =
          new PeerDiscoveryServiceException(
              String.format(
                  "Failed to bind Ethereum UDP discovery listener to %s:%d: %s",
                  config.getBindHost(), config.getBindPort(), cause.getMessage()));
    }
    addressFuture.completeExceptionally(cause);
    return;
  }

  this.socket = listenResult.result();

  // TODO: when using wildcard hosts (0.0.0.0), we need to handle multiple addresses by
  // selecting
  // the correct 'announce' address.
  final String effectiveHost = socket.localAddress().host();
  final int effectivePort = socket.localAddress().port();

  LOG.info(
      "Started peer discovery agent successfully, on effective host={} and port={}",
      effectiveHost,
      effectivePort);

  socket.exceptionHandler(this::handleException);
  socket.handler(this::handlePacket);

  InetSocketAddress address =
      new InetSocketAddress(socket.localAddress().host(), socket.localAddress().port());
  addressFuture.complete(address);
}
 
Example #2
Source File: VertxDatagramSocketMetricsTest.java    From vertx-micrometer-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReportDatagramMetrics(TestContext context) throws InterruptedException {
  vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MicrometerMetricsOptions()
      .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true))
    .addLabels(Label.LOCAL)
    .setEnabled(true)))
    .exceptionHandler(context.exceptionHandler());

  String datagramContent = "some text";
  int loops = 5;

  // Setup server
  int port = 9192;
  String host = "localhost";
  Async receiveLatch = context.async(loops);
  Async listenLatch = context.async();
  vertx.createDatagramSocket().listen(port, host, context.asyncAssertSuccess(so -> {
    so.handler(packet -> receiveLatch.countDown());
    listenLatch.complete();
  }));
  listenLatch.awaitSuccess(15000);

  // Send to server
  DatagramSocket client = vertx.createDatagramSocket();
  for (int i = 0; i < loops; i++) {
    client.send(datagramContent, port, host, context.asyncAssertSuccess());
  }
  receiveLatch.awaitSuccess(15000);

  waitForValue(vertx, context, "vertx.datagram.bytesSent[]$COUNT", value -> value.intValue() == 5);
  List<RegistryInspector.Datapoint> datapoints = listDatapoints(startsWith("vertx.datagram."));
  assertThat(datapoints).containsOnly(
    dp("vertx.datagram.bytesSent[]$COUNT", 5),
    dp("vertx.datagram.bytesSent[]$TOTAL", 45),  // 45 = size("some text") * loops
    dp("vertx.datagram.bytesReceived[local=localhost:9192]$COUNT", 5),
    dp("vertx.datagram.bytesReceived[local=localhost:9192]$TOTAL", 45));
}
 
Example #3
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 #4
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 #5
Source File: SfsVertxImpl.java    From sfs with Apache License 2.0 4 votes vote down vote up
@Override
public DatagramSocket createDatagramSocket() {
    return vertx.createDatagramSocket();
}
 
Example #6
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());
}