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,17 +0,0 @@
//
// $Id: GameCodes.java,v 1.4 2002/04/15 16:28:02 shaper Exp $
package com.threerings.parlor.game;
import com.threerings.presents.data.InvocationCodes;
/**
* Contains codes used by the game services.
*/
public interface GameCodes extends InvocationCodes
{
/** The message identifier for a player ready notification. This is
* delivered by the game controller when the client has loaded the
* user interface for the game and is ready to play. */
public static final String PLAYER_READY_NOTIFICATION = "PlayerReady";
}
@@ -1,5 +1,5 @@
//
// $Id: GameController.java,v 1.17 2002/06/18 02:35:10 shaper Exp $
// $Id: GameController.java,v 1.18 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
@@ -31,7 +31,7 @@ import com.threerings.parlor.util.ParlorContext;
* distributed object events.
*/
public abstract class GameController extends PlaceController
implements AttributeChangeListener, GameCodes
implements AttributeChangeListener
{
/**
* Initializes this game controller with the game configuration that
@@ -70,10 +70,16 @@ public abstract class GameController extends PlaceController
// and add ourselves as a listener
_gobj.addListener(this);
// finally let the game manager know that we're ready to roll
MessageEvent mevt = new MessageEvent(
_gobj.getOid(), PLAYER_READY_NOTIFICATION, null);
_ctx.getDObjectManager().postEvent(mevt);
// we don't want to claim to be finished until any derived classes
// that overrode this method have executed, so we'll queue up a
// runnable here that will let the game manager know that we're
// ready on the next pass through the distributed event loop
_ctx.getClient().getInvoker().invokeLater(new Runnable() {
public void run () {
// finally let the game manager know that we're ready to roll
_gobj.service.playerReady(_ctx.getClient());
}
});
}
/**
@@ -0,0 +1,51 @@
//
// $Id: GameDispatcher.java,v 1.1 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
import com.threerings.parlor.game.GameMarshaller;
import com.threerings.parlor.game.GameService;
import com.threerings.presents.client.Client;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.server.InvocationDispatcher;
import com.threerings.presents.server.InvocationException;
/**
* Dispatches requests to the {@link GameProvider}.
*/
public class GameDispatcher extends InvocationDispatcher
{
/**
* Creates a dispatcher that may be registered to dispatch invocation
* service requests for the specified provider.
*/
public GameDispatcher (GameProvider provider)
{
this.provider = provider;
}
// documentation inherited
public InvocationMarshaller createMarshaller ()
{
return new GameMarshaller();
}
// documentation inherited
public void dispatchRequest (
ClientObject source, int methodId, Object[] args)
throws InvocationException
{
switch (methodId) {
case GameMarshaller.PLAYER_READY:
((GameProvider)provider).playerReady(
source
);
return;
default:
super.dispatchRequest(source, methodId, args);
}
}
}
@@ -1,5 +1,5 @@
//
// $Id: GameManager.java,v 1.37 2002/06/19 23:41:25 shaper Exp $
// $Id: GameManager.java,v 1.38 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
@@ -7,11 +7,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.dobj.AttributeChangeListener;
import com.threerings.presents.dobj.AttributeChangedEvent;
import com.threerings.presents.dobj.MessageEvent;
import com.threerings.crowd.chat.ChatProvider;
import com.threerings.crowd.chat.SpeakProvider;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.PlaceObject;
@@ -23,6 +24,7 @@ import com.threerings.crowd.server.PlaceManagerDelegate;
import com.threerings.parlor.Log;
import com.threerings.parlor.data.ParlorCodes;
import com.threerings.parlor.server.ParlorSender;
/**
* The game manager handles the server side management of a game. It
@@ -34,34 +36,15 @@ import com.threerings.parlor.data.ParlorCodes;
* bodies in that location.
*/
public class GameManager extends PlaceManager
implements ParlorCodes, GameCodes, AttributeChangeListener
implements ParlorCodes, GameProvider, AttributeChangeListener
{
// documentation inherited
protected Class getPlaceObjectClass ()
{
return GameObject.class;
}
// documentation inherited
protected void didInit ()
{
super.didInit();
// cast our configuration object (do we need to do this?)
_gconfig = (GameConfig)_config;
// register our message handlers
MessageHandler handler = new PlayerReadyHandler();
registerMessageHandler(PLAYER_READY_NOTIFICATION, handler);
}
/**
* Initializes the game manager with the supplied game configuration
* object. This happens before startup and before the game object has
* been created.
* Provides the game manager with a list of the usernames of all
* players in the game. This happens before startup and before the
* game object has been created.
*
* @param players the usernames of all of the players in this game or
* null if the game has no specific set of players.
* a zero-length array if the game has no specific set of players.
*/
public void setPlayers (String[] players)
{
@@ -167,8 +150,9 @@ public class GameManager extends PlaceManager
public void systemMessage (
String msgbundle, String msg, boolean waitForStart)
{
if (waitForStart && ((_gameobj == null) ||
(_gameobj.state == GameObject.AWAITING_PLAYERS))) {
if (waitForStart &&
((_gameobj == null) ||
(_gameobj.state == GameObject.AWAITING_PLAYERS))) {
// queue up the message.
if (_startmsgs == null) {
_startmsgs = new ArrayList();
@@ -179,7 +163,13 @@ public class GameManager extends PlaceManager
}
// otherwise, just deliver the message
ChatProvider.sendSystemMessage(_gameobj.getOid(), msgbundle, msg);
SpeakProvider.sendSystemSpeak(_gameobj, msgbundle, msg);
}
// documentation inherited
protected Class getPlaceObjectClass ()
{
return GameObject.class;
}
// documentation inherited
@@ -193,24 +183,27 @@ public class GameManager extends PlaceManager
// stick the players into the game object
_gameobj.setPlayers(_players);
// create and fill in our game service object
GameMarshaller service = (GameMarshaller)
_invmgr.registerDispatcher(new GameDispatcher(this), false);
_gameobj.setService(service);
// let the players of this game know that we're ready to roll (if
// we have a specific set of players)
if (_players != null) {
Object[] args = new Object[] {
new Integer(_gameobj.getOid()) };
int gameOid = _gameobj.getOid();
for (int i = 0; i < _players.length; i++) {
BodyObject bobj = CrowdServer.lookupBody(_players[i]);
if (bobj == null) {
Log.warning("Unable to deliver game ready to " +
"non-existent player " +
"[gameOid=" + _gameobj.getOid() +
"[gameOid=" + gameOid +
", player=" + _players[i] + "].");
continue;
}
// deliver a game ready notification to the player
CrowdServer.invmgr.sendNotification(
bobj.getOid(), MODULE_NAME, GAME_READY_NOTIFICATION, args);
ParlorSender.gameIsReady(bobj, gameOid);
}
}
}
@@ -448,6 +441,35 @@ public class GameManager extends PlaceManager
});
}
// documentation inherited from interface
public void playerReady (ClientObject caller)
{
BodyObject plobj = (BodyObject)caller;
// make a note of this player's oid
int pidx = _gameobj.getPlayerIndex(plobj.username);
if (pidx == -1) {
Log.warning("Received playerReady() from non-player? " +
"[caller=" + caller + "].");
return;
}
_playerOids[pidx] = plobj.getOid();
// and check to see if we're all set
boolean allSet = true;
for (int ii = 0; ii < _players.length; ii++) {
if ((_playerOids[ii] == 0) &&
((_AIs == null) || (_AIs[ii] == -1))) {
allSet = false;
}
}
// if everyone is now ready to go, make a note of it
if (allSet) {
playersAllHere();
}
}
// documentation inherited
public void attributeChanged (AttributeChangedEvent event)
{
@@ -463,39 +485,6 @@ public class GameManager extends PlaceManager
}
}
/** Handles player ready notifications. */
protected class PlayerReadyHandler implements MessageHandler
{
public void handleEvent (MessageEvent event, PlaceManager pmgr)
{
int cloid = event.getSourceOid();
BodyObject body = (BodyObject)CrowdServer.omgr.getObject(cloid);
if (body == null) {
Log.warning("Player sent am ready notification and then " +
"disappeared [event=" + event + "].");
return;
}
// make a note of this player's oid and check to see if we're
// all set at the same time
boolean allSet = true;
for (int i = 0; i < _players.length; i++) {
if (_players[i].equals(body.username)) {
_playerOids[i] = body.getOid();
}
if ((_playerOids[i] == 0) &&
((_AIs == null) || (_AIs[i] == -1))) {
allSet = false;
}
}
// if everyone is now ready to go, make a note of it
if (allSet) {
playersAllHere();
}
}
}
/**
* A helper operation to distribute AI ticks to our delegates.
*/
@@ -515,9 +504,6 @@ public class GameManager extends PlaceManager
protected byte _level;
}
/** A reference to our game configuration. */
protected GameConfig _gconfig;
/** A reference to our game object. */
protected GameObject _gameobj;
@@ -0,0 +1,33 @@
//
// $Id: GameMarshaller.java,v 1.1 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
import com.threerings.parlor.game.GameService;
import com.threerings.presents.client.Client;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.dobj.InvocationResponseEvent;
/**
* Provides the implementation of the {@link GameService} interface
* that marshalls the arguments and delivers the request to the provider
* on the server. Also provides an implementation of the response listener
* interfaces that marshall the response arguments and deliver them back
* to the requesting client.
*/
public class GameMarshaller extends InvocationMarshaller
implements GameService
{
/** The method id used to dispatch {@link #playerReady} requests. */
public static final int PLAYER_READY = 1;
// documentation inherited from interface
public void playerReady (Client arg1)
{
sendRequest(arg1, PLAYER_READY, new Object[] {
});
}
// Class file generated on 00:26:01 08/11/02.
}
@@ -1,5 +1,5 @@
//
// $Id: GameObject.dobj,v 1.10 2002/08/09 23:34:10 shaper Exp $
// $Id: GameObject.dobj,v 1.11 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
@@ -33,6 +33,9 @@ public class GameObject extends PlaceObject
/** A game state constant indicating that the game was cancelled. */
public static final int CANCELLED = GAME_OVER+3;
/** Provides general game invocation services. */
public GameMarshaller service;
/** The game state, one of {@link #AWAITING_PLAYERS}, {@link #IN_PLAY},
* {@link #GAME_OVER}, or {@link #CANCELLED}. */
public int state;
@@ -1,5 +1,5 @@
//
// $Id: GameObject.java,v 1.5 2002/08/09 23:34:10 shaper Exp $
// $Id: GameObject.java,v 1.6 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
@@ -20,6 +20,9 @@ import com.threerings.crowd.data.PlaceObject;
*/
public class GameObject extends PlaceObject
{
/** The field name of the <code>service</code> field. */
public static final String SERVICE = "service";
/** The field name of the <code>state</code> field. */
public static final String STATE = "state";
@@ -45,6 +48,9 @@ public class GameObject extends PlaceObject
/** A game state constant indicating that the game was cancelled. */
public static final int CANCELLED = GAME_OVER+3;
/** Provides general game invocation services. */
public GameMarshaller service;
/** The game state, one of {@link #AWAITING_PLAYERS}, {@link #IN_PLAY},
* {@link #GAME_OVER}, or {@link #CANCELLED}. */
public int state;
@@ -68,6 +74,20 @@ public class GameObject extends PlaceObject
ListUtil.indexOfEqual(players, username);
}
/**
* Requests that the <code>service</code> field be set to the specified
* value. The local value will be updated immediately and an event
* will be propagated through the system to notify all listeners that
* the attribute did change. Proxied copies of this object (on
* clients) will apply the value change when they received the
* attribute changed notification.
*/
public void setService (GameMarshaller service)
{
this.service = service;
requestAttributeChange(SERVICE, service);
}
/**
* Requests that the <code>state</code> field be set to the specified
* value. The local value will be updated immediately and an event
@@ -0,0 +1,20 @@
//
// $Id: GameProvider.java,v 1.1 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.server.InvocationProvider;
/**
* Provides access to the server-side implementation of the game
* invocation services.
*/
public interface GameProvider extends InvocationProvider
{
/**
* Called when the client has sent a {@link GameService#playerReady}
* service request.
*/
public void playerReady (ClientObject caller);
}
@@ -0,0 +1,20 @@
//
// $Id: GameService.java,v 1.1 2002/08/14 19:07:53 mdb Exp $
package com.threerings.parlor.game;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.InvocationService;
/**
* Provides services used by game clients to request that actions be taken
* by the game manager.
*/
public interface GameService extends InvocationService
{
/**
* Lets the game manager know that the calling player is in the game
* room and ready to play.
*/
public void playerReady (Client client);
}