From 0a2b8b8f521d25fbd2374bbde90613b079a7aa5c Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 7 Jun 2008 17:45:30 +0000 Subject: [PATCH] Basic Guice compliance. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@614 c613c5cb-e716-0410-b11b-feb51c14d237 --- etc/libs-incl.xml | 1 + .../micasa/server/MiCasaServer.java | 19 ++++++++------- .../micasa/simulator/client/SimulatorApp.java | 24 +++++++++---------- .../micasa/simulator/server/SimpleServer.java | 11 +++++---- .../simulator/server/SimulatorServer.java | 20 ++++++++-------- .../tourney/server/TourniesManager.java | 21 +++++++++------- .../threerings/stage/server/StageServer.java | 9 +++---- .../whirled/server/WhirledServer.java | 20 +++++++++++----- .../com/threerings/parlor/TestServer.java | 14 +++++++---- 9 files changed, 80 insertions(+), 59 deletions(-) diff --git a/etc/libs-incl.xml b/etc/libs-incl.xml index e3daffbe..f0815d6d 100644 --- a/etc/libs-incl.xml +++ b/etc/libs-incl.xml @@ -9,6 +9,7 @@ + diff --git a/src/java/com/threerings/micasa/server/MiCasaServer.java b/src/java/com/threerings/micasa/server/MiCasaServer.java index c48a28fe..d60da2d9 100644 --- a/src/java/com/threerings/micasa/server/MiCasaServer.java +++ b/src/java/com/threerings/micasa/server/MiCasaServer.java @@ -21,6 +21,9 @@ package com.threerings.micasa.server; +import com.google.inject.Guice; +import com.google.inject.Injector; + import com.threerings.util.Name; import com.threerings.presents.net.AuthRequest; @@ -48,17 +51,14 @@ public class MiCasaServer extends CrowdServer /** The lobby registry operating on this server. */ public static LobbyRegistry lobreg = new LobbyRegistry(); - /** - * Initializes all of the server services and prepares for operation. - */ - public void init () + @Override // from CrowdServer + public void init (Injector injector) throws Exception { - // do the base server initialization - super.init(); + super.init(injector); // configure the client manager to use our client class - clmgr.setClientFactory(new ClientFactory() { + _clmgr.setClientFactory(new ClientFactory() { public PresentsClient createClient (AuthRequest areq) { return new MiCasaClient(); } @@ -78,9 +78,10 @@ public class MiCasaServer extends CrowdServer public static void main (String[] args) { - MiCasaServer server = new MiCasaServer(); + Injector injector = Guice.createInjector(new Module()); + MiCasaServer server = injector.getInstance(MiCasaServer.class); try { - server.init(); + server.init(injector); server.run(); } catch (Exception e) { log.warning("Unable to initialize server.", e); diff --git a/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java b/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java index 75ff081e..d3ed4c65 100644 --- a/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java +++ b/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java @@ -23,6 +23,9 @@ package com.threerings.micasa.simulator.client; import javax.swing.JFrame; +import com.google.inject.Guice; +import com.google.inject.Injector; + import com.samskivert.swing.Controller; import com.samskivert.swing.util.SwingUtil; import com.samskivert.util.Interval; @@ -41,8 +44,7 @@ import com.threerings.micasa.simulator.server.SimulatorServer; import static com.threerings.micasa.Log.log; /** - * The simulator application is a test harness to facilitate development - * and debugging of games. + * The simulator application is a test harness to facilitate development and debugging of games. */ public class SimulatorApp { @@ -55,8 +57,7 @@ public class SimulatorApp SimulatorInfo siminfo = new SimulatorInfo(); siminfo.gameConfigClass = args[0]; siminfo.simClass = args[1]; - siminfo.playerCount = getInt( - System.getProperty("playercount"), DEFAULT_PLAYER_COUNT); + siminfo.playerCount = getInt(System.getProperty("playercount"), DEFAULT_PLAYER_COUNT); // create our client instance _client = createSimulatorClient(_frame); @@ -66,14 +67,14 @@ public class SimulatorApp _frame.setController(ctrl); // create the server - SimulatorServer server = createSimulatorServer(); - server.init(new ResultListener() { + Injector injector = Guice.createInjector(new SimpleServer.Module()); + SimulatorServer server = createSimulatorServer(injector); + server.init(injector, new ResultListener() { public void requestCompleted (Object result) { try { run(); } catch (Exception e) { - log.warning("Simulator initialization failed " + - "[e=" + e + "]."); + log.warning("Simulator initialization failed [e=" + e + "]."); } } public void requestFailed (Exception e) { @@ -83,14 +84,13 @@ public class SimulatorApp // run the server on a separate thread _serverThread = new ServerThread(server); - // start up the server so that we can be notified when - // initialization is complete + // start up the server so that we can be notified when initialization is complete _serverThread.start(); } - protected SimulatorServer createSimulatorServer () + protected SimulatorServer createSimulatorServer (Injector injector) { - return new SimpleServer(); + return injector.getInstance(SimpleServer.class); } protected SimulatorFrame createSimulatorFrame () diff --git a/src/java/com/threerings/micasa/simulator/server/SimpleServer.java b/src/java/com/threerings/micasa/simulator/server/SimpleServer.java index 53659eb6..2febe83a 100644 --- a/src/java/com/threerings/micasa/simulator/server/SimpleServer.java +++ b/src/java/com/threerings/micasa/simulator/server/SimpleServer.java @@ -21,22 +21,23 @@ package com.threerings.micasa.simulator.server; +import com.google.inject.Injector; + import com.samskivert.util.ResultListener; import com.threerings.micasa.server.MiCasaServer; /** - * A simple simulator server implementation that extends the MiCasa server - * and provides no special functionality. + * A simple simulator server implementation that extends the MiCasa server and provides no special + * functionality. */ public class SimpleServer extends MiCasaServer implements SimulatorServer { - // documentation inherited - public void init (ResultListener obs) + public void init (Injector injector, ResultListener obs) throws Exception { - super.init(); + init(injector); // do our standard initialization // create the simulator manager SimulatorManager simmgr = new SimulatorManager(); diff --git a/src/java/com/threerings/micasa/simulator/server/SimulatorServer.java b/src/java/com/threerings/micasa/simulator/server/SimulatorServer.java index 3a49028d..d564e52a 100644 --- a/src/java/com/threerings/micasa/simulator/server/SimulatorServer.java +++ b/src/java/com/threerings/micasa/simulator/server/SimulatorServer.java @@ -21,29 +21,29 @@ package com.threerings.micasa.simulator.server; +import com.google.inject.Injector; + import com.samskivert.util.ResultListener; /** - * The simulator manager needs a mechanism for faking body object - * registrations, which is provided by implementations of this interface. + * The simulator manager needs a mechanism for faking body object registrations, which is provided + * by implementations of this interface. */ public interface SimulatorServer { /** * Called to initialize this server instance. * - * @param obs the observer to notify when the server has finished - * starting up, or null if no notification is desired. + * @param obs the observer to notify when the server has finished starting up, or + * null if no notification is desired. * - * @exception Exception thrown if anything goes wrong initializing the - * server. + * @exception Exception thrown if anything goes wrong initializing the server. */ - public void init (ResultListener obs) throws Exception; + public void init (Injector injector, ResultListener obs) throws Exception; /** - * Called to perform the main body of server processing. This is - * called from the server thread and should do the simulator server's - * primary business. + * Called to perform the main body of server processing. This is called from the server thread + * and should do the simulator server's primary business. */ public void run (); } diff --git a/src/java/com/threerings/parlor/tourney/server/TourniesManager.java b/src/java/com/threerings/parlor/tourney/server/TourniesManager.java index 0f3cae9b..78f1d215 100644 --- a/src/java/com/threerings/parlor/tourney/server/TourniesManager.java +++ b/src/java/com/threerings/parlor/tourney/server/TourniesManager.java @@ -38,24 +38,24 @@ import com.threerings.parlor.tourney.data.TourneyConfig; import com.threerings.presents.client.InvocationService; import com.threerings.presents.server.InvocationException; import com.threerings.presents.server.PresentsServer; +import com.threerings.presents.server.ShutdownManager; import com.threerings.presents.data.ClientObject; /** * An extensible tournament manager. */ public abstract class TourniesManager - implements TourniesProvider, PresentsServer.Shutdowner + implements TourniesProvider, ShutdownManager.Shutdowner { - public TourniesManager (ConnectionProvider conprov) + /** + * Initializes the tournies manager and starts its periodic update task. + */ + public void init (ConnectionProvider conprov) throws PersistenceException { _tournrep = new TourneyRepository(conprov, getDBIdent()); - loadTourneyConfigs(); - } - public void init () - { _interval = new Interval(getRunQueue()) { public void expired () { updateTournies(); @@ -64,13 +64,13 @@ public abstract class TourniesManager _interval.schedule(getIntervalDelay(), true); } - // documentation inherited from interface PresentsServer.Shutdowner + // from interface ShutdownManager.Shutdowner public void shutdown () { _interval.cancel(); } - // documentation inherited from interface TourniesService + // from interface TourniesService public void createTourney (ClientObject caller, TourneyConfig config, final InvocationService.ResultListener listener) throws InvocationException @@ -78,6 +78,11 @@ public abstract class TourniesManager makeTourney(config, listener); } + protected TourniesManager (ShutdownManager shutmgr) + { + shutmgr.registerShutdowner(this); + } + /** * Called to actually create a tourney once it has been validated and the prize has been * reserved. diff --git a/src/java/com/threerings/stage/server/StageServer.java b/src/java/com/threerings/stage/server/StageServer.java index 27e94a84..4bcd2c7c 100644 --- a/src/java/com/threerings/stage/server/StageServer.java +++ b/src/java/com/threerings/stage/server/StageServer.java @@ -21,6 +21,8 @@ package com.threerings.stage.server; +import com.google.inject.Injector; + import com.threerings.resource.ResourceManager; import com.threerings.media.tile.TileManager; @@ -46,12 +48,11 @@ public abstract class StageServer extends WhirledServer /** Provides access to our tile repository. */ public static TileManager tilemgr; - // documentation inherited - public void init () + @Override // from WhirledServer + public void init (Injector injector) throws Exception { - // do the base server initialization - super.init(); + super.init(injector); // create the resource manager rsrcmgr = new ResourceManager("rsrc"); diff --git a/src/java/com/threerings/whirled/server/WhirledServer.java b/src/java/com/threerings/whirled/server/WhirledServer.java index b541e33d..62faf8d2 100644 --- a/src/java/com/threerings/whirled/server/WhirledServer.java +++ b/src/java/com/threerings/whirled/server/WhirledServer.java @@ -21,6 +21,8 @@ package com.threerings.whirled.server; +import com.google.inject.Injector; + import com.threerings.util.Name; import com.threerings.presents.net.AuthRequest; @@ -41,17 +43,23 @@ import static com.threerings.whirled.Log.log; */ public abstract class WhirledServer extends CrowdServer { + /** Configures dependencies needed by the Whirled server. */ + public static class Module extends CrowdServer.Module + { + @Override protected void configure () { + super.configure(); + // nada + } + } + /** The scene registry. */ public static SceneRegistry screg; - /** - * Initializes all of the server services and prepares for operation. - */ - public void init () + @Override // from CrowdServer + public void init (Injector injector) throws Exception { - // do the base server initialization - super.init(); + super.init(injector); // configure the client to use our whirled client clmgr.setClientFactory(new ClientFactory() { diff --git a/tests/src/java/com/threerings/parlor/TestServer.java b/tests/src/java/com/threerings/parlor/TestServer.java index a3b9d895..603a5e84 100644 --- a/tests/src/java/com/threerings/parlor/TestServer.java +++ b/tests/src/java/com/threerings/parlor/TestServer.java @@ -21,6 +21,9 @@ package com.threerings.parlor; +import com.google.inject.Guice; +import com.google.inject.Injector; + import com.threerings.crowd.server.CrowdServer; import com.threerings.parlor.server.ParlorManager; @@ -35,11 +38,11 @@ public class TestServer extends CrowdServer /** The parlor manager in operation on this server. */ public static ParlorManager parmgr = new ParlorManager(); - /** Initializes the Parlor test server. */ - public void init () + @Override // from CrowdServer + public void init (Injector injector) throws Exception { - super.init(); + super.init(injector); // initialize our parlor manager parmgr.init(invmgr, plreg); @@ -50,9 +53,10 @@ public class TestServer extends CrowdServer /** Main entry point for test server. */ public static void main (String[] args) { - TestServer server = new TestServer(); + Injector injector = Guice.createInjector(new Module()); + TestServer server = injector.getInstance(TestServer.class); try { - server.init(); + server.init(injector); server.run(); } catch (Exception e) { log.warning("Unable to initialize server.", e);