The great invocation services rethink of 2002! Rearchitected the remote

method invocation services and converted everything to the new style.
Could this be my biggest checkin ever?


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1642 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-08-14 19:08:01 +00:00
parent 4481c5f835
commit e54a4d41f4
161 changed files with 6083 additions and 2805 deletions
@@ -1,5 +1,5 @@
//
// $Id: DObject.java,v 1.46 2002/07/23 05:52:48 mdb Exp $
// $Id: DObject.java,v 1.47 2002/08/14 19:07:55 mdb Exp $
package com.threerings.presents.dobj;
@@ -473,6 +473,14 @@ public class DObject implements Streamable
_oid = oid;
}
/**
* Generates a concise string representation of this object.
*/
public String which ()
{
return "[" + getClass().getName() + " " + getOid() + "]";
}
/**
* Generates a string representation of this object.
*/
+82 -109
View File
@@ -1,11 +1,13 @@
//
// $Id: DSet.java,v 1.17 2002/07/23 05:52:48 mdb Exp $
// $Id: DSet.java,v 1.18 2002/08/14 19:07:55 mdb Exp $
package com.threerings.presents.dobj;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import com.samskivert.util.ArrayUtil;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
@@ -45,7 +47,7 @@ public class DSet
* its uniqueness in the set. See the {@link DSet} class
* documentation for further information.
*/
public Object getKey ();
public Comparable getKey ();
}
/**
@@ -61,17 +63,8 @@ public class DSet
*/
public DSet (Iterator source)
{
for (int index = 0; source.hasNext(); index++) {
Entry elem = (Entry)source.next();
// expand the array if necessary
if (index >= _entries.length) {
expand(index);
}
// insert the item
_entries[index] = elem;
_size++;
while (source.hasNext()) {
add((Entry)source.next());
}
}
@@ -140,37 +133,16 @@ public class DSet
public Iterator entries ()
{
return new Iterator() {
public boolean hasNext ()
{
// we need to scan to the next entry the first time
if (_index < 0) {
scanToNext();
}
return (_index < _entries.length);
public boolean hasNext () {
return (_index < _size);
}
public Object next ()
{
Object val = _entries[_index];
scanToNext();
return val;
public Object next () {
return _entries[_index++];
}
public void remove ()
{
public void remove () {
throw new UnsupportedOperationException();
}
protected void scanToNext ()
{
for (_index++; _index < _entries.length; _index++) {
if (_entries[_index] != null) {
return;
}
}
}
int _index = -1;
protected int _index = 0;
};
}
@@ -185,32 +157,44 @@ public class DSet
*/
protected boolean add (Entry elem)
{
Object key = elem.getKey();
// determine where we'll be adding the new element
int eidx = ArrayUtil.binarySearch(
_entries, 0, _size, elem, ENTRY_COMP);
// if the element is already in the set, bail now
if (eidx >= 0) {
return false;
}
// convert the index into happy positive land
eidx = (eidx+1)*-1;
// expand our entries array if necessary
int elength = _entries.length;
int index = elength;
// scan the array looking for a slot and/or the entry already in
// the set
for (int i = 0; i < elength; i++) {
Entry el = _entries[i];
// the array may be sparse
if (el == null) {
if (index == elength) {
index = i;
}
} else if (el.getKey().equals(key)) {
return false;
if (_size == elength-1) {
// sanity check
if (elength > 2048) {
Log.warning("Requested to expand to questionably large size " +
"[length=" + elength + "].");
Thread.dumpStack();
}
// create a new array and copy our data into it
Entry[] elems = new Entry[elength*2];
System.arraycopy(_entries, 0, elems, 0, elength);
_entries = elems;
}
// expand the array if necessary
if (index >= _entries.length) {
expand(index);
// if the entry doesn't go at the end, shift the elements down to
// accomodate it
if (eidx < _size) {
System.arraycopy(_entries, eidx, _entries, eidx+1, _size-eidx);
}
// insert the item
_entries[index] = elem;
// stuff the entry into the array and note that we're bigger
_entries[eidx] = elem;
_size++;
return true;
}
@@ -239,17 +223,20 @@ public class DSet
*/
protected boolean removeKey (Object key)
{
// scan the array looking for a matching entry
int elength = _entries.length;
for (int i = 0; i < elength; i++) {
Entry el = _entries[i];
if (el != null && el.getKey().equals(key)) {
_entries[i] = null;
_size--;
return true;
}
// look up this entry's position in our set
int eidx = ArrayUtil.binarySearch(
_entries, 0, _size, key, ENTRY_COMP);
// if we found it, remove it
if (eidx >= 0) {
// shift the remaining elements down
System.arraycopy(_entries, eidx+1, _entries, eidx, _size-eidx-1);
_entries[--_size] = null;
return true;
} else {
return false;
}
return false;
}
/**
@@ -264,19 +251,17 @@ public class DSet
*/
protected boolean update (Entry elem)
{
Object key = elem.getKey();
// look up this entry's position in our set
int eidx = ArrayUtil.binarySearch(
_entries, 0, _size, elem, ENTRY_COMP);
// scan the array looking for a matching entry
int elength = _entries.length;
for (int i = 0; i < elength; i++) {
Entry el = _entries[i];
if (el != null && el.getKey().equals(key)) {
_entries[i] = elem;
return true;
}
// if we found it, update it
if (eidx >= 0) {
_entries[eidx] = elem;
return true;
} else {
return false;
}
return false;
}
/**
@@ -340,35 +325,6 @@ public class DSet
return buf.toString();
}
protected void expand (int index)
{
// sanity check
if (index < 0 || index > Short.MAX_VALUE) {
Log.warning("Requested to expand to accomodate bogus index! " +
"[index=" + index + "].");
Thread.dumpStack();
index = 0;
}
// increase our length in powers of two until we're big enough
int tlength = _entries.length;
while (index >= tlength) {
tlength *= 2;
}
// further sanity checks
if (tlength > 4096) {
Log.warning("Requested to expand to questionably large size " +
"[index=" + index + ", tlength=" + tlength + "].");
Thread.dumpStack();
}
// create a new array and copy our data into it
Entry[] elems = new Entry[tlength];
System.arraycopy(_entries, 0, elems, 0, _entries.length);
_entries = elems;
}
/** The entries of the set (in a sparse array). */
protected Entry[] _entries = new Entry[INITIAL_CAPACITY];
@@ -377,4 +333,21 @@ public class DSet
/** The default capacity of a set instance. */
protected static final int INITIAL_CAPACITY = 2;
/** Used for lookups and to keep the set contents sorted on
* insertions. */
protected static Comparator ENTRY_COMP = new Comparator() {
public int compare (Object o1, Object o2) {
Comparable c1 = (o1 instanceof Entry) ?
((Entry)o1).getKey() : (Comparable)o1;
Comparable c2 = (o2 instanceof Entry) ?
((Entry)o2).getKey() : (Comparable)o2;
return c1.compareTo(c2);
}
public boolean equals (Object obj) {
// we don't care about comparing comparators
return (obj == this);
}
};
}
@@ -0,0 +1,135 @@
//
// $Id: InvocationNotificationEvent.java,v 1.1 2002/08/14 19:07:55 mdb Exp $
package com.threerings.presents.dobj;
import java.io.IOException;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Used to dispatch an invocation notification from the server to a
* client.
*
* @see DObjectManager#postEvent
*/
public class InvocationNotificationEvent extends DEvent
{
/**
* Constructs a new invocation notification event on the specified
* target object with the supplied receiver id, method id and
* arguments.
*
* @param targetOid the object id of the object on which the event is
* to be dispatched.
* @param receiverId identifies the receiver to which this notification
* is being dispatched.
* @param methodId the id of the method to be invoked.
* @param args the arguments for the method. This array should contain
* only values of valid distributed object types.
*/
public InvocationNotificationEvent (
int targetOid, short receiverId, int methodId, Object[] args)
{
super(targetOid);
_receiverId = receiverId;
_methodId = (byte)methodId;
_args = args;
}
/**
* Constructs a blank instance of this event in preparation for
* unserialization from the network.
*/
public InvocationNotificationEvent ()
{
}
/**
* Returns the receiver id associated with this notification.
*/
public int getReceiverId ()
{
return _receiverId;
}
/**
* Returns the id of the method associated with this notification.
*/
public int getMethodId ()
{
return _methodId;
}
/**
* Returns the arguments associated with this notification.
*/
public Object[] getArgs ()
{
return _args;
}
/**
* Applies this attribute change to the object.
*/
public boolean applyToObject (DObject target)
throws ObjectAccessException
{
// nothing to do here
return true;
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeObject(out);
out.writeShort(_receiverId);
out.writeByte(_methodId);
out.writeObject(_args);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readObject(in);
_receiverId = in.readShort();
_methodId = in.readByte();
_args = (Object[])in.readObject();
}
// documentation inherited
protected void notifyListener (Object listener)
{
// nothing to do here
}
// documentation inherited
protected void toString (StringBuffer buf)
{
buf.append("INOT:");
super.toString(buf);
buf.append(", rcvId=").append(_receiverId);
buf.append(", methodId=").append(_methodId);
buf.append(", args=").append(StringUtil.toString(_args));
}
/** Identifies the receiver to which this notification is being
* dispatched. */
protected short _receiverId;
/** The id of the receiver method being invoked. */
protected byte _methodId;
/** The arguments to the receiver method being invoked. */
protected Object[] _args;
}
@@ -0,0 +1,132 @@
//
// $Id: InvocationRequestEvent.java,v 1.1 2002/08/14 19:07:55 mdb Exp $
package com.threerings.presents.dobj;
import java.io.IOException;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Used to dispatch an invocation request from the client to the server.
*
* @see DObjectManager#postEvent
*/
public class InvocationRequestEvent extends DEvent
{
/**
* Constructs a new invocation request event on the specified target
* object with the supplied code, method and arguments.
*
* @param targetOid the object id of the object on which the event is
* to be dispatched.
* @param invCode the invocation provider identification code.
* @param methodId the id of the method to be invoked.
* @param args the arguments for the method. This array should contain
* only values of valid distributed object types.
*/
public InvocationRequestEvent (
int targetOid, int invCode, int methodId, Object[] args)
{
super(targetOid);
_invCode = invCode;
_methodId = (byte)methodId;
_args = args;
}
/**
* Constructs a blank instance of this event in preparation for
* unserialization from the network.
*/
public InvocationRequestEvent ()
{
}
/**
* Returns the invocation code associated with this request.
*/
public int getInvCode ()
{
return _invCode;
}
/**
* Returns the id of the method associated with this request.
*/
public int getMethodId ()
{
return _methodId;
}
/**
* Returns the arguments associated with this request.
*/
public Object[] getArgs ()
{
return _args;
}
/**
* Applies this attribute change to the object.
*/
public boolean applyToObject (DObject target)
throws ObjectAccessException
{
// nothing to do here
return true;
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeObject(out);
out.writeInt(_invCode);
out.writeByte(_methodId);
out.writeObject(_args);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readObject(in);
_invCode = in.readInt();
_methodId = in.readByte();
_args = (Object[])in.readObject();
}
// documentation inherited
protected void notifyListener (Object listener)
{
// nothing to do here
}
// documentation inherited
protected void toString (StringBuffer buf)
{
buf.append("IREQ:");
super.toString(buf);
buf.append(", code=").append(_invCode);
buf.append(", methodId=").append(_methodId);
buf.append(", args=").append(StringUtil.toString(_args));
}
/** The code identifying which invocation provider to which this
* request is directed. */
protected int _invCode;
/** The id of the method being invoked. */
protected byte _methodId;
/** The arguments to the method being invoked. */
protected Object[] _args;
}
@@ -0,0 +1,131 @@
//
// $Id: InvocationResponseEvent.java,v 1.1 2002/08/14 19:07:55 mdb Exp $
package com.threerings.presents.dobj;
import java.io.IOException;
import com.samskivert.util.StringUtil;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Used to dispatch an invocation response from the server to the client.
*
* @see DObjectManager#postEvent
*/
public class InvocationResponseEvent extends DEvent
{
/**
* Constructs a new invocation response event on the specified target
* object with the supplied code, method and arguments.
*
* @param targetOid the object id of the object on which the event is
* to be dispatched.
* @param requestId the id of the request to which we are responding.
* @param methodId the method to be invoked.
* @param args the arguments for the method. This array should contain
* only values of valid distributed object types.
*/
public InvocationResponseEvent (
int targetOid, int requestId, int methodId, Object[] args)
{
super(targetOid);
_requestId = (short)requestId;
_methodId = (byte)methodId;
_args = args;
}
/**
* Constructs a blank instance of this event in preparation for
* unserialization from the network.
*/
public InvocationResponseEvent ()
{
}
/**
* Returns the invocation request id associated with this response.
*/
public int getRequestId ()
{
return _requestId;
}
/**
* Returns the method associated with this response.
*/
public int getMethodId ()
{
return _methodId;
}
/**
* Returns the arguments associated with this response.
*/
public Object[] getArgs ()
{
return _args;
}
/**
* Applies this attribute change to the object.
*/
public boolean applyToObject (DObject target)
throws ObjectAccessException
{
// nothing to do here
return true;
}
/**
* Writes our custom streamable fields.
*/
public void writeObject (ObjectOutputStream out)
throws IOException
{
super.writeObject(out);
out.writeShort(_requestId);
out.writeByte(_methodId);
out.writeObject(_args);
}
/**
* Reads our custom streamable fields.
*/
public void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
super.readObject(in);
_requestId = in.readShort();
_methodId = in.readByte();
_args = (Object[])in.readObject();
}
// documentation inherited
protected void notifyListener (Object listener)
{
// nothing to do here
}
// documentation inherited
protected void toString (StringBuffer buf)
{
buf.append("IRSP:");
super.toString(buf);
buf.append(", reqid=").append(_requestId);
buf.append(", methodId=").append(_methodId);
buf.append(", args=").append(StringUtil.toString(_args));
}
/** The id of the request with which this response is associated. */
protected short _requestId;
/** The id of the method being invoked. */
protected byte _methodId;
/** The arguments to the method being invoked. */
protected Object[] _args;
}