List of usage examples for io.vertx.core Future succeededFuture
static <T> Future<T> succeededFuture(T result)
From source file:org.eclipse.hono.authentication.impl.AcceptAllPlainAuthenticationService.java
License:Open Source License
@Override protected void verify(String authzid, String authcid, String password, Handler<AsyncResult<String>> authenticationResultHandler) { authenticationResultHandler.handle(Future.succeededFuture(authzid.length() > 0 ? authzid : authcid)); }
From source file:org.eclipse.hono.client.impl.AbstractRequestResponseClient.java
License:Open Source License
/** * Creates a client for a vert.x context. * * @param context The context to run all interactions with the server on. * @param con The connection to use for interacting with the service. * @param tenantId The tenant that the client will be scoped to. * @param creationHandler The handler to invoke with the created client. *//*from ww w .ja v a 2 s .co m*/ protected AbstractRequestResponseClient(final Context context, final ProtonConnection con, final String tenantId, final Handler<AsyncResult<C>> creationHandler) { super(context); requestResponseAddressTemplate = String.format("%s/%%s", getName()); requestResponseReplyToAddressTemplate = String.format("%s/%%s/%%s", getName()); this.replyToAddress = String.format(requestResponseReplyToAddressTemplate, Objects.requireNonNull(tenantId), UUID.randomUUID()); final Future<ProtonSender> senderTracker = Future.future(); senderTracker.setHandler(r -> { if (r.succeeded()) { LOG.debug("request response client created"); this.sender = r.result(); creationHandler.handle(Future.succeededFuture((C) this)); } else { creationHandler.handle(Future.failedFuture(r.cause())); } }); final Future<ProtonReceiver> receiverTracker = Future.future(); context.runOnContext(create -> { final ProtonReceiver receiver = con.createReceiver(replyToAddress); receiver.setAutoAccept(true).setPrefetch(DEFAULT_SENDER_CREDITS).handler((delivery, message) -> { final Handler<AsyncResult<R>> handler = replyMap.remove(message.getCorrelationId()); if (handler != null) { R result = getRequestResponseResult(message); LOG.debug("received response [correlation ID: {}, status: {}]", message.getCorrelationId(), result.getStatus()); handler.handle(Future.succeededFuture(result)); } else { LOG.debug("discarding unexpected response [correlation ID: {}]", message.getCorrelationId()); } }).openHandler(receiverTracker.completer()).open(); receiverTracker.compose(openReceiver -> { this.receiver = openReceiver; ProtonSender sender = con.createSender(String.format(requestResponseAddressTemplate, tenantId)); sender.setQoS(ProtonQoS.AT_LEAST_ONCE).openHandler(senderTracker.completer()).open(); }, senderTracker); }); }
From source file:org.eclipse.hono.client.impl.AuthenticationServerClient.java
License:Open Source License
/** * Verifies username/password credentials with a remote authentication server using SASL PLAIN. * //from w ww .j av a2 s.com * @param authzid The identity to act as. * @param authcid The username. * @param password The password. * @param authenticationResultHandler The handler to invoke with the authentication result. On successful authentication, * the result contains a JWT with the authenticated user's claims. */ public void verifyPlain(final String authzid, final String authcid, final String password, final Handler<AsyncResult<HonoUser>> authenticationResultHandler) { final ProtonClientOptions options = new ProtonClientOptions(); options.setReconnectAttempts(3).setReconnectInterval(50); options.addEnabledSaslMechanism(AuthenticationConstants.MECHANISM_PLAIN); factory.connect(options, authcid, password, null, null, conAttempt -> { if (conAttempt.failed()) { authenticationResultHandler.handle(Future.failedFuture("cannot connect to Authentication service")); } else { final ProtonConnection openCon = conAttempt.result(); final Future<HonoUser> userTracker = Future.future(); userTracker.setHandler(s -> { if (s.succeeded()) { authenticationResultHandler.handle(Future.succeededFuture(s.result())); } else { authenticationResultHandler.handle(Future.failedFuture(s.cause())); } final ProtonConnection con = conAttempt.result(); if (con != null) { LOG.debug("closing connection to Authentication service"); con.close(); } }); vertx.setTimer(5000, tid -> { if (!userTracker.isComplete()) { userTracker.fail("time out reached while waiting for token from Authentication service"); } }); getToken(openCon, userTracker); } }); }
From source file:org.eclipse.hono.client.impl.CommandClientImpl.java
License:Open Source License
/** * Creates a new command client for a tenant and device. * * @param tenantId The tenant to create the client for. * @param deviceId The device to create the client for. * @param context The vert.x context to run all interactions with the server on. * @param clientConfig The configuration properties to use. * @param con The AMQP connection to the server. * @param senderCloseHook A handler to invoke if the peer closes the sender link unexpectedly. * @param receiverCloseHook A handler to invoke if the peer closes the receiver link unexpectedly. * @param creationHandler The handler to invoke with the outcome of the creation attempt. * @throws NullPointerException if any of the parameters is {@code null}. *//*from www . j a v a 2 s. co m*/ public static final void create(final String tenantId, final String deviceId, final Context context, final ClientConfigProperties clientConfig, final ProtonConnection con, final Handler<String> senderCloseHook, final Handler<String> receiverCloseHook, final Handler<AsyncResult<CommandClient>> creationHandler) { final CommandClientImpl client = new CommandClientImpl(context, clientConfig, tenantId, deviceId); client.createLinks(con, senderCloseHook, receiverCloseHook).setHandler(s -> { if (s.succeeded()) { LOG.debug("successfully created command client for [{}]", tenantId); creationHandler.handle(Future.succeededFuture(client)); } else { LOG.debug("failed to create command client for [{}]", tenantId, s.cause()); creationHandler.handle(Future.failedFuture(s.cause())); } }); }
From source file:org.eclipse.hono.client.impl.EventConsumerImpl.java
License:Open Source License
/** * Creates a new event consumer for a tenant. * /* ww w . ja v a2 s . co m*/ * @param context The vert.x context to run all interactions with the server on. * @param con The AMQP connection to the server. * @param tenantId The tenant to consumer events for. * @param pathSeparator The address path separator character used by the server. * @param prefetch the number of message credits the consumer grants and replenishes automatically as messages are * delivered. To manage credit manually, you can instead set prefetch to 0. * @param eventConsumer The consumer to invoke with each event received. * @param creationHandler The handler to invoke with the outcome of the creation attempt. * @throws NullPointerException if any of the parameters is {@code null}. */ public static void create(final Context context, final ProtonConnection con, final String tenantId, final String pathSeparator, final int prefetch, final BiConsumer<ProtonDelivery, Message> eventConsumer, final Handler<AsyncResult<MessageConsumer>> creationHandler) { Objects.requireNonNull(context); Objects.requireNonNull(con); Objects.requireNonNull(tenantId); Objects.requireNonNull(pathSeparator); Objects.requireNonNull(eventConsumer); Objects.requireNonNull(creationHandler); createConsumer(context, con, tenantId, pathSeparator, EVENT_ADDRESS_TEMPLATE, ProtonQoS.AT_LEAST_ONCE, prefetch, eventConsumer).setHandler(created -> { if (created.succeeded()) { creationHandler .handle(Future.succeededFuture(new EventConsumerImpl(context, created.result()))); } else { creationHandler.handle(Future.failedFuture(created.cause())); } }); }
From source file:org.eclipse.hono.client.impl.EventSenderImpl.java
License:Open Source License
/** * Creates a new sender for publishing events to a Hono server. * /*from w w w .ja v a 2 s .c om*/ * @param context The vertx context to run all interactions with the server on. * @param con The connection to the Hono server. * @param tenantId The tenant that the events will be published for. * @param deviceId The device that the events will be published for or {@code null} * if the events are going to be be produced by arbitrary devices of the * tenant. * @param closeHook The handler to invoke when the Hono server closes the sender. The sender's * target address is provided as an argument to the handler. * @param creationHandler The handler to invoke with the result of the creation attempt. * @throws NullPointerException if any of context, connection, tenant or handler is {@code null}. */ public static void create(final Context context, final ProtonConnection con, final String tenantId, final String deviceId, final Handler<String> closeHook, final Handler<AsyncResult<MessageSender>> creationHandler) { Objects.requireNonNull(context); Objects.requireNonNull(con); Objects.requireNonNull(tenantId); Objects.requireNonNull(creationHandler); final String targetAddress = getTargetAddress(tenantId, deviceId); createSender(context, con, targetAddress, ProtonQoS.AT_LEAST_ONCE, closeHook).setHandler(created -> { if (created.succeeded()) { creationHandler.handle(Future.succeededFuture( new EventSenderImpl(created.result(), tenantId, targetAddress, context, closeHook))); } else { creationHandler.handle(Future.failedFuture(created.cause())); } }); }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
@Override public HonoClient connect(final ProtonClientOptions options, final Handler<AsyncResult<HonoClient>> connectionHandler, final Handler<ProtonConnection> disconnectHandler) { Objects.requireNonNull(connectionHandler); if (isConnected()) { LOG.debug("already connected to server [{}:{}]", connectionFactory.getHost(), connectionFactory.getPort()); connectionHandler.handle(Future.succeededFuture(this)); } else if (connecting.compareAndSet(false, true)) { setConnection(null);/*ww w. j av a 2s . c o m*/ if (options == null) { clientOptions = new ProtonClientOptions(); } else { clientOptions = options; } connectionFactory.connect(clientOptions, remoteClose -> onRemoteClose(remoteClose, disconnectHandler), failedConnection -> onRemoteDisconnect(failedConnection, disconnectHandler), conAttempt -> { connecting.compareAndSet(true, false); if (conAttempt.failed()) { reconnect(connectionHandler, disconnectHandler); } else { setConnection(conAttempt.result()); setContext(Vertx.currentContext()); connectionHandler.handle(Future.succeededFuture(this)); } }); } else { LOG.debug("already trying to connect to server ..."); } return this; }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
void getOrCreateSender(final String key, final Consumer<Handler<AsyncResult<MessageSender>>> newSenderSupplier, final Handler<AsyncResult<MessageSender>> resultHandler) { final MessageSender sender = activeSenders.get(key); if (sender != null && sender.isOpen()) { LOG.debug("reusing existing message sender [target: {}, credit: {}]", key, sender.getCredit()); resultHandler.handle(Future.succeededFuture(sender)); } else if (!senderCreationLocks.computeIfAbsent(key, k -> Boolean.FALSE)) { // register a handler to be notified if the underlying connection to the server fails // so that we can fail the result handler passed in final Handler<Void> connectionFailureHandler = connectionLost -> { // remove lock so that next attempt to open a sender doesn't fail senderCreationLocks.remove(key); resultHandler.handle(Future.failedFuture("connection to server lost")); };// www. j a v a 2 s . c o m creationRequests.add(connectionFailureHandler); senderCreationLocks.put(key, Boolean.TRUE); LOG.debug("creating new message sender for {}", key); newSenderSupplier.accept(creationAttempt -> { if (creationAttempt.succeeded()) { MessageSender newSender = creationAttempt.result(); LOG.debug("successfully created new message sender for {}", key); activeSenders.put(key, newSender); } else { LOG.debug("failed to create new message sender for {}", key, creationAttempt.cause()); activeSenders.remove(key); } senderCreationLocks.remove(key); creationRequests.remove(connectionFailureHandler); resultHandler.handle(creationAttempt); }); } else { LOG.debug("already trying to create a message sender for {}", key); resultHandler.handle(Future.failedFuture("sender link not established yet")); } }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
private Future<ProtonConnection> checkConnection() { if (connection == null || connection.isDisconnected()) { return Future.failedFuture("client is not connected to server (yet)"); } else {//from ww w. ja va 2 s . c o m return Future.succeededFuture(connection); } }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
@Override public HonoClient getOrCreateRegistrationClient(final String tenantId, final Handler<AsyncResult<RegistrationClient>> resultHandler) { final RegistrationClient regClient = activeRegClients.get(Objects.requireNonNull(tenantId)); if (regClient != null && regClient.isOpen()) { LOG.debug("reusing existing registration client for [{}]", tenantId); resultHandler.handle(Future.succeededFuture(regClient)); } else {/* w w w. j a va 2s .c o m*/ createRegistrationClient(tenantId, resultHandler); } return this; }