This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
[PAN-2730] Create MaintainedPeers class #1484
Merged
mbaxter
merged 4 commits into
PegaSysEng:master
from
mbaxter:PAN-2730/create-maintained-peers
May 22, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/peers/MaintainedPeers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.p2p.peers; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import tech.pegasys.pantheon.util.Subscribers; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import io.vertx.core.impl.ConcurrentHashSet; | ||
|
||
/** Represents a set of peers for which connections should be actively maintained. */ | ||
public class MaintainedPeers { | ||
private final Set<Peer> maintainedPeers = new ConcurrentHashSet<>(); | ||
private final Subscribers<PeerAddedCallback> addedSubscribers = new Subscribers<>(); | ||
private final Subscribers<PeerRemovedCallback> removedCallbackSubscribers = new Subscribers<>(); | ||
|
||
public boolean add(final Peer peer) { | ||
checkArgument( | ||
peer.getEnodeURL().isListening(), | ||
"Invalid enode url. Enode url must contain a non-zero listening port."); | ||
boolean wasAdded = maintainedPeers.add(peer); | ||
addedSubscribers.forEach(s -> s.onPeerAdded(peer, wasAdded)); | ||
return wasAdded; | ||
} | ||
|
||
public boolean remove(final Peer peer) { | ||
boolean wasRemoved = maintainedPeers.remove(peer); | ||
removedCallbackSubscribers.forEach(s -> s.onPeerRemoved(peer, wasRemoved)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous. |
||
return wasRemoved; | ||
} | ||
|
||
public boolean contains(final Peer peer) { | ||
return maintainedPeers.contains(peer); | ||
} | ||
|
||
public int size() { | ||
return maintainedPeers.size(); | ||
} | ||
|
||
public void subscribeAdd(final PeerAddedCallback callback) { | ||
addedSubscribers.subscribe(callback); | ||
} | ||
|
||
public void subscribeRemove(final PeerRemovedCallback callback) { | ||
removedCallbackSubscribers.subscribe(callback); | ||
} | ||
|
||
public Stream<Peer> streamPeers() { | ||
return maintainedPeers.stream(); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface PeerAddedCallback { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FunctionalInterface ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call - done |
||
void onPeerAdded(Peer peer, boolean wasAdded); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface PeerRemovedCallback { | ||
void onPeerRemoved(Peer peer, boolean wasRemoved); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not invoking subscribers in parallel ? Since there is no particular check in the foreach loop it is doable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving this as-is for now because I don't want to get into changing the
Subscribers
interface.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have too.
I was just suggesting to do :
addedSubscribers.parallelStream().forEach(s -> s.onPeerAdded(peer, wasAdded));
instead ofaddedSubscribers.forEach(s -> s.onPeerAdded(peer, wasAdded));