Initial revision of cocktail multiplayer networked gaming platform. (Far
from complete.) git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// $Id: AuthRequest.java,v 1.1 2001/05/22 06:07:59 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
import com.samskivert.cocktail.cher.io.TypedObjectFactory;
|
||||
|
||||
public class AuthenticationRequest extends UpstreamMessage
|
||||
{
|
||||
/** The code for an object subscription request. */
|
||||
public static final short TYPE = TYPE_BASE + 0;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public AuthenticationRequest ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a authentication request with the supplied credentials.
|
||||
*/
|
||||
public AuthenticationRequest (Credentials creds)
|
||||
{
|
||||
_creds = creds;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
super.writeTo(out);
|
||||
_creds.writeTo(out);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
super.readFrom(in);
|
||||
_creds = (Credentials)TypedObjectFactory.readFrom(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* The object id of the distributed object to which we are
|
||||
* subscribing.
|
||||
*/
|
||||
protected Credentials _creds;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// $Id: Credentials.java,v 1.1 2001/05/22 06:07:59 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import com.samskivert.cocktail.cher.io.TypedObject;
|
||||
import com.samskivert.cocktail.cher.io.TypedObjectFactory;
|
||||
|
||||
/**
|
||||
* Credentials are supplied by the client implementation and sent along to
|
||||
* the server during the authentication process. To provide support for a
|
||||
* variety of authentication methods, the credentials class is meant to be
|
||||
* subclassed for the particular method (ie. username + password) in use
|
||||
* in a given system.
|
||||
*
|
||||
* <p> All derived classes should provide a no argument constructor so
|
||||
* that they can be instantiated prior to reconstruction from a data input
|
||||
* stream.
|
||||
*/
|
||||
public abstract class Credentials extends TypedObject
|
||||
{
|
||||
/**
|
||||
* All credential derived classes should base their typed object code
|
||||
* on this base value.
|
||||
*/
|
||||
public static final short TYPE_BASE = 300;
|
||||
|
||||
// register our credential classes
|
||||
static {
|
||||
TypedObjectFactory.registerClass(UsernamePasswordCreds.TYPE,
|
||||
UsernamePasswordCreds.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// $Id: DownstreamMessage.java,v 1.1 2001/05/22 06:07:59 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import com.samskivert.cocktail.cher.io.TypedObject;
|
||||
import com.samskivert.cocktail.cher.io.TypedObjectFactory;
|
||||
|
||||
/**
|
||||
* The <code>DownstreamMessage</code> class encapsulates a message in the
|
||||
* Distributed Object Protocol that flows from the server to the
|
||||
* client. Downstream messages include object subscription, event
|
||||
* forwarding and session management.
|
||||
*/
|
||||
public class DownstreamMessage
|
||||
{
|
||||
/**
|
||||
* All downstream message derived classes should base their typed
|
||||
* object code on this base value.
|
||||
*/
|
||||
public static final short TYPE_BASE = 200;
|
||||
|
||||
/**
|
||||
* The message id of the upstream message with which this downstream
|
||||
* message is associated (or -1 if it is not associated with any
|
||||
* upstream message). Because not every downstream message class cares
|
||||
* to provide an upstream message id, this field is not serialized
|
||||
* when the base downstream message class is serialized. Thus derived
|
||||
* classes that care about message id should take care to initialize,
|
||||
* serialize and unserialize the value theirselves.
|
||||
*/
|
||||
public short messageId = -1;
|
||||
|
||||
/**
|
||||
* Each downstream message derived class must provide a zero argument
|
||||
* constructor so that the <code>TypedObjectFactory</code> can create
|
||||
* a new instance of said class prior to unserializing it.
|
||||
*/
|
||||
public DownstreamMessage ()
|
||||
{
|
||||
// nothing to do...
|
||||
}
|
||||
|
||||
// register our downstream message classes
|
||||
static {
|
||||
TypedObjectFactory.registerClass(AuthenticationRequest.TYPE,
|
||||
AuthenticationRequest.class);
|
||||
TypedObjectFactory.registerClass(SubscribeRequest.TYPE,
|
||||
SubscribeRequest.class);
|
||||
TypedObjectFactory.registerClass(FetchRequest.TYPE,
|
||||
FetchRequest.class);
|
||||
TypedObjectFactory.registerClass(UnsubscribeNotification.TYPE,
|
||||
UnsubscribeNotification.class);
|
||||
TypedObjectFactory.registerClass(ForwardEventNotification.TYPE,
|
||||
ForwardEventNotification.class);
|
||||
TypedObjectFactory.registerClass(PingNotification.TYPE,
|
||||
PingNotification.class);
|
||||
TypedObjectFactory.registerClass(LogoffNotification.TYPE,
|
||||
LogoffNotification.class);
|
||||
}
|
||||
|
||||
/** The code for an event notification. */
|
||||
public static final byte EVENT = 0x00;
|
||||
/** The code for a fetch/subscribe object response. */
|
||||
public static final byte OBJECT = 0x01;
|
||||
/** The code for a request failure. */
|
||||
public static final byte FAILURE = 0x02;
|
||||
/** The code for a pong response. */
|
||||
public static final byte PONG = 0x03;
|
||||
/** The code for an end of transmission notification. */
|
||||
public static final byte EOT = 0x04;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// $Id: EventNotification.java,v 1.1 2001/05/22 06:07:59 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
public class EventNotification extends DownstreamMessage
|
||||
{
|
||||
/** The code for an event notification. */
|
||||
public static final short TYPE = TYPE_BASE + 1;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public EventNotification ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an event notification for the supplied event.
|
||||
*/
|
||||
public EventNotification (Event event)
|
||||
{
|
||||
_event = event;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
super.writeTo(out);
|
||||
_event.writeTo(out);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
super.readFrom(in);
|
||||
_event = EventFactory.readFrom(in);
|
||||
}
|
||||
|
||||
/** The event which we are forwarding. */
|
||||
protected Event _event;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// $Id: FetchRequest.java,v 1.1 2001/05/22 06:07:59 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
public class FetchRequest extends UpstreamMessage
|
||||
{
|
||||
/** The code for an object fetch request. */
|
||||
public static final short TYPE = TYPE_BASE + 2;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public FetchRequest ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a fetch request for the distributed object with the
|
||||
* specified object id.
|
||||
*/
|
||||
public FetchRequest (int oid)
|
||||
{
|
||||
_oid = oid;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
super.writeTo(out);
|
||||
out.writeInt(_oid);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
super.readFrom(in);
|
||||
_oid = in.readInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* The object id of the distributed object which we are fetching.
|
||||
*/
|
||||
protected int _oid;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// $Id: ForwardEventRequest.java,v 1.1 2001/05/22 06:07:59 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
import com.samskivert.cocktail.cher.dobj.Event;
|
||||
import com.samskivert.cocktail.cher.dobj.EventFactory;
|
||||
|
||||
public class ForwardEventNotification extends UpstreamMessage
|
||||
{
|
||||
/** The code for a forward event notification. */
|
||||
public static final short TYPE = TYPE_BASE + 4;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public ForwardEventNotification ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a forward event notification for the supplied event.
|
||||
*/
|
||||
public ForwardEventNotification (Event event)
|
||||
{
|
||||
_event = event;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
super.writeTo(out);
|
||||
_event.writeTo(out);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
super.readFrom(in);
|
||||
_event = EventFactory.readFrom(in);
|
||||
}
|
||||
|
||||
/** The event which we are forwarding. */
|
||||
protected Event _event;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// $Id: LogoffRequest.java,v 1.1 2001/05/22 06:08:00 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
public class LogoffNotification extends UpstreamMessage
|
||||
{
|
||||
/** The code for a logoff notification. */
|
||||
public static final short TYPE = TYPE_BASE + 6;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public LogoffNotification ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// $Id: PingRequest.java,v 1.1 2001/05/22 06:08:00 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
public class PingNotification extends UpstreamMessage
|
||||
{
|
||||
/** The code for a ping notification. */
|
||||
public static final short TYPE = TYPE_BASE + 5;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public PingNotification ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// $Id: SubscribeRequest.java,v 1.1 2001/05/22 06:08:00 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
public class SubscribeRequest extends UpstreamMessage
|
||||
{
|
||||
/** The code for an object subscription request. */
|
||||
public static final short TYPE = TYPE_BASE + 1;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public SubscribeRequest ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a subscribe request for the distributed object with the
|
||||
* specified object id.
|
||||
*/
|
||||
public SubscribeRequest (int oid)
|
||||
{
|
||||
_oid = oid;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
super.writeTo(out);
|
||||
out.writeInt(_oid);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
super.readFrom(in);
|
||||
_oid = in.readInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* The object id of the distributed object to which we are
|
||||
* subscribing.
|
||||
*/
|
||||
protected int _oid;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// $Id: UnsubscribeRequest.java,v 1.1 2001/05/22 06:08:00 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
public class UnsubscribeNotification extends UpstreamMessage
|
||||
{
|
||||
/** The code for an unsubscribe notification. */
|
||||
public static final short TYPE = TYPE_BASE + 3;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public UnsubscribeNotification ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a unsubscribe notification for the distributed object
|
||||
* with the specified object id.
|
||||
*/
|
||||
public UnsubscribeNotification (int oid)
|
||||
{
|
||||
_oid = oid;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
super.writeTo(out);
|
||||
out.writeInt(_oid);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
super.readFrom(in);
|
||||
_oid = in.readInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* The object id of the distributed object from which we are
|
||||
* unsubscribing.
|
||||
*/
|
||||
protected int _oid;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// $Id: UpstreamMessage.java,v 1.1 2001/05/22 06:08:00 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
import com.samskivert.cocktail.cher.io.TypedObject;
|
||||
import com.samskivert.cocktail.cher.io.TypedObjectFactory;
|
||||
|
||||
/**
|
||||
* The <code>UpstreamMessage</code> class encapsulates a message in the
|
||||
* Distributed Object Protocol that flows from the client to the server.
|
||||
* Upstream messages include object subscription, event forwarding and
|
||||
* session management.
|
||||
*/
|
||||
public abstract class UpstreamMessage extends TypedObject
|
||||
{
|
||||
/**
|
||||
* All upstream message derived classes should base their typed object
|
||||
* code on this base value.
|
||||
*/
|
||||
public static final short TYPE_BASE = 100;
|
||||
|
||||
/**
|
||||
* This is a unique (within the context of a reasonable period of
|
||||
* time) identifier assigned to each upstream message. The message ids
|
||||
* are used to correlate a downstream response message to the
|
||||
* appropriate upstream request message.
|
||||
*/
|
||||
public short messageId;
|
||||
|
||||
/**
|
||||
* Each upstream message derived class must provide a zero argument
|
||||
* constructor so that the <code>TypedObjectFactory</code> can create
|
||||
* a new instance of said class prior to unserializing it.
|
||||
*/
|
||||
public UpstreamMessage ()
|
||||
{
|
||||
// automatically generate a valid message id; on the client, this
|
||||
// will be used, on the server it will be overwritten by the
|
||||
// unserialized value
|
||||
this.messageId = nextMessageId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
super.writeTo(out);
|
||||
out.writeShort(messageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
super.readFrom(in);
|
||||
messageId = in.readShort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next message id suitable for use by an upstream
|
||||
* message.
|
||||
*/
|
||||
protected static synchronized short nextMessageId ()
|
||||
{
|
||||
_nextMessageId = (short)((_nextMessageId + 1) % Short.MAX_VALUE);
|
||||
return _nextMessageId;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used to generate monotonically increasing message ids on
|
||||
* the client as new messages are generated.
|
||||
*/
|
||||
protected static short _nextMessageId;
|
||||
|
||||
// register our upstream message classes
|
||||
static {
|
||||
TypedObjectFactory.registerClass(AuthenticationRequest.TYPE,
|
||||
AuthenticationRequest.class);
|
||||
TypedObjectFactory.registerClass(SubscribeRequest.TYPE,
|
||||
SubscribeRequest.class);
|
||||
TypedObjectFactory.registerClass(FetchRequest.TYPE,
|
||||
FetchRequest.class);
|
||||
TypedObjectFactory.registerClass(UnsubscribeNotification.TYPE,
|
||||
UnsubscribeNotification.class);
|
||||
TypedObjectFactory.registerClass(ForwardEventNotification.TYPE,
|
||||
ForwardEventNotification.class);
|
||||
TypedObjectFactory.registerClass(PingNotification.TYPE,
|
||||
PingNotification.class);
|
||||
TypedObjectFactory.registerClass(LogoffNotification.TYPE,
|
||||
LogoffNotification.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// $Id: UsernamePasswordCreds.java,v 1.1 2001/05/22 06:08:00 mdb Exp $
|
||||
|
||||
package com.samskivert.cocktail.cher.net;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class UsernamePasswordCreds
|
||||
{
|
||||
public static final short TYPE = TYPE_BASE + 0;
|
||||
|
||||
/**
|
||||
* Zero argument constructor used when unserializing an instance.
|
||||
*/
|
||||
public UsernamePasswordCreds ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct credentials with the supplied username and password.
|
||||
*/
|
||||
public UsernamePasswordCreds (String username, String password)
|
||||
{
|
||||
_username = username;
|
||||
_password = password;
|
||||
}
|
||||
|
||||
public short getType ()
|
||||
{
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public void writeTo (DataOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
out.writeUTF(_username);
|
||||
out.writeUTF(_password);
|
||||
}
|
||||
|
||||
public void readFrom (DataInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
_username = in.readUTF();
|
||||
_password = in.readUTF();
|
||||
}
|
||||
|
||||
protected String _username;
|
||||
protected String _password;
|
||||
}
|
||||
Reference in New Issue
Block a user