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
This commit is contained in:
Michael Bayne
2001-06-11 17:44:04 +00:00
parent a8bddf0238
commit 89083f4708
11 changed files with 183 additions and 43 deletions
@@ -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();
}
}
@@ -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));
}
/**
@@ -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 +
@@ -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 attribute<em>s</em> 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;
@@ -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
* <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,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();
}
}
@@ -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);
}
}
@@ -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;
}
@@ -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;
}
@@ -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 + "].");
@@ -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();
}
}