Search in sources :

Example 1 with P2PServiceListener

use of io.bisq.network.p2p.P2PServiceListener in project bisq-api by mrosseel.

the class MainViewModelHeadless 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;
}
Also used : Alert(io.bisq.core.alert.Alert) AccountAgeWitnessService(io.bisq.core.payment.AccountAgeWitnessService) Transaction(org.bitcoinj.core.Transaction) NotificationCenter(io.bisq.gui.main.overlays.notifications.NotificationCenter) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) User(io.bisq.core.user.User) UserThread(io.bisq.common.UserThread) Security(java.security.Security) TimeoutException(java.util.concurrent.TimeoutException) PrivateNotificationPayload(io.bisq.core.alert.PrivateNotificationPayload) ChainFileLockedException(org.bitcoinj.store.ChainFileLockedException) ListChangeListener(javafx.collections.ListChangeListener) FeeService(io.bisq.core.provider.fee.FeeService) ClosedTradableManager(io.bisq.core.trade.closed.ClosedTradableManager) DecryptedDataTuple(io.bisq.network.crypto.DecryptedDataTuple) DisputeManager(io.bisq.core.arbitration.DisputeManager) CurrencyUtil(io.bisq.common.locale.CurrencyUtil) BlockStoreException(org.bitcoinj.store.BlockStoreException) PaymentMethod(io.bisq.core.payment.payload.PaymentMethod) MonadicBinding(org.fxmisc.easybind.monadic.MonadicBinding) BootstrapListener(io.bisq.network.p2p.BootstrapListener) GUIUtil(io.bisq.gui.util.GUIUtil) PrivateNotificationManager(io.bisq.core.alert.PrivateNotificationManager) Ping(io.bisq.network.p2p.peers.keepalive.messages.Ping) DaoManager(io.bisq.core.dao.DaoManager) BSFormatter(io.bisq.gui.util.BSFormatter) PriceFeedComboBoxItem(io.bisq.gui.main.PriceFeedComboBoxItem) GlobalSettings(io.bisq.common.GlobalSettings) Res(io.bisq.common.locale.Res) WalletsManager(io.bisq.core.btc.wallet.WalletsManager) Subscription(org.fxmisc.easybind.Subscription) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) BtcWalletService(io.bisq.core.btc.wallet.BtcWalletService) EncryptionService(io.bisq.network.crypto.EncryptionService) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) OpenOfferManager(io.bisq.core.offer.OpenOfferManager) Address(org.bitcoinj.core.Address) KeyRing(io.bisq.common.crypto.KeyRing) ObservableList(javafx.collections.ObservableList) PriceFeedService(io.bisq.core.provider.price.PriceFeedService) CryptoCurrencyAccount(io.bisq.core.payment.CryptoCurrencyAccount) TradeManager(io.bisq.core.trade.TradeManager) Socket(java.net.Socket) java.util(java.util) Dispute(io.bisq.core.arbitration.Dispute) ArbitratorManager(io.bisq.core.arbitration.ArbitratorManager) TradeCurrency(io.bisq.common.locale.TradeCurrency) Clock(io.bisq.common.Clock) Preferences(io.bisq.core.user.Preferences) FXCollections(javafx.collections.FXCollections) PerfectMoneyAccount(io.bisq.core.payment.PerfectMoneyAccount) TradeStatisticsManager(io.bisq.core.trade.statistics.TradeStatisticsManager) DontShowAgainLookup(io.bisq.core.user.DontShowAgainLookup) AlertManager(io.bisq.core.alert.AlertManager) FilterManager(io.bisq.core.filter.FilterManager) ConnectionListener(io.bisq.network.p2p.network.ConnectionListener) Trade(io.bisq.core.trade.Trade) FailedTradesManager(io.bisq.core.trade.failed.FailedTradesManager) MarketPrice(io.bisq.core.provider.price.MarketPrice) P2PServiceListener(io.bisq.network.p2p.P2PServiceListener) OpenOffer(io.bisq.core.offer.OpenOffer) Connection(io.bisq.network.p2p.network.Connection) Nullable(javax.annotation.Nullable) javafx.beans.property(javafx.beans.property) BisqEnvironment(io.bisq.core.app.BisqEnvironment) AddressEntry(io.bisq.core.btc.AddressEntry) SealedAndSigned(io.bisq.common.crypto.SealedAndSigned) SetupUtils(io.bisq.core.app.SetupUtils) AppOptionKeys(io.bisq.core.app.AppOptionKeys) IOException(java.io.IOException) CryptoException(io.bisq.common.crypto.CryptoException) TimeUnit(java.util.concurrent.TimeUnit) WalletsSetup(io.bisq.core.btc.wallet.WalletsSetup) CloseConnectionReason(io.bisq.network.p2p.network.CloseConnectionReason) BalanceListener(io.bisq.core.btc.listeners.BalanceListener) P2PService(io.bisq.network.p2p.P2PService) EasyBind(org.fxmisc.easybind.EasyBind) DevEnv(io.bisq.common.app.DevEnv) Timer(io.bisq.common.Timer) InetAddresses(com.google.common.net.InetAddresses) ChangeListener(javafx.beans.value.ChangeListener) Connection(io.bisq.network.p2p.network.Connection) P2PServiceListener(io.bisq.network.p2p.P2PServiceListener) CloseConnectionReason(io.bisq.network.p2p.network.CloseConnectionReason) ConnectionListener(io.bisq.network.p2p.network.ConnectionListener)

Aggregations

InetAddresses (com.google.common.net.InetAddresses)1 Inject (com.google.inject.Inject)1 Clock (io.bisq.common.Clock)1 GlobalSettings (io.bisq.common.GlobalSettings)1 Timer (io.bisq.common.Timer)1 UserThread (io.bisq.common.UserThread)1 DevEnv (io.bisq.common.app.DevEnv)1 CryptoException (io.bisq.common.crypto.CryptoException)1 KeyRing (io.bisq.common.crypto.KeyRing)1 SealedAndSigned (io.bisq.common.crypto.SealedAndSigned)1 CurrencyUtil (io.bisq.common.locale.CurrencyUtil)1 Res (io.bisq.common.locale.Res)1 TradeCurrency (io.bisq.common.locale.TradeCurrency)1 Alert (io.bisq.core.alert.Alert)1 AlertManager (io.bisq.core.alert.AlertManager)1 PrivateNotificationManager (io.bisq.core.alert.PrivateNotificationManager)1 PrivateNotificationPayload (io.bisq.core.alert.PrivateNotificationPayload)1 AppOptionKeys (io.bisq.core.app.AppOptionKeys)1 BisqEnvironment (io.bisq.core.app.BisqEnvironment)1 SetupUtils (io.bisq.core.app.SetupUtils)1