Use a hashset for listeners

This commit is contained in:
chimp1984 2021-01-10 23:39:04 -05:00
parent 41a92eae7f
commit 9edad02e8f
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3

View File

@ -40,9 +40,11 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@ -89,7 +91,7 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
private final P2PDataStorage dataStorage;
private final PeerManager peerManager;
private final List<NodeAddress> seedNodeAddresses;
private Listener listener;
private final Set<Listener> listeners = new HashSet<>();
private final Map<NodeAddress, RequestDataHandler> handlerMap = new HashMap<>();
private final Map<String, GetDataRequestHandler> getDataRequestHandlers = new HashMap<>();
@ -146,7 +148,7 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
///////////////////////////////////////////////////////////////////////////////////////////
public void addListener(Listener listener) {
this.listener = listener;
listeners.add(listener);
}
public boolean requestPreliminaryData() {
@ -332,16 +334,16 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
// We delay because it can be that we get the HS published before we receive the
// preliminary data and the onPreliminaryDataReceived call triggers the
// dataUpdateRequested set to true, so we would also call the onUpdatedDataReceived.
UserThread.runAfter(listener::onPreliminaryDataReceived, 100, TimeUnit.MILLISECONDS);
UserThread.runAfter(() -> listeners.forEach(Listener::onPreliminaryDataReceived), 100, TimeUnit.MILLISECONDS);
}
// 2. Later we get a response from requestUpdatesData
if (dataUpdateRequested) {
dataUpdateRequested = false;
listener.onUpdatedDataReceived();
listeners.forEach(Listener::onUpdatedDataReceived);
}
listener.onDataReceived();
listeners.forEach(Listener::onDataReceived);
}
@Override
@ -368,10 +370,11 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
// Notify listeners
if (!nodeAddressOfPreliminaryDataRequest.isPresent()) {
if (peerManager.isSeedNode(nodeAddress))
listener.onNoSeedNodeAvailable();
else
listener.onNoPeersAvailable();
if (peerManager.isSeedNode(nodeAddress)) {
listeners.forEach(Listener::onNoSeedNodeAvailable);
} else {
listeners.forEach(Listener::onNoPeersAvailable);
}
}
requestFromNonSeedNodePeers();