/** * Creates a command consumer for a specific device. * * @param tenantId The tenant of the command receiver. * @param deviceId The device of the command receiver. * @param commandConsumer The handler to invoke for each command destined to the device. * @param closeHandler Called when the peer detaches the link. * @return Result of the receiver creation. */ protected final Future<MessageConsumer> createCommandConsumer( final String tenantId, final String deviceId, final Handler<CommandContext> commandConsumer, final Handler<Void> closeHandler) { return commandConnection.createCommandConsumer( tenantId, deviceId, commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); commandConsumer.handle(commandContext); }, closeHandler); }
/** * Creates a command consumer for a specific device. * * @param tenantId The tenant of the command receiver. * @param deviceId The device of the command receiver. * @param commandConsumer The handler to invoke for each command destined to the device. * @param closeHandler Called when the peer detaches the link. * @return Result of the receiver creation. */ protected final Future<MessageConsumer> createCommandConsumer( final String tenantId, final String deviceId, final Handler<CommandContext> commandConsumer, final Handler<Void> closeHandler) { return commandConnection.createCommandConsumer( tenantId, deviceId, commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); commandConsumer.handle(commandContext); }, closeHandler); }
private long startTimer(final Integer msgId) { LOG.trace("Start a timer for [{}] ms", config.getCommandAckTimeout()); return vertx.setTimer(config.getCommandAckTimeout(), timerId -> { LOG.trace("Timer [{}] expired", timerId); Optional.ofNullable(removeFromWaitingForAcknowledgement(msgId)).ifPresent(value -> { final CommandSubscription subscription = value.two(); final CommandContext commandContext = value.three(); LOG.debug( "Timed out waiting for acknowledgment for command sent to device [tenant-id: {}, device-id: {}, MQTT client-id: {}, QoS: {}]", subscription.getTenant(), subscription.getDeviceId(), subscription.getClientId(), subscription.getQos()); final Map<String, String> items = new HashMap<>(3); items.put(Fields.EVENT, "Timed out waiting for acknowledgment for command sent to device"); items.put(TracingHelper.TAG_CLIENT_ID.getKey(), subscription.getClientId()); items.put(TracingHelper.TAG_QOS.getKey(), subscription.getQos().toString()); commandContext.getCurrentSpan().log(items); commandContext.release(1); }); }); }
private void afterCommandPublished(final CommandSubscription subscription, final CommandContext commandContext) { metrics.incrementCommandDeliveredToDevice(subscription.getTenant()); LOG.debug("Published command to device [tenant-id: {}, device-id: {}, MQTT client-id: {}, QoS: {}]", subscription.getTenant(), subscription.getDeviceId(), subscription.getClientId(), subscription.getQos()); final Map<String, String> items = new HashMap<>(4); items.put(Fields.EVENT, "Published command to device"); items.put(Tags.MESSAGE_BUS_DESTINATION.getKey(), subscription.getTopic()); items.put(TracingHelper.TAG_CLIENT_ID.getKey(), subscription.getClientId()); items.put(TracingHelper.TAG_QOS.getKey(), subscription.getQos().toString()); commandContext.getCurrentSpan().log(items); commandContext.accept(1); }
/** * Invoked when a device sends an MQTT <em>PUBACK</em> packet. * * @param msgId The msgId of the command published with QoS 1. * @param consumer The consumer to be invoked if not {@code null} on arrival of PUBACK. * @throws NullPointerException if msgId is {@code null}. */ public void handlePubAck(final Integer msgId, final BiConsumer<CommandSubscription, CommandContext> consumer) { Objects.requireNonNull(msgId); LOG.trace("Acknowledgement received for command [Msg-id: {}] that has been sent to device.", msgId); Optional.ofNullable(removeFromWaitingForAcknowledgement(msgId)).ifPresent(value -> { cancelTimer(value.one()); final CommandSubscription subscription = value.two(); final CommandContext commandContext = value.three(); if (consumer != null) { consumer.accept(subscription, commandContext); } LOG.debug( "Acknowledged [Msg-id: {}] command to device [tenant-id: {}, device-id: {}, MQTT client-id: {}, QoS: {}]", msgId, subscription.getTenant(), subscription.getDeviceId(), subscription.getClientId(), subscription.getQos()); final Map<String, String> items = new HashMap<>(3); items.put(Fields.EVENT, "Published command has been acknowledged"); items.put(TracingHelper.TAG_CLIENT_ID.getKey(), subscription.getClientId()); items.put(TracingHelper.TAG_QOS.getKey(), subscription.getQos().toString()); commandContext.getCurrentSpan().log(items); }); }
LOG.trace("device accepted command message [command: {}]", command.getName()); logItems.put(Fields.EVENT, "device accepted command"); commandContext.getCurrentSpan().log(logItems); commandContext.accept(1); } else if (Rejected.class.isInstance(remoteState)) { LOG.debug("device rejected command message [command: {}, error: {}]", command.getName(), error); logItems.put(Fields.EVENT, "device rejected command"); commandContext.getCurrentSpan().log(logItems); commandContext.reject(error, 1); } else if (Released.class.isInstance(remoteState)) { LOG.debug("device released command message [command: {}]", command.getName()); logItems.put(Fields.EVENT, "device released command"); commandContext.getCurrentSpan().log(logItems); commandContext.release(1); logItems.put(Fields.EVENT, "device did not settle command"); logItems.put("remote state", remoteState.getClass().getSimpleName()); commandContext.getCurrentSpan().log(logItems); commandContext.release(1); commandContext.getCurrentSpan().log(items);
TracingHelper.TAG_CLIENT_ID.set(commandContext.getCurrentSpan(), endpoint.clientIdentifier()); final Command command = commandContext.getCommand(); items.put(Tags.MESSAGE_BUS_DESTINATION.getKey(), topic); items.put(TracingHelper.TAG_QOS.getKey(), qos.toString()); commandContext.getCurrentSpan().log(items); commandContext.accept(1);
TracingHelper.TAG_CLIENT_ID.set(commandContext.getCurrentSpan(), endpoint.clientIdentifier()); final Command command = commandContext.getCommand(); items.put(TracingHelper.TAG_CLIENT_ID.getKey(), subscription.getClientId()); items.put(TracingHelper.TAG_QOS.getKey(), subscription.getQos().toString()); commandContext.getCurrentSpan().log(items); subscription.getQos(), sentHandler.cause()); TracingHelper.logError(commandContext.getCurrentSpan(), sentHandler.cause()); commandContext.release(1);
commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (command.isValid()) {
commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (command.isValid()) {
private Future<MessageConsumer> createCommandConsumer(final MqttEndpoint mqttEndpoint, final CommandSubscription sub, final CommandHandler<T> cmdHandler) { // if a device does not specify a keep alive in its CONNECT packet then // the default value of the CommandConnection will be used final long livenessCheckInterval = mqttEndpoint.keepAliveTimeSeconds() * 1000 / 2; return getCommandConnection().createCommandConsumer( sub.getTenant(), sub.getDeviceId(), commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (command.isValid()) { onCommandReceived(mqttEndpoint, sub, commandContext, cmdHandler); } else { // issue credit so that application(s) can send the next command commandContext.reject(new ErrorCondition(Constants.AMQP_BAD_REQUEST, "malformed command message"), 1); } }, remoteClose -> {}, livenessCheckInterval); }
private Future<MessageConsumer> createCommandConsumer(final MqttEndpoint mqttEndpoint, final CommandSubscription sub) { // if a device does not specify a keep alive in its CONNECT packet then // the default value of the CommandConnection will be used final long livenessCheckInterval = mqttEndpoint.keepAliveTimeSeconds() * 1000 / 2; return getCommandConnection().createCommandConsumer( sub.getTenant(), sub.getDeviceId(), commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (command.isValid()) { onCommandReceived(mqttEndpoint, sub, commandContext); } else { // issue credit so that application(s) can send the next command commandContext.reject(new ErrorCondition(Constants.AMQP_BAD_REQUEST, "malformed command message"), 1); } }, remoteClose -> {}, livenessCheckInterval); }
private Future<MessageConsumer> createCommandConsumer( final ProtonSender sender, final ResourceIdentifier sourceAddress) { return getCommandConnection().createCommandConsumer( sourceAddress.getTenantId(), sourceAddress.getResourceId(), commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (!command.isValid()) { final Exception ex = new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "malformed command message"); commandContext.reject(AmqpContext.getErrorCondition(ex), 1); } else if (!sender.isOpen()) { commandContext.release(1); } else { onCommandReceived(sender, commandContext); } }, closeHandler -> {}, DEFAULT_COMMAND_CONSUMER_CHECK_INTERVAL_MILLIS); }
metrics.incrementProcessedPayload(endpointName, tenant, messagePayloadSize(ctx)); if (commandContext != null) { commandContext.getCurrentSpan().log("forwarded command to device in HTTP response body"); commandContext.accept(); metrics.incrementCommandDeliveredToDevice(tenant); endpointName, tenant, deviceId, t); if (commandContext != null) { commandContext.getCurrentSpan().log("failed to forward command to device in HTTP response body"); TracingHelper.logError(commandContext.getCurrentSpan(), t); commandContext.release();
metrics.incrementProcessedPayload(endpointName, tenant, messagePayloadSize(ctx)); if (commandContext != null) { commandContext.getCurrentSpan().log("forwarded command to device in HTTP response body"); commandContext.accept(); metrics.incrementCommandDeliveredToDevice(tenant); endpointName, tenant, deviceId, t); if (commandContext != null) { commandContext.getCurrentSpan().log("failed to forward command to device in HTTP response body"); TracingHelper.logError(commandContext.getCurrentSpan(), t); commandContext.release();