Use the number of subscribers to determine how many responses to expect.

Take the lock immediately if there are no subscribers.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4576 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2007-02-14 02:30:37 +00:00
parent 9c03e16434
commit 51fed0676e
2 changed files with 34 additions and 16 deletions
@@ -60,20 +60,20 @@ public class NodeObject extends DObject
{ {
/** The resource type. Only resources of the same type will have their ids compared. */ /** The resource type. Only resources of the same type will have their ids compared. */
public String type; public String type;
/** The resource identifier, which can be <code>null</code> for singleton resources. */ /** The resource identifier, which can be <code>null</code> for singleton resources. */
public Comparable id; public Comparable id;
public Lock () public Lock ()
{ {
} }
public Lock (String type, Comparable id) public Lock (String type, Comparable id)
{ {
this.type = type; this.type = type;
this.id = id; this.id = id;
} }
// documentation inherited from interface Comparable // documentation inherited from interface Comparable
public int compareTo (Object other) public int compareTo (Object other)
{ {
@@ -85,19 +85,19 @@ public class NodeObject extends DObject
@SuppressWarnings("unchecked") int v2 = id.compareTo(olock.id); @SuppressWarnings("unchecked") int v2 = id.compareTo(olock.id);
return v2; return v2;
} }
// documentation inherited from interface DSet.Entry // documentation inherited from interface DSet.Entry
public Comparable getKey () public Comparable getKey ()
{ {
return this; return this;
} }
@Override // documentation inherited @Override // documentation inherited
public int hashCode () public int hashCode ()
{ {
return type.hashCode() + (id == null ? 0 : id.hashCode()); return type.hashCode() + (id == null ? 0 : id.hashCode());
} }
@Override // documentation inherited @Override // documentation inherited
public boolean equals (Object other) public boolean equals (Object other)
{ {
@@ -105,7 +105,7 @@ public class NodeObject extends DObject
return type.equals(olock.type) && ObjectUtil.equals(id, olock.id); return type.equals(olock.type) && ObjectUtil.equals(id, olock.id);
} }
} }
/** Used for informing peers of changes to persistent data. */ /** Used for informing peers of changes to persistent data. */
public static class CacheData extends SimpleStreamableObject public static class CacheData extends SimpleStreamableObject
{ {
@@ -115,8 +115,8 @@ public class NodeObject extends DObject
/** The stale data in the cache. */ /** The stale data in the cache. */
public Streamable data; public Streamable data;
public CacheData () public CacheData ()
{ {
} }
public CacheData (String cache, Streamable data) public CacheData (String cache, Streamable data)
@@ -128,7 +128,7 @@ public class NodeObject extends DObject
/** The service used to make requests of the node. */ /** The service used to make requests of the node. */
public PeerMarshaller peerService; public PeerMarshaller peerService;
/** Contains information on all clients connected to this node. */ /** Contains information on all clients connected to this node. */
public DSet<ClientInfo> clients = new DSet<ClientInfo>(); public DSet<ClientInfo> clients = new DSet<ClientInfo>();
@@ -137,13 +137,21 @@ public class NodeObject extends DObject
/** Used to broadcast a node's desire to acquire a lock. */ /** Used to broadcast a node's desire to acquire a lock. */
public Lock acquiringLock; public Lock acquiringLock;
/** Used to broadcast a node's desire to release a lock. */ /** Used to broadcast a node's desire to release a lock. */
public Lock releasingLock; public Lock releasingLock;
/** A field we use to broadcast changes to possible cached data. */ /** A field we use to broadcast changes to possible cached data. */
public CacheData cacheData; public CacheData cacheData;
/**
* Returns the number of subscribers to this node.
*/
public int getSubscriberCount ()
{
return _scount;
}
// AUTO-GENERATED: METHODS START // AUTO-GENERATED: METHODS START
/** /**
* Requests that the <code>peerService</code> field be set to the * Requests that the <code>peerService</code> field be set to the
@@ -296,7 +296,12 @@ public class PeerManager
queryLock(lock, new ResultListener<String>() { queryLock(lock, new ResultListener<String>() {
public void requestCompleted (String result) { public void requestCompleted (String result) {
if (result == null) { if (result == null) {
_locks.put(lock, new LockHandler(lock, true, listener)); if (_nodeobj.getSubscriberCount() == 0) {
_nodeobj.addToLocks(lock);
listener.requestCompleted(_nodeName);
} else {
_locks.put(lock, new LockHandler(lock, true, listener));
}
} else { } else {
listener.requestCompleted(result); listener.requestCompleted(result);
} }
@@ -318,7 +323,12 @@ public class PeerManager
queryLock(lock, new ResultListener<String>() { queryLock(lock, new ResultListener<String>() {
public void requestCompleted (String result) { public void requestCompleted (String result) {
if (_nodeName.equals(result)) { if (_nodeName.equals(result)) {
_locks.put(lock, new LockHandler(lock, false, listener)); if (_nodeobj.getSubscriberCount() == 0) {
_nodeobj.removeFromLocks(lock);
listener.requestCompleted(null);
} else {
_locks.put(lock, new LockHandler(lock, false, listener));
}
} else { } else {
if (result != null) { if (result != null) {
log.warning("Tried to release lock held by another peer [lock=" + log.warning("Tried to release lock held by another peer [lock=" +
@@ -796,7 +806,7 @@ public class PeerManager
} }
// find out exactly how many responses we need // find out exactly how many responses we need
_remaining = _peers.size(); _remaining = _nodeobj.getSubscriberCount();
// schedule a timeout to act if something goes wrong // schedule a timeout to act if something goes wrong
(_timeout = new Interval(PresentsServer.omgr) { (_timeout = new Interval(PresentsServer.omgr) {