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);
}
}
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;
}
}
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);
}
}
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.");
}
}
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.");
}
}
}
});
}
}
Aggregations