Added the necessary machinations to make getOldValue() and getOldEntry()

work on the server.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2510 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-04-30 22:32:04 +00:00
parent 99fa7fd6ff
commit 43edd884c0
6 changed files with 63 additions and 44 deletions
@@ -1,5 +1,5 @@
// //
// $Id: AttributeChangedEvent.java,v 1.14 2002/12/20 23:29:04 mdb Exp $ // $Id: AttributeChangedEvent.java,v 1.15 2003/04/30 22:32:04 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -26,10 +26,12 @@ public class AttributeChangedEvent extends NamedEvent
* primitive types, the reflection-defined object-alternative is * primitive types, the reflection-defined object-alternative is
* used). * used).
*/ */
public AttributeChangedEvent (int targetOid, String name, Object value) public AttributeChangedEvent (int targetOid, String name,
Object value, Object oldValue)
{ {
super(targetOid, name); super(targetOid, name);
_value = value; _value = value;
_oldValue = oldValue;
} }
/** /**
@@ -108,8 +110,10 @@ public class AttributeChangedEvent extends NamedEvent
public boolean applyToObject (DObject target) public boolean applyToObject (DObject target)
throws ObjectAccessException throws ObjectAccessException
{ {
// grab the previous value // grab the previous value (if we're on the client)
_oldValue = target.getAttribute(_name); if (_oldValue == UNSET_OLD_VALUE) {
_oldValue = target.getAttribute(_name);
}
// pass the new value on to the object // pass the new value on to the object
target.setAttribute(_name, _value); target.setAttribute(_name, _value);
return true; return true;
@@ -133,5 +137,5 @@ public class AttributeChangedEvent extends NamedEvent
} }
protected Object _value; protected Object _value;
protected transient Object _oldValue; protected transient Object _oldValue = UNSET_OLD_VALUE;
} }
@@ -1,5 +1,5 @@
// //
// $Id: DEvent.java,v 1.12 2002/12/20 23:41:26 mdb Exp $ // $Id: DEvent.java,v 1.13 2003/04/30 22:32:04 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -115,4 +115,16 @@ public abstract class DEvent implements Streamable
/** The oid of the client that generated this event. */ /** The oid of the client that generated this event. */
protected transient int _soid = -1; protected transient int _soid = -1;
/** Used to differentiate between null meaning we haven't initialized
* our old value and null being the actual old value. */
protected static final Object UNSET_OLD_VALUE = new Object();
/** Used to differentiate between null meaning we haven't initialized
* our old entry and null being the actual old entry. */
protected static final DSet.Entry UNSET_OLD_ENTRY = new DSet.Entry() {
public Comparable getKey () {
return null;
}
};
} }
@@ -1,8 +1,9 @@
// //
// $Id: DObject.java,v 1.62 2003/04/10 17:48:42 mdb Exp $ // $Id: DObject.java,v 1.63 2003/04/30 22:32:04 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
@@ -723,8 +724,15 @@ public class DObject implements Streamable
*/ */
protected void requestAttributeChange (String name, Object value) protected void requestAttributeChange (String name, Object value)
{ {
// dispatch an attribute changed event try {
postEvent(new AttributeChangedEvent(_oid, name, value)); // dispatch an attribute changed event
postEvent(new AttributeChangedEvent(
_oid, name, value, getAttribute(name)));
} catch (ObjectAccessException oae) {
Log.warning("Unable to request attributeChange [name=" + name +
", value=" + value + ", error=" + oae + "].");
}
} }
/** /**
@@ -733,8 +741,17 @@ public class DObject implements Streamable
*/ */
protected void requestElementUpdate (String name, Object value, int index) protected void requestElementUpdate (String name, Object value, int index)
{ {
// dispatch an attribute changed event try {
postEvent(new ElementUpdatedEvent(_oid, name, value, index)); // dispatch an attribute changed event
Object oldValue = Array.get(getAttribute(name), index);
postEvent(new ElementUpdatedEvent(
_oid, name, value, oldValue, index));
} catch (ObjectAccessException oae) {
Log.warning("Unable to request elementUpdate [name=" + name +
", value=" + value + ", index=" + index +
", error=" + oae + "].");
}
} }
/** /**
@@ -766,8 +783,6 @@ public class DObject implements Streamable
// immediately // immediately
boolean alreadyApplied = false; boolean alreadyApplied = false;
if (_omgr != null && _omgr.isManager(this)) { if (_omgr != null && _omgr.isManager(this)) {
// Log.info("Immediately adding [name=" + name +
// ", entry=" + entry + "].");
set.add(entry); set.add(entry);
alreadyApplied = true; alreadyApplied = true;
} }
@@ -791,8 +806,6 @@ public class DObject implements Streamable
// immediately // immediately
DSet.Entry oldEntry = null; DSet.Entry oldEntry = null;
if (_omgr != null && _omgr.isManager(this)) { if (_omgr != null && _omgr.isManager(this)) {
// Log.info("Immediately removing [name=" + name +
// ", key=" + key + "].");
oldEntry = set.get(key); oldEntry = set.get(key);
set.removeKey(key); set.removeKey(key);
} }
@@ -816,8 +829,6 @@ public class DObject implements Streamable
// immediately // immediately
DSet.Entry oldEntry = null; DSet.Entry oldEntry = null;
if (_omgr != null && _omgr.isManager(this)) { if (_omgr != null && _omgr.isManager(this)) {
// Log.info("Immediately updating [name=" + name +
// ", entry=" + entry + "].");
oldEntry = set.get(entry.getKey()); oldEntry = set.get(entry.getKey());
set.update(entry); set.update(entry);
} }
@@ -1,5 +1,5 @@
// //
// $Id: ElementUpdatedEvent.java,v 1.4 2002/12/20 23:29:04 mdb Exp $ // $Id: ElementUpdatedEvent.java,v 1.5 2003/04/30 22:32:04 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -29,13 +29,17 @@ public class ElementUpdatedEvent extends NamedEvent
* element has changed. * element has changed.
* @param value the new value of the element (in the case of primitive * @param value the new value of the element (in the case of primitive
* types, the reflection-defined object-alternative is used). * types, the reflection-defined object-alternative is used).
* @param oldValue the previous value of the element (in the case of
* primitive types, the reflection-defined object-alternative is
* used).
* @param index the index in the array of the updated element. * @param index the index in the array of the updated element.
*/ */
public ElementUpdatedEvent ( public ElementUpdatedEvent (
int targetOid, String name, Object value, int index) int targetOid, String name, Object value, Object oldValue, int index)
{ {
super(targetOid, name); super(targetOid, name);
_value = value; _value = value;
_oldValue = oldValue;
_index = index; _index = index;
} }
@@ -135,7 +139,9 @@ public class ElementUpdatedEvent extends NamedEvent
} }
// grab the previous value to provide to interested parties // grab the previous value to provide to interested parties
_oldValue = Array.get(field.get(target), _index); if (_oldValue != UNSET_OLD_VALUE) {
_oldValue = Array.get(field.get(target), _index);
}
// we don't do any magical expansion or any funny business; // we don't do any magical expansion or any funny business;
// the array should be big enough to contain the value being // the array should be big enough to contain the value being
@@ -1,5 +1,5 @@
// //
// $Id: EntryRemovedEvent.java,v 1.14 2003/03/10 18:29:54 mdb Exp $ // $Id: EntryRemovedEvent.java,v 1.15 2003/04/30 22:32:04 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -22,15 +22,7 @@ public class EntryRemovedEvent extends NamedEvent
* @param name the name of the attribute from which to remove the * @param name the name of the attribute from which to remove the
* specified entry. * specified entry.
* @param key the entry key that identifies the entry to remove. * @param key the entry key that identifies the entry to remove.
*/ * @param oldEntry the previous value of the entry.
public EntryRemovedEvent (int targetOid, String name, Comparable key)
{
this(targetOid, name, key, null);
}
/**
* Used when the distributed object already removed the entry before
* generating the event.
*/ */
public EntryRemovedEvent (int targetOid, String name, Comparable key, public EntryRemovedEvent (int targetOid, String name, Comparable key,
DSet.Entry oldEntry) DSet.Entry oldEntry)
@@ -70,7 +62,7 @@ public class EntryRemovedEvent extends NamedEvent
public boolean applyToObject (DObject target) public boolean applyToObject (DObject target)
throws ObjectAccessException throws ObjectAccessException
{ {
if (_oldEntry == null) { if (_oldEntry == UNSET_OLD_ENTRY) {
DSet set = (DSet)target.getAttribute(_name); DSet set = (DSet)target.getAttribute(_name);
// fetch the previous value for interested callers // fetch the previous value for interested callers
_oldEntry = set.get(_key); _oldEntry = set.get(_key);
@@ -97,5 +89,5 @@ public class EntryRemovedEvent extends NamedEvent
} }
protected Object _key; protected Object _key;
protected transient DSet.Entry _oldEntry; protected transient DSet.Entry _oldEntry = UNSET_OLD_ENTRY;
} }
@@ -1,5 +1,5 @@
// //
// $Id: EntryUpdatedEvent.java,v 1.10 2003/03/10 18:29:54 mdb Exp $ // $Id: EntryUpdatedEvent.java,v 1.11 2003/04/30 22:32:04 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -25,15 +25,7 @@ public class EntryUpdatedEvent extends NamedEvent
* @param name the name of the attribute in which to update the * @param name the name of the attribute in which to update the
* specified entry. * specified entry.
* @param entry the entry to update. * @param entry the entry to update.
*/ * @param oldEntry the previous value of the entry.
public EntryUpdatedEvent (int targetOid, String name, DSet.Entry entry)
{
this(targetOid, name, entry, null);
}
/**
* Used when the distributed object already updated the entry before
* generating the event.
*/ */
public EntryUpdatedEvent (int targetOid, String name, DSet.Entry entry, public EntryUpdatedEvent (int targetOid, String name, DSet.Entry entry,
DSet.Entry oldEntry) DSet.Entry oldEntry)
@@ -77,7 +69,9 @@ public class EntryUpdatedEvent extends NamedEvent
DSet set = (DSet)target.getAttribute(_name); DSet set = (DSet)target.getAttribute(_name);
// fetch the previous value for interested callers // fetch the previous value for interested callers
_oldEntry = set.get(_entry.getKey()); if (_oldEntry != UNSET_OLD_ENTRY) {
_oldEntry = set.get(_entry.getKey());
}
// update the entry // update the entry
if (!set.update(_entry)) { if (!set.update(_entry)) {
@@ -108,5 +102,5 @@ public class EntryUpdatedEvent extends NamedEvent
} }
protected DSet.Entry _entry; protected DSet.Entry _entry;
protected transient DSet.Entry _oldEntry; protected transient DSet.Entry _oldEntry = UNSET_OLD_ENTRY;
} }