Handle the BodyService with a BodyManager instead of a concrete BodyProvider.

More dependency injection (or removal in the case of SpeakUtil).


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5163 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2008-06-08 13:06:03 +00:00
parent 8c37ca7bfa
commit a7c7d853fe
7 changed files with 167 additions and 126 deletions
@@ -0,0 +1,123 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.crowd.server;
import com.google.inject.Inject;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationManager;
import com.threerings.presents.server.InvocationProvider;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.CrowdCodes;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.Place;
import static com.threerings.crowd.Log.log;
/**
* Handles body related services.
*/
public class BodyManager
implements BodyProvider
{
/** Used by {@link #updateOccupantInfo}. */
public static interface OccupantInfoOp
{
/**
* Updates the supplied occupant info record.
*
* @return true if changes were made and thus the object should be published anew to the
* place object, false if no publish should be done.
*/
public boolean update (OccupantInfo oinfo);
}
/**
* Constructs and initializes the body manager.
*/
@Inject public BodyManager (InvocationManager invmgr)
{
invmgr.registerDispatcher(new BodyDispatcher(this), CrowdCodes.CROWD_GROUP);
}
/**
* Locates the specified body's occupant info in the specified location, applies the supplied
* occuapnt info operation to it and then broadcasts the updated info (assuming the occop
* returned true indicating that an update was made).
*/
public void updateOccupantInfo (BodyObject body, Place location, OccupantInfoOp occop)
{
PlaceManager pmgr = (location == null) ? null :
CrowdServer.plreg.getPlaceManager(location.placeOid);
if (pmgr == null) {
return;
}
OccupantInfo info = pmgr.getOccupantInfo(body.getOid());
if (info != null && occop.update(info)) {
pmgr.updateOccupantInfo(info);
}
}
/**
* Updates the connection status for the given body object's occupant info in the specified
* location.
*/
public void updateOccupantStatus (BodyObject body, Place location, final byte status)
{
// no need to NOOP
if (body.status != status) {
// update the status in their body object
body.setStatus(status);
body.statusTime = System.currentTimeMillis();
}
updateOccupantInfo(body, location, new OccupantInfoOp() {
public boolean update (OccupantInfo info) {
if (info.status != status) {
info.status = status;
return true;
} else {
return false;
}
}
});
}
// from interface BodyProvider
public void setIdle (ClientObject caller, boolean idle)
{
BodyObject bobj = (BodyObject)caller;
// determine the body's proposed new status
byte nstatus = (idle) ? OccupantInfo.IDLE : OccupantInfo.ACTIVE;
if (bobj.status == nstatus) {
return; // ignore NOOP attempts
}
// update their status!
log.debug("Setting user idle state [user=" + bobj.username + ", status=" + nstatus + "].");
updateOccupantStatus(bobj, bobj.location, nstatus);
}
}
@@ -2,7 +2,7 @@
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
@@ -22,106 +22,15 @@
package com.threerings.crowd.server;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationManager;
import com.threerings.presents.server.InvocationProvider;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.CrowdCodes;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.Place;
import static com.threerings.crowd.Log.log;
/**
* Provides the server-side side of the body-related invocation services.
* Defines the server-side of the {@link BodyService}.
*/
public class BodyProvider
implements InvocationProvider
public interface BodyProvider extends InvocationProvider
{
/** Used by {@link #updateOccupantInfo}. */
public static interface OccupantInfoOp
{
/**
* Updates the supplied occupant info record, returning true if
* changes were made and thus the object should be published anew
* to the place object, false if no publish should be done.
*/
public boolean update (OccupantInfo oinfo);
}
/**
* Constructs and initializes a body provider instance which will be
* used to handle all body-related invocation service requests.
* Handles a {@link BodyService#setIdle} request.
*/
public static void init (InvocationManager invmgr)
{
// register a provider instance
invmgr.registerDispatcher(new BodyDispatcher(new BodyProvider()), CrowdCodes.CROWD_GROUP);
}
/**
* Handles a request to set the idle state of a client to the specified value.
*/
public void setIdle (ClientObject caller, boolean idle)
throws InvocationException
{
BodyObject bobj = (BodyObject)caller;
// determine the body's proposed new status
byte nstatus = (idle) ? OccupantInfo.IDLE : OccupantInfo.ACTIVE;
// report NOOP attempts
if (bobj.status == nstatus) {
throw new InvocationException((idle) ? "m.already_idle" : "m.already_active");
}
// update their status!
log.debug("Setting user idle state [user=" + bobj.username + ", status=" + nstatus + "].");
updateOccupantStatus(bobj, bobj.location, nstatus);
}
/**
* Locates the specified body's occupant info in the specified location, applies the supplied
* occuapnt info operation to it and then broadcasts the updated info (assuming the occop
* returned true indicating that an update was made).
*/
public static void updateOccupantInfo (BodyObject body, Place location, OccupantInfoOp occop)
{
PlaceManager pmgr = (location == null) ? null :
CrowdServer.plreg.getPlaceManager(location.placeOid);
if (pmgr == null) {
return;
}
OccupantInfo info = pmgr.getOccupantInfo(body.getOid());
if (info != null && occop.update(info)) {
pmgr.updateOccupantInfo(info);
}
}
/**
* Updates the connection status for the given body object's occupant info in the specified
* location.
*/
public static void updateOccupantStatus (BodyObject body, Place location, final byte status)
{
// no need to NOOP
if (body.status != status) {
// update the status in their body object
body.setStatus(status);
body.statusTime = System.currentTimeMillis();
}
updateOccupantInfo(body, location, new OccupantInfoOp() {
public boolean update (OccupantInfo info) {
if (info.status != status) {
info.status = status;
return true;
} else {
return false;
}
}
});
}
public void setIdle (ClientObject caller, boolean arg1);
}
@@ -42,7 +42,7 @@ public class CrowdClient extends PresentsClient
if (_clobj != null) {
// note that the user is disconnected
BodyObject bobj = (BodyObject)_clobj;
BodyProvider.updateOccupantStatus(bobj, bobj.location, OccupantInfo.DISCONNECTED);
CrowdServer.bodyman.updateOccupantStatus(bobj, bobj.location, OccupantInfo.DISCONNECTED);
}
}
@@ -53,7 +53,7 @@ public class CrowdClient extends PresentsClient
// note that the user's active once more
BodyObject bobj = (BodyObject)_clobj;
BodyProvider.updateOccupantStatus(bobj, bobj.location, OccupantInfo.ACTIVE);
CrowdServer.bodyman.updateOccupantStatus(bobj, bobj.location, OccupantInfo.ACTIVE);
}
// documentation inherited
@@ -68,7 +68,7 @@ public class CrowdClient extends PresentsClient
// reset our status in case this object remains around until they start their next session
// (which could happen very soon)
BodyProvider.updateOccupantStatus(body, null, OccupantInfo.ACTIVE);
CrowdServer.bodyman.updateOccupantStatus(body, null, OccupantInfo.ACTIVE);
// clear our chat history
if (body != null) {
@@ -64,6 +64,9 @@ public class CrowdServer extends PresentsServer
/** Our chat provider. */
public static ChatProvider chatprov;
/** Our body manager. */
public static BodyManager bodyman;
/**
* Initializes all of the server services and prepares for operation.
*/
@@ -75,6 +78,7 @@ public class CrowdServer extends PresentsServer
// LEGACY: set up our legacy static references
plreg = _plreg;
chatprov = _chatprov;
bodyman = _bodyman;
// configure the client manager to use our bits
clmgr.setClientFactory(new ClientFactory() {
@@ -88,9 +92,6 @@ public class CrowdServer extends PresentsServer
// create our body locator
_lookup = createBodyLocator();
// initialize the body services
BodyProvider.init(invmgr);
}
/**
@@ -151,6 +152,9 @@ public class CrowdServer extends PresentsServer
/** Handles the creation and tracking of place managers. */
@Inject protected PlaceRegistry _plreg;
/** Handles body-related invocation services. */
@Inject protected BodyManager _bodyman;
/** Provides chat-related invocation services. */
@Inject protected ChatProvider _chatprov;