From 0e575a8b6ff912fb8c941c476f21ff26245c9209 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Tue, 24 Aug 2010 22:51:15 +0000 Subject: [PATCH] Allow peers to specify a namespace. If they do, only hook up to peers with node names starting with the namespace. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6133 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/peer/server/PeerManager.java | 27 ++++++++++++++----- .../peer/server/persist/NodeRepository.java | 26 +++++++++++++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/java/com/threerings/presents/peer/server/PeerManager.java b/src/java/com/threerings/presents/peer/server/PeerManager.java index b103818a8..89cb38cb6 100644 --- a/src/java/com/threerings/presents/peer/server/PeerManager.java +++ b/src/java/com/threerings/presents/peer/server/PeerManager.java @@ -37,7 +37,6 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.inject.Inject; import com.google.inject.Injector; - import com.samskivert.util.ArrayIntSet; import com.samskivert.util.ChainedResultListener; import com.samskivert.util.Interval; @@ -245,6 +244,17 @@ public abstract class PeerManager Predicates.notNull()); } + /** + * Initializes this peer manager to connect to all other nodes in the NODES table. See + * {@link #init(String, String, String, String, int, String)} for the behavior of the method + * and the meaning of its parameters. + */ + public void init (String nodeName, String sharedSecret, String hostName, + String publicHostName, int port) + { + init(nodeName, sharedSecret, hostName, publicHostName, port, ""); + } + /** * Initializes this peer manager and initiates the process of connecting to its peer nodes. * This will also reconfigure the ConnectionManager and ClientManager with peer related bits, @@ -255,14 +265,18 @@ public abstract class PeerManager * @param sharedSecret a shared secret used to allow the peers to authenticate with one * another. * @param hostName the DNS name of the server running this node. - * @param publicHostName if non-null, a separate public DNS hostname by which the node is to be - * known to normal clients (we may want inter-peer communication to take place over a different - * network than the communication between real clients and the various peer servers). + * @param publicHostName if non-null, a separate public DNS hostname by which the node is to + * be known to normal clients (we may want inter-peer communication to take place over a + * different network than the communication between real clients and the various peer + * servers). * @param port the port on which other nodes should connect to us. + * @param nodeNamespace The namespace for nodes to peer with. This node will connect to other + * nodes with the same prefix from the NODES table. */ public void init (String nodeName, String sharedSecret, - String hostName, String publicHostName, int port) + String hostName, String publicHostName, int port, String nodeNamespace) { + _nodeNamespace = nodeNamespace; _nodeName = nodeName; _sharedSecret = sharedSecret; @@ -993,7 +1007,7 @@ public abstract class PeerManager // let the world know that we're alive _noderepo.heartbeatNode(_nodeName); // then load up all the peer records - _nodes = _noderepo.loadNodes(); + _nodes = _noderepo.loadNodes(_nodeNamespace); } @Override public void handleSuccess () { @@ -1494,6 +1508,7 @@ public abstract class PeerManager protected String _nodeName, _sharedSecret; protected NodeRecord _self; protected NodeObject _nodeobj; + protected String _nodeNamespace; protected Map _peers = Maps.newHashMap(); /** Used to resolve dependencies in unserialized {@link NodeAction} instances. */ diff --git a/src/java/com/threerings/presents/peer/server/persist/NodeRepository.java b/src/java/com/threerings/presents/peer/server/persist/NodeRepository.java index b56fb454f..3330450c1 100644 --- a/src/java/com/threerings/presents/peer/server/persist/NodeRepository.java +++ b/src/java/com/threerings/presents/peer/server/persist/NodeRepository.java @@ -21,19 +21,23 @@ package com.threerings.presents.peer.server.persist; -import java.sql.Timestamp; - import java.util.Collections; import java.util.List; import java.util.Set; +import java.sql.Timestamp; + +import com.google.common.collect.Lists; import com.google.inject.Inject; import com.google.inject.Singleton; +import com.samskivert.util.StringUtil; + import com.samskivert.depot.DepotRepository; import com.samskivert.depot.PersistenceContext; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.clause.QueryClause; +import com.samskivert.depot.clause.Where; /** * Used to share information on active nodes in a Presents server cluster. @@ -54,9 +58,25 @@ public class NodeRepository extends DepotRepository */ public List loadNodes () { + return loadNodes(""); + } + + /** + * Returns a list of all nodes registered in the repository with names starting with the given + * string. + */ + public List loadNodes (String namespace) + { + Iterable clauses; + if (StringUtil.isBlank(namespace)) { + clauses = Collections.emptyList(); + } else { + QueryClause clause = new Where(NodeRecord.NODE_NAME.like(namespace + "%")); + clauses = Lists.newArrayList(clause); + } // we specifically avoid caching this query because we want the servers to always see the // most up to date set of nodes - return findAll(NodeRecord.class, CacheStrategy.NONE, Collections.emptySet()); + return findAll(NodeRecord.class, CacheStrategy.NONE, clauses); } /**