use of bisq.network.p2p.network.Connection in project bisq-core by bisq-network.
the class RequestBlocksHandler method requestBlocks.
// /////////////////////////////////////////////////////////////////////////////////////////
// API
// /////////////////////////////////////////////////////////////////////////////////////////
public void requestBlocks() {
if (!stopped) {
GetBsqBlocksRequest getBsqBlocksRequest = new GetBsqBlocksRequest(startBlockHeight, nonce);
log.debug("getBsqBlocksRequest " + getBsqBlocksRequest);
if (timeoutTimer == null) {
timeoutTimer = UserThread.runAfter(() -> {
// setup before sending to avoid race conditions
if (!stopped) {
String errorMessage = "A timeout occurred at sending getBsqBlocksRequest:" + getBsqBlocksRequest + " on peersNodeAddress:" + nodeAddress;
log.debug(errorMessage + " / RequestDataHandler=" + RequestBlocksHandler.this);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_TIMEOUT);
} else {
log.trace("We have stopped already. We ignore that timeoutTimer.run call. " + "Might be caused by an previous networkNode.sendMessage.onFailure.");
}
}, TIMEOUT);
}
log.debug("We send a {} to peer {}. ", getBsqBlocksRequest.getClass().getSimpleName(), nodeAddress);
networkNode.addMessageListener(this);
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getBsqBlocksRequest);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
log.trace("Send " + getBsqBlocksRequest + " to " + nodeAddress + " succeeded.");
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call." + "Might be caused by an previous timeout.");
}
}
@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending getBsqBlocksRequest to " + nodeAddress + " failed. That is expected if the peer is offline.\n\t" + "getBsqBlocksRequest=" + getBsqBlocksRequest + "." + "\n\tException=" + throwable.getMessage();
log.error(errorMessage);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call. " + "Might be caused by an previous timeout.");
}
}
});
} else {
log.warn("We have stopped already. We ignore that requestData call.");
}
}
use of bisq.network.p2p.network.Connection in project bisq-desktop by bisq-network.
the class TakeOfferViewModel method createListeners.
private void createListeners() {
amountListener = (ov, oldValue, newValue) -> {
if (isBtcInputValid(newValue).isValid) {
setAmountToModel();
calculateVolume();
dataModel.calculateTotalToPay();
applyTakerFee();
}
updateButtonDisableState();
};
amountAsCoinListener = (ov, oldValue, newValue) -> {
amount.set(btcFormatter.formatCoin(newValue));
applyTakerFee();
};
isWalletFundedListener = (ov, oldValue, newValue) -> updateButtonDisableState();
tradeStateListener = (ov, oldValue, newValue) -> applyTradeState(newValue);
tradeErrorListener = (ov, oldValue, newValue) -> applyTradeErrorMessage(newValue);
offerStateListener = (ov, oldValue, newValue) -> applyOfferState(newValue);
connectionListener = new ConnectionListener() {
@Override
public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) {
if (connection.getPeersNodeAddressOptional().isPresent() && connection.getPeersNodeAddressOptional().get().equals(offer.getMakerNodeAddress())) {
offerWarning.set(Res.get("takeOffer.warning.connectionToPeerLost"));
updateSpinnerInfo();
}
}
@Override
public void onConnection(Connection connection) {
}
@Override
public void onError(Throwable throwable) {
}
};
}
use of bisq.network.p2p.network.Connection in project bisq-core by bisq-network.
the class AppSetupWithP2P method initP2PNetwork.
// /////////////////////////////////////////////////////////////////////////////////////////
// Initialisation
// /////////////////////////////////////////////////////////////////////////////////////////
private BooleanProperty initP2PNetwork() {
log.info("initP2PNetwork");
p2PService.getNetworkNode().addConnectionListener(new ConnectionListener() {
@Override
public void onConnection(Connection connection) {
}
@Override
public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) {
// Other disconnects might be caused by peers running an older version
if (connection.getPeerType() == Connection.PeerType.SEED_NODE && closeConnectionReason == CloseConnectionReason.RULE_VIOLATION) {
log.warn("RULE_VIOLATION onDisconnect closeConnectionReason=" + closeConnectionReason);
log.warn("RULE_VIOLATION onDisconnect connection=" + connection);
}
}
@Override
public void onError(Throwable throwable) {
}
});
final BooleanProperty p2pNetworkInitialized = new SimpleBooleanProperty();
p2PService.start(new P2PServiceListener() {
@Override
public void onTorNodeReady() {
}
@Override
public void onHiddenServicePublished() {
log.info("onHiddenServicePublished");
}
@Override
public void onDataReceived() {
log.info("onRequestingDataCompleted");
p2pNetworkInitialized.set(true);
}
@Override
public void onNoSeedNodeAvailable() {
log.info("onNoSeedNodeAvailable");
p2pNetworkInitialized.set(true);
}
@Override
public void onNoPeersAvailable() {
log.info("onNoPeersAvailable");
p2pNetworkInitialized.set(true);
}
@Override
public void onUpdatedDataReceived() {
log.info("onUpdatedDataReceived");
}
@Override
public void onSetupFailed(Throwable throwable) {
log.error(throwable.toString());
}
@Override
public void onRequestCustomBridges() {
}
});
return p2pNetworkInitialized;
}
use of bisq.network.p2p.network.Connection in project bisq-core by bisq-network.
the class GetBsqBlocksRequestHandler method onGetBsqBlocksRequest.
// /////////////////////////////////////////////////////////////////////////////////////////
// API
// /////////////////////////////////////////////////////////////////////////////////////////
public void onGetBsqBlocksRequest(GetBsqBlocksRequest getBsqBlocksRequest, final Connection connection) {
Log.traceCall(getBsqBlocksRequest + "\n\tconnection=" + connection);
List<BsqBlock> bsqBlocks = readableBsqBlockChain.getClonedBlocksFrom(getBsqBlocksRequest.getFromBlockHeight());
final GetBsqBlocksResponse bsqBlocksResponse = new GetBsqBlocksResponse(bsqBlocks, getBsqBlocksRequest.getNonce());
log.debug("bsqBlocksResponse " + bsqBlocksResponse.getRequestNonce());
if (timeoutTimer == null) {
timeoutTimer = UserThread.runAfter(() -> {
// setup before sending to avoid race conditions
String errorMessage = "A timeout occurred for bsqBlocksResponse:" + bsqBlocksResponse + " on connection:" + connection;
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection);
}, TIMEOUT, TimeUnit.SECONDS);
}
SettableFuture<Connection> future = networkNode.sendMessage(connection, bsqBlocksResponse);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
log.trace("Send DataResponse to {} succeeded. bsqBlocksResponse={}", connection.getPeersNodeAddressOptional(), bsqBlocksResponse);
cleanup();
listener.onComplete();
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call.");
}
}
@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending bsqBlocksResponse to " + connection + " failed. That is expected if the peer is offline. bsqBlocksResponse=" + bsqBlocksResponse + "." + "Exception: " + throwable.getMessage();
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, connection);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
}
}
});
}
use of bisq.network.p2p.network.Connection in project bisq-desktop by bisq-network.
the class MainViewModel method initP2PNetwork.
// /////////////////////////////////////////////////////////////////////////////////////////
// Initialisation
// /////////////////////////////////////////////////////////////////////////////////////////
private BooleanProperty initP2PNetwork() {
log.info("initP2PNetwork");
StringProperty bootstrapState = new SimpleStringProperty();
StringProperty bootstrapWarning = new SimpleStringProperty();
BooleanProperty hiddenServicePublished = new SimpleBooleanProperty();
BooleanProperty initialP2PNetworkDataReceived = new SimpleBooleanProperty();
p2PNetworkInfoBinding = EasyBind.combine(bootstrapState, bootstrapWarning, p2PService.getNumConnectedPeers(), hiddenServicePublished, initialP2PNetworkDataReceived, (state, warning, numPeers, hiddenService, dataReceived) -> {
String result = "";
int peers = (int) numPeers;
if (warning != null && peers == 0) {
result = warning;
} else {
String p2pInfo = Res.get("mainView.footer.p2pInfo", numPeers);
if (dataReceived && hiddenService) {
result = p2pInfo;
} else if (peers == 0)
result = state;
else
result = state + " / " + p2pInfo;
}
return result;
});
p2PNetworkInfoBinding.subscribe((observable, oldValue, newValue) -> {
p2PNetworkInfo.set(newValue);
});
bootstrapState.set(Res.get("mainView.bootstrapState.connectionToTorNetwork"));
p2PService.getNetworkNode().addConnectionListener(new ConnectionListener() {
@Override
public void onConnection(Connection connection) {
}
@Override
public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) {
// Other disconnects might be caused by peers running an older version
if (connection.getPeerType() == Connection.PeerType.SEED_NODE && closeConnectionReason == CloseConnectionReason.RULE_VIOLATION) {
log.warn("RULE_VIOLATION onDisconnect closeConnectionReason=" + closeConnectionReason);
log.warn("RULE_VIOLATION onDisconnect connection=" + connection);
}
}
@Override
public void onError(Throwable throwable) {
}
});
final BooleanProperty p2pNetworkInitialized = new SimpleBooleanProperty();
p2PService.start(new P2PServiceListener() {
@Override
public void onTorNodeReady() {
log.debug("onTorNodeReady");
bootstrapState.set(Res.get("mainView.bootstrapState.torNodeCreated"));
p2PNetworkIconId.set("image-connection-tor");
if (preferences.getUseTorForBitcoinJ())
initWalletService();
// We want to get early connected to the price relay so we call it already now
priceFeedService.setCurrencyCodeOnInit();
priceFeedService.initialRequestPriceFeed();
}
@Override
public void onHiddenServicePublished() {
log.debug("onHiddenServicePublished");
hiddenServicePublished.set(true);
bootstrapState.set(Res.get("mainView.bootstrapState.hiddenServicePublished"));
}
@Override
public void onDataReceived() {
log.debug("onRequestingDataCompleted");
initialP2PNetworkDataReceived.set(true);
bootstrapState.set(Res.get("mainView.bootstrapState.initialDataReceived"));
splashP2PNetworkAnimationVisible.set(false);
p2pNetworkInitialized.set(true);
}
@Override
public void onNoSeedNodeAvailable() {
log.warn("onNoSeedNodeAvailable");
if (p2PService.getNumConnectedPeers().get() == 0)
bootstrapWarning.set(Res.get("mainView.bootstrapWarning.noSeedNodesAvailable"));
else
bootstrapWarning.set(null);
splashP2PNetworkAnimationVisible.set(false);
p2pNetworkInitialized.set(true);
}
@Override
public void onNoPeersAvailable() {
log.warn("onNoPeersAvailable");
if (p2PService.getNumConnectedPeers().get() == 0) {
p2pNetworkWarnMsg.set(Res.get("mainView.p2pNetworkWarnMsg.noNodesAvailable"));
bootstrapWarning.set(Res.get("mainView.bootstrapWarning.noNodesAvailable"));
p2pNetworkLabelId.set("splash-error-state-msg");
} else {
bootstrapWarning.set(null);
p2pNetworkLabelId.set("footer-pane");
}
splashP2PNetworkAnimationVisible.set(false);
p2pNetworkInitialized.set(true);
}
@Override
public void onUpdatedDataReceived() {
log.debug("onBootstrapComplete");
splashP2PNetworkAnimationVisible.set(false);
bootstrapComplete.set(true);
}
@Override
public void onSetupFailed(Throwable throwable) {
log.warn("onSetupFailed");
p2pNetworkWarnMsg.set(Res.get("mainView.p2pNetworkWarnMsg.connectionToP2PFailed", throwable.getMessage()));
splashP2PNetworkAnimationVisible.set(false);
bootstrapWarning.set(Res.get("mainView.bootstrapWarning.bootstrappingToP2PFailed"));
p2pNetworkLabelId.set("splash-error-state-msg");
}
@Override
public void onRequestCustomBridges() {
showTorNetworkSettingsWindow();
}
});
return p2pNetworkInitialized;
}
Aggregations