Add missing initPersistedDataHosts calls. Refactorings

This commit is contained in:
Manfred Karrer 2017-06-08 12:05:06 +02:00
parent 677f6c81c6
commit 8c051c3873
3 changed files with 41 additions and 116 deletions

View File

@ -19,18 +19,17 @@ package io.bisq.core.app;
import io.bisq.common.app.Version;
import io.bisq.common.crypto.KeyRing;
import io.bisq.common.proto.persistable.PersistedDataHost;
import io.bisq.core.trade.statistics.TradeStatisticsManager;
import io.bisq.network.crypto.EncryptionService;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
import java.util.ArrayList;
@Slf4j
public class AppSetup {
public abstract class AppSetup {
protected final EncryptionService encryptionService;
protected final KeyRing keyRing;
protected final TradeStatisticsManager tradeStatisticsManager;
@Inject
public AppSetup(BisqEnvironment bisqEnvironment,
@ -40,28 +39,26 @@ public class AppSetup {
// we need to reference it so the seed node stores tradeStatistics
this.encryptionService = encryptionService;
this.keyRing = keyRing;
// All classes which are persisting objects need to be added here
// Maintain order!
ArrayList<PersistedDataHost> persistedDataHosts = new ArrayList<>();
persistedDataHosts.add(tradeStatisticsManager);
// we apply at startup the reading of persisted data but don't want to get it triggered in the constructor
persistedDataHosts.stream().forEach(PersistedDataHost::readPersisted);
this.tradeStatisticsManager = tradeStatisticsManager;
Version.setBtcNetworkId(bisqEnvironment.getBitcoinNetwork().ordinal());
Version.printVersion();
}
public void start() {
SetupUtils.checkCryptoSetup(keyRing, encryptionService, this::startBasicServices, throwable -> {
SetupUtils.checkCryptoSetup(keyRing, encryptionService, () -> {
initPersistedDataHosts();
initBasicServices();
}, throwable -> {
log.error(throwable.getMessage());
throwable.printStackTrace();
System.exit(1);
});
initPersistedDataHosts();
}
protected void startBasicServices() {
}
abstract void initPersistedDataHosts();
abstract void initBasicServices();
}

View File

@ -18,6 +18,7 @@
package io.bisq.core.app;
import io.bisq.common.crypto.KeyRing;
import io.bisq.common.proto.persistable.PersistedDataHost;
import io.bisq.core.trade.statistics.TradeStatisticsManager;
import io.bisq.network.crypto.EncryptionService;
import io.bisq.network.p2p.P2PService;
@ -30,11 +31,12 @@ import javafx.beans.property.SimpleBooleanProperty;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
import java.util.ArrayList;
@Slf4j
public class AppSetupWithP2P extends AppSetup {
private final P2PService p2PService;
private BooleanProperty p2pNetWorkReady;
protected final P2PService p2PService;
protected BooleanProperty p2pNetWorkReady;
@Inject
public AppSetupWithP2P(BisqEnvironment bisqEnvironment,
@ -50,7 +52,24 @@ public class AppSetupWithP2P extends AppSetup {
}
@Override
protected void startBasicServices() {
public void initPersistedDataHosts() {
ArrayList<PersistedDataHost> persistedDataHosts = new ArrayList<>();
persistedDataHosts.add(tradeStatisticsManager);
persistedDataHosts.add(p2PService);
// we apply at startup the reading of persisted data but don't want to get it triggered in the constructor
persistedDataHosts.stream().forEach(e -> {
try {
log.info("call readPersisted at " + e.getClass().getSimpleName());
e.readPersisted();
} catch (Throwable e1) {
log.error("readPersisted error", e1);
}
});
}
@Override
protected void initBasicServices() {
p2pNetWorkReady = initP2PNetwork();
p2pNetWorkReady.addListener((observable, oldValue, newValue) -> {
@ -131,7 +150,7 @@ public class AppSetupWithP2P extends AppSetup {
return p2pNetworkInitialized;
}
private void onBasicServicesInitialized() {
protected void onBasicServicesInitialized() {
log.info("onBasicServicesInitialized");
p2PService.onAllServicesInitialized();
}

View File

@ -22,124 +22,33 @@ import io.bisq.core.dao.blockchain.BsqBlockchainManager;
import io.bisq.core.trade.statistics.TradeStatisticsManager;
import io.bisq.network.crypto.EncryptionService;
import io.bisq.network.p2p.P2PService;
import io.bisq.network.p2p.P2PServiceListener;
import io.bisq.network.p2p.network.CloseConnectionReason;
import io.bisq.network.p2p.network.Connection;
import io.bisq.network.p2p.network.ConnectionListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
@Slf4j
public class AppSetupWithP2PAndDAO extends AppSetup {
private final P2PService p2PService;
public class AppSetupWithP2PAndDAO extends AppSetupWithP2P {
private BsqBlockchainManager bsqBlockchainManager;
private BooleanProperty p2pNetWorkReady;
@Inject
public AppSetupWithP2PAndDAO(BisqEnvironment bisqEnvironment,
EncryptionService encryptionService,
KeyRing keyRing,
P2PService p2PService,
BsqBlockchainManager bsqBlockchainManager,
TradeStatisticsManager tradeStatisticsManager) {
TradeStatisticsManager tradeStatisticsManager,
BsqBlockchainManager bsqBlockchainManager) {
super(bisqEnvironment,
encryptionService,
keyRing,
p2PService,
tradeStatisticsManager);
this.p2PService = p2PService;
this.bsqBlockchainManager = bsqBlockchainManager;
}
@Override
protected void startBasicServices() {
p2pNetWorkReady = initP2PNetwork();
protected void onBasicServicesInitialized() {
super.onBasicServicesInitialized();
p2pNetWorkReady.addListener((observable, oldValue, newValue) -> {
if (newValue) {
onBasicServicesInitialized();
}
});
}
///////////////////////////////////////////////////////////////////////////////////////////
// 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) {
// We only check at seed nodes as they are running the latest version
// 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 onRequestingDataCompleted() {
log.info("p2pNetworkInitialized");
p2pNetworkInitialized.set(true);
}
@Override
public void onNoSeedNodeAvailable() {
log.info("p2pNetworkInitialized");
p2pNetworkInitialized.set(true);
}
@Override
public void onNoPeersAvailable() {
log.info("p2pNetworkInitialized");
p2pNetworkInitialized.set(true);
}
@Override
public void onBootstrapComplete() {
log.info("onBootstrapComplete");
}
@Override
public void onSetupFailed(Throwable throwable) {
log.error(throwable.toString());
}
});
return p2pNetworkInitialized;
}
private void onBasicServicesInitialized() {
log.info("onBasicServicesInitialized");
p2PService.onAllServicesInitialized();
bsqBlockchainManager.onAllServicesInitialized(log::error);
}
}