diff --git a/src/java/com/threerings/micasa/lobby/LobbyRegistry.java b/src/java/com/threerings/micasa/lobby/LobbyRegistry.java index 67cb2779..77cebe6f 100644 --- a/src/java/com/threerings/micasa/lobby/LobbyRegistry.java +++ b/src/java/com/threerings/micasa/lobby/LobbyRegistry.java @@ -21,8 +21,17 @@ package com.threerings.micasa.lobby; -import java.util.*; -import com.samskivert.util.*; +import java.util.Iterator; +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.presents.data.ClientObject; @@ -30,11 +39,11 @@ import com.threerings.presents.data.InvocationCodes; import com.threerings.presents.server.InvocationManager; 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.LobbiesListener; import com.threerings.micasa.server.MiCasaConfig; -import com.threerings.micasa.server.MiCasaServer; import static com.threerings.micasa.Log.log; @@ -87,24 +96,22 @@ import static com.threerings.micasa.Log.log; public class LobbyRegistry implements LobbyProvider { - /** - * 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) + @Inject public LobbyRegistry (InvocationManager invmgr) { // register ourselves as an invocation service handler invmgr.registerDispatcher(new LobbyDispatcher(this), InvocationCodes.GLOBAL_GROUP); + } + /** + * Initializes the registry, creating our default lobbies. + */ + public void init () + { // create our lobby managers String[] lmgrs = null; lmgrs = MiCasaConfig.config.getValue(LOBIDS_KEY, lmgrs); if (lmgrs == null || lmgrs.length == 0) { - log.warning("No lobbies specified in config file (via '" + - LOBIDS_KEY + "' parameter)."); - + log.warning("No lobbies specified in config file (via '" + LOBIDS_KEY + "' parameter)."); } else { for (int i = 0; i < lmgrs.length; i++) { loadLobby(lmgrs[i]); @@ -142,7 +149,7 @@ public class LobbyRegistry // 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 - LobbyManager lobmgr = (LobbyManager)MiCasaServer.plreg.createPlace(config); + LobbyManager lobmgr = (LobbyManager)_plreg.createPlace(config); lobmgr.init(this, props); } catch (Exception e) { @@ -159,9 +166,9 @@ public class LobbyRegistry * @param category the category of game for which the lobbies are desired. * @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 target) { - ArrayList list = (ArrayList)_lobbies.get(category); + List list = _lobbies.get(category); if (list != null) { target.addAll(list); } @@ -176,12 +183,10 @@ public class LobbyRegistry */ 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()]; - Iterator iter = _lobbies.keySet().iterator(); - for (int i = 0; iter.hasNext(); i++) { - cats[i] = (String)iter.next(); + Iterator iter = _lobbies.keySet().iterator(); + for (int ii = 0; iter.hasNext(); ii++) { + cats[ii] = iter.next(); } return cats; } @@ -202,7 +207,7 @@ public class LobbyRegistry public void getLobbies (ClientObject caller, String category, LobbiesListener listener) { StreamableArrayList target = new StreamableArrayList(); - ArrayList list = (ArrayList)_lobbies.get(category); + List list = _lobbies.get(category); if (list != null) { target.addAll(list); } @@ -234,18 +239,19 @@ public class LobbyRegistry /** Registers the supplied lobby in the specified category table. */ protected void registerLobby (String category, Lobby record) { - ArrayList catlist = (ArrayList)_lobbies.get(category); + List catlist = _lobbies.get(category); if (catlist == null) { - catlist = new ArrayList(); + catlist = Lists.newArrayList(); _lobbies.put(category, catlist); } catlist.add(record); log.info("Registered lobby [cat=" + category + ", record=" + record + "]."); } - /** A table containing references to all of our lobby records (in the form of category - * lists. */ - protected HashMap _lobbies = new HashMap(); + @Inject protected PlaceRegistry _plreg; + + /** A table containing references to our lobby records (in the form of category lists. */ + protected Map> _lobbies = Maps.newHashMap(); /** The oid of the default lobby. */ protected int _defLobbyOid = -1; diff --git a/src/java/com/threerings/micasa/server/MiCasaClient.java b/src/java/com/threerings/micasa/server/MiCasaClient.java index 33a78783..80b439b6 100644 --- a/src/java/com/threerings/micasa/server/MiCasaClient.java +++ b/src/java/com/threerings/micasa/server/MiCasaClient.java @@ -21,30 +21,33 @@ package com.threerings.micasa.server; +import com.google.inject.Inject; + import com.threerings.presents.net.BootstrapData; import com.threerings.crowd.server.CrowdClient; import com.threerings.micasa.data.MiCasaBootstrapData; +import com.threerings.micasa.lobby.LobbyRegistry; /** - * Extends the Crowd client and provides bootstrap data specific to the - * MiCasa services. + * Extends the Crowd client and provides bootstrap data specific to the MiCasa services. */ public class MiCasaClient extends CrowdClient { - // documentation inherited + @Override // from PresentsClient protected BootstrapData createBootstrapData () { return new MiCasaBootstrapData(); } - // documentation inherited + @Override // from PresentsClient protected void populateBootstrapData (BootstrapData data) { super.populateBootstrapData(data); // let the client know their default lobby oid - ((MiCasaBootstrapData)data).defLobbyOid = - MiCasaServer.lobreg.getDefaultLobbyOid(); + ((MiCasaBootstrapData)data).defLobbyOid = _lobreg.getDefaultLobbyOid(); } + + @Inject protected LobbyRegistry _lobreg; } diff --git a/src/java/com/threerings/micasa/server/MiCasaServer.java b/src/java/com/threerings/micasa/server/MiCasaServer.java index 6ecad902..14428f79 100644 --- a/src/java/com/threerings/micasa/server/MiCasaServer.java +++ b/src/java/com/threerings/micasa/server/MiCasaServer.java @@ -22,6 +22,7 @@ package com.threerings.micasa.server; import com.google.inject.Guice; +import com.google.inject.Inject; import com.google.inject.Injector; import com.threerings.util.Name; @@ -40,16 +41,21 @@ import com.threerings.micasa.lobby.LobbyRegistry; import static com.threerings.micasa.Log.log; /** - * This class is the main entry point and general organizer of everything - * that goes on in the MiCasa game server process. + * The main general organizer of everything that goes on in the MiCasa game server process. */ public class MiCasaServer extends CrowdServer { - /** The parlor manager in operation on this server. */ - public static ParlorManager parmgr = new ParlorManager(); - - /** The lobby registry operating on this server. */ - public static LobbyRegistry lobreg = new LobbyRegistry(); + public static void main (String[] args) + { + 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); + } + } @Override // from CrowdServer 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 - lobreg.init(invmgr); + _lobreg.init(); log.info("MiCasa server initialized."); } - public static void main (String[] args) - { - 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); - } - } + @Inject protected LobbyRegistry _lobreg; + @Inject protected ParlorManager _parmgr; }