diff --git a/src/java/com/threerings/crowd/client/LocationDirector.java b/src/java/com/threerings/crowd/client/LocationDirector.java index fc90921b7..c1cd21c9e 100644 --- a/src/java/com/threerings/crowd/client/LocationDirector.java +++ b/src/java/com/threerings/crowd/client/LocationDirector.java @@ -1,13 +1,113 @@ // -// $Id: LocationDirector.java,v 1.2 2001/07/20 23:23:50 mdb Exp $ +// $Id: LocationDirector.java,v 1.3 2001/07/23 21:14:27 mdb Exp $ package com.threerings.cocktail.party.client; -import com.threerings.cocktail.cher.client.Client; +import com.threerings.cocktail.cher.client.*; +import com.threerings.cocktail.cher.dobj.*; +import com.threerings.cocktail.party.Log; +import com.threerings.cocktail.party.data.BodyObject; +import com.threerings.cocktail.party.util.PartyContext; + +/** + * The location manager provides a means by which entities on the client + * can request to move from place to place and can be notified if other + * entities have caused the client to move to a new place. It also + * provides a mechanism for ratifying a request to move to a new place + * before actually issuing the request. + */ public class LocationManager + implements ClientObserver { - public LocationManager (Client client) + public LocationManager (PartyContext ctx) { + // keep this around for later + _ctx = ctx; + + // register ourselves as a client observer + ctx.getClient().addObserver(this); } + + /** + * Requests that this client be moved to the specified place. A + * request will be made and when the response is received, the + * location observers will be notified of success or failure. + */ + public void moveTo (int placeId) + { + // issue a moveTo request + LocationService.moveTo(_ctx.getClient(), placeId, this); + } + + public void clientDidLogon (Client client) + { + // get a copy of our body object + Subscriber sub = new Subscriber() { + public void objectAvailable (DObject object) + { + gotBodyObject((BodyObject)object); + } + + public void requestFailed (int oid, ObjectAccessException cause) + { + Log.warning("Location manager unable to fetch body " + + "object; all has gone horribly wrong" + + "[cause=" + cause + "]."); + } + + public boolean handleEvent (DEvent event, DObject target) + { + return false; + } + }; + int cloid = client.getClientOid(); + client.getDObjectManager().subscribeToObject(cloid, sub); + } + + protected void gotBodyObject (BodyObject clobj) + { + // check to see if we are already in a location, in which case + // we'll want to be going there straight away + } + + public void clientFailedToLogon (Client client, Exception cause) + { + // we're fair weather observers. we do nothing until logon + // succeeds + } + + public void clientConnectionFailed (Client client, Exception cause) + { + // nothing doing + } + + public boolean clientWillLogoff (Client client) + { + // we have no objections + return true; + } + + public void clientDidLogoff (Client client) + { + // nothing doing + } + + /** + * Called in response to a successful moveTo request. + */ + public void handleMoveSucceeded () + { + Log.info("Move succeeded."); + } + + /** + * Called in response to a failed moveTo request. + */ + public void handleMoveFailed (String reason) + { + Log.info("Move failed [reason=" + reason + "]."); + } + + protected PartyContext _ctx; } diff --git a/src/java/com/threerings/crowd/client/LocationService.java b/src/java/com/threerings/crowd/client/LocationService.java new file mode 100644 index 000000000..8c1a4a061 --- /dev/null +++ b/src/java/com/threerings/crowd/client/LocationService.java @@ -0,0 +1,35 @@ +// +// $Id: LocationService.java,v 1.1 2001/07/23 21:14:27 mdb Exp $ + +package com.threerings.cocktail.party.client; + +import com.threerings.cocktail.cher.client.Client; +import com.threerings.cocktail.cher.client.InvocationManager; +import com.threerings.cocktail.party.Log; + +/** + * The location services provide a mechanism by which the client can + * request to move from place to place in the server. These services + * should not be used directly, but instead should be accessed via the + * location manager. + * + * @see LocationManager + */ +public class LocationService +{ + /** The module name for the location services. */ + public static final String MODULE = "location"; + + /** + * Requests that that this client's body be moved to the specified + * location. + */ + public static void moveTo (Client client, int placeId, + LocationManager rsptarget) + { + InvocationManager invmgr = client.getInvocationManager(); + Object[] args = new Object[] { new Integer(placeId) }; + invmgr.invoke(MODULE, "MoveTo", args, rsptarget); + Log.info("Sent moveTo request [place=" + placeId + "]."); + } +} diff --git a/src/java/com/threerings/crowd/data/PlaceObject.dobj b/src/java/com/threerings/crowd/data/PlaceObject.dobj index 2577b6848..138ad021e 100644 --- a/src/java/com/threerings/crowd/data/PlaceObject.dobj +++ b/src/java/com/threerings/crowd/data/PlaceObject.dobj @@ -1,10 +1,36 @@ // -// $Id: PlaceObject.dobj,v 1.1 2001/07/20 20:07:37 mdb Exp $ +// $Id: PlaceObject.dobj,v 1.2 2001/07/23 21:14:27 mdb Exp $ -package com.threerings.cocktail.party.place; +package com.threerings.cocktail.party.data; -import com.threerings.cocktail.cher.dobj.DObject; +import com.threerings.cocktail.cher.dobj.*; public class PlaceObject extends DObject { + /** The field name of the occupants field. */ + public static final String OCCUPANTS = "occupants"; + + /** + * Tracks the oid of the body objects of all of the occupants of this + * place. + */ + public OidList occupants; + + /** + * Requests that the specified oid be added to the + * occupants oid list. + */ + public void addToOccupants (int oid) + { + requestOidAdd(OCCUPANTS, oid); + } + + /** + * Requests that the specified oid be removed from the + * occupants oid list. + */ + public void removeFromOccupants (int oid) + { + requestOidRemove(OCCUPANTS, oid); + } } diff --git a/src/java/com/threerings/crowd/server/CrowdServer.java b/src/java/com/threerings/crowd/server/CrowdServer.java new file mode 100644 index 000000000..37cb0887f --- /dev/null +++ b/src/java/com/threerings/crowd/server/CrowdServer.java @@ -0,0 +1,61 @@ +// +// $Id: CrowdServer.java,v 1.1 2001/07/23 21:14:27 mdb Exp $ + +package com.threerings.cocktail.party.server; + +import java.io.IOException; + +import com.threerings.cocktail.cher.server.CherServer; + +import com.threerings.cocktail.party.Log; +import com.threerings.cocktail.party.data.BodyObject; + +/** + * The party server extends the cher server by configuring it to use the + * extensions provided by the party layer to support party services. + */ +public class PartyServer extends CherServer +{ + /** The namespace used for server config properties. */ + public static final String CONFIG_KEY = "party"; + + /** + * Initializes all of the server services and prepares for operation. + */ + public void init () + throws IOException + { + // do the cher server initialization + super.init(); + + // bind the party server config into the namespace + config.bindProperties(CONFIG_KEY, CONFIG_PATH); + + // configure the client to use the body object + clmgr.setClientObjectClass(BodyObject.class); + + // register our invocation service providers + registerProviders(config.getValue(PROVIDERS_KEY, (String[])null)); + + Log.info("Party server initialized."); + } + + public static void main (String[] args) + { + PartyServer server = new PartyServer(); + try { + server.init(); + server.run(); + } catch (Exception e) { + Log.warning("Unable to initialize server."); + Log.logStackTrace(e); + } + } + + // the path to the config file + protected final static String CONFIG_PATH = + "rsrc/config/cocktail/party/server"; + + // the config key for our list of invocation provider mappings + protected final static String PROVIDERS_KEY = CONFIG_KEY + ".providers"; +} diff --git a/src/java/com/threerings/crowd/server/LocationProvider.java b/src/java/com/threerings/crowd/server/LocationProvider.java new file mode 100644 index 000000000..13e214d90 --- /dev/null +++ b/src/java/com/threerings/crowd/server/LocationProvider.java @@ -0,0 +1,36 @@ +// +// $Id: LocationProvider.java,v 1.1 2001/07/23 21:14:27 mdb Exp $ + +package com.threerings.cocktail.party.server; + +import com.threerings.cocktail.cher.dobj.DObject; +import com.threerings.cocktail.cher.server.CherServer; +import com.threerings.cocktail.cher.server.InvocationProvider; + +import com.threerings.cocktail.party.Log; +import com.threerings.cocktail.party.data.*; + +/** + * This class provides the server end of the location services. + */ +public class LocationProvider extends InvocationProvider +{ + /** + * Processes a request from a client to move to a new place. + */ + public Object[] handleMoveToRequest (BodyObject source, int placeId) + { + // make sure the place in question actually exists + DObject pobj = CherServer.omgr.getObject(placeId); + if (pobj == null || !(pobj instanceof PlaceObject)) { + Log.info("Requested to move to non-existent place " + + "[source=" + source + ", place=" + placeId + "]."); + return createResponse("MoveFailed", "m.no_such_place"); + } + + // add the body object id to the place object's occupant list + PlaceObject place = (PlaceObject)pobj; + place.addToOccupants(source.getOid()); + return createResponse("MoveSucceeded"); + } +} diff --git a/src/java/com/threerings/crowd/server/PlaceManager.java b/src/java/com/threerings/crowd/server/PlaceManager.java index 84e8dbb44..6ff21aa79 100644 --- a/src/java/com/threerings/crowd/server/PlaceManager.java +++ b/src/java/com/threerings/crowd/server/PlaceManager.java @@ -1,7 +1,7 @@ // -// $Id: PlaceManager.java,v 1.1 2001/07/20 20:07:37 mdb Exp $ +// $Id: PlaceManager.java,v 1.2 2001/07/23 21:14:27 mdb Exp $ -package com.threerings.cocktail.party.place; +package com.threerings.cocktail.party.server; public class PlaceManager { diff --git a/tests/src/java/com/threerings/crowd/client/TestClient.java b/tests/src/java/com/threerings/crowd/client/TestClient.java index e8bde63c7..bb0de67a7 100644 --- a/tests/src/java/com/threerings/crowd/client/TestClient.java +++ b/tests/src/java/com/threerings/crowd/client/TestClient.java @@ -1,5 +1,5 @@ // -// $Id: TestClient.java,v 1.1 2001/07/20 23:23:50 mdb Exp $ +// $Id: TestClient.java,v 1.2 2001/07/23 21:14:27 mdb Exp $ package com.threerings.cocktail.party.client.test; @@ -19,13 +19,13 @@ public class TestClient { public TestClient (Credentials creds) { + // create our context + _ctx = new PartyContextImpl(); + // create the handles on our various services _config = new Config(); _client = new Client(creds, this); - _locmgr = new LocationManager(_client); - - // create our context - _ctx = new PartyContextImpl(); + _locmgr = new LocationManager(_ctx); // we want to know about logon/logoff _client.addObserver(this); @@ -55,6 +55,9 @@ public class TestClient public void clientDidLogon (Client client) { Log.info("Client did logon [client=" + client + "]."); + + // request to move to a place + _ctx.getLocationManager().moveTo(15); } public void clientFailedToLogon (Client client, Exception cause)