Created
August 28, 2019 10:25
-
-
Save josh-richardson/4a8abf9077de9223fd3226c748dded79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CompletableFuture[] sendRequestsToOthers(Stream<Box.PublicKey> addresses, Serializable request, String endpoint) { | |
return addresses.map(pKey -> { | |
URL recipientURL = networkNodes.urlForRecipient(pKey); | |
log.info("Propagating request to {} with URL {} {}", pKey, recipientURL.toString(), endpoint); | |
final CompletableFuture<Boolean> responseFuture = new CompletableFuture<>(); | |
final byte[] payload = Serializer.serialize(CBOR, request); | |
httpClient | |
.post(recipientURL.getPort(), recipientURL.getHost(), endpoint) | |
.putHeader("Content-Type", "application/cbor") | |
.handler(response -> response.bodyHandler(responseBody -> { | |
log.info("{} with URL {} responded with {}", pKey, recipientURL.toString(), response.statusCode()); | |
if (response.statusCode() != 200) { | |
responseFuture.completeExceptionally(new OrionException(OrionErrorCode.NODE_PROPAGATING_TO_ALL_PEERS)); | |
} else { | |
log.info("Success for {}", endpoint); | |
responseFuture.complete(true); | |
} | |
})) | |
.exceptionHandler( | |
ex -> responseFuture.completeExceptionally(new OrionException(OrionErrorCode.NODE_PUSHING_TO_PEER, ex))) | |
.end(Buffer.buffer(payload)); | |
return responseFuture; | |
}).toArray(CompletableFuture[]::new); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment