Depends injection and other cleanup.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@634 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2008-06-29 14:12:30 +00:00
parent 925a5aa982
commit 8f4f4a3e53
3 changed files with 58 additions and 55 deletions
@@ -21,8 +21,17 @@
package com.threerings.micasa.lobby; package com.threerings.micasa.lobby;
import java.util.*; import java.util.Iterator;
import com.samskivert.util.*; import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.samskivert.util.StringUtil;
import com.google.inject.Inject;
import com.threerings.util.StreamableArrayList; import com.threerings.util.StreamableArrayList;
import com.threerings.presents.data.ClientObject; import com.threerings.presents.data.ClientObject;
@@ -30,11 +39,11 @@ import com.threerings.presents.data.InvocationCodes;
import com.threerings.presents.server.InvocationManager; import com.threerings.presents.server.InvocationManager;
import com.threerings.crowd.data.BodyObject; import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.server.PlaceRegistry;
import com.threerings.micasa.lobby.LobbyService.CategoriesListener; import com.threerings.micasa.lobby.LobbyService.CategoriesListener;
import com.threerings.micasa.lobby.LobbyService.LobbiesListener; import com.threerings.micasa.lobby.LobbyService.LobbiesListener;
import com.threerings.micasa.server.MiCasaConfig; import com.threerings.micasa.server.MiCasaConfig;
import com.threerings.micasa.server.MiCasaServer;
import static com.threerings.micasa.Log.log; import static com.threerings.micasa.Log.log;
@@ -87,24 +96,22 @@ import static com.threerings.micasa.Log.log;
public class LobbyRegistry public class LobbyRegistry
implements LobbyProvider implements LobbyProvider
{ {
/** @Inject public LobbyRegistry (InvocationManager invmgr)
* Initializes the registry. It will use the supplied configuration instance to determine which
* lobbies to load, etc.
*
* @param invmgr a reference to the server's invocation manager.
*/
public void init (InvocationManager invmgr)
{ {
// register ourselves as an invocation service handler // register ourselves as an invocation service handler
invmgr.registerDispatcher(new LobbyDispatcher(this), InvocationCodes.GLOBAL_GROUP); invmgr.registerDispatcher(new LobbyDispatcher(this), InvocationCodes.GLOBAL_GROUP);
}
/**
* Initializes the registry, creating our default lobbies.
*/
public void init ()
{
// create our lobby managers // create our lobby managers
String[] lmgrs = null; String[] lmgrs = null;
lmgrs = MiCasaConfig.config.getValue(LOBIDS_KEY, lmgrs); lmgrs = MiCasaConfig.config.getValue(LOBIDS_KEY, lmgrs);
if (lmgrs == null || lmgrs.length == 0) { if (lmgrs == null || lmgrs.length == 0) {
log.warning("No lobbies specified in config file (via '" + log.warning("No lobbies specified in config file (via '" + LOBIDS_KEY + "' parameter).");
LOBIDS_KEY + "' parameter).");
} else { } else {
for (int i = 0; i < lmgrs.length; i++) { for (int i = 0; i < lmgrs.length; i++) {
loadLobby(lmgrs[i]); loadLobby(lmgrs[i]);
@@ -142,7 +149,7 @@ public class LobbyRegistry
// create and initialize the lobby manager. it will call lobbyReady() when it has // create and initialize the lobby manager. it will call lobbyReady() when it has
// obtained a reference to its lobby object and is ready to roll // obtained a reference to its lobby object and is ready to roll
LobbyManager lobmgr = (LobbyManager)MiCasaServer.plreg.createPlace(config); LobbyManager lobmgr = (LobbyManager)_plreg.createPlace(config);
lobmgr.init(this, props); lobmgr.init(this, props);
} catch (Exception e) { } catch (Exception e) {
@@ -159,9 +166,9 @@ public class LobbyRegistry
* @param category the category of game for which the lobbies are desired. * @param category the category of game for which the lobbies are desired.
* @param target the list into which the matching lobbies will be deposited. * @param target the list into which the matching lobbies will be deposited.
*/ */
public void getLobbies (BodyObject requester, String category, List target) public void getLobbies (BodyObject requester, String category, List<Lobby> target)
{ {
ArrayList list = (ArrayList)_lobbies.get(category); List<Lobby> list = _lobbies.get(category);
if (list != null) { if (list != null) {
target.addAll(list); target.addAll(list);
} }
@@ -176,12 +183,10 @@ public class LobbyRegistry
*/ */
public String[] getCategories (BodyObject requester) public String[] getCategories (BodyObject requester)
{ {
// might want to cache this some day so that we don't recreate it every time someone wants
// it. we can cache the array until the category count changes
String[] cats = new String[_lobbies.size()]; String[] cats = new String[_lobbies.size()];
Iterator iter = _lobbies.keySet().iterator(); Iterator<String> iter = _lobbies.keySet().iterator();
for (int i = 0; iter.hasNext(); i++) { for (int ii = 0; iter.hasNext(); ii++) {
cats[i] = (String)iter.next(); cats[ii] = iter.next();
} }
return cats; return cats;
} }
@@ -202,7 +207,7 @@ public class LobbyRegistry
public void getLobbies (ClientObject caller, String category, LobbiesListener listener) public void getLobbies (ClientObject caller, String category, LobbiesListener listener)
{ {
StreamableArrayList target = new StreamableArrayList(); StreamableArrayList target = new StreamableArrayList();
ArrayList list = (ArrayList)_lobbies.get(category); List<Lobby> list = _lobbies.get(category);
if (list != null) { if (list != null) {
target.addAll(list); target.addAll(list);
} }
@@ -234,18 +239,19 @@ public class LobbyRegistry
/** Registers the supplied lobby in the specified category table. */ /** Registers the supplied lobby in the specified category table. */
protected void registerLobby (String category, Lobby record) protected void registerLobby (String category, Lobby record)
{ {
ArrayList catlist = (ArrayList)_lobbies.get(category); List<Lobby> catlist = _lobbies.get(category);
if (catlist == null) { if (catlist == null) {
catlist = new ArrayList(); catlist = Lists.newArrayList();
_lobbies.put(category, catlist); _lobbies.put(category, catlist);
} }
catlist.add(record); catlist.add(record);
log.info("Registered lobby [cat=" + category + ", record=" + record + "]."); log.info("Registered lobby [cat=" + category + ", record=" + record + "].");
} }
/** A table containing references to all of our lobby records (in the form of category @Inject protected PlaceRegistry _plreg;
* lists. */
protected HashMap _lobbies = new HashMap(); /** A table containing references to our lobby records (in the form of category lists. */
protected Map<String,List<Lobby>> _lobbies = Maps.newHashMap();
/** The oid of the default lobby. */ /** The oid of the default lobby. */
protected int _defLobbyOid = -1; protected int _defLobbyOid = -1;
@@ -21,30 +21,33 @@
package com.threerings.micasa.server; package com.threerings.micasa.server;
import com.google.inject.Inject;
import com.threerings.presents.net.BootstrapData; import com.threerings.presents.net.BootstrapData;
import com.threerings.crowd.server.CrowdClient; import com.threerings.crowd.server.CrowdClient;
import com.threerings.micasa.data.MiCasaBootstrapData; import com.threerings.micasa.data.MiCasaBootstrapData;
import com.threerings.micasa.lobby.LobbyRegistry;
/** /**
* Extends the Crowd client and provides bootstrap data specific to the * Extends the Crowd client and provides bootstrap data specific to the MiCasa services.
* MiCasa services.
*/ */
public class MiCasaClient extends CrowdClient public class MiCasaClient extends CrowdClient
{ {
// documentation inherited @Override // from PresentsClient
protected BootstrapData createBootstrapData () protected BootstrapData createBootstrapData ()
{ {
return new MiCasaBootstrapData(); return new MiCasaBootstrapData();
} }
// documentation inherited @Override // from PresentsClient
protected void populateBootstrapData (BootstrapData data) protected void populateBootstrapData (BootstrapData data)
{ {
super.populateBootstrapData(data); super.populateBootstrapData(data);
// let the client know their default lobby oid // let the client know their default lobby oid
((MiCasaBootstrapData)data).defLobbyOid = ((MiCasaBootstrapData)data).defLobbyOid = _lobreg.getDefaultLobbyOid();
MiCasaServer.lobreg.getDefaultLobbyOid();
} }
@Inject protected LobbyRegistry _lobreg;
} }
@@ -22,6 +22,7 @@
package com.threerings.micasa.server; package com.threerings.micasa.server;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.threerings.util.Name; import com.threerings.util.Name;
@@ -40,16 +41,21 @@ import com.threerings.micasa.lobby.LobbyRegistry;
import static com.threerings.micasa.Log.log; import static com.threerings.micasa.Log.log;
/** /**
* This class is the main entry point and general organizer of everything * The main general organizer of everything that goes on in the MiCasa game server process.
* that goes on in the MiCasa game server process.
*/ */
public class MiCasaServer extends CrowdServer public class MiCasaServer extends CrowdServer
{ {
/** The parlor manager in operation on this server. */ public static void main (String[] args)
public static ParlorManager parmgr = new ParlorManager(); {
Injector injector = Guice.createInjector(new Module());
/** The lobby registry operating on this server. */ MiCasaServer server = injector.getInstance(MiCasaServer.class);
public static LobbyRegistry lobreg = new LobbyRegistry(); try {
server.init(injector);
server.run();
} catch (Exception e) {
log.warning("Unable to initialize server.", e);
}
}
@Override // from CrowdServer @Override // from CrowdServer
public void init (Injector injector) public void init (Injector injector)
@@ -67,24 +73,12 @@ public class MiCasaServer extends CrowdServer
} }
}); });
// initialize our parlor manager
parmgr.init(invmgr, plreg);
// initialize the lobby registry // initialize the lobby registry
lobreg.init(invmgr); _lobreg.init();
log.info("MiCasa server initialized."); log.info("MiCasa server initialized.");
} }
public static void main (String[] args) @Inject protected LobbyRegistry _lobreg;
{ @Inject protected ParlorManager _parmgr;
Injector injector = Guice.createInjector(new Module());
MiCasaServer server = injector.getInstance(MiCasaServer.class);
try {
server.init(injector);
server.run();
} catch (Exception e) {
log.warning("Unable to initialize server.", e);
}
}
} }