From 89083f4708bd65cf525bd39ade9d3923a899d509 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 11 Jun 2001 17:44:04 +0000 Subject: [PATCH] Moved the typed object registry into the same package with the rest of the typed object stuff; made events typed so they can be transported; further wiring up of event dispatch over the network. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@34 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/presents/client/Client.java | 8 +-- .../presents/client/ClientDObjectMgr.java | 9 ++- .../presents/dobj/AttributeChangedEvent.java | 32 ++++++++- .../presents/dobj/AttributesChangedEvent.java | 40 +++++++++++- .../threerings/presents/dobj/TypedEvent.java | 65 +++++++++++++++++++ .../presents/io/TypedObjectFactory.java | 7 +- .../TypedObjectRegistry.java} | 16 +++-- .../presents/net/EventNotification.java | 17 +++-- .../presents/net/ForwardEventRequest.java | 17 +++-- .../presents/server/PresentsClient.java | 7 +- .../server/net/ConnectionManager.java | 8 +-- 11 files changed, 183 insertions(+), 43 deletions(-) create mode 100644 src/java/com/threerings/presents/dobj/TypedEvent.java rename src/java/com/threerings/presents/{net/Registry.java => io/TypedObjectRegistry.java} (79%) diff --git a/src/java/com/threerings/presents/client/Client.java b/src/java/com/threerings/presents/client/Client.java index 31a2a4390..40ce632cb 100644 --- a/src/java/com/threerings/presents/client/Client.java +++ b/src/java/com/threerings/presents/client/Client.java @@ -1,5 +1,5 @@ // -// $Id: Client.java,v 1.5 2001/06/09 23:39:03 mdb Exp $ +// $Id: Client.java,v 1.6 2001/06/11 17:44:03 mdb Exp $ package com.threerings.cocktail.cher.client; @@ -9,7 +9,6 @@ import java.util.List; import com.threerings.cocktail.cher.Log; import com.threerings.cocktail.cher.dobj.DObjectManager; import com.threerings.cocktail.cher.net.Credentials; -import com.threerings.cocktail.cher.net.Registry; /** * Through the client object, a connection to the system is established @@ -243,9 +242,4 @@ public class Client static final int CLIENT_CONNECTION_FAILED = 2; static final int CLIENT_WILL_LOGOFF = 3; static final int CLIENT_DID_LOGOFF = 4; - - // register our shared objects - static { - Registry.registerTypedObjects(); - } } diff --git a/src/java/com/threerings/presents/client/ClientDObjectMgr.java b/src/java/com/threerings/presents/client/ClientDObjectMgr.java index cf0cc534f..64413df6c 100644 --- a/src/java/com/threerings/presents/client/ClientDObjectMgr.java +++ b/src/java/com/threerings/presents/client/ClientDObjectMgr.java @@ -1,5 +1,5 @@ // -// $Id: ClientDObjectMgr.java,v 1.1 2001/06/09 23:39:03 mdb Exp $ +// $Id: ClientDObjectMgr.java,v 1.2 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.client; @@ -69,8 +69,13 @@ public class ClientDObjectMgr // inherit documentation from the interface public void postEvent (DEvent event) { + // we can cast the event to a typed event because only typed + // events will be kicking around on the client; bare DEvent + // instances are only used for internal messages on the server + TypedEvent tevent = (TypedEvent)event; + // send a forward event request to the server - _comm.postMessage(new ForwardEventRequest(event)); + _comm.postMessage(new ForwardEventRequest(tevent)); } /** diff --git a/src/java/com/threerings/presents/dobj/AttributeChangedEvent.java b/src/java/com/threerings/presents/dobj/AttributeChangedEvent.java index 4b51b7bdf..60d3ef9b0 100644 --- a/src/java/com/threerings/presents/dobj/AttributeChangedEvent.java +++ b/src/java/com/threerings/presents/dobj/AttributeChangedEvent.java @@ -1,9 +1,13 @@ // -// $Id: AttributeChangedEvent.java,v 1.3 2001/06/01 20:35:39 mdb Exp $ +// $Id: AttributeChangedEvent.java,v 1.4 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.dobj; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.lang.reflect.Method; + import com.threerings.cocktail.cher.Log; /** @@ -13,8 +17,11 @@ import com.threerings.cocktail.cher.Log; * * @see DObjectManager.postEvent */ -public class AttributeChangedEvent extends DEvent +public class AttributeChangedEvent extends TypedEvent { + /** 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. @@ -106,6 +113,27 @@ public class AttributeChangedEvent extends DEvent return true; } + public short getType () + { + return TYPE; + } + + public void writeTo (DataOutputStream out) + throws IOException + { + super.writeTo(out); + out.writeUTF(_name); + // out.write...(_value); + } + + public void readFrom (DataInputStream in) + throws IOException + { + super.readFrom(in); + _name = in.readUTF(); + // _value = in.read... + } + public String toString () { return "[CHANGE:targetOid=" + _toid + ", name=" + _name + diff --git a/src/java/com/threerings/presents/dobj/AttributesChangedEvent.java b/src/java/com/threerings/presents/dobj/AttributesChangedEvent.java index 56472fc45..8c359c89a 100644 --- a/src/java/com/threerings/presents/dobj/AttributesChangedEvent.java +++ b/src/java/com/threerings/presents/dobj/AttributesChangedEvent.java @@ -1,8 +1,12 @@ // -// $Id: AttributesChangedEvent.java,v 1.2 2001/06/02 01:30:37 mdb Exp $ +// $Id: AttributesChangedEvent.java,v 1.3 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.dobj; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + /** * An attributes changed event is dispatched when multiple * attributes of a distributed object have been changed in a single @@ -10,8 +14,11 @@ package com.threerings.cocktail.cher.dobj; * * @see DObjectManager.postEvent */ -public class AttributesChangedEvent extends DEvent +public class AttributesChangedEvent extends TypedEvent { + /** 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. @@ -152,6 +159,35 @@ public class AttributesChangedEvent extends DEvent return true; } + public short getType () + { + return TYPE; + } + + public void writeTo (DataOutputStream out) + throws IOException + { + super.writeTo(out); + out.writeInt(_count); + for (int i = 0; i < _count; i++) { + out.writeUTF(_names[i]); + // out.write...(_values[i]); + } + } + + public void readFrom (DataInputStream in) + throws IOException + { + super.readFrom(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] = ... + } + } + protected int _count; protected String[] _names; protected Object[] _values; diff --git a/src/java/com/threerings/presents/dobj/TypedEvent.java b/src/java/com/threerings/presents/dobj/TypedEvent.java new file mode 100644 index 000000000..275c2972f --- /dev/null +++ b/src/java/com/threerings/presents/dobj/TypedEvent.java @@ -0,0 +1,65 @@ +// +// $Id: TypedEvent.java,v 1.1 2001/06/11 17:44:04 mdb Exp $ + +package com.threerings.cocktail.cher.dobj; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import com.threerings.cocktail.cher.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 + * DEvent 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 must be sure + * to first call super.writeTo(). + */ + 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 must be sure to + * first call super.readFrom(). + */ + public void readFrom (DataInputStream in) + throws IOException + { + _toid = in.readInt(); + } +} diff --git a/src/java/com/threerings/presents/io/TypedObjectFactory.java b/src/java/com/threerings/presents/io/TypedObjectFactory.java index 3f86ab5f3..80123310a 100644 --- a/src/java/com/threerings/presents/io/TypedObjectFactory.java +++ b/src/java/com/threerings/presents/io/TypedObjectFactory.java @@ -1,5 +1,5 @@ // -// $Id: TypedObjectFactory.java,v 1.4 2001/05/30 23:58:31 mdb Exp $ +// $Id: TypedObjectFactory.java,v 1.5 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.io; @@ -92,4 +92,9 @@ public class TypedObjectFactory /** Our type to class mapping table. */ protected static HashMap _classes = new HashMap(); + + // register our typed object + static { + TypedObjectRegistry.registerTypedObjects(); + } } diff --git a/src/java/com/threerings/presents/net/Registry.java b/src/java/com/threerings/presents/io/TypedObjectRegistry.java similarity index 79% rename from src/java/com/threerings/presents/net/Registry.java rename to src/java/com/threerings/presents/io/TypedObjectRegistry.java index 1623f251d..db5ca72f9 100644 --- a/src/java/com/threerings/presents/net/Registry.java +++ b/src/java/com/threerings/presents/io/TypedObjectRegistry.java @@ -1,18 +1,17 @@ // -// $Id: Registry.java,v 1.5 2001/06/09 23:39:04 mdb Exp $ +// $Id: TypedObjectRegistry.java,v 1.1 2001/06/11 17:44:04 mdb Exp $ -package com.threerings.cocktail.cher.net; +package com.threerings.cocktail.cher.io; -import com.threerings.cocktail.cher.dobj.DObject; -import com.threerings.cocktail.cher.io.TypedObject; -import com.threerings.cocktail.cher.io.TypedObjectFactory; +import com.threerings.cocktail.cher.dobj.*; +import com.threerings.cocktail.cher.net.*; /** * The registry provides a single place where all typed objects that are * exchanged between the client and the server can be registered with the * typed object factory. */ -public class Registry +public class TypedObjectRegistry { /** * Must be called once by the client and the server to ensure that all @@ -50,5 +49,10 @@ public class Registry TypedObjectFactory.registerClass(UsernamePasswordCreds.TYPE, UsernamePasswordCreds.class); + // register our event classes + TypedObjectFactory.registerClass(AttributeChangedEvent.TYPE, + AttributeChangedEvent.class); + TypedObjectFactory.registerClass(AttributesChangedEvent.TYPE, + AttributesChangedEvent.class); } } diff --git a/src/java/com/threerings/presents/net/EventNotification.java b/src/java/com/threerings/presents/net/EventNotification.java index 743ffc295..c343970df 100644 --- a/src/java/com/threerings/presents/net/EventNotification.java +++ b/src/java/com/threerings/presents/net/EventNotification.java @@ -1,5 +1,5 @@ // -// $Id: EventNotification.java,v 1.6 2001/06/09 23:39:04 mdb Exp $ +// $Id: EventNotification.java,v 1.7 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.net; @@ -7,7 +7,8 @@ import java.io.IOException; import java.io.DataInputStream; import java.io.DataOutputStream; -import com.threerings.cocktail.cher.dobj.DEvent; +import com.threerings.cocktail.cher.dobj.TypedEvent; +import com.threerings.cocktail.cher.io.TypedObjectFactory; public class EventNotification extends DownstreamMessage { @@ -25,7 +26,7 @@ public class EventNotification extends DownstreamMessage /** * Constructs an event notification for the supplied event. */ - public EventNotification (DEvent event) + public EventNotification (TypedEvent event) { _event = event; } @@ -35,7 +36,7 @@ public class EventNotification extends DownstreamMessage return TYPE; } - public DEvent getEvent () + public TypedEvent getEvent () { return _event; } @@ -44,16 +45,18 @@ public class EventNotification extends DownstreamMessage throws IOException { super.writeTo(out); - // _event.writeTo(out); + // write the event out to the stream + TypedObjectFactory.writeTo(out, _event); } public void readFrom (DataInputStream in) throws IOException { super.readFrom(in); - // _event = EventFactory.readFrom(in); + // read the event in from the stream + _event = (TypedEvent)TypedObjectFactory.readFrom(in); } /** The event which we are forwarding. */ - protected DEvent _event; + protected TypedEvent _event; } diff --git a/src/java/com/threerings/presents/net/ForwardEventRequest.java b/src/java/com/threerings/presents/net/ForwardEventRequest.java index 0fc480bec..2ce07c213 100644 --- a/src/java/com/threerings/presents/net/ForwardEventRequest.java +++ b/src/java/com/threerings/presents/net/ForwardEventRequest.java @@ -1,5 +1,5 @@ // -// $Id: ForwardEventRequest.java,v 1.6 2001/06/05 22:44:31 mdb Exp $ +// $Id: ForwardEventRequest.java,v 1.7 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.net; @@ -7,7 +7,8 @@ import java.io.IOException; import java.io.DataInputStream; import java.io.DataOutputStream; -import com.threerings.cocktail.cher.dobj.DEvent; +import com.threerings.cocktail.cher.dobj.TypedEvent; +import com.threerings.cocktail.cher.io.TypedObjectFactory; public class ForwardEventRequest extends UpstreamMessage { @@ -25,7 +26,7 @@ public class ForwardEventRequest extends UpstreamMessage /** * Constructs a forward event request for the supplied event. */ - public ForwardEventRequest (DEvent event) + public ForwardEventRequest (TypedEvent event) { _event = event; } @@ -38,7 +39,7 @@ public class ForwardEventRequest extends UpstreamMessage /** * Returns the event that we wish to have forwarded. */ - public DEvent getEvent () + public TypedEvent getEvent () { return _event; } @@ -47,16 +48,18 @@ public class ForwardEventRequest extends UpstreamMessage throws IOException { super.writeTo(out); - // _event.writeTo(out); + // write the event out to the stream + TypedObjectFactory.writeTo(out, _event); } public void readFrom (DataInputStream in) throws IOException { super.readFrom(in); - // _event = EventFactory.readFrom(in); + // read the event in from the stream + _event = (TypedEvent)TypedObjectFactory.readFrom(in); } /** The event which we are forwarding. */ - protected DEvent _event; + protected TypedEvent _event; } diff --git a/src/java/com/threerings/presents/server/PresentsClient.java b/src/java/com/threerings/presents/server/PresentsClient.java index e4d1e7b36..933871a15 100644 --- a/src/java/com/threerings/presents/server/PresentsClient.java +++ b/src/java/com/threerings/presents/server/PresentsClient.java @@ -1,5 +1,5 @@ // -// $Id: PresentsClient.java,v 1.3 2001/06/09 23:39:04 mdb Exp $ +// $Id: PresentsClient.java,v 1.4 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.server; @@ -155,7 +155,10 @@ public class Client implements Subscriber, MessageHandler // forward the event to the client Connection conn = getConnection(); if (conn != null) { - conn.postMessage(new EventNotification(event)); + // only typed events will be forwarded to the client, so we + // need not worry that a non-typed event would make it here + TypedEvent tevent = (TypedEvent)event; + conn.postMessage(new EventNotification(tevent)); } else { Log.info("Dropped event forward notification " + "[client=" + this + ", event=" + event + "]."); diff --git a/src/java/com/threerings/presents/server/net/ConnectionManager.java b/src/java/com/threerings/presents/server/net/ConnectionManager.java index 071c4004d..7e5e1d1bf 100644 --- a/src/java/com/threerings/presents/server/net/ConnectionManager.java +++ b/src/java/com/threerings/presents/server/net/ConnectionManager.java @@ -1,5 +1,5 @@ // -// $Id: ConnectionManager.java,v 1.4 2001/06/02 01:30:37 mdb Exp $ +// $Id: ConnectionManager.java,v 1.5 2001/06/11 17:44:04 mdb Exp $ package com.threerings.cocktail.cher.server.net; @@ -17,7 +17,6 @@ import com.threerings.cocktail.cher.io.FramingOutputStream; import com.threerings.cocktail.cher.io.TypedObjectFactory; import com.threerings.cocktail.cher.net.Credentials; import com.threerings.cocktail.cher.net.DownstreamMessage; -import com.threerings.cocktail.cher.net.Registry; /** * The connection manager manages the socket on which connections are @@ -322,9 +321,4 @@ public class ConnectionManager extends LoopingThread protected static final int CONNECTION_ESTABLISHED = 0; protected static final int CONNECTION_FAILED = 1; protected static final int CONNECTION_CLOSED = 2; - - // register our shared objects - static { - Registry.registerTypedObjects(); - } }