Extract BodyObject location into a separate service that can be customized by

extending systems and depended upon by services that need to lookup bodies.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5212 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2008-06-29 14:11:42 +00:00
parent f043a81ee4
commit 6a9723fb11
5 changed files with 75 additions and 54 deletions
@@ -34,7 +34,6 @@ import com.threerings.util.TimeUtil;
import com.threerings.presents.client.InvocationService.InvocationListener;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.DObjectManager;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationManager;
@@ -44,7 +43,9 @@ import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.CrowdCodes;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.server.BodyLocator;
import com.threerings.crowd.server.CrowdServer;
import com.threerings.crowd.server.PlaceRegistry;
import com.threerings.crowd.chat.client.ChatService.TellListener;
import com.threerings.crowd.chat.client.ChatService;
@@ -140,7 +141,7 @@ public class ChatProvider
// inform the auto-responder if needed
BodyObject targobj;
if (_autoRespond != null && (targobj = CrowdServer.lookupBody(target)) != null) {
if (_autoRespond != null && (targobj = _locator.lookupBody(target)) != null) {
_autoRespond.sentTell(source, targobj, message);
}
}
@@ -185,9 +186,8 @@ public class ChatProvider
broadcastTo(_broadcastObject, from, bundle, msg, attention);
} else {
Iterator iter = CrowdServer.plreg.enumeratePlaces();
while (iter.hasNext()) {
PlaceObject plobj = (PlaceObject)iter.next();
for (Iterator<PlaceObject> iter = _plreg.enumeratePlaces(); iter.hasNext(); ) {
PlaceObject plobj = iter.next();
if (plobj.shouldBroadcast()) {
broadcastTo(plobj, from, bundle, msg, attention);
}
@@ -207,7 +207,7 @@ public class ChatProvider
throws InvocationException
{
// make sure the target user is online
BodyObject tobj = CrowdServer.lookupBody(target);
BodyObject tobj = _locator.lookupBody(target);
if (tobj == null) {
// if we have a forwarder configured, try forwarding the tell
if (_chatForwarder != null && _chatForwarder.forwardTell(message, target, listener)) {
@@ -277,6 +277,12 @@ public class ChatProvider
}
}
/** Provides access to place managers. */
@Inject protected PlaceRegistry _plreg;
/** Used to look up body objects by name. */
@Inject protected BodyLocator _locator;
/** Generates auto-responses to tells. May be null. */
protected TellAutoResponder _autoRespond;
@@ -0,0 +1,52 @@
//
// $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.google.inject.Singleton;
import com.threerings.util.Name;
import com.threerings.presents.annotation.EventThread;
import com.threerings.presents.server.ClientManager;
import com.threerings.crowd.data.BodyObject;
/**
* Used to lookup {@link BodyObject} instances by name.
*/
@Singleton
public class BodyLocator
{
/**
* Returns the body object for the user with the specified visible name, or null if they are
* not online.
*/
@EventThread
public BodyObject lookupBody (Name visibleName)
{
// by default visibleName is username
return (BodyObject)_clmgr.getClientObject(visibleName);
}
@Inject protected ClientManager _clmgr;
}
@@ -21,8 +21,6 @@
package com.threerings.crowd.server;
import java.util.Iterator;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
@@ -31,7 +29,6 @@ import com.samskivert.util.Invoker;
import com.threerings.util.Name;
import com.threerings.presents.dobj.RootDObjectManager;
import com.threerings.presents.net.AuthRequest;
import com.threerings.presents.server.ClientFactory;
import com.threerings.presents.server.ClientManager;
@@ -120,33 +117,6 @@ public class CrowdServer extends PresentsServer
// configure the place registry with the injector
_plreg.setInjector(injector);
// create our body locator
_lookup = createBodyLocator();
}
/**
* Allow derived instances to create a custom {@link BodyLocator}. If the system opts not to
* use {@link BodyObject#username} as a user's visible name, it will need to provide a custom
* {@link BodyLocator}.
*/
protected BodyLocator createBodyLocator ()
{
return new BodyLocator() {
public BodyObject get (Name visibleName) {
// by default visibleName is username
return (BodyObject)_clmgr.getClientObject(visibleName);
}
};
}
/**
* Looks up the {@link BodyObject} for the user with the specified visible name, returns null
* if they are not online. This should only be called from the dobjmgr thread.
*/
public static BodyObject lookupBody (Name visibleName)
{
return _lookup.get(visibleName);
}
public static void main (String[] args)
@@ -161,15 +131,6 @@ public class CrowdServer extends PresentsServer
}
}
/** An interface that allows server extensions to reconfigure the body lookup process. See
* {@link #lookupBody}, {@link #createBodyLocator}. */
protected static interface BodyLocator
{
/** Returns the body object for the user with the specified visible name, or null if they
* are not online. This will only be called from the dobjmgr thread. */
public BodyObject get (Name visibleName);
}
/** Handles the creation and tracking of place managers. */
@Inject protected PlaceRegistry _plreg;
@@ -182,9 +143,6 @@ public class CrowdServer extends PresentsServer
/** Provides chat-related invocation services. */
@Inject protected ChatProvider _chatprov;
/** Used to look up {@link BodyObject} instance for online users. See {@link #lookupBody}. */
protected static BodyLocator _lookup;
/** The config key for our list of invocation provider mappings. */
protected final static String PROVIDERS_KEY = "providers";
}
@@ -55,7 +55,6 @@ import com.threerings.crowd.data.Place;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.chat.data.SpeakMarshaller;
import com.threerings.crowd.chat.server.SpeakDispatcher;
import com.threerings.crowd.chat.server.SpeakHandler;
@@ -174,12 +173,13 @@ public class PlaceManager
/**
* Called by the place registry after creating this place manager.
*/
public void init (PlaceRegistry registry, InvocationManager invmgr,
RootDObjectManager omgr, PlaceConfig config)
public void init (PlaceRegistry registry, InvocationManager invmgr, RootDObjectManager omgr,
BodyLocator locator, PlaceConfig config)
{
_registry = registry;
_invmgr = invmgr;
_omgr = omgr;
_locator = locator;
_config = config;
// initialize our delegates
@@ -246,7 +246,7 @@ public class PlaceManager
// we usually want to create and register a speaker service instance that clients can use
// to speak in this place
if (shouldCreateSpeakService()) {
plobj.setSpeakService((SpeakMarshaller)_invmgr.registerDispatcher(
plobj.setSpeakService(_invmgr.registerDispatcher(
new SpeakDispatcher(new SpeakHandler(plobj, this))));
}
@@ -703,6 +703,9 @@ public class PlaceManager
/** A distributed object manager for doing dobj stuff. */
protected RootDObjectManager _omgr;
/** Used to look up body objects by name. */
protected BodyLocator _locator;
/** A reference to the place object that we manage. */
protected PlaceObject _plobj;
@@ -36,7 +36,6 @@ import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationManager;
import com.threerings.presents.server.ShutdownManager;
import com.threerings.crowd.data.CrowdCodes;
import com.threerings.crowd.data.Place;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.data.PlaceObject;
@@ -202,7 +201,7 @@ public class PlaceRegistry
}
// let the pmgr know about us and its configuration
pmgr.init(this, _invmgr, _omgr, config);
pmgr.init(this, _invmgr, _omgr, _locator, config);
} catch (Exception e) {
log.warning(e);
@@ -274,6 +273,9 @@ public class PlaceRegistry
/** The distributed object manager with which we operate. */
@Inject protected RootDObjectManager _omgr;
/** Used to look body objects up by name. */
@Inject protected BodyLocator _locator;
/** We use this to inject dependencies into place managers that we create. */
protected Injector _injector;