Add a super-interface containing just isApplicable for NodeRequest and NodeAction to let findApplicableNodes be used on both.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6661 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Charlie Groves
2011-06-18 22:47:23 +00:00
parent 2873c31f45
commit 8f63caef6e
2 changed files with 16 additions and 17 deletions
@@ -134,7 +134,7 @@ public abstract class ChatChannelManager
public void collectChatHistory (final Name user, final ResultListener<ChatHistoryResult> lner) public void collectChatHistory (final Name user, final ResultListener<ChatHistoryResult> lner)
{ {
_peerMan.invokeNodeRequest(new NodeRequest() { _peerMan.invokeNodeRequest(new NodeRequest() {
@Override public boolean isApplicable (NodeObject nodeobj) { public boolean isApplicable (NodeObject nodeobj) {
return true; // poll all nodes return true; // poll all nodes
} }
@Override protected void execute (InvocationService.ResultListener listener) { @Override protected void execute (InvocationService.ResultListener listener) {
@@ -401,7 +401,7 @@ public abstract class ChatChannelManager
public ChannelAction (ChatChannel channel) { public ChannelAction (ChatChannel channel) {
_channel = channel; _channel = channel;
} }
@Override public boolean isApplicable (NodeObject nodeobj) { public boolean isApplicable (NodeObject nodeobj) {
return ((CrowdNodeObject)nodeobj).hostedChannels.contains(_channel); return ((CrowdNodeObject)nodeobj).hostedChannels.contains(_channel);
} }
protected ChatChannel _channel; protected ChatChannel _channel;
@@ -143,15 +143,19 @@ public abstract class PeerManager
void fail (String peerName); void fail (String peerName);
} }
public static interface NodeApplicant
{
/** Returns true if this should be executed on the specified node. This will be
* called on the originating server to decide whether or not to deliver the action to the
* server in question. */
boolean isApplicable (NodeObject nodeobj);
}
/** /**
* Encapsulates code that is meant to be executed one or more servers. * Encapsulates code that is meant to be executed one or more servers.
*/ */
public static abstract class NodeAction implements Streamable.Closure public static abstract class NodeAction implements Streamable.Closure, NodeApplicant
{ {
/** Returns true if this action should be executed on the specified node. This will be
* called on the originating server to decide whether or not to deliver the action to the
* server in question. */
public abstract boolean isApplicable (NodeObject nodeobj);
/** Invokes the action on the target server. */ /** Invokes the action on the target server. */
public void invoke () { public void invoke () {
@@ -168,13 +172,8 @@ public abstract class PeerManager
/** /**
* Encapsulates code that is meant to be executed one or more servers and return a result. * Encapsulates code that is meant to be executed one or more servers and return a result.
*/ */
public static abstract class NodeRequest implements Streamable.Closure public static abstract class NodeRequest implements Streamable.Closure, NodeApplicant
{ {
/** Returns true if this request should be executed on the specified node. This will be
* called on the originating server to decide whether or not to deliver the request to the
* server in question. */
public abstract boolean isApplicable (NodeObject nodeobj);
/** Invokes the action on the target server. */ /** Invokes the action on the target server. */
public void invoke (final InvocationService.ResultListener listener) { public void invoke (final InvocationService.ResultListener listener) {
try { try {
@@ -529,16 +528,16 @@ public abstract class PeerManager
} }
/** /**
* Returns all nodes for which <code>request.isApplicable</code> returns true. * Returns all nodes for which <code>applicant.isApplicable</code> returns true.
*/ */
public Set<String> findApplicableNodes (NodeRequest request) public Set<String> findApplicableNodes (NodeApplicant applicant)
{ {
Set<String> nodes = Sets.newHashSet(); Set<String> nodes = Sets.newHashSet();
if (request.isApplicable(_nodeobj)) { if (applicant.isApplicable(_nodeobj)) {
nodes.add(_nodeobj.nodeName); nodes.add(_nodeobj.nodeName);
} }
for (PeerNode peer : _peers.values()) { for (PeerNode peer : _peers.values()) {
if (request.isApplicable(peer.nodeobj)) { if (applicant.isApplicable(peer.nodeobj)) {
nodes.add(peer.getNodeName()); nodes.add(peer.getNodeName());
} }
} }