io.vertx.core.datagram.DatagramPacket Java Examples

The following examples show how to use io.vertx.core.datagram.DatagramPacket. 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: ScuttlebuttLocalDiscoveryService.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
void listen(DatagramPacket datagramPacket) {
  logger.debug("Received new packet from {}", datagramPacket.sender());
  Buffer buffer = datagramPacket.data();
  if (buffer.length() > 100) {
    logger.debug("Packet too long, disregard");
    return;
  }
  String packetString = buffer.toString();
  try {
    LocalIdentity id = LocalIdentity.fromString(packetString);
    for (Consumer<LocalIdentity> listener : listeners) {
      listener.accept(id);
    }
  } catch (IllegalArgumentException e) {
    logger.debug("Invalid identity payload {}", packetString);
  }
}
 
Example #2
Source File: ScuttlebuttLocalDiscoveryService.java    From cava with Apache License 2.0 6 votes vote down vote up
void listen(DatagramPacket datagramPacket) {
  logger.debug("Received new packet from {}", datagramPacket.sender());
  Buffer buffer = datagramPacket.data();
  if (buffer.length() > 100) {
    logger.debug("Packet too long, disregard");
    return;
  }
  String packetString = buffer.toString();
  try {
    LocalIdentity id = LocalIdentity.fromString(packetString);
    for (Consumer<LocalIdentity> listener : listeners) {
      listener.accept(id);
    }
  } catch (IllegalArgumentException e) {
    logger.debug("Invalid identity payload {}", packetString);
  }
}
 
Example #3
Source File: VertxPeerDiscoveryAgent.java    From besu with Apache License 2.0 5 votes vote down vote up
/**
 * The UDP packet handler. This is the entrypoint for all received datagrams.
 *
 * @param datagram the received datagram.
 */
private void handlePacket(final DatagramPacket datagram) {
  final int length = datagram.data().length();
  if (!validatePacketSize(length)) {
    LOG.debug("Discarding over-sized packet. Actual size (bytes): " + length);
    return;
  }
  vertx.<Packet>executeBlocking(
      future -> {
        try {
          future.complete(Packet.decode(datagram.data()));
        } catch (final Throwable t) {
          future.fail(t);
        }
      },
      event -> {
        if (event.succeeded()) {
          // Acquire the senders coordinates to build a Peer representation from them.
          final String host = datagram.sender().host();
          final int port = datagram.sender().port();
          final Endpoint endpoint = new Endpoint(host, port, OptionalInt.empty());
          handleIncomingPacket(endpoint, event.result());
        } else {
          if (event.cause() instanceof PeerDiscoveryPacketDecodingException) {
            LOG.debug("Discarding invalid peer discovery packet: {}", event.cause().getMessage());
          } else {
            LOG.error("Encountered error while handling packet", event.cause());
          }
        }
      });
}