/** * Creates a new result for a status code and payload. * * @param status The status code. * @param contentType A media type describing the payload or {@code null} if unknown. * @param payload The payload to include in the result. * @return The result. */ public static BufferResult from(final int status, final String contentType, final Buffer payload) { return new BufferResult(status, contentType, payload); }
@Override protected BufferResult getResult(final int status, final String contentType, final Buffer payload, final CacheDirective cacheDirective) { return BufferResult.from(status, contentType, payload); }
private Void printResponse(final BufferResult result) { LOG.info("Received Command response : {}", Optional.ofNullable(result.getPayload()).orElse(Buffer.buffer()).toString()); return null; }
/** * {@inheritDoc} * <p> * This method uses the {@linkplain #createMessageId() message ID} to correlate the response received * from a device with the request. */ @Override public Future<BufferResult> sendCommand(final String command, final String contentType, final Buffer data, final Map<String, Object> properties) { Objects.requireNonNull(command); final Future<BufferResult> responseTracker = Future.future(); createAndSendRequest(command, properties, data, contentType, responseTracker, null); return responseTracker.map(response -> { if (response.isOk()) { return response; } else { throw StatusCodeMapper.from(response); } }); }
private void sendCommandAndReceiveResponse(final String tenantId, final String deviceId) { client.getOrCreateCommandClient(tenantId, deviceId) .map(this::setCommandTimeOut) .compose(commandClient -> commandClient .sendCommand(sampler.getCommand(), Buffer.buffer(sampler.getCommandPayload())) .map(commandResponse -> { final String commandResponseText = Optional.ofNullable(commandResponse.getPayload()) .orElse(Buffer.buffer()).toString(); LOG.debug("Command response from device [{}:{}] received [{}]", tenantId, deviceId, commandResponseText); synchronized (lock) { successCount.incrementAndGet(); bytesReceived.addAndGet(sampler.getCommandPayload().getBytes().length); bytesSent.addAndGet(commandResponseText.getBytes().length); } return commandResponse; }) .map(x -> closeCommandClient(commandClient, tenantId, deviceId)) .recover(error -> { if (triggerType.equals("device") || devicesReadyToReceiveCommands.contains(deviceId)) { errorCount.incrementAndGet(); } LOG.error("Error processing command [{}] to device [{}:{}]", error.getMessage(), tenantId, deviceId); closeCommandClient(commandClient, tenantId, deviceId); return Future.failedFuture(error); })); }
if (LOG.isDebugEnabled()) { LOG.debug("Successfully sent command payload: [{}].", commandBuffer.toString()); LOG.debug("And received response: [{}].", Optional.ofNullable(result.getPayload()).orElse(Buffer.buffer()).toString());
/** * Creates a new result for a status code and payload. * * @param status The status code. * @param contentType A media type describing the payload or {@code null} if unknown. * @param payload The payload to include in the result. * @return The result. */ public static BufferResult from(final int status, final String contentType, final Buffer payload) { return new BufferResult(status, contentType, payload); }
private Future<Void> processCommand(final Command command) { LOG.info("Command sent to device... [request will timeout in {} seconds]", requestTimeoutInSecs); final Future<CommandClient> commandClient = client.getOrCreateCommandClient(tenantId, deviceId); return commandClient .map(this::setRequestTimeOut) .compose(c -> { if (command.isOneWay()) { return c .sendOneWayCommand(command.getName(), command.getContentType(), Buffer.buffer(command.getPayload()), null) .map(ok -> c); } else { return c .sendCommand(command.getName(), command.getContentType(), Buffer.buffer(command.getPayload()), null) .map(this::printResponse) .map(ok -> c); } }) .map(this::closeCommandClient) .otherwise(error -> { LOG.error("Error sending command: {}", error.getMessage()); if (commandClient.succeeded()) { return closeCommandClient(commandClient.result()); } else { return null; } }); }
/** * Creates a new result for a status code. * * @param status The status code. * @return The result. */ public static BufferResult from(final int status) { return new BufferResult(status, null, null); }
/** * Creates a new result for a status code and a payload. * * @param status The status code. * @param payload The payload to include in the result. * @return The result. */ public static BufferResult from(final int status, final Buffer payload) { return new BufferResult(status, null, payload); }
/** * Creates a new result for a status code. * * @param status The status code. * @return The result. */ public static BufferResult from(final int status) { return new BufferResult(status, null, null); }
/** * Creates a new result for a status code and a payload. * * @param status The status code. * @param payload The payload to include in the result. * @return The result. */ public static BufferResult from(final int status, final Buffer payload) { return new BufferResult(status, null, payload); }