use of com.google.common.util.concurrent.FutureCallback in project openflowplugin by opendaylight.
the class MultiLayerExperimenterMultipartService method handleAndReply.
@Override
@SuppressWarnings("unchecked")
public Future<RpcResult<SendExperimenterMpRequestOutput>> handleAndReply(SendExperimenterMpRequestInput input) {
final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
final SettableFuture<RpcResult<SendExperimenterMpRequestOutput>> finalFuture = SettableFuture.create();
class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
@Override
public void onSuccess(@Nonnull final RpcResult<List<MultipartReply>> result) {
if (result.isSuccessful()) {
final List<MultipartReply> multipartReplies = result.getResult();
if (multipartReplies.isEmpty()) {
LOG.warn("Multipart reply to Experimenter-Mp request shouldn't be empty list.");
finalFuture.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withError(ErrorType.RPC, "Multipart reply list is empty.").build());
} else {
LOG.debug("OnSuccess, rpc result successful," + " multipart response for rpc sendExperimenterMpRequest with xid {} obtained.", multipartReplies.get(0).getXid());
final SendExperimenterMpRequestOutputBuilder sendExpMpReqOutputBuilder = new SendExperimenterMpRequestOutputBuilder();
final List<ExperimenterCoreMessageItem> expCoreMessageItem = new ArrayList<>();
for (MultipartReply multipartReply : multipartReplies) {
final MultipartReplyExperimenterCase caseBody = (MultipartReplyExperimenterCase) multipartReply.getMultipartReplyBody();
final MultipartReplyExperimenter replyBody = caseBody.getMultipartReplyExperimenter();
final ExperimenterDataOfChoice vendorData = replyBody.getExperimenterDataOfChoice();
final MessageTypeKey<? extends ExperimenterDataOfChoice> key = new MessageTypeKey<>(getVersion(), (Class<? extends ExperimenterDataOfChoice>) vendorData.getImplementedInterface());
final ConvertorMessageFromOFJava<ExperimenterDataOfChoice, MessagePath> messageConverter = getExtensionConverterProvider().getMessageConverter(key);
if (messageConverter == null) {
LOG.warn("Custom converter for {}[OF:{}] not found", vendorData.getImplementedInterface(), getVersion());
finalFuture.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withError(ErrorType.RPC, "Custom converter not found.").build());
return;
}
try {
final ExperimenterMessageOfChoice messageOfChoice = messageConverter.convert(vendorData, MessagePath.MPMESSAGE_RPC_OUTPUT);
final ExperimenterCoreMessageItemBuilder expCoreMessageItemBuilder = new ExperimenterCoreMessageItemBuilder();
expCoreMessageItemBuilder.setExperimenterMessageOfChoice(messageOfChoice);
expCoreMessageItem.add(expCoreMessageItemBuilder.build());
} catch (final ConversionException e) {
LOG.error("Conversion of experimenter message reply failed. Exception: {}", e);
finalFuture.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withError(ErrorType.RPC, "Conversion of experimenter rpc output failed.").build());
return;
}
}
sendExpMpReqOutputBuilder.setExperimenterCoreMessageItem(expCoreMessageItem);
finalFuture.set(RpcResultBuilder.success(sendExpMpReqOutputBuilder.build()).build());
}
} else {
LOG.warn("OnSuccess, rpc result unsuccessful," + " multipart response for rpc sendExperimenterMpRequest was unsuccessful.");
finalFuture.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withRpcErrors(result.getErrors()).build());
}
}
@Override
public void onFailure(final Throwable throwable) {
LOG.warn("Failure multipart response for Experimenter-Mp request. Exception: {}", throwable);
finalFuture.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withError(ErrorType.RPC, "Future error", throwable).build());
}
}
Futures.addCallback(multipartFuture, new CallBackImpl(), MoreExecutors.directExecutor());
return finalFuture;
}
use of com.google.common.util.concurrent.FutureCallback in project openflowplugin by opendaylight.
the class MultiLayerTableMultipartService method handleAndReply.
@Override
public Future<RpcResult<UpdateTableOutput>> handleAndReply(UpdateTableInput input) {
final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public void onSuccess(@Nonnull final RpcResult<List<MultipartReply>> result) {
if (result.isSuccessful()) {
final List<MultipartReply> multipartReplies = result.getResult();
if (multipartReplies.isEmpty()) {
LOG.debug("Multipart reply to table features request shouldn't be empty list.");
finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withError(ErrorType.RPC, "Multipart reply list is empty.").build());
} else {
final Long xid = multipartReplies.get(0).getXid();
LOG.debug("OnSuccess, rpc result successful," + " multipart response for rpc update-table with xid {} obtained.", xid);
final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
try {
storeStatistics(convertToSalTableFeatures(multipartReplies));
} catch (Exception e) {
LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
}
}
} else {
LOG.debug("OnSuccess, rpc result unsuccessful," + " multipart response for rpc update-table was unsuccessful.");
finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors()).build());
}
}
@Override
public void onFailure(final Throwable throwable) {
LOG.error("Failure multipart response for table features request. Exception: {}", throwable);
finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withError(ErrorType.RPC, "Future error", throwable).build());
}
}
Futures.addCallback(multipartFuture, new CallBackImpl(), MoreExecutors.directExecutor());
return finalFuture;
}
use of com.google.common.util.concurrent.FutureCallback in project bisq-core by bisq-network.
the class CreateTakerFeeTx method run.
@Override
protected void run() {
try {
runInterceptHook();
User user = processModel.getUser();
NodeAddress selectedArbitratorNodeAddress = ArbitratorSelectionRule.select(user.getAcceptedArbitratorAddresses(), processModel.getOffer());
log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
Arbitrator selectedArbitrator = user.getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateTakeOfferFeeTx");
BtcWalletService walletService = processModel.getBtcWalletService();
String id = processModel.getOffer().getId();
AddressEntry addressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING);
AddressEntry reservedForTradeAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
AddressEntry changeAddressEntry = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE);
Address fundingAddress = addressEntry.getAddress();
Address reservedForTradeAddress = reservedForTradeAddressEntry.getAddress();
Address changeAddress = changeAddressEntry.getAddress();
final TradeWalletService tradeWalletService = processModel.getTradeWalletService();
if (trade.isCurrencyForTakerFeeBtc()) {
tradeFeeTx = tradeWalletService.createBtcTradingFeeTx(fundingAddress, reservedForTradeAddress, changeAddress, processModel.getFundsNeededForTradeAsLong(), processModel.isUseSavingsWallet(), trade.getTakerFee(), trade.getTxFee(), selectedArbitrator.getBtcAddress(), new FutureCallback<Transaction>() {
@Override
public void onSuccess(Transaction transaction) {
// we delay one render frame to be sure we don't get called before the method call has
// returned (tradeFeeTx would be null in that case)
UserThread.execute(() -> {
if (!completed) {
processModel.setTakeOfferFeeTx(tradeFeeTx);
trade.setTakerFeeTxId(tradeFeeTx.getHashAsString());
walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
trade.setState(Trade.State.TAKER_PUBLISHED_TAKER_FEE_TX);
complete();
} else {
log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
}
});
}
@Override
public void onFailure(@NotNull Throwable t) {
if (!completed) {
failed(t);
} else {
log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
}
}
});
} else {
final BsqWalletService bsqWalletService = processModel.getBsqWalletService();
Transaction preparedBurnFeeTx = processModel.getBsqWalletService().getPreparedBurnFeeTx(trade.getTakerFee());
Transaction txWithBsqFee = tradeWalletService.completeBsqTradingFeeTx(preparedBurnFeeTx, fundingAddress, reservedForTradeAddress, changeAddress, processModel.getFundsNeededForTradeAsLong(), processModel.isUseSavingsWallet(), trade.getTxFee());
Transaction signedTx = processModel.getBsqWalletService().signTx(txWithBsqFee);
WalletService.checkAllScriptSignaturesForTx(signedTx);
bsqWalletService.commitTx(signedTx);
// We need to create another instance, otherwise the tx would trigger an invalid state exception
// if it gets committed 2 times
tradeWalletService.commitTx(tradeWalletService.getClonedTransaction(signedTx));
bsqWalletService.broadcastTx(signedTx, new FutureCallback<Transaction>() {
@Override
public void onSuccess(@Nullable Transaction transaction) {
if (!completed) {
if (transaction != null) {
log.debug("Successfully sent tx with id " + transaction.getHashAsString());
trade.setTakerFeeTxId(transaction.getHashAsString());
processModel.setTakeOfferFeeTx(transaction);
walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
trade.setState(Trade.State.TAKER_PUBLISHED_TAKER_FEE_TX);
complete();
}
} else {
log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
}
}
@Override
public void onFailure(@NotNull Throwable t) {
if (!completed) {
log.error(t.toString());
t.printStackTrace();
trade.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
failed(t);
} else {
log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
}
}
});
}
} catch (Throwable t) {
failed(t);
}
}
use of com.google.common.util.concurrent.FutureCallback in project bisq-api by mrosseel.
the class BisqProxy method withdrawFunds.
public void withdrawFunds(Set<String> sourceAddresses, Coin amountAsCoin, boolean feeExcluded, String targetAddress) throws AddressEntryException, InsufficientFundsException, AmountTooLowException {
// get all address entries
final List<AddressEntry> sourceAddressEntries = sourceAddresses.stream().filter(address -> null != address).map(address -> btcWalletService.getAddressEntryListAsImmutableList().stream().filter(addressEntry -> address.equals(addressEntry.getAddressString())).findFirst().orElse(null)).filter(item -> null != item).collect(Collectors.toList());
// this filter matches all unauthorized address types
Predicate<AddressEntry> filterNotAllowedAddressEntries = addressEntry -> !(AddressEntry.Context.AVAILABLE.equals(addressEntry.getContext()) || AddressEntry.Context.TRADE_PAYOUT.equals(addressEntry.getContext()));
// check if there are any unauthorized address types
if (sourceAddressEntries.stream().anyMatch(filterNotAllowedAddressEntries)) {
throw new ValidationException("Funds can be withdrawn only from addresses with context AVAILABLE and TRADE_PAYOUT");
}
Coin sendersAmount;
// We do not know sendersAmount if senderPaysFee is true. We repeat fee calculation after first attempt if senderPaysFee is true.
Transaction feeEstimationTransaction;
try {
feeEstimationTransaction = btcWalletService.getFeeEstimationTransactionForMultipleAddresses(sourceAddresses, amountAsCoin);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("dust limit")) {
throw new AmountTooLowException(e.getMessage());
}
throw e;
}
if (feeExcluded && feeEstimationTransaction != null) {
sendersAmount = amountAsCoin.add(feeEstimationTransaction.getFee());
feeEstimationTransaction = btcWalletService.getFeeEstimationTransactionForMultipleAddresses(sourceAddresses, sendersAmount);
}
checkNotNull(feeEstimationTransaction, "feeEstimationTransaction must not be null");
Coin fee = feeEstimationTransaction.getFee();
sendersAmount = feeExcluded ? amountAsCoin.add(fee) : amountAsCoin;
Coin receiverAmount = feeExcluded ? amountAsCoin : amountAsCoin.subtract(fee);
final Coin totalAvailableAmountOfSelectedItems = sourceAddressEntries.stream().map(address -> btcWalletService.getBalanceForAddress(address.getAddress())).reduce(Coin.ZERO, Coin::add);
if (!sendersAmount.isPositive())
throw new ValidationException("Senders amount must be positive");
if (!new BtcAddressValidator().validate(targetAddress).isValid)
throw new ValidationException("Invalid target address");
if (sourceAddresses.isEmpty())
throw new ValidationException("List of source addresses must not be empty");
if (sendersAmount.compareTo(totalAvailableAmountOfSelectedItems) > 0)
throw new InsufficientFundsException("Not enough funds in selected addresses");
if (receiverAmount.isPositive()) {
try {
// TODO return completable future
btcWalletService.sendFundsForMultipleAddresses(sourceAddresses, targetAddress, amountAsCoin, fee, null, null, new FutureCallback<Transaction>() {
@Override
public void onSuccess(@javax.annotation.Nullable Transaction transaction) {
if (transaction != null) {
log.debug("onWithdraw onSuccess tx ID:" + transaction.getHashAsString());
} else {
log.error("onWithdraw transaction is null");
}
List<Trade> trades = new ArrayList<>(tradeManager.getTradableList());
trades.stream().filter(Trade::isPayoutPublished).forEach(trade -> btcWalletService.getAddressEntry(trade.getId(), AddressEntry.Context.TRADE_PAYOUT).ifPresent(addressEntry -> {
if (btcWalletService.getBalanceForAddress(addressEntry.getAddress()).isZero())
tradeManager.addTradeToClosedTrades(trade);
}));
}
@Override
public void onFailure(@NotNull Throwable t) {
log.error("onWithdraw onFailure");
}
});
} catch (org.bitcoinj.core.InsufficientMoneyException e) {
throw new InsufficientFundsException(e.getMessage());
}
} else {
throw new AmountTooLowException(Res.get("portfolio.pending.step5_buyer.amountTooLow"));
}
}
use of com.google.common.util.concurrent.FutureCallback in project qpid-broker-j by apache.
the class AbstractVirtualHost method doRestart.
private ListenableFuture<Void> doRestart() {
createHousekeepingExecutor();
final VirtualHostStoreUpgraderAndRecoverer virtualHostStoreUpgraderAndRecoverer = new VirtualHostStoreUpgraderAndRecoverer((VirtualHostNode<?>) getParent());
virtualHostStoreUpgraderAndRecoverer.reloadAndRecoverVirtualHost(getDurableConfigurationStore());
final Collection<VirtualHostAccessControlProvider> accessControlProviders = getChildren(VirtualHostAccessControlProvider.class);
if (!accessControlProviders.isEmpty()) {
accessControlProviders.forEach(child -> child.addChangeListener(_accessControlProviderListener));
}
final List<ListenableFuture<Void>> childOpenFutures = new ArrayList<>();
Subject.doAs(getSubjectWithAddedSystemRights(), (PrivilegedAction<Object>) () -> {
applyToChildren(child -> {
final ListenableFuture<Void> childOpenFuture = child.openAsync();
childOpenFutures.add(childOpenFuture);
addFutureCallback(childOpenFuture, new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
}
@Override
public void onFailure(final Throwable t) {
LOGGER.error("Exception occurred while opening {} : {}", child.getClass().getSimpleName(), child.getName(), t);
onRestartFailure();
}
}, getTaskExecutor());
});
return null;
});
ListenableFuture<List<Void>> combinedFuture = Futures.allAsList(childOpenFutures);
return Futures.transformAsync(combinedFuture, input -> onActivate(), MoreExecutors.directExecutor());
}
Aggregations