Brought code into line with the new streaming world order.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1606 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-07-23 05:52:49 +00:00
parent 2330afc3cd
commit c3191b9170
42 changed files with 805 additions and 1232 deletions
@@ -1,14 +1,15 @@
//
// $Id: AttributeChangedEvent.java,v 1.11 2002/02/01 23:32:37 mdb Exp $
// $Id: AttributeChangedEvent.java,v 1.12 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import com.threerings.presents.io.ValueMarshaller;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* An attribute changed event is dispatched when a single attribute of a
@@ -17,11 +18,8 @@ import com.threerings.presents.io.ValueMarshaller;
*
* @see DObjectManager#postEvent
*/
public class AttributeChangedEvent extends TypedEvent
public class AttributeChangedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 1;
/**
* Constructs a new attribute changed event on the specified target
* object with the supplied attribute name and value.
@@ -121,28 +119,26 @@ public class AttributeChangedEvent extends TypedEvent
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
ValueMarshaller.writeTo(out, _value);
out.writeObject(_value);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
_value = ValueMarshaller.readFrom(in);
_value = in.readObject();
}
// documentation inherited
@@ -159,7 +155,8 @@ public class AttributeChangedEvent extends TypedEvent
buf.append("CHANGE:");
super.toString(buf);
buf.append(", name=").append(_name);
buf.append(", value=").append(_value);
buf.append(", value=");
StringUtil.toString(buf, _value);
}
protected String _name;
@@ -1,14 +1,14 @@
//
// $Id: AttributesChangedEvent.java,v 1.9 2002/02/01 23:32:37 mdb Exp $
// $Id: AttributesChangedEvent.java,v 1.10 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.samskivert.util.StringUtil;
import com.threerings.presents.io.ValueMarshaller;
/**
* An attribute<em>s</em> changed event is dispatched when multiple
@@ -17,11 +17,8 @@ import com.threerings.presents.io.ValueMarshaller;
*
* @see DObjectManager#postEvent
*/
public class AttributesChangedEvent extends TypedEvent
public class AttributesChangedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 2;
/**
* Constructs a new attribute changed event on the specified target
* object with the supplied attribute name and value.
@@ -170,32 +167,33 @@ public class AttributesChangedEvent extends TypedEvent
return true;
}
public short getType ()
{
return TYPE;
}
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeInt(_count);
for (int i = 0; i < _count; i++) {
out.writeUTF(_names[i]);
ValueMarshaller.writeTo(out, _values[i]);
for (int ii = 0; ii < _count; ii++) {
out.writeUTF(_names[ii]);
out.writeObject(_values[ii]);
}
}
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_count = in.readInt();
_names = new String[_count];
_values = new Object[_count];
for (int i = 0; i < _count; i++) {
_names[i] = in.readUTF();
_values[i] = ValueMarshaller.readFrom(in);
for (int ii = 0; ii < _count; ii++) {
_names[ii] = in.readUTF();
_values[ii] = in.readObject();
}
}
@@ -203,8 +201,10 @@ public class AttributesChangedEvent extends TypedEvent
{
buf.append("CHANGES:");
super.toString(buf);
buf.append(", names=").append(StringUtil.toString(_names));
buf.append(", values=").append(StringUtil.toString(_values));
buf.append(", names=");
StringUtil.toString(buf, _names);
buf.append(", values=");
StringUtil.toString(buf, _values);
}
protected int _count;
@@ -1,16 +1,15 @@
//
// $Id: CompoundEvent.java,v 1.4 2002/03/21 01:58:10 mdb Exp $
// $Id: CompoundEvent.java,v 1.5 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.threerings.presents.io.TypedObjectFactory;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.StreamableArrayList;
/**
* Used to manage and submit groups of events on a collection of
@@ -18,11 +17,8 @@ import com.threerings.presents.io.TypedObjectFactory;
*
* @see DObject#startTransaction
*/
public class CompoundEvent extends TypedEvent
public class CompoundEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 50;
/**
* Constructs a blank compound event in preparation for
* unserialization.
@@ -38,6 +34,7 @@ public class CompoundEvent extends TypedEvent
{
super(0); // we don't have a single target object oid
_omgr = omgr;
_events = new StreamableArrayList();
}
/**
@@ -58,7 +55,7 @@ public class CompoundEvent extends TypedEvent
* part of the entire transaction if it is committed or discarded if
* the transaction is cancelled.
*/
public void postEvent (TypedEvent event)
public void postEvent (DEvent event)
{
_events.add(event);
}
@@ -123,34 +120,24 @@ public class CompoundEvent extends TypedEvent
return false;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
int ecount = _events.size();
out.writeInt(ecount);
for (int i = 0; i < ecount; i++) {
TypedEvent event = (TypedEvent)_events.get(i);
TypedObjectFactory.writeTo(out, event);
}
super.writeObject(out);
out.writeObject(_events);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
int ecount = in.readInt();
for (int i = 0; i < ecount; i++) {
_events.add(TypedObjectFactory.readFrom(in));
}
super.readObject(in);
_events = (StreamableArrayList)in.readObject();
}
/**
@@ -181,13 +168,13 @@ public class CompoundEvent extends TypedEvent
/** The object manager that we'll post ourselves to when we're
* committed. */
protected DObjectManager _omgr;
/** A list of the events associated with this compound event. */
protected ArrayList _events = new ArrayList();
protected transient DObjectManager _omgr;
/** A list of the dobject participants in this transaction. They will
* be notified when we are committed or cancelled so that they can
* stop posting their events to us. */
protected ArrayList _participants;
protected transient ArrayList _participants;
/** A list of the events associated with this compound event. */
protected StreamableArrayList _events;
}
@@ -1,16 +1,38 @@
//
// $Id: DEvent.java,v 1.10 2002/03/20 19:06:55 mdb Exp $
// $Id: DEvent.java,v 1.11 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.IOException;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* A distributed object event is dispatched whenever any modification is
* made to a distributed object. It can also be dispatched purely for
* notification purposes, without making any modifications to the object
* that defines the delivery group (the object's subscribers).
*/
public abstract class DEvent
public abstract class DEvent implements Streamable
{
/**
* A zero argument constructor for unserialization from yon network.
*/
public DEvent ()
{
}
/**
* Constructs a new distributed object event that pertains to the
* specified distributed object.
*/
public DEvent (int targetOid)
{
_toid = targetOid;
}
/**
* Returns the oid of the object that is the target of this event.
*/
@@ -57,15 +79,6 @@ public abstract class DEvent
_soid = sourceOid;
}
/**
* Constructs a new distributed object event that pertains to the
* specified distributed object.
*/
protected DEvent (int targetOid)
{
_toid = targetOid;
}
/**
* Events with associated listener interfaces should implement this
* function and notify the supplied listener if it implements their
@@ -78,6 +91,26 @@ public abstract class DEvent
// the default is to do nothing
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
out.writeInt(_toid);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
_toid = in.readInt();
}
/**
* Constructs and returns a string representation of this event.
*/
@@ -105,5 +138,5 @@ public abstract class DEvent
protected int _toid;
/** The oid of the client that generated this event. */
protected int _soid = -1;
protected transient int _soid = -1;
}
@@ -1,13 +1,19 @@
//
// $Id: DObject.java,v 1.45 2002/07/18 00:41:59 mdb Exp $
// $Id: DObject.java,v 1.46 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import com.samskivert.util.ListUtil;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
import com.threerings.presents.Log;
/**
@@ -84,7 +90,7 @@ import com.threerings.presents.Log;
* byte[], short[], int[], long[], float[], double[], String[]
* </pre></code>
*/
public class DObject
public class DObject implements Streamable
{
/**
* Returns the object id of this object. All objects in the system
@@ -473,8 +479,12 @@ public class DObject
public String toString ()
{
StringBuffer buf = new StringBuffer();
buf.append("[oid=").append(_oid).append(", ");
StringUtil.fieldsToString(buf, this);
if (buf.length() > 0) {
buf.insert(0, ", ");
}
buf.insert(0, _oid);
buf.insert(0, "[oid=");
return buf.append("]").toString();
}
@@ -482,7 +492,7 @@ public class DObject
* Posts the specified event either to our dobject manager or to the
* compound event for which we are currently transacting.
*/
public void postEvent (TypedEvent event)
public void postEvent (DEvent event)
{
if (_tevent != null) {
_tevent.postEvent(event);
@@ -592,6 +602,26 @@ public class DObject
_tevent.cancel();
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
out.writeInt(_oid);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
_oid = in.readInt();
}
/**
* Removes this object from participation in any transaction in which
* it might be taking part.
@@ -647,8 +677,7 @@ public class DObject
try {
DSet set = (DSet)getAttribute(name);
// dispatch an entry added event
postEvent(new EntryAddedEvent(
_oid, name, entry, !set.homogenous()));
postEvent(new EntryAddedEvent(_oid, name, entry));
} catch (ObjectAccessException oae) {
Log.warning("Unable to request entryAdd [name=" + name +
@@ -673,8 +702,7 @@ public class DObject
try {
DSet set = (DSet)getAttribute(name);
// dispatch an entry updated event
postEvent(new EntryUpdatedEvent(
_oid, name, entry, !set.homogenous()));
postEvent(new EntryUpdatedEvent(_oid, name, entry));
} catch (ObjectAccessException oae) {
Log.warning("Unable to request entryUpdate [name=" + name +
@@ -683,7 +711,7 @@ public class DObject
}
/** Our object id. */
protected transient int _oid;
protected int _oid;
/** A reference to our object manager. */
protected transient DObjectManager _omgr;
+47 -171
View File
@@ -1,17 +1,18 @@
//
// $Id: DSet.java,v 1.16 2002/03/18 23:21:26 mdb Exp $
// $Id: DSet.java,v 1.17 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
import com.threerings.presents.Log;
import com.threerings.presents.io.Streamable;
/**
* The distributed set class provides a means by which an unordered set of
@@ -19,24 +20,17 @@ import com.threerings.presents.io.Streamable;
* added to and removed from the set, requests for which will generate
* events much like other distributed object fields.
*
* <p> A set can be either homogenous, whereby the type of object to be
* contained in the set is configured before the set is used and does not
* change; or heterogenous, whereby a set can contain any type of entry as
* long as it implements {@link Entry}. Homogenous sets take advantage of
* their homogeneity by not transfering the classname of each entry as it
* is sent over the wire.
*
* <p> Classes that wish to act as set entries must implement the {@link
* Entry} interface which extends {@link Streamable} and adds the
* requirement that the object provide a key which will be used to
* identify entry equality. Thus an entry is declared to be in a set of
* the object returned by that entry's <code>geyKey()</code> method is
* equal (using <code>equal()</code>) to the entry returned by the
* <code>getKey()</code> method of some other entry in the set.
* Additionally, in the case of entry removal, only the key for the entry
* to be removed will be transmitted with the removal event to save
* network bandwidth. Lastly, the object returned by <code>getKey()</code>
* must be a valid distributed object type.
* the object returned by that entry's {@link Entry#getKey} method is
* equal (using {@link Object#equal}) to the entry returned by the {@link
* Entry#getKey} method of some other entry in the set. Additionally, in
* the case of entry removal, only the key for the entry to be removed
* will be transmitted with the removal event to save network
* bandwidth. Lastly, the object returned by {@link Entry#getKey} must be
* a {@link Streamable} type.
*/
public class DSet
implements Streamable, Cloneable
@@ -54,35 +48,6 @@ public class DSet
public Object getKey ();
}
/**
* Constructs a distributed set that will contain the specified entry
* type.
*/
public DSet (Class entryType)
{
setEntryType(entryType);
}
/**
* Creates a distributed set and populates it with values from the
* supplied iterator. This should be done before the set is unleashed
* into the wild distributed object world because no associated entry
* added events will be generated. Additionally, this operation does
* not check for duplicates when adding entries, so one should be sure
* that the iterator contains only unique entries.
*
* @param entryType the type of entries that will be stored in this
* set. <em>Only</em> entries of this <em>exact</em> type may be
* stored in the set.
* @param source an iterator from which we will initially populate the
* set.
*/
public DSet (Class entryType, Iterator source)
{
this(source);
setEntryType(entryType);
}
/**
* Creates a distributed set and populates it with values from the
* supplied iterator. This should be done before the set is unleashed
@@ -111,39 +76,12 @@ public class DSet
}
/**
* Constructs a distributed set without specifying the entry type. The
* set will assume that it is heterogenous, unless a homogenous class
* type is otherwise specified via {@link #setEntryType}.
* Constructs an empty distributed set.
*/
public DSet ()
{
}
/**
* Returns true if this set contains only entries of exactly the same
* type, false if not.
*/
public boolean homogenous ()
{
return _entryType != null;
}
/**
* Indicates what type of entries will be stored in this set. This can
* be called multiple times before the set is used (in the event that
* one wishes to further specialize the contents of a set that has
* already been configured to use a particular entry type), but once
* the set goes into use, it must not be changed. Also bear in mind
* that the class of entries added to the set are not checked at
* runtime, and adding entries of invalid class will simply result in
* the serialization mechanism failing when an event is dispatched to
* broadcast the addition of an entry.
*/
public void setEntryType (Class entryType)
{
_entryType = entryType;
}
/**
* Returns the number of entries in this set.
*/
@@ -341,113 +279,54 @@ public class DSet
return false;
}
/**
* Serializes this instance to the supplied output stream.
*/
public void writeTo (DataOutputStream out)
throws IOException
{
if (_entryType == null) {
out.writeUTF("");
} else {
out.writeUTF(_entryType.getName());
}
out.writeInt(_size);
int elength = _entries.length;
for (int i = 0; i < elength; i++) {
Entry elem = _entries[i];
if (elem != null) {
if (_entryType == null) {
out.writeUTF(elem.getClass().getName());
}
elem.writeTo(out);
}
}
}
/**
* Unserializes this instance from the supplied input stream.
*/
public void readFrom (DataInputStream in)
throws IOException
{
// read our entry class and forName() it (if we read an entry
// class, we're a homogenous set; otherwise we're heterogenous)
String eclass = in.readUTF();
try {
if (!StringUtil.blank(eclass)) {
_entryType = Class.forName(eclass);
}
} catch (Exception e) {
String err = "Unable to instantiate entry class [err=" + e + "]";
throw new IOException(err);
}
// find out how many entries we'll be reading
_size = in.readInt();
// make sure we can fit _size entries
expand(_size);
for (int i = 0; i < _size; i++) {
_entries[i] = readEntry(in);
}
}
/**
* Reads an entry from the wire and unserializes it. Takes into
* account whether or not we're a homogenous set.
*/
public Entry readEntry (DataInputStream in)
throws IOException
{
try {
Entry elem = null;
// instantiate the appropriate entry instance
if (_entryType != null) {
elem = (Entry)_entryType.newInstance();
} else {
elem = (Entry)Class.forName(in.readUTF()).newInstance();
}
// unserialize it and return it
elem.readFrom(in);
return elem;
} catch (Exception e) {
Log.warning("Unable to unserialize set entry " +
"[set=" + this + "].");
Log.logStackTrace(e);
return null;
}
}
/**
* Generates a shallow copy of this object.
*/
public Object clone ()
{
DSet nset = new DSet(_entryType);
DSet nset = new DSet();
nset._entries = new Entry[_entries.length];
System.arraycopy(_entries, 0, nset._entries, 0, _entries.length);
nset._size = _size;
return nset;
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
out.writeInt(_size);
int ecount = _entries.length;
for (int ii = 0; ii < ecount; ii++) {
if (_entries[ii] != null) {
out.writeObject(_entries[ii]);
}
}
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
_size = in.readInt();
_entries = new Entry[Math.max(_size, INITIAL_CAPACITY)];
for (int ii = 0; ii < _size; ii++) {
_entries[ii] = (Entry)in.readObject();
}
}
/**
* Generates a string representation of this set instance.
*/
public String toString ()
{
StringBuffer buf = new StringBuffer("[");
if (_entryType == null) {
buf.append("etype=NONE");
} else {
buf.append("etype=").append(_entryType.getName());
}
buf.append(", elems=(");
StringBuffer buf = new StringBuffer("(");
String prefix = "";
for (int i = 0; i < _entries.length; i++) {
Entry elem = _entries[i];
@@ -457,14 +336,14 @@ public class DSet
buf.append(elem);
}
}
buf.append(")]");
buf.append(")");
return buf.toString();
}
protected void expand (int index)
{
// sanity check
if (index < 0) {
if (index < 0 || index > Short.MAX_VALUE) {
Log.warning("Requested to expand to accomodate bogus index! " +
"[index=" + index + "].");
Thread.dumpStack();
@@ -490,9 +369,6 @@ public class DSet
_entries = elems;
}
/** The type of entry this set holds. */
protected Class _entryType;
/** The entries of the set (in a sparse array). */
protected Entry[] _entries = new Entry[INITIAL_CAPACITY];
@@ -1,17 +1,18 @@
//
// $Id: ElementUpdatedEvent.java,v 1.1 2002/03/19 01:10:03 mdb Exp $
// $Id: ElementUpdatedEvent.java,v 1.2 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.presents.Log;
import com.threerings.presents.io.ValueMarshaller;
/**
* An element updated event is dispatched when an element of an array
@@ -20,11 +21,8 @@ import com.threerings.presents.io.ValueMarshaller;
*
* @see DObjectManager#postEvent
*/
public class ElementUpdatedEvent extends TypedEvent
public class ElementUpdatedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 11;
/**
* Constructs a new element updated event on the specified target
* object with the supplied attribute name, element and index.
@@ -153,30 +151,28 @@ public class ElementUpdatedEvent extends TypedEvent
}
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
out.writeObject(_value);
out.writeInt(_index);
ValueMarshaller.writeTo(out, _value);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
_value = in.readObject();
_index = in.readInt();
_value = ValueMarshaller.readFrom(in);
}
// documentation inherited
@@ -193,7 +189,8 @@ public class ElementUpdatedEvent extends TypedEvent
buf.append("UPDATE:");
super.toString(buf);
buf.append(", name=").append(_name);
buf.append(", value=").append(_value);
buf.append(", value=");
StringUtil.toString(buf, _value);
buf.append(", index=").append(_index);
}
@@ -1,14 +1,16 @@
//
// $Id: EntryAddedEvent.java,v 1.7 2002/03/18 23:21:26 mdb Exp $
// $Id: EntryAddedEvent.java,v 1.8 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.presents.Log;
import com.threerings.presents.dobj.io.EntryUtil;
/**
* An entry added event is dispatched when an entry is added to a {@link
@@ -17,11 +19,8 @@ import com.threerings.presents.dobj.io.EntryUtil;
*
* @see DObjectManager#postEvent
*/
public class EntryAddedEvent extends TypedEvent
public class EntryAddedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 8;
/**
* Constructs a new entry added event on the specified target object
* with the supplied set attribute name and entry to add.
@@ -31,17 +30,12 @@ public class EntryAddedEvent extends TypedEvent
* @param name the name of the attribute to which to add the specified
* entry.
* @param entry the entry to add to the set attribute.
* @param qualified whether or not the entry need be qualified with
* its class when serializing (true for heterogenous sets, false for
* homogenous sets).
*/
public EntryAddedEvent (int targetOid, String name, DSet.Entry entry,
boolean qualified)
public EntryAddedEvent (int targetOid, String name, DSet.Entry entry)
{
super(targetOid);
_name = name;
_entry = entry;
_qualified = qualified;
}
/**
@@ -76,51 +70,30 @@ public class EntryAddedEvent extends TypedEvent
throws ObjectAccessException
{
DSet set = (DSet)target.getAttribute(_name);
// now that we have access to our target set, we can unflatten our
// entry (if need be)
if (_entry == null) {
try {
_entry = EntryUtil.unflatten(set, _bytes);
} catch (Exception e) {
Log.warning("Error unflattening entry " + this + ".");
Log.logStackTrace(e);
return false;
}
}
set.add(_entry);
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
EntryUtil.flatten(out, _entry, _qualified);
out.writeObject(_entry);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
// we read in the raw entry data now and decode it later when we
// have access to the object and the DSet instance that knows what
// type of entry we need to decode
int bcount = in.readInt();
_bytes = new byte[bcount];
in.readFully(_bytes, 0, bcount);
_entry = (DSet.Entry)in.readObject();
}
// documentation inherited
@@ -137,11 +110,10 @@ public class EntryAddedEvent extends TypedEvent
buf.append("ELADD:");
super.toString(buf);
buf.append(", name=").append(_name);
buf.append(", entry=").append(_entry);
buf.append(", entry=");
StringUtil.toString(buf, _entry);
}
protected String _name;
protected byte[] _bytes;
protected DSet.Entry _entry;
protected boolean _qualified;
}
@@ -1,13 +1,12 @@
//
// $Id: EntryRemovedEvent.java,v 1.8 2002/03/18 23:21:26 mdb Exp $
// $Id: EntryRemovedEvent.java,v 1.9 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.threerings.presents.io.ValueMarshaller;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* An entry removed event is dispatched when an entry is removed from a
@@ -17,11 +16,8 @@ import com.threerings.presents.io.ValueMarshaller;
*
* @see DObjectManager#postEvent
*/
public class EntryRemovedEvent extends TypedEvent
public class EntryRemovedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 9;
/**
* Constructs a new entry removed event on the specified target object
* with the supplied set attribute name and entry key to remove.
@@ -76,28 +72,26 @@ public class EntryRemovedEvent extends TypedEvent
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
ValueMarshaller.writeTo(out, _key);
out.writeObject(_key);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
_key = ValueMarshaller.readFrom(in);
_key = in.readObject();
}
// documentation inherited
@@ -1,14 +1,16 @@
//
// $Id: EntryUpdatedEvent.java,v 1.6 2002/04/18 00:31:26 mdb Exp $
// $Id: EntryUpdatedEvent.java,v 1.7 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.presents.Log;
import com.threerings.presents.dobj.io.EntryUtil;
/**
* An entry updated event is dispatched when an entry of a {@link DSet} is
@@ -17,11 +19,8 @@ import com.threerings.presents.dobj.io.EntryUtil;
*
* @see DObjectManager#postEvent
*/
public class EntryUpdatedEvent extends TypedEvent
public class EntryUpdatedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 10;
/**
* Constructs a new entry updated event on the specified target object
* for the specified set name and with the supplied updated entry.
@@ -31,17 +30,12 @@ public class EntryUpdatedEvent extends TypedEvent
* @param name the name of the attribute in which to update the
* specified entry.
* @param entry the entry to update.
* @param qualified whether or not the entry need be qualified with
* its class when serializing (true for heterogenous sets, false for
* homogenous sets).
*/
public EntryUpdatedEvent (int targetOid, String name, DSet.Entry entry,
boolean qualified)
public EntryUpdatedEvent (int targetOid, String name, DSet.Entry entry)
{
super(targetOid);
_name = name;
_entry = entry;
_qualified = qualified;
}
/**
@@ -77,18 +71,6 @@ public class EntryUpdatedEvent extends TypedEvent
{
DSet set = (DSet)target.getAttribute(_name);
// now that we have access to our target set, we can unflatten our
// entry (if need be)
if (_entry == null) {
try {
_entry = EntryUtil.unflatten(set, _bytes);
} catch (Exception e) {
Log.warning("Error unflattening entry " + this + ".");
Log.logStackTrace(e);
return false;
}
}
// update the entry
if (!set.update(_entry)) {
// complain if we didn't update anything
@@ -100,34 +82,26 @@ public class EntryUpdatedEvent extends TypedEvent
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
EntryUtil.flatten(out, _entry, _qualified);
out.writeObject(_entry);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
// we read in the raw entry data now and decode it later when we
// have access to the object and the DSet instance that knows what
// type of entry we need to decode
int bcount = in.readInt();
_bytes = new byte[bcount];
in.readFully(_bytes, 0, bcount);
_entry = (DSet.Entry)in.readObject();
}
// documentation inherited
@@ -144,11 +118,10 @@ public class EntryUpdatedEvent extends TypedEvent
buf.append("ELUPD:");
super.toString(buf);
buf.append(", name=").append(_name);
buf.append(", entry=").append(_entry);
buf.append(", entry=");
StringUtil.toString(buf, _entry);
}
protected String _name;
protected byte[] _bytes;
protected DSet.Entry _entry;
protected boolean _qualified;
}
@@ -1,15 +1,15 @@
//
// $Id: MessageEvent.java,v 1.8 2002/02/01 23:32:37 mdb Exp $
// $Id: MessageEvent.java,v 1.9 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import com.samskivert.util.StringUtil;
import com.threerings.presents.io.ValueMarshaller;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* A message event is used to dispatch a message to all subscribers of a
@@ -20,11 +20,8 @@ import com.threerings.presents.io.ValueMarshaller;
*
* @see DObjectManager#postEvent
*/
public class MessageEvent extends TypedEvent
public class MessageEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 3;
/**
* Constructs a new message event on the specified target object with
* the supplied name and arguments.
@@ -86,41 +83,26 @@ public class MessageEvent extends TypedEvent
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
if (_args != null) {
out.writeInt(_args.length);
for (int i = 0; i < _args.length; i++) {
ValueMarshaller.writeTo(out, _args[i]);
}
} else {
out.writeInt(0);
}
out.writeObject(_args);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
int args = in.readInt();
if (args > 0) {
_args = new Object[args];
for (int i = 0; i < args; i++) {
_args[i] = ValueMarshaller.readFrom(in);
}
}
_args = (Object[])in.readObject();
}
// documentation inherited
@@ -1,12 +1,13 @@
//
// $Id: ObjectAddedEvent.java,v 1.6 2001/10/12 00:03:03 mdb Exp $
// $Id: ObjectAddedEvent.java,v 1.7 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* An object added event is dispatched when an object is added to an
* <code>OidList</code> attribute of a distributed object. It can also be
@@ -15,11 +16,8 @@ import java.io.IOException;
*
* @see DObjectManager#postEvent
*/
public class ObjectAddedEvent extends TypedEvent
public class ObjectAddedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 4;
/**
* Constructs a new object added event on the specified target object
* with the supplied oid list attribute name and object id to add.
@@ -73,26 +71,24 @@ public class ObjectAddedEvent extends TypedEvent
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
out.writeInt(_oid);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
_oid = in.readInt();
}
@@ -1,13 +1,14 @@
//
// $Id: ObjectDestroyedEvent.java,v 1.3 2001/10/23 23:56:12 mdb Exp $
// $Id: ObjectDestroyedEvent.java,v 1.4 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* An object destroyed event is dispatched when an object has been removed
* from the distributed object system. It can also be constructed to
@@ -15,11 +16,8 @@ import java.lang.reflect.Method;
*
* @see DObjectManager#postEvent
*/
public class ObjectDestroyedEvent extends TypedEvent
public class ObjectDestroyedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 7;
/**
* Constructs a new object destroyed event for the specified
* distributed object.
@@ -48,12 +46,6 @@ public class ObjectDestroyedEvent extends TypedEvent
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
protected void notifyListener (Object listener)
{
@@ -1,12 +1,13 @@
//
// $Id: ObjectRemovedEvent.java,v 1.6 2001/10/12 00:03:03 mdb Exp $
// $Id: ObjectRemovedEvent.java,v 1.7 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* An object removed event is dispatched when an object is removed from an
* <code>OidList</code> attribute of a distributed object. It can also be
@@ -15,11 +16,8 @@ import java.io.IOException;
*
* @see DObjectManager#postEvent
*/
public class ObjectRemovedEvent extends TypedEvent
public class ObjectRemovedEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 5;
/**
* Constructs a new object removed event on the specified target
* object with the supplied oid list attribute name and object id to
@@ -74,30 +72,6 @@ public class ObjectRemovedEvent extends TypedEvent
return true;
}
// documentation inherited
public short getType ()
{
return TYPE;
}
// documentation inherited
public void writeTo (DataOutputStream out)
throws IOException
{
super.writeTo(out);
out.writeUTF(_name);
out.writeInt(_oid);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
{
super.readFrom(in);
_name = in.readUTF();
_oid = in.readInt();
}
// documentation inherited
protected void notifyListener (Object listener)
{
@@ -106,6 +80,28 @@ public class ObjectRemovedEvent extends TypedEvent
}
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeObject(out);
out.writeUTF(_name);
out.writeInt(_oid);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readObject(in);
_name = in.readUTF();
_oid = in.readInt();
}
// documentation inherited
protected void toString (StringBuffer buf)
{
@@ -1,19 +1,21 @@
//
// $Id: OidList.java,v 1.4 2001/10/11 04:07:52 mdb Exp $
// $Id: OidList.java,v 1.5 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* An oid list is used to store lists of object ids. The list will not
* allow duplicate ids. This class is not synchronized, with the
* expectation that all modifications of instances will take place on the
* dobjmgr thread.
*/
public class OidList
public class OidList implements Streamable
{
/**
* Creates an empty oid list.
@@ -111,6 +113,28 @@ public class OidList
return _oids[index];
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
out.writeInt(_size);
out.writeObject(_oids);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
_size = in.readInt();
_oids = (int[])in.readObject();
}
public String toString ()
{
StringBuffer buf = new StringBuffer();
@@ -132,25 +156,6 @@ public class OidList
_oids = oids;
}
public void writeTo (DataOutputStream out)
throws IOException
{
out.writeInt(_size);
for (int i = 0; i < _size; i++) {
out.writeInt(_oids[i]);
}
}
public void readFrom (DataInputStream in)
throws IOException
{
_size = in.readInt();
_oids = new int[Math.max(DEFAULT_SIZE, _size*2)];
for (int i = 0; i < _size; i++) {
_oids[i] = in.readInt();
}
}
private int[] _oids;
private int _size;
@@ -1,13 +1,14 @@
//
// $Id: ReleaseLockEvent.java,v 1.5 2002/02/01 23:32:37 mdb Exp $
// $Id: ReleaseLockEvent.java,v 1.6 2002/07/23 05:52:48 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
/**
* A release lock event is dispatched at the end of a chain of events to
* release a lock that is intended to prevent some application defined
@@ -19,11 +20,8 @@ import java.lang.reflect.Method;
*
* @see DObjectManager#postEvent
*/
public class ReleaseLockEvent extends TypedEvent
public class ReleaseLockEvent extends DEvent
{
/** The typed object code for this event. */
public static final short TYPE = TYPE_BASE + 6;
/**
* Constructs a new release lock event for the specified target object
* with the supplied lock name.
@@ -65,22 +63,23 @@ public class ReleaseLockEvent extends TypedEvent
return false;
}
public short getType ()
{
return TYPE;
}
public void writeTo (DataOutputStream out)
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeTo(out);
super.writeObject(out);
out.writeUTF(_name);
}
public void readFrom (DataInputStream in)
throws IOException
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readFrom(in);
super.readObject(in);
_name = in.readUTF();
}
@@ -1,65 +0,0 @@
//
// $Id: TypedEvent.java,v 1.2 2001/10/11 04:07:52 mdb Exp $
package com.threerings.presents.dobj;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.threerings.presents.io.TypedObject;
/**
* A typed event is one that can be transmitted over the network. All
* event classes that will be shared between the client and server should
* derive from this class and be registered with the typed object factory
* so that they can be serialized and unserialized.
*/
public abstract class TypedEvent extends DEvent implements TypedObject
{
/**
* All event derived classes should base their typed object code on
* this base value.
*/
public static final short TYPE_BASE = 400;
/**
* Constructs a typed event, passing the target object id on to the
* <code>DEvent</code> constructor.
*/
public TypedEvent (int targetOid)
{
super(targetOid);
}
/**
* Constructs a blank typed event instance that will be unserialized
* from the network.
*/
public TypedEvent ()
{
super(0);
}
/**
* Derived classes should override this function to write their fields
* out to the supplied data output stream. They <em>must</em> be sure
* to first call <code>super.writeTo()</code>.
*/
public void writeTo (DataOutputStream out)
throws IOException
{
out.writeInt(_toid);
}
/**
* Derived classes should override this function to read their fields
* from the supplied data input stream. They <em>must</em> be sure to
* first call <code>super.readFrom()</code>.
*/
public void readFrom (DataInputStream in)
throws IOException
{
_toid = in.readInt();
}
}
@@ -1,65 +0,0 @@
//
// $Id: DObjectFactory.java,v 1.12 2002/07/18 00:42:30 mdb Exp $
package com.threerings.presents.dobj.io;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import com.threerings.presents.Log;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.io.Marshaller;
import com.threerings.presents.io.ObjectStreamException;
/**
* The distributed object factory is responsible for marshalling and
* unmarshalling distributed objects to and from streams so that they can
* be communicated between the client and server.
*/
public class DObjectFactory
{
/**
* Writes the supplied distributed object out to the specified data
* output stream.
*/
public static void writeTo (DataOutputStream out, DObject dobj)
throws IOException
{
// Log.info("Marshalling object: " + dobj);
// write the class of the object to the stream
out.writeUTF(dobj.getClass().getName());
// write out the oid
out.writeInt(dobj.getOid());
// then use the marshaller to write the object itself
Marshaller.writeObject(out, dobj);
}
/**
* Reads a distributed object from the specified input stream.
*/
public static DObject readFrom (DataInputStream in)
throws IOException
{
try {
// read in the class name and create an instance of that class
Class clazz = Class.forName(in.readUTF());
DObject dobj = (DObject)clazz.newInstance();
dobj.setOid(in.readInt()); // read and set the oid
// Log.info("Unmarshalling object " +
// "[class=" + dobj.getClass().getName() +
// ", object=" + dobj + "].");
// use a marshaller to reconstitute the object from the stream
Marshaller.readObject(in, dobj);
return dobj;
} catch (Exception e) {
String errmsg = "Failure unserializing dobj";
throw new ObjectStreamException(errmsg, e);
}
}
}
@@ -1,89 +0,0 @@
//
// $Id: EntryUtil.java,v 1.5 2002/03/18 23:21:26 mdb Exp $
package com.threerings.presents.dobj.io;
import java.io.*;
import com.threerings.presents.Log;
import com.threerings.presents.dobj.DSet;
/**
* Routines to simplify the process of moving set entries over the wire.
* Because we don't know the type of the entry when the event is
* unserialized (we only know later when the event is applied to the
* object and the event has access to the target set object), then we need
* to do some jockeying.
*/
public class EntryUtil
{
/**
* Flattens the supplied entry into a byte array, counts the number of
* bytes in the array and writes the count followed by the bytes to
* the supplied data output stream. This method should really only be
* called by the conmgr thread, but we synchronize just in case
* someone decides to write an event out in some other peculiar
* context; uncontested syncs are pretty fast.
*/
public static synchronized void flatten (
DataOutputStream out, DSet.Entry entry, boolean qualified)
throws IOException
{
// write the entry classname out if requested
if (qualified) {
_dout.writeUTF(entry.getClass().getName());
}
entry.writeTo(_dout);
_dout.flush();
out.writeInt(_bout.size());
_bout.writeTo(out);
_bout.reset();
}
/**
* Unflattens an entry given the serialized entry data. We know
* this will always be called on the dobjmgr thread, so we need not
* synchronize.
*/
public static DSet.Entry unflatten (DSet set, byte[] data)
throws IOException
{
_bin.setBytes(data);
return set.readEntry(_din);
}
/**
* We extend byte array input stream to avoid having to create a new
* input stream every time we unserialize an entry. Our extensions
* allow us to repurpose this input stream to read from a new byte
* array each time we unserialize.
*/
protected static class ReByteArrayInputStream extends ByteArrayInputStream
{
public ReByteArrayInputStream ()
{
super(new byte[0]);
}
public void setBytes (byte[] bytes)
{
buf = bytes;
pos = 0;
count = buf.length;
mark = 0;
}
}
/** Used when serializing entries. */
protected static ByteArrayOutputStream _bout = new ByteArrayOutputStream();
/** Used when serializing entries. */
protected static DataOutputStream _dout = new DataOutputStream(_bout);
/** Used when unserializing entries. */
protected static ReByteArrayInputStream _bin =
new ReByteArrayInputStream();
/** Used when unserializing entries. */
protected static DataInputStream _din = new DataInputStream(_bin);
}