Search in sources :

Example 1 with Clock

use of io.bitsquare.common.Clock in project bitsquare by bitsquare.

the class P2PService method init.

private void init(boolean useLocalhost, int networkId, File storageDir, String seedNodes, String myAddress, String banList) {
    if (!useLocalhost)
        FileUtil.rollingBackup(new File(Paths.get(torDir.getAbsolutePath(), "hiddenservice").toString()), "private_key", 20);
    if (banList != null && !banList.isEmpty())
        BanList.setList(Arrays.asList(banList.replace(" ", "").split(",")).stream().map(NodeAddress::new).collect(Collectors.toList()));
    if (myAddress != null && !myAddress.isEmpty())
        seedNodesRepository.setNodeAddressToExclude(new NodeAddress(myAddress));
    networkNode = useLocalhost ? new LocalhostNetworkNode(port) : new TorNetworkNode(port, torDir);
    networkNode.addConnectionListener(this);
    networkNode.addMessageListener(this);
    Set<NodeAddress> seedNodeAddresses;
    if (seedNodes != null && !seedNodes.isEmpty())
        seedNodeAddresses = Arrays.asList(seedNodes.replace(" ", "").split(",")).stream().map(NodeAddress::new).collect(Collectors.toSet());
    else
        seedNodeAddresses = seedNodesRepository.getSeedNodeAddresses(useLocalhost, networkId);
    peerManager = new PeerManager(networkNode, maxConnections, seedNodeAddresses, storageDir, clock);
    broadcaster = new Broadcaster(networkNode, peerManager);
    p2PDataStorage = new P2PDataStorage(broadcaster, networkNode, storageDir);
    p2PDataStorage.addHashMapChangedListener(this);
    requestDataManager = new RequestDataManager(networkNode, p2PDataStorage, peerManager, seedNodeAddresses, this);
    peerExchangeManager = new PeerExchangeManager(networkNode, peerManager, seedNodeAddresses);
    keepAliveManager = new KeepAliveManager(networkNode, peerManager);
    // We need to have both the initial data delivered and the hidden service published
    networkReadyBinding = EasyBind.combine(hiddenServicePublished, preliminaryDataReceived, (hiddenServicePublished, preliminaryDataReceived) -> hiddenServicePublished && preliminaryDataReceived);
    networkReadySubscription = networkReadyBinding.subscribe((observable, oldValue, newValue) -> {
        if (newValue)
            onNetworkReady();
    });
}
Also used : Clock(io.bitsquare.common.Clock) PubKeyRing(io.bitsquare.common.crypto.PubKeyRing) Inject(com.google.inject.Inject) AddDataMessage(io.bitsquare.p2p.storage.messages.AddDataMessage) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry) LoggerFactory(org.slf4j.LoggerFactory) Socks5ProxyProvider(io.bitsquare.network.Socks5ProxyProvider) SettableFuture(com.google.common.util.concurrent.SettableFuture) CryptoException(io.bitsquare.common.crypto.CryptoException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PeerManager(io.bitsquare.p2p.peers.PeerManager) KeyRing(io.bitsquare.common.crypto.KeyRing) RefreshTTLMessage(io.bitsquare.p2p.storage.messages.RefreshTTLMessage) MonadicBinding(org.fxmisc.easybind.monadic.MonadicBinding) FileUtil(io.bitsquare.storage.FileUtil) RequestDataManager(io.bitsquare.p2p.peers.getdata.RequestDataManager) ProtectedMailboxStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry) EncryptionService(io.bitsquare.crypto.EncryptionService) Subscription(org.fxmisc.easybind.Subscription) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull) BanList(io.bitsquare.p2p.peers.BanList) java.util(java.util) MailboxStoragePayload(io.bitsquare.p2p.storage.payload.MailboxStoragePayload) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) BroadcastMessage(io.bitsquare.p2p.storage.messages.BroadcastMessage) io.bitsquare.p2p.network(io.bitsquare.p2p.network) PeerExchangeManager(io.bitsquare.p2p.peers.peerexchange.PeerExchangeManager) Log(io.bitsquare.app.Log) P2PDataStorage(io.bitsquare.p2p.storage.P2PDataStorage) BroadcastHandler(io.bitsquare.p2p.peers.BroadcastHandler) javafx.beans.property(javafx.beans.property) Utilities(io.bitsquare.common.util.Utilities) Logger(org.slf4j.Logger) UserThread(io.bitsquare.common.UserThread) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PublicKey(java.security.PublicKey) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) Futures(com.google.common.util.concurrent.Futures) SeedNodesRepository(io.bitsquare.p2p.seed.SeedNodesRepository) EasyBind(org.fxmisc.easybind.EasyBind) Broadcaster(io.bitsquare.p2p.peers.Broadcaster) Paths(java.nio.file.Paths) KeepAliveManager(io.bitsquare.p2p.peers.keepalive.KeepAliveManager) Named(com.google.inject.name.Named) Storage(io.bitsquare.storage.Storage) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DecryptedMsgWithPubKey(io.bitsquare.crypto.DecryptedMsgWithPubKey) io.bitsquare.p2p.messaging(io.bitsquare.p2p.messaging) NetworkOptionKeys(io.bitsquare.network.NetworkOptionKeys) PeerManager(io.bitsquare.p2p.peers.PeerManager) PeerExchangeManager(io.bitsquare.p2p.peers.peerexchange.PeerExchangeManager) P2PDataStorage(io.bitsquare.p2p.storage.P2PDataStorage) Broadcaster(io.bitsquare.p2p.peers.Broadcaster) RequestDataManager(io.bitsquare.p2p.peers.getdata.RequestDataManager) KeepAliveManager(io.bitsquare.p2p.peers.keepalive.KeepAliveManager) File(java.io.File)

Example 2 with Clock

use of io.bitsquare.common.Clock in project bitsquare by bitsquare.

the class StressTestMailboxMessage method createPeerNode.

@NotNull
private P2PService createPeerNode(int n, int port) {
    // peer data directories
    final File peerDir = new File(testDataDir.toFile(), String.format("peer-%06d", n));
    final File peerTorDir = new File(peerDir, "tor");
    final File peerStorageDir = new File(peerDir, "db");
    final File peerKeysDir = new File(peerDir, "keys");
    //noinspection ResultOfMethodCallIgnored
    // needed for creating the key ring
    peerKeysDir.mkdirs();
    // peer keys
    final KeyStorage peerKeyStorage = new KeyStorage(peerKeysDir);
    final KeyRing peerKeyRing = new KeyRing(peerKeyStorage);
    final EncryptionService peerEncryptionService = new EncryptionService(peerKeyRing);
    return new P2PService(seedNodesRepository, port, peerTorDir, useLocalhost, REGTEST_NETWORK_ID, P2PService.MAX_CONNECTIONS_DEFAULT, peerStorageDir, null, null, null, new Clock(), null, peerEncryptionService, peerKeyRing);
}
Also used : PubKeyRing(io.bitsquare.common.crypto.PubKeyRing) KeyRing(io.bitsquare.common.crypto.KeyRing) KeyStorage(io.bitsquare.common.crypto.KeyStorage) EncryptionService(io.bitsquare.crypto.EncryptionService) Clock(io.bitsquare.common.Clock) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Clock

use of io.bitsquare.common.Clock in project bitsquare by bitsquare.

the class DummySeedNode method createAndStartP2PService.

@VisibleForTesting
public void createAndStartP2PService(NodeAddress mySeedNodeAddress, int maxConnections, boolean useLocalhost, int networkId, boolean useDetailedLogging, @Nullable Set<NodeAddress> progArgSeedNodes, @Nullable P2PServiceListener listener) {
    Path appPath = Paths.get(defaultUserDataDir, "Bitsquare_seed_node_" + String.valueOf(mySeedNodeAddress.getFullAddress().replace(":", "_")));
    String logPath = Paths.get(appPath.toString(), "logs").toString();
    Log.setup(logPath);
    log.debug("Log files under: " + logPath);
    Version.printVersion();
    Utilities.printSysInfo();
    Log.setLevel(logLevel);
    SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
    if (progArgSeedNodes != null && !progArgSeedNodes.isEmpty()) {
        if (useLocalhost)
            seedNodesRepository.setLocalhostSeedNodeAddresses(progArgSeedNodes);
        else
            seedNodesRepository.setTorSeedNodeAddresses(progArgSeedNodes);
    }
    File storageDir = Paths.get(appPath.toString(), "db").toFile();
    if (storageDir.mkdirs())
        log.debug("Created storageDir at " + storageDir.getAbsolutePath());
    File torDir = Paths.get(appPath.toString(), "tor").toFile();
    if (torDir.mkdirs())
        log.debug("Created torDir at " + torDir.getAbsolutePath());
    seedNodesRepository.setNodeAddressToExclude(mySeedNodeAddress);
    seedNodeP2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, maxConnections, torDir, useLocalhost, networkId, storageDir, null, null, null, new Clock(), null, null, null);
    seedNodeP2PService.start(listener);
}
Also used : Path(java.nio.file.Path) SeedNodesRepository(io.bitsquare.p2p.seed.SeedNodesRepository) Clock(io.bitsquare.common.Clock) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with Clock

use of io.bitsquare.common.Clock in project bitsquare by bitsquare.

the class TestUtils method getAndAuthenticateP2PService.

public static P2PService getAndAuthenticateP2PService(int port, EncryptionService encryptionService, KeyRing keyRing, boolean useLocalhost, Set<NodeAddress> seedNodes) throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
    if (seedNodes != null && !seedNodes.isEmpty()) {
        if (useLocalhost)
            seedNodesRepository.setLocalhostSeedNodeAddresses(seedNodes);
        else
            seedNodesRepository.setTorSeedNodeAddresses(seedNodes);
    }
    P2PService p2PService = new P2PService(seedNodesRepository, port, new File("seed_node_" + port), useLocalhost, 2, P2PService.MAX_CONNECTIONS_DEFAULT, new File("dummy"), null, null, null, new Clock(), null, encryptionService, keyRing);
    p2PService.start(new P2PServiceListener() {

        @Override
        public void onRequestingDataCompleted() {
        }

        @Override
        public void onNoSeedNodeAvailable() {
        }

        @Override
        public void onNoPeersAvailable() {
        }

        @Override
        public void onTorNodeReady() {
        }

        @Override
        public void onBootstrapComplete() {
            latch.countDown();
        }

        @Override
        public void onHiddenServicePublished() {
        }

        @Override
        public void onSetupFailed(Throwable throwable) {
        }
    });
    latch.await();
    Thread.sleep(2000);
    return p2PService;
}
Also used : SeedNodesRepository(io.bitsquare.p2p.seed.SeedNodesRepository) CountDownLatch(java.util.concurrent.CountDownLatch) Clock(io.bitsquare.common.Clock)

Aggregations

Clock (io.bitsquare.common.Clock)4 SeedNodesRepository (io.bitsquare.p2p.seed.SeedNodesRepository)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 KeyRing (io.bitsquare.common.crypto.KeyRing)2 PubKeyRing (io.bitsquare.common.crypto.PubKeyRing)2 EncryptionService (io.bitsquare.crypto.EncryptionService)2 File (java.io.File)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 Log (io.bitsquare.app.Log)1 UserThread (io.bitsquare.common.UserThread)1 CryptoException (io.bitsquare.common.crypto.CryptoException)1 KeyStorage (io.bitsquare.common.crypto.KeyStorage)1 Utilities (io.bitsquare.common.util.Utilities)1 DecryptedMsgWithPubKey (io.bitsquare.crypto.DecryptedMsgWithPubKey)1