Java Code Examples for io.vertx.proton.ProtonConnection#receiverOpenHandler()

The following examples show how to use io.vertx.proton.ProtonConnection#receiverOpenHandler() . 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: AmqpServiceBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Sets default handlers on a connection that has been opened
 * by a peer.
 * <p>
 * This method registers the following handlers
 * <ul>
 * <li>sessionOpenHandler - {@link #handleSessionOpen(ProtonConnection, ProtonSession)}</li>
 * <li>receiverOpenHandler - {@link #handleReceiverOpen(ProtonConnection, ProtonReceiver)}</li>
 * <li>senderOpenHandler - {@link #handleSenderOpen(ProtonConnection, ProtonSender)}</li>
 * <li>disconnectHandler - {@link #handleRemoteDisconnect(ProtonConnection)}</li>
 * <li>closeHandler - {@link #handleRemoteConnectionClose(ProtonConnection, AsyncResult)}</li>
 * <li>openHandler - {@link #processRemoteOpen(ProtonConnection)}</li>
 * </ul>
 * <p>
 * Subclasses should override this method in order to register service
 * specific handlers and/or to prevent registration of default handlers.
 *
 * @param connection The connection.
 */
protected void setRemoteConnectionOpenHandler(final ProtonConnection connection) {

    log.debug("received connection request from client");
    connection.sessionOpenHandler(session -> {
        HonoProtonHelper.setDefaultCloseHandler(session);
        handleSessionOpen(connection, session);
    });
    connection.receiverOpenHandler(receiver -> {
        HonoProtonHelper.setDefaultCloseHandler(receiver);
        handleReceiverOpen(connection, receiver);
    });
    connection.senderOpenHandler(sender -> {
        HonoProtonHelper.setDefaultCloseHandler(sender);
        handleSenderOpen(connection, sender);
    });
    connection.disconnectHandler(this::handleRemoteDisconnect);
    connection.closeHandler(remoteClose -> handleRemoteConnectionClose(connection, remoteClose));
    connection.openHandler(remoteOpen -> {
        if (remoteOpen.failed()) {
            log.debug("ignoring peer's open frame containing error", remoteOpen.cause());
        } else {
            processRemoteOpen(remoteOpen.result());
        }
    });
}
 
Example 2
Source File: PubSubBroker.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private void connectHandler(ProtonConnection connection) {
    connection.setContainer(containerId);
    connection.openHandler(conn -> {
        log.info("[{}]: Connection opened", containerId);
    }).closeHandler(conn -> {
        connection.close();
        connection.disconnect();
        log.info("[{}]: Connection closed", containerId);
    }).disconnectHandler(protonConnection -> {
        connection.disconnect();
        log.debug("Disconnected");
    }).open();

    connection.sessionOpenHandler(ProtonSession::open);
    connection.senderOpenHandler(sender -> senderOpenHandler(connection, sender));
    connection.receiverOpenHandler(receiver -> receiverOpenHandler(connection, receiver));
}
 
Example 3
Source File: VertxBasedAmqpProtocolAdapter.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Handles a remote peer's request to open a connection.
 *
 * @param con The connection to be opened.
 */
protected void onConnectRequest(final ProtonConnection con) {

    con.disconnectHandler(lostConnection -> {
        log.debug("lost connection to device [container: {}]", con.getRemoteContainer());
        onConnectionLoss(con);
        decrementConnectionCount(con);
    });
    con.closeHandler(remoteClose -> {
        handleRemoteConnectionClose(con, remoteClose);
        onConnectionLoss(con);
        decrementConnectionCount(con);
    });

    // when a begin frame is received
    con.sessionOpenHandler(session -> {
        HonoProtonHelper.setDefaultCloseHandler(session);
        handleSessionOpen(con, session);
    });
    // when the device wants to open a link for
    // uploading messages
    con.receiverOpenHandler(receiver -> {
        HonoProtonHelper.setDefaultCloseHandler(receiver);
        receiver.setMaxMessageSize(UnsignedLong.valueOf(getConfig().getMaxPayloadSize()));
        handleRemoteReceiverOpen(con, receiver);
    });
    // when the device wants to open a link for
    // receiving commands
    con.senderOpenHandler(sender -> {
        handleRemoteSenderOpenForCommands(con, sender);
    });
    con.openHandler(remoteOpen -> {
        final Device authenticatedDevice = getAuthenticatedDevice(con);
        if (authenticatedDevice == null) {
            metrics.incrementUnauthenticatedConnections();
        } else {
            metrics.incrementConnections(authenticatedDevice.getTenantId());
        }

        if (remoteOpen.failed()) {
            log.debug("ignoring device's open frame containing error", remoteOpen.cause());
        } else {
            processRemoteOpen(remoteOpen.result());
        }
    });
}
 
Example 4
Source File: HelloWorldServer.java    From vertx-proton with Apache License 2.0 4 votes vote down vote up
private static void helloProcessConnection(Vertx vertx, ProtonConnection connection) {
  connection.openHandler(res -> {
    System.out.println("Client connected: " + connection.getRemoteContainer());
    connection.open();
  }).closeHandler(c -> {
    System.out.println("Client closing amqp connection: " + connection.getRemoteContainer());
    connection.close();
    connection.disconnect();
  }).disconnectHandler(c -> {
    System.out.println("Client socket disconnected: " + connection.getRemoteContainer());
    connection.disconnect();
  })
  .sessionOpenHandler(session -> session.open());

  connection.receiverOpenHandler(receiver -> {
    receiver.setTarget(receiver.getRemoteTarget()) // This is rather naive, for example use only, proper
                                                   // servers should ensure that they advertise their own
                                                   // Target settings that actually reflect what is in place.
                                                   // The request may have also been for a dynamic address.
        .handler((delivery, msg) -> {

          String address = msg.getAddress();
          if (address == null) {
            address = receiver.getRemoteTarget().getAddress();
          }

          Section body = msg.getBody();
          if (body instanceof AmqpValue) {
            String content = (String) ((AmqpValue) body).getValue();
            System.out.println("message to:" + address + ", body: " + content);
          }
        }).open();
  });

  connection.senderOpenHandler(sender -> {
    System.out.println("Sending to client from: " + sender.getRemoteSource().getAddress());
    sender.setSource(sender.getRemoteSource()); // This is rather naive, for example use only, proper
                                                // servers should ensure that they advertise their own
                                                // Source settings that actually reflect what is in place.
                                                // The request may have also been for a dynamic address.
    sender.open();
    vertx.setPeriodic(1000, timer -> {
      if (connection.isDisconnected()) {
        vertx.cancelTimer(timer);
      } else {
        System.out.println("Sending message to client");
        Message m = message("Hello World from Server!");
        sender.send(m, delivery -> {
          System.out.println("The message was received by the client.");
        });
      }
    });
  });

}