use of org.eclipse.hono.client.command.Commands in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapter method openCommandSenderLink.
private Future<CommandConsumer> openCommandSenderLink(final ProtonConnection connection, final ProtonSender sender, final ResourceIdentifier address, final Device authenticatedDevice, final Span span, final OptionalInt traceSamplingPriority) {
return createCommandConsumer(sender, address, authenticatedDevice, span).map(consumer -> {
final String tenantId = address.getTenantId();
final String deviceId = address.getResourceId();
sender.setSource(sender.getRemoteSource());
sender.setTarget(sender.getRemoteTarget());
sender.setQoS(ProtonQoS.AT_LEAST_ONCE);
final Handler<AsyncResult<ProtonSender>> detachHandler = link -> {
final Span detachHandlerSpan = newSpan("detach device command receiver link", authenticatedDevice, traceSamplingPriority);
removeCommandSubscription(connection, address.toString());
onLinkDetach(sender);
closeCommandConsumer(consumer, address, authenticatedDevice, true, detachHandlerSpan).onComplete(v -> detachHandlerSpan.finish());
};
HonoProtonHelper.setCloseHandler(sender, detachHandler);
HonoProtonHelper.setDetachHandler(sender, detachHandler);
sender.open();
// At this point, the remote peer's receiver link is successfully opened and is ready to receive
// commands. Send "device ready for command" notification downstream.
log.debug("established link [address: {}] for sending commands to device", address);
sendConnectedTtdEvent(tenantId, deviceId, authenticatedDevice, span.context());
registerCommandSubscription(connection, new CommandSubscription(consumer, address));
return consumer;
}).recover(t -> Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "cannot create command consumer")));
}
use of org.eclipse.hono.client.command.Commands in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapter method handleRemoteSenderOpenForCommands.
/**
* This method is invoked when a device wants to open a link for receiving commands.
* <p>
* The link will be closed immediately if
* <ul>
* <li>the device does not specify a source address for its receive link or</li>
* <li>the source address cannot be parsed or does not point to the command endpoint or</li>
* <li>the AMQP adapter is disabled for the tenant that the device belongs to.</li>
* </ul>
*
* @param connection The AMQP connection to the device.
* @param sender The link to use for sending commands to the device.
*/
protected void handleRemoteSenderOpenForCommands(final ProtonConnection connection, final ProtonSender sender) {
final Device authenticatedDevice = getAuthenticatedDevice(connection);
final OptionalInt traceSamplingPriority = getTraceSamplingPriority(connection);
final Span span = newSpan("attach device command receiver link", authenticatedDevice, traceSamplingPriority);
getResourceIdentifier(sender.getRemoteSource()).compose(address -> validateAddress(address, authenticatedDevice)).compose(validAddress -> {
// validAddress ALWAYS contains the tenant and device ID
if (CommandConstants.isCommandEndpoint(validAddress.getEndpoint())) {
return openCommandSenderLink(connection, sender, validAddress, authenticatedDevice, span, traceSamplingPriority);
} else {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "no such node"));
}
}).map(consumer -> {
span.log("link established");
return consumer;
}).recover(t -> {
if (t instanceof ServiceInvocationException) {
closeLinkWithError(sender, t, span);
} else {
closeLinkWithError(sender, new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "Invalid source address"), span);
}
return Future.failedFuture(t);
}).onComplete(s -> {
span.finish();
});
}
Aggregations