Widened, modified traversePortal() so that the entire portal being traversed is

kept around and passed to mapEnteringBody() so that in Whirled instead of
having a portal map to a destination portal we can simply have the source
portal contain the location in the destination scene at which the arriver is to
be mapped.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@321 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-06-22 21:59:04 +00:00
parent 114fc2f0e7
commit fe2fc21830
2 changed files with 164 additions and 227 deletions
@@ -55,11 +55,10 @@ public class SpotProvider
implements SpotCodes, InvocationProvider implements SpotCodes, InvocationProvider
{ {
/** /**
* Creates a spot provider that can be registered with the invocation * Creates a spot provider that can be registered with the invocation manager to handle spot
* manager to handle spot services. * services.
*/ */
public SpotProvider (RootDObjectManager omgr, PlaceRegistry plreg, public SpotProvider (RootDObjectManager omgr, PlaceRegistry plreg, SceneRegistry screg)
SceneRegistry screg)
{ {
// we'll need these later // we'll need these later
_plreg = plreg; _plreg = plreg;
@@ -77,9 +76,9 @@ public class SpotProvider
// le sanity check // le sanity check
int cSceneId = getCallerSceneId(caller); int cSceneId = getCallerSceneId(caller);
if (cSceneId != sceneId) { if (cSceneId != sceneId) {
Log.info("Ignoring stale traverse portal request " + Log.info("Ignoring stale traverse portal request [caller=" + caller.who() +
"[caller=" + caller.who() + ", oSceneId=" + sceneId + ", oSceneId=" + sceneId + ", portalId=" + portalId +
", portalId=" + portalId + ", cSceneId=" + cSceneId + "]."); ", cSceneId=" + cSceneId + "].");
InvocationMarshaller.setNoResponse(listener); InvocationMarshaller.setNoResponse(listener);
return; return;
} }
@@ -91,8 +90,7 @@ public class SpotProvider
final SceneMoveListener flistener = listener; final SceneMoveListener flistener = listener;
// obtain the source scene // obtain the source scene
final SpotSceneManager srcmgr = (SpotSceneManager) final SpotSceneManager srcmgr = (SpotSceneManager)_screg.getSceneManager(sceneId);
_screg.getSceneManager(sceneId);
if (srcmgr == null) { if (srcmgr == null) {
Log.warning("Traverse portal missing source scene " + Log.warning("Traverse portal missing source scene " +
"[user=" + fsource.who() + ", sceneId=" + sceneId + "[user=" + fsource.who() + ", sceneId=" + sceneId +
@@ -112,60 +110,52 @@ public class SpotProvider
// make sure this portal has valid info // make sure this portal has valid info
if (fdest == null || !fdest.isValid()) { if (fdest == null || !fdest.isValid()) {
Log.warning("Traverse portal with invalid portal " + Log.warning("Traverse portal with invalid portal [user=" + fsource.who() +
"[user=" + fsource.who() + ", scene=" + srcmgr.where() + ", scene=" + srcmgr.where() + ", pid=" + portalId + ", portal=" + fdest +
", pid=" + portalId + ", portal=" + fdest + ", portals=" + StringUtil.toString(rss.getPortals()) + "].");
", portals=" + StringUtil.toString(rss.getPortals()) +
"].");
throw new InvocationException(NO_SUCH_PORTAL); throw new InvocationException(NO_SUCH_PORTAL);
} }
// resolve their destination scene // resolve their destination scene
SceneRegistry.ResolutionListener rl = SceneRegistry.ResolutionListener rl = new SceneRegistry.ResolutionListener() {
new SceneRegistry.ResolutionListener() { public void sceneWasResolved (SceneManager scmgr) {
public void sceneWasResolved (SceneManager scmgr) { // make sure our caller is still around; under heavy load, clients might end their
// make sure our caller is still around; under heavy // session while the scene is resolving
// load, clients might end their session while the if (!fsource.isActive()) {
// scene is resolving Log.info("Abandoning portal traversal, client gone [who=" + fsource.who() +
if (!fsource.isActive()) { ", dest=" + scmgr.where() + "].");
Log.info("Abandoning portal traversal, client gone " + InvocationMarshaller.setNoResponse(flistener);
"[who=" + fsource.who() + return;
", dest=" + scmgr.where() + "].");
InvocationMarshaller.setNoResponse(flistener);
return;
}
// let the source manager know that this guy is
// departing via the specified portal
srcmgr.willTraversePortal(fsource, fdest);
SpotSceneManager sscmgr = (SpotSceneManager)scmgr;
finishTraversePortalRequest(
fsource, sscmgr, fsceneVer, fdest, flistener);
} }
public void sceneFailedToResolve ( // let the source manager know that this guy is departing via the specified portal
int rsceneId, Exception reason) { srcmgr.willTraversePortal(fsource, fdest);
Log.warning("Unable to resolve target scene " +
"[sceneId=" + rsceneId + SpotSceneManager sscmgr = (SpotSceneManager)scmgr;
", reason=" + reason + "]."); finishTraversePortalRequest(fsource, sscmgr, fsceneVer, fdest, flistener);
// pretend like the scene doesn't exist to the client }
flistener.requestFailed(NO_SUCH_PLACE);
} public void sceneFailedToResolve (
}; int rsceneId, Exception reason) {
Log.warning("Unable to resolve target scene [sceneId=" + rsceneId +
", reason=" + reason + "].");
// pretend like the scene doesn't exist to the client
flistener.requestFailed(NO_SUCH_PLACE);
}
};
_screg.resolveScene(fdest.targetSceneId, rl); _screg.resolveScene(fdest.targetSceneId, rl);
} }
/** /**
* This is called after the scene to which we are moving is guaranteed * This is called after the scene to which we are moving is guaranteed to have been loaded into
* to have been loaded into the server. * the server.
*/ */
protected void finishTraversePortalRequest ( protected void finishTraversePortalRequest (
BodyObject source, SpotSceneManager destmgr, int sceneVer, BodyObject source, SpotSceneManager destmgr, int sceneVer, Portal dest,
Portal dest, SceneMoveListener listener) SceneMoveListener listener)
{ {
// let the destination scene manager know that we're coming in // let the destination scene manager know that we're coming in
destmgr.mapEnteringBody(source, dest.targetPortalId); destmgr.mapEnteringBody(source, dest);
try { try {
// move to the place object associated with this scene // move to the place object associated with this scene
@@ -189,19 +179,16 @@ public class SpotProvider
BodyObject source = (BodyObject)caller; BodyObject source = (BodyObject)caller;
int cSceneId = getCallerSceneId(caller); int cSceneId = getCallerSceneId(caller);
if (cSceneId != sceneId) { if (cSceneId != sceneId) {
Log.info("Rejecting changeLocation for invalid scene " + Log.info("Rejecting changeLocation for invalid scene [user=" + source.who() +
"[user=" + source.who() + ", insid=" + cSceneId + ", insid=" + cSceneId + ", wantsid=" + sceneId + ", loc=" + loc + "].");
", wantsid=" + sceneId + ", loc=" + loc + "].");
throw new InvocationException(INVALID_LOCATION); throw new InvocationException(INVALID_LOCATION);
} }
// look up the scene manager for the specified scene // look up the scene manager for the specified scene
SpotSceneManager smgr = (SpotSceneManager) SpotSceneManager smgr = (SpotSceneManager)_screg.getSceneManager(sceneId);
_screg.getSceneManager(sceneId);
if (smgr == null) { if (smgr == null) {
Log.warning("User requested to change location from " + Log.warning("User requested to change location from non-existent scene " +
"non-existent scene [user=" + source.who() + "[user=" + source.who() + ", sceneId=" + sceneId + ", loc=" + loc +"].");
", sceneId=" + sceneId + ", loc=" + loc +"].");
throw new InvocationException(INTERNAL_ERROR); throw new InvocationException(INTERNAL_ERROR);
} }
@@ -223,12 +210,11 @@ public class SpotProvider
BodyObject source = (BodyObject)caller; BodyObject source = (BodyObject)caller;
// look up the scene manager for the specified scene // look up the scene manager for the specified scene
SpotSceneManager smgr = (SpotSceneManager) SpotSceneManager smgr = (SpotSceneManager)_screg.getSceneManager(sceneId);
_screg.getSceneManager(sceneId);
if (smgr == null) { if (smgr == null) {
Log.warning("User requested to join cluster from " + Log.warning("User requested to join cluster from non-existent scene " +
"non-existent scene [user=" + source.who() + "[user=" + source.who() + ", sceneId=" + sceneId +
", sceneId=" + sceneId + ", foid=" + friendOid +"]."); ", foid=" + friendOid +"].");
throw new InvocationException(INTERNAL_ERROR); throw new InvocationException(INTERNAL_ERROR);
} }
@@ -249,57 +235,50 @@ public class SpotProvider
BodyObject source = (BodyObject)caller; BodyObject source = (BodyObject)caller;
String errmsg = source.checkAccess(ChatCodes.CHAT_ACCESS, null); String errmsg = source.checkAccess(ChatCodes.CHAT_ACCESS, null);
if (errmsg != null) { if (errmsg != null) {
SpeakProvider.sendFeedback(source, SpeakProvider.sendFeedback(source, MessageManager.GLOBAL_BUNDLE, errmsg);
MessageManager.GLOBAL_BUNDLE, errmsg); } else {
return; sendClusterChatMessage(getCallerSceneId(caller), source.getOid(),
source.getVisibleName(), null, message, mode);
} }
sendClusterChatMessage(getCallerSceneId(caller), source.getOid(),
source.getVisibleName(), null, message, mode);
} }
/** /**
* Sends a cluster chat notification to the specified location in the * Sends a cluster chat notification to the specified location in the specified place object
* specified place object originating with the specified speaker (the * originating with the specified speaker (the speaker can be a server entity that wishes to
* speaker can be a server entity that wishes to fake a "speak" * fake a "speak" message, in which case the bundle argument should be non-null and should
* message, in which case the bundle argument should be non-null and * contain the id of the bundle to be used to translate the message text) and with the supplied
* should contain the id of the bundle to be used to translate the * message content.
* message text) and with the supplied message content.
* *
* @param sceneId the scene id in which to deliver the chat message. * @param sceneId the scene id in which to deliver the chat message.
* @param speakerOid the body object id of the speaker (used to verify * @param speakerOid the body object id of the speaker (used to verify that they are in the
* that they are in the cluster in question). * cluster in question).
* @param speaker the username of the user that generated the message * @param speaker the username of the user that generated the message (or some special speaker
* (or some special speaker name for server messages). * name for server messages).
* @param bundle the bundle identifier that will be used by the client * @param bundle the bundle identifier that will be used by the client to translate the message
* to translate the message text (or null if the message originated * text (or null if the message originated from a real live human who wrote it in their native
* from a real live human who wrote it in their native tongue). * tongue).
* @param message the text of the chat message. * @param message the text of the chat message.
*/ */
public void sendClusterChatMessage ( public void sendClusterChatMessage (int sceneId, int speakerOid, Name speaker,
int sceneId, int speakerOid, Name speaker, String bundle, String message, byte mode)
String bundle, String message, byte mode)
{ {
// look up the scene manager for the specified scene // look up the scene manager for the specified scene
SpotSceneManager smgr = (SpotSceneManager) SpotSceneManager smgr = (SpotSceneManager)_screg.getSceneManager(sceneId);
_screg.getSceneManager(sceneId);
if (smgr == null) { if (smgr == null) {
Log.warning("User requested cluster chat in non-existent scene " + Log.warning("User requested cluster chat in non-existent scene [user=" + speaker +
"[user=" + speaker + ", sceneId=" + sceneId + ", sceneId=" + sceneId + ", message=" + message + "].");
", message=" + message + "].");
return; return;
} }
// pass this request on to the spot scene manager // pass this request on to the spot scene manager
smgr.handleClusterSpeakRequest( smgr.handleClusterSpeakRequest(speakerOid, speaker, bundle, message, mode);
speakerOid, speaker, bundle, message, mode);
} }
/** /**
* Obtains the scene id occupied by the supplied caller. * Obtains the scene id occupied by the supplied caller.
* *
* @exception InvocationException thrown if the caller does not * @exception InvocationException thrown if the caller does not implement {@link
* implement {@link ScenedBodyObject}. * ScenedBodyObject}.
*/ */
protected static int getCallerSceneId (ClientObject caller) protected static int getCallerSceneId (ClientObject caller)
throws InvocationException throws InvocationException
@@ -307,8 +286,7 @@ public class SpotProvider
if (caller instanceof ScenedBodyObject) { if (caller instanceof ScenedBodyObject) {
return ((ScenedBodyObject)caller).getSceneId(); return ((ScenedBodyObject)caller).getSceneId();
} else { } else {
Log.warning("Can't get scene from non-scened caller " + Log.warning("Can't get scene from non-scened caller " + caller.who() + ".");
caller.who() + ".");
throw new InvocationException(INTERNAL_ERROR); throw new InvocationException(INTERNAL_ERROR);
} }
} }
@@ -21,10 +21,10 @@
package com.threerings.whirled.spot.server; package com.threerings.whirled.spot.server;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import com.samskivert.util.HashIntMap; import com.samskivert.util.HashIntMap;
import com.samskivert.util.IntIntMap;
import com.threerings.util.Name; import com.threerings.util.Name;
import com.threerings.presents.dobj.DObject; import com.threerings.presents.dobj.DObject;
@@ -50,9 +50,8 @@ import com.threerings.whirled.spot.data.SpotScene;
import com.threerings.whirled.spot.data.SpotSceneObject; import com.threerings.whirled.spot.data.SpotSceneObject;
/** /**
* Handles the movement of bodies between locations in the scene and * Handles the movement of bodies between locations in the scene and creates the necessary
* creates the necessary distributed objects to allow bodies in clusters * distributed objects to allow bodies in clusters to chat with one another.
* to chat with one another.
*/ */
public class SpotSceneManager extends SceneManager public class SpotSceneManager extends SceneManager
implements SpotCodes implements SpotCodes
@@ -62,8 +61,7 @@ public class SpotSceneManager extends SceneManager
*/ */
public static void moveBodyToDefaultPortal (BodyObject body) public static void moveBodyToDefaultPortal (BodyObject body)
{ {
SpotSceneManager mgr = (SpotSceneManager) SpotSceneManager mgr = (SpotSceneManager)CrowdServer.plreg.getPlaceManager(body.location);
CrowdServer.plreg.getPlaceManager(body.location);
if (mgr != null) { if (mgr != null) {
SpotScene scene = (SpotScene)mgr.getScene(); SpotScene scene = (SpotScene)mgr.getScene();
if (scene == null) { if (scene == null) {
@@ -84,24 +82,20 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* Assigns a starting location for an entering body. This will happen * Assigns a starting location for an entering body. This will happen before the body is made
* before the body is made to "occupy" the scene (defined by their * to "occupy" the scene (defined by their having an occupant info record). So when they do
* having an occupant info record). So when they do finally occupy the * finally occupy the scene, the client will know where to render them.
* scene, the client will know where to render them.
*/ */
public void mapEnteringBody (BodyObject body, int portalId) public void mapEnteringBody (BodyObject body, Portal from)
{ {
// small optimization: don't save portalId -1, because it simply means // don't save a null from portal, because it simply means "use the default entrance"
// "use the default entrance" and retrieving an unset value will if (from != null) {
// return -1 anyway _enterers.put(body.getOid(), from);
if (portalId != -1) {
_enterers.put(body.getOid(), portalId);
} }
} }
/** /**
* Called if a body failed to enter our scene after we assigned them * Called if a body failed to enter our scene after we assigned them an entering position.
* an entering position.
*/ */
public void clearEnteringBody (BodyObject body) public void clearEnteringBody (BodyObject body)
{ {
@@ -109,10 +103,9 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* This is called when a user requests to traverse a portal from this * This is called when a user requests to traverse a portal from this scene to another scene.
* scene to another scene. The manager may return an error code string * The manager may return an error code string that will be reported back to the caller
* that will be reported back to the caller explaining the failure or * explaining the failure or <code>null</code> indicating that it is OK for the caller to
* <code>null</code> indicating that it is OK for the caller to
* traverse the portal. * traverse the portal.
*/ */
public String mayTraversePortal (BodyObject body, Portal portal) public String mayTraversePortal (BodyObject body, Portal portal)
@@ -121,11 +114,10 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* This is called to let this scene manager know that the user is * This is called to let this scene manager know that the user is about to traverse the
* about to traverse the specified portal. The default implementation * specified portal. The default implementation relocates the user to the location associated
* relocates the user to the location associated with the portal. It * with the portal. It is still possible that the traversal will fail, so don't do anything too
* is still possible that the traversal will fail, so don't do * crazy.
* anything too crazy.
*/ */
public void willTraversePortal (BodyObject body, Portal portal) public void willTraversePortal (BodyObject body, Portal portal)
{ {
@@ -135,10 +127,9 @@ public class SpotSceneManager extends SceneManager
// documentation inherited // documentation inherited
protected void didStartup () protected void didStartup ()
{ {
// get a casted reference to our place object (we need to do this // get a casted reference to our place object (we need to do this before calling
// before calling super.didStartup() because that will call // super.didStartup() because that will call sceneManagerDidResolve() which may start
// sceneManagerDidResolve() which may start letting people into // letting people into the scene)
// the scene)
_ssobj = (SpotSceneObject)_plobj; _ssobj = (SpotSceneObject)_plobj;
super.didStartup(); super.didStartup();
@@ -165,17 +156,16 @@ public class SpotSceneManager extends SceneManager
removeFromCluster(bodyOid); removeFromCluster(bodyOid);
// let's make damned sure they're not in any cluster // let's make damned sure they're not in any cluster
Iterator cliter = _clusters.values().iterator(); Iterator<ClusterRecord> cliter = _clusters.values().iterator();
while (cliter.hasNext()) { while (cliter.hasNext()) {
ClusterRecord clrec = (ClusterRecord)cliter.next(); ClusterRecord clrec = cliter.next();
if (clrec.containsKey(bodyOid)) { if (clrec.containsKey(bodyOid)) {
Log.info("Pruning departed body from cluster [boid=" + bodyOid + Log.info("Pruning departed body from cluster [boid=" + bodyOid +
", cluster=" + clrec + "]."); ", cluster=" + clrec + "].");
clrec.removeBody(bodyOid); clrec.removeBody(bodyOid);
if (clrec.size() == 0) { if (clrec.size() == 0) {
// If we just removed the last body, destroy the cluster, // if we just removed the last body, destroy the cluster, need to use the
// need to use the iterator's removal so we don't // iterator's removal so we don't hose ourselves
// hose ourselves.
clrec.destroy(false); clrec.destroy(false);
cliter.remove(); cliter.remove();
} }
@@ -188,25 +178,23 @@ public class SpotSceneManager extends SceneManager
{ {
super.insertOccupantInfo(info, body); super.insertOccupantInfo(info, body);
// we don't actually populate their occupant info, but instead assign // we don't actually populate their occupant info, but instead assign them their starting
// them their starting location in the scene // location in the scene
assignStartingLocation(body); assignStartingLocation(body);
} }
/** /**
* Give our new body a starting location. * Give our new body a starting location.
* @param body The new body entering the scene.
*/ */
protected void assignStartingLocation (BodyObject body) protected void assignStartingLocation (BodyObject body)
{ {
int portalId = _enterers.remove(body.getOid()); Portal from = _enterers.remove(body.getOid());
Portal entry; Portal entry;
if (portalId != -1) { if (from != null && from.portalId != -1) {
entry = _sscene.getPortal(portalId); entry = _sscene.getPortal(from.portalId);
if (entry == null) { if (entry == null) {
Log.warning("Body mapped at invalid portal [where=" + where() + Log.warning("Body mapped at invalid portal [where=" + where() +
", who=" + body.who() + ", who=" + body.who() + ", from=" + from + "].");
", portalId=" + portalId + "].");
entry = _sscene.getDefaultEntrance(); entry = _sscene.getDefaultEntrance();
} }
} else { } else {
@@ -216,40 +204,37 @@ public class SpotSceneManager extends SceneManager
// Log.debug("Positioning entering body [who=" + body.who() + // Log.debug("Positioning entering body [who=" + body.who() +
// ", where=" + entry.getOppLocation() + "]."); // ", where=" + entry.getOppLocation() + "].");
// create a scene location for them located on the entrance portal // create a scene location for them located on the entrance portal but facing the opposite
// but facing the opposite direction // direction
_ssobj.addToOccupantLocs(computeEnteringLocation(body, entry)); _ssobj.addToOccupantLocs(computeEnteringLocation(body, entry));
} }
/** /**
* Called when the supplied body is entering our scene via the * Called when the supplied body is entering our scene via the specified portal. The default
* specified portal. The default location is the one associated with * location is the one associated with the portal, but derived classes may wish to adjust this.
* the portal, but derived classes may wish to adjust this.
*/ */
protected SceneLocation computeEnteringLocation ( protected SceneLocation computeEnteringLocation (BodyObject body, Portal entry)
BodyObject body, Portal entry)
{ {
return new SceneLocation(entry.getOppLocation(), body.getOid()); return new SceneLocation(entry.getOppLocation(), body.getOid());
} }
/** /**
* Called by the {@link SpotProvider} when we receive a request by a * Called by the {@link SpotProvider} when we receive a request by a user to occupy a
* user to occupy a particular location. * particular location.
* *
* @param source the body to be moved. * @param source the body to be moved.
* @param loc the location to which to move the body. * @param loc the location to which to move the body.
* *
* @exception InvocationException thrown with a reason code explaining * @exception InvocationException thrown with a reason code explaining the failure if there is
* the failure if there is a problem processing the request. * a problem processing the request.
*/ */
protected void handleChangeLoc (BodyObject source, Location loc) protected void handleChangeLoc (BodyObject source, Location loc)
throws InvocationException throws InvocationException
{ {
// make sure they are in our scene // make sure they are in our scene
if (!_ssobj.occupants.contains(source.getOid())) { if (!_ssobj.occupants.contains(source.getOid())) {
Log.warning("Refusing change loc from non-scene occupant " + Log.warning("Refusing change loc from non-scene occupant [where=" + where() +
"[where=" + where() + ", who=" + source.who() + ", who=" + source.who() + ", loc=" + loc + "].");
", loc=" + loc + "].");
throw new InvocationException(INTERNAL_ERROR); throw new InvocationException(INTERNAL_ERROR);
} }
@@ -258,9 +243,8 @@ public class SpotSceneManager extends SceneManager
throw new InvocationException(INVALID_LOCATION); throw new InvocationException(INVALID_LOCATION);
} }
// update the user's location information in the scene which will // update the user's location information in the scene which will indicate to the client
// indicate to the client that their avatar should be moved from // that their avatar should be moved from its current position to their new position
// its current position to their new position
updateLocation(source, loc); updateLocation(source, loc);
// remove them from any cluster as they've departed // remove them from any cluster as they've departed
@@ -268,10 +252,9 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* Derived classes can override this method and validate that the * Derived classes can override this method and validate that the specified body can stand in
* specified body can stand in the requested location. The default * the requested location. The default implementation returns <code>true</code> in all
* implementation returns <code>true</code> in all circumstances; * circumstances; stand where ye may!
* stand where ye may!
*/ */
protected boolean validateLocation (BodyObject source, Location loc) protected boolean validateLocation (BodyObject source, Location loc)
{ {
@@ -286,9 +269,8 @@ public class SpotSceneManager extends SceneManager
SceneLocation sloc = new SceneLocation(loc, source.getOid()); SceneLocation sloc = new SceneLocation(loc, source.getOid());
if (!_ssobj.occupantLocs.contains(sloc)) { if (!_ssobj.occupantLocs.contains(sloc)) {
// complain if they don't already have a location configured // complain if they don't already have a location configured
Log.warning("Changing loc for occupant without previous loc " + Log.warning("Changing loc for occupant without previous loc [where=" + where() +
"[where=" + where() + ", who=" + source.who() + ", who=" + source.who() + ", nloc=" + loc + "].");
", nloc=" + loc + "].");
Thread.dumpStack(); Thread.dumpStack();
_ssobj.addToOccupantLocs(sloc); _ssobj.addToOccupantLocs(sloc);
} else { } else {
@@ -297,22 +279,21 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* Called by the {@link SpotProvider} when we receive a request by a * Called by the {@link SpotProvider} when we receive a request by a user to join a particular
* user to join a particular cluster. * cluster.
* *
* @param joiner the body to be moved. * @param joiner the body to be moved.
* @param targetOid the bodyOid of another user or the oid of an * @param targetOid the bodyOid of another user or the oid of an existing cluster; the moving
* existing cluster; the moving user will be made to join the other * user will be made to join the other user's cluster.
* user's cluster.
* *
* @exception InvocationException thrown with a reason code explaining * @exception InvocationException thrown with a reason code explaining the failure if there is
* the failure if there is a problem processing the request. * a problem processing the request.
*/ */
protected void handleJoinCluster (BodyObject joiner, int targetOid) protected void handleJoinCluster (BodyObject joiner, int targetOid)
throws InvocationException throws InvocationException
{ {
// if the cluster already exists, add this user and be done // if the cluster already exists, add this user and be done
ClusterRecord clrec = (ClusterRecord)_clusters.get(targetOid); ClusterRecord clrec = _clusters.get(targetOid);
if (clrec != null) { if (clrec != null) {
clrec.addBody(joiner); clrec.addBody(joiner);
return; return;
@@ -321,8 +302,7 @@ public class SpotSceneManager extends SceneManager
// otherwise see if they sent us the user's oid // otherwise see if they sent us the user's oid
DObject tobj = CrowdServer.omgr.getObject(targetOid); DObject tobj = CrowdServer.omgr.getObject(targetOid);
if (!(tobj instanceof BodyObject)) { if (!(tobj instanceof BodyObject)) {
Log.info("Can't join cluster, missing target " + Log.info("Can't join cluster, missing target [creator=" + joiner.who() +
"[creator=" + joiner.who() +
", targetOid=" + targetOid + "]."); ", targetOid=" + targetOid + "].");
throw new InvocationException(NO_SUCH_CLUSTER); throw new InvocationException(NO_SUCH_CLUSTER);
} }
@@ -330,9 +310,8 @@ public class SpotSceneManager extends SceneManager
// make sure we're in the same scene as said user // make sure we're in the same scene as said user
BodyObject friend = (BodyObject)tobj; BodyObject friend = (BodyObject)tobj;
if (friend.location != joiner.location) { if (friend.location != joiner.location) {
Log.info("Refusing cluster join from non-proximate user " + Log.info("Refusing cluster join from non-proximate user [joiner=" + joiner.who() +
"[joiner=" + joiner.who() + ", jloc=" + joiner.location + ", jloc=" + joiner.location + ", target=" + friend.who() +
", target=" + friend.who() +
", tloc=" + friend.location + "]."); ", tloc=" + friend.location + "].");
throw new InvocationException(NO_SUCH_CLUSTER); throw new InvocationException(NO_SUCH_CLUSTER);
} }
@@ -344,21 +323,18 @@ public class SpotSceneManager extends SceneManager
return; return;
} }
// confirm that they can start a cluster with this unsuspecting // confirm that they can start a cluster with this unsuspecting other person
// other person
checkCanCluster(joiner, friend); checkCanCluster(joiner, friend);
// otherwise we create a new cluster and add our charter members! // otherwise we create a new cluster and add our charter members!
// Log.debug("Creating cluster [starter=" + joiner.who() + // Log.debug("Creating cluster [starter=" + joiner.who() + ", tgt=" + friend.who() + "].");
// ", target=" + friend.who() + "].");
clrec = createClusterRecord(); clrec = createClusterRecord();
clrec.addBody(friend); clrec.addBody(friend);
clrec.addBody(joiner); clrec.addBody(joiner);
} }
/** /**
* Creates the cluster record instance that we'll use to manage our * Creates the cluster record instance that we'll use to manage our cluster.
* cluster.
*/ */
protected ClusterRecord createClusterRecord () protected ClusterRecord createClusterRecord ()
{ {
@@ -366,10 +342,9 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* Gives derived classes an opportunity to veto a user's attempt to * Gives derived classes an opportunity to veto a user's attempt to start a cluster with
* start a cluster with another user. If the attempt should be vetoed, * another user. If the attempt should be vetoed, this method should throw an {@link
* this method should throw an {@link InvocationException} indicating * InvocationException} indicating the reason for veto.
* the reason for veto.
*/ */
protected void checkCanCluster (BodyObject initiator, BodyObject target) protected void checkCanCluster (BodyObject initiator, BodyObject target)
throws InvocationException throws InvocationException
@@ -397,33 +372,27 @@ public class SpotSceneManager extends SceneManager
*/ */
protected ClusterRecord getCluster (int bodyOid) protected ClusterRecord getCluster (int bodyOid)
{ {
ClusteredBodyObject bobj = (ClusteredBodyObject) ClusteredBodyObject bobj = (ClusteredBodyObject)CrowdServer.omgr.getObject(bodyOid);
CrowdServer.omgr.getObject(bodyOid); return (bobj == null) ? null : _clusters.get(bobj.getClusterOid());
return (bobj == null) ? null :
(ClusterRecord)_clusters.get(bobj.getClusterOid());
} }
/** /**
* Called by the {@link SpotProvider} when we receive a cluster speak * Called by the {@link SpotProvider} when we receive a cluster speak request.
* request.
*/ */
protected void handleClusterSpeakRequest ( protected void handleClusterSpeakRequest (int sourceOid, Name source, String bundle,
int sourceOid, Name source, String bundle, String message, byte mode) String message, byte mode)
{ {
ClusterRecord clrec = getCluster(sourceOid); ClusterRecord clrec = getCluster(sourceOid);
if (clrec == null) { if (clrec == null) {
Log.warning("Non-clustered user requested cluster speak " + Log.warning("Non-clustered user requested cluster speak [where=" + where() +
"[where=" + where() + ", chatter=" + source + ", chatter=" + source + " (" + sourceOid + "), msg=" + message + "].");
" (" + sourceOid + "), msg=" + message + "].");
} else { } else {
SpeakProvider.sendSpeak(clrec.getClusterObject(), SpeakProvider.sendSpeak(clrec.getClusterObject(), source, bundle, message, mode);
source, bundle, message, mode);
} }
} }
/** /**
* Returns the location of the specified body or null if they have no * Returns the location of the specified body or null if they have no location in this scene.
* location in this scene.
*/ */
protected SceneLocation locationForBody (int bodyOid) protected SceneLocation locationForBody (int bodyOid)
{ {
@@ -431,8 +400,7 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* Verifies that the specified cluster can be expanded to include * Verifies that the specified cluster can be expanded to include another body.
* another body.
*/ */
protected boolean canAddBody (ClusterRecord clrec, BodyObject body) protected boolean canAddBody (ClusterRecord clrec, BodyObject body)
{ {
@@ -440,26 +408,23 @@ public class SpotSceneManager extends SceneManager
} }
/** /**
* Called when a user is added to a cluster. The scene manager * Called when a user is added to a cluster. The scene manager implementation should take this
* implementation should take this opportunity to rearrange everyone * opportunity to rearrange everyone in the cluster appropriately for the new size.
* in the cluster appropriately for the new size.
*/ */
protected void bodyAdded (ClusterRecord clrec, BodyObject body) protected void bodyAdded (ClusterRecord clrec, BodyObject body)
{ {
} }
/** /**
* Called when a user is removed from a cluster. The scene manager * Called when a user is removed from a cluster. The scene manager implementation should take
* implementation should take this opportunity to rearrange everyone * this opportunity to rearrange everyone in the cluster appropriately for the new size.
* in the cluster appropriately for the new size.
*/ */
protected void bodyRemoved (ClusterRecord clrec, BodyObject body) protected void bodyRemoved (ClusterRecord clrec, BodyObject body)
{ {
} }
/** /**
* Used to manage clusters which are groups of users that can chat to * Used to manage clusters which are groups of users that can chat to one another.
* one another.
*/ */
protected class ClusterRecord extends HashIntMap protected class ClusterRecord extends HashIntMap
{ {
@@ -485,9 +450,8 @@ public class SpotSceneManager extends SceneManager
throws InvocationException throws InvocationException
{ {
if (!(body instanceof ClusteredBodyObject)) { if (!(body instanceof ClusteredBodyObject)) {
Log.warning("Refusing to add non-clustered body to cluster " + Log.warning("Refusing to add non-clustered body to cluster [cloid=" +
"[cloid=" + _clobj.getOid() + _clobj.getOid() + ", size=" + size() + ", who=" + body.who() + "].");
", size=" + size() + ", who=" + body.who() + "].");
throw new InvocationException(INTERNAL_ERROR); throw new InvocationException(INTERNAL_ERROR);
} }
@@ -511,10 +475,8 @@ public class SpotSceneManager extends SceneManager
body.startTransaction(); body.startTransaction();
try { try {
bodyAdded(this, body); // do the hokey pokey bodyAdded(this, body); // do the hokey pokey
if (_clobj != null) { if (_clobj != null) {
((ClusteredBodyObject)body).setClusterOid( ((ClusteredBodyObject)body).setClusterOid(_clobj.getOid());
_clobj.getOid());
_clobj.addToOccupants(body.getOid()); _clobj.addToOccupants(body.getOid());
_ssobj.updateClusters(_cluster); _ssobj.updateClusters(_cluster);
} }
@@ -534,9 +496,8 @@ public class SpotSceneManager extends SceneManager
{ {
BodyObject body = (BodyObject)remove(bodyOid); BodyObject body = (BodyObject)remove(bodyOid);
if (body == null) { if (body == null) {
Log.warning("Requested to remove unknown body from cluster " + Log.warning("Requested to remove unknown body from cluster [cloid=" +
"[cloid=" + _clobj.getOid() + _clobj.getOid() + ", size=" + size() + ", who=" + bodyOid + "].");
", size=" + size() + ", who=" + bodyOid + "].");
return; return;
} }
@@ -548,7 +509,6 @@ public class SpotSceneManager extends SceneManager
try { try {
((ClusteredBodyObject)body).setClusterOid(-1); ((ClusteredBodyObject)body).setClusterOid(-1);
bodyRemoved(this, body); // do the hokey pokey bodyRemoved(this, body); // do the hokey pokey
if (_clobj != null) { if (_clobj != null) {
_clobj.removeFromOccupants(bodyOid); _clobj.removeFromOccupants(bodyOid);
_ssobj.updateClusters(_cluster); _ssobj.updateClusters(_cluster);
@@ -557,6 +517,7 @@ public class SpotSceneManager extends SceneManager
} finally { } finally {
_ssobj.commitTransaction(); _ssobj.commitTransaction();
} }
} finally { } finally {
if (body.isActive()) { if (body.isActive()) {
body.commitTransaction(); body.commitTransaction();
@@ -584,12 +545,10 @@ public class SpotSceneManager extends SceneManager
protected void destroy (boolean doRemoval) protected void destroy (boolean doRemoval)
{ {
// Log.debug("Cluster empty, going away " + // Log.debug("Cluster empty, going away [cloid=" + _clobj.getOid() + "].");
// "[cloid=" + _clobj.getOid() + "].");
_ssobj.removeFromClusters(_cluster.getKey()); _ssobj.removeFromClusters(_cluster.getKey());
// If we've also been requested to remove ourself from the clusters // if we've also been requested to remove ourself from the clusters list, do that
// list, do that.
if (doRemoval) { if (doRemoval) {
_clusters.remove(_clobj.getOid()); _clusters.remove(_clobj.getOid());
} }
@@ -607,8 +566,8 @@ public class SpotSceneManager extends SceneManager
protected SpotScene _sscene; protected SpotScene _sscene;
/** Records with information on all clusters in this scene. */ /** Records with information on all clusters in this scene. */
protected HashIntMap _clusters = new HashIntMap(); protected HashIntMap<ClusterRecord> _clusters = new HashIntMap<ClusterRecord>();
/** A mapping of entering bodies to portal ids. */ /** A mapping of entering bodies to portal ids. */
protected IntIntMap _enterers = new IntIntMap(); protected HashMap<Integer,Portal> _enterers = new HashMap<Integer,Portal>();
} }