Search in sources :

Example 26 with NodeAddress

use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.

the class PeerManager method onDisconnect.

@Override
public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) {
    Log.logIfStressTests("onDisconnect of peer " + (connection.getPeersNodeAddressOptional().isPresent() ? connection.getPeersNodeAddressOptional().get() : "PeersNode unknown") + " / No. of connections: " + networkNode.getAllConnections().size() + " / closeConnectionReason: " + closeConnectionReason);
    final Optional<NodeAddress> addressOptional = connection.getPeersNodeAddressOptional();
    log.debug("onDisconnect: peer = {}{} / closeConnectionReason: {}", (addressOptional.isPresent() ? addressOptional.get().hostName : "not known yet (connection id=" + connection.getUid() + ")"), isSeedNode(connection) ? " (SeedNode)" : "", closeConnectionReason);
    handleConnectionFault(connection);
    lostAllConnections = networkNode.getAllConnections().isEmpty();
    if (lostAllConnections) {
        stopped = true;
        listeners.stream().forEach(Listener::onAllConnectionsLost);
    }
    if (connection.getPeersNodeAddressOptional().isPresent() && isNodeBanned(closeConnectionReason, connection)) {
        final NodeAddress nodeAddress = connection.getPeersNodeAddressOptional().get();
        seedNodeAddresses.remove(nodeAddress);
        removePersistedPeer(nodeAddress);
        removeReportedPeer(nodeAddress);
    }
}
Also used : NodeAddress(io.bitsquare.p2p.NodeAddress)

Example 27 with NodeAddress

use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.

the class PeerManager method removeReportedPeer.

@Nullable
private Peer removeReportedPeer(NodeAddress nodeAddress) {
    List<Peer> reportedPeersClone = new ArrayList<>(reportedPeers);
    Optional<Peer> reportedPeerOptional = reportedPeersClone.stream().filter(e -> e.nodeAddress.equals(nodeAddress)).findAny();
    if (reportedPeerOptional.isPresent()) {
        Peer reportedPeer = reportedPeerOptional.get();
        removeReportedPeer(reportedPeer);
        return reportedPeer;
    } else {
        return null;
    }
}
Also used : Clock(io.bitsquare.common.Clock) java.util(java.util) Logger(org.slf4j.Logger) Peer(io.bitsquare.p2p.peers.peerexchange.Peer) UserThread(io.bitsquare.common.UserThread) LoggerFactory(org.slf4j.LoggerFactory) NodeAddress(io.bitsquare.p2p.NodeAddress) Timer(io.bitsquare.common.Timer) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) io.bitsquare.p2p.network(io.bitsquare.p2p.network) Storage(io.bitsquare.storage.Storage) Nullable(javax.annotation.Nullable) Log(io.bitsquare.app.Log) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Peer(io.bitsquare.p2p.peers.peerexchange.Peer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Nullable(javax.annotation.Nullable)

Example 28 with NodeAddress

use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.

the class PeerExchangeManager method closeHandler.

private void closeHandler(Connection connection) {
    Log.traceCall();
    Optional<NodeAddress> peersNodeAddressOptional = connection.getPeersNodeAddressOptional();
    if (peersNodeAddressOptional.isPresent()) {
        NodeAddress nodeAddress = peersNodeAddressOptional.get();
        if (handlerMap.containsKey(nodeAddress)) {
            handlerMap.get(nodeAddress).cancel();
            handlerMap.remove(nodeAddress);
        }
    } else {
        log.trace("closeHandler: nodeAddress not set in connection " + connection);
    }
}
Also used : NodeAddress(io.bitsquare.p2p.NodeAddress)

Example 29 with NodeAddress

use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.

the class PeerExchangeManager method requestWithAvailablePeers.

private void requestWithAvailablePeers() {
    Log.traceCall();
    if (!stopped) {
        if (!peerManager.hasSufficientConnections()) {
            // We create a new list of not connected candidates
            // 1. shuffled reported peers  
            // 2. shuffled persisted peers 
            // 3. Add as last shuffled seedNodes (least priority)
            List<NodeAddress> list = getFilteredNonSeedNodeList(getNodeAddresses(peerManager.getReportedPeers()), new ArrayList<>());
            Collections.shuffle(list);
            List<NodeAddress> filteredPersistedPeers = getFilteredNonSeedNodeList(getNodeAddresses(peerManager.getPersistedPeers()), list);
            Collections.shuffle(filteredPersistedPeers);
            list.addAll(filteredPersistedPeers);
            List<NodeAddress> filteredSeedNodeAddresses = getFilteredList(new ArrayList<>(seedNodeAddresses), list);
            Collections.shuffle(filteredSeedNodeAddresses);
            list.addAll(filteredSeedNodeAddresses);
            log.debug("Number of peers in list for connectToMorePeers: {}", list.size());
            log.trace("Filtered connectToMorePeers list: list=" + list);
            if (!list.isEmpty()) {
                // Dont shuffle as we want the seed nodes at the last entries
                NodeAddress nextCandidate = list.get(0);
                list.remove(nextCandidate);
                requestReportedPeers(nextCandidate, list);
            } else {
                log.debug("No more peers are available for requestReportedPeers. We will try again after a pause.");
                if (retryTimer == null)
                    retryTimer = UserThread.runAfter(() -> {
                        if (!stopped) {
                            log.trace("retryTimer called from requestWithAvailablePeers code path");
                            stopRetryTimer();
                            requestWithAvailablePeers();
                        } else {
                            stopRetryTimer();
                            log.warn("We have stopped already. We ignore that retryTimer.run call.");
                        }
                    }, RETRY_DELAY_SEC);
            }
        } else {
            log.debug("We have already sufficient connections.");
        }
    } else {
        log.trace("We have stopped already. We ignore that requestWithAvailablePeers call.");
    }
}
Also used : NodeAddress(io.bitsquare.p2p.NodeAddress)

Example 30 with NodeAddress

use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.

the class P2PDataStorage method onDisconnect.

@Override
public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) {
    if (connection.hasPeersNodeAddress() && !closeConnectionReason.isIntended) {
        map.values().stream().forEach(protectedData -> {
            ExpirablePayload expirablePayload = protectedData.getStoragePayload();
            if (expirablePayload instanceof RequiresOwnerIsOnlinePayload) {
                RequiresOwnerIsOnlinePayload requiresOwnerIsOnlinePayload = (RequiresOwnerIsOnlinePayload) expirablePayload;
                NodeAddress ownerNodeAddress = requiresOwnerIsOnlinePayload.getOwnerNodeAddress();
                if (ownerNodeAddress.equals(connection.getPeersNodeAddressOptional().get())) {
                    ByteArray hashOfPayload = getHashAsByteArray(expirablePayload);
                    boolean containsKey = map.containsKey(hashOfPayload);
                    if (containsKey) {
                        log.debug("We remove the data as the data owner got disconnected with " + "closeConnectionReason=" + closeConnectionReason);
                        Log.logIfStressTests("We remove the data as the data owner got disconnected with " + "closeConnectionReason=" + closeConnectionReason + " / isIntended=" + closeConnectionReason.isIntended + " / peer=" + (connection.getPeersNodeAddressOptional().isPresent() ? connection.getPeersNodeAddressOptional().get() : "PeersNode unknown"));
                        protectedData.backDate();
                        if (protectedData.isExpired())
                            doRemoveProtectedExpirableData(protectedData, hashOfPayload);
                    } else {
                        log.debug("Remove data ignored as we don't have an entry for that data.");
                    }
                }
            }
        });
    }
}
Also used : NodeAddress(io.bitsquare.p2p.NodeAddress)

Aggregations

NodeAddress (io.bitsquare.p2p.NodeAddress)34 WalletService (io.bitsquare.btc.WalletService)5 Contract (io.bitsquare.trade.Contract)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Log (io.bitsquare.app.Log)4 AddressEntry (io.bitsquare.btc.AddressEntry)4 Timer (io.bitsquare.common.Timer)4 UserThread (io.bitsquare.common.UserThread)4 PubKeyRing (io.bitsquare.common.crypto.PubKeyRing)4 P2PService (io.bitsquare.p2p.P2PService)4 SendMailboxMessageListener (io.bitsquare.p2p.messaging.SendMailboxMessageListener)4 TimeUnit (java.util.concurrent.TimeUnit)4 DummySeedNode (io.bitsquare.p2p.DummySeedNode)3 P2PServiceListener (io.bitsquare.p2p.P2PServiceListener)3 Connection (io.bitsquare.p2p.network.Connection)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Collectors (java.util.stream.Collectors)3 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 Futures (com.google.common.util.concurrent.Futures)2