From 40ffa60f504fef41646fab562f00317f53efb345 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 30 Jun 2008 13:30:17 +0000 Subject: [PATCH] Inject depends into Tournies and ToureyManager. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@646 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../parlor/tourney/server/TourneyManager.java | 40 +++++------ .../tourney/server/TourniesManager.java | 68 ++++++++++--------- .../server/persist/TourneyRepository.java | 12 +++- 3 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/java/com/threerings/parlor/tourney/server/TourneyManager.java b/src/java/com/threerings/parlor/tourney/server/TourneyManager.java index ec18ff0d..a3a0bf46 100644 --- a/src/java/com/threerings/parlor/tourney/server/TourneyManager.java +++ b/src/java/com/threerings/parlor/tourney/server/TourneyManager.java @@ -21,12 +21,14 @@ package com.threerings.parlor.tourney.server; +import com.google.inject.Inject; import com.samskivert.util.Interval; import com.samskivert.util.ResultListener; import com.threerings.util.Name; import com.threerings.crowd.data.BodyObject; +import com.threerings.crowd.server.BodyLocator; import com.threerings.parlor.tourney.data.Participant; import com.threerings.parlor.tourney.data.TourneyConfig; @@ -45,14 +47,14 @@ import com.threerings.presents.server.InvocationManager; public abstract class TourneyManager implements TourneyProvider, TourneyCodes { - public TourneyManager (InvocationManager invmgr, RootDObjectManager omgr, TourneyConfig config, - TourniesManager tmgr, Comparable key, - InvocationService.ResultListener listener) + /** + * Initializes this tourney manager and prepares it for operation. + * + * @return the oid of this manager's tourney object. + */ + public int init (TourneyConfig config, Comparable key) { - _invmgr = invmgr; - _omgr = omgr; _config = config; - _tmgr = tmgr; _key = key; // creare and configure our Tourney object @@ -70,9 +72,7 @@ public abstract class TourneyManager _startTime = System.currentTimeMillis() + (MINUTE * _config.startsIn); } - if (listener != null) { - listener.requestProcessed(Integer.valueOf(_trobj.getOid())); - } + return _trobj.getOid(); } // documentation inherited from TourneyProvider @@ -185,7 +185,7 @@ public abstract class TourneyManager // return the fees if (_trobj.config.entryFee != null) { for (Participant part : _trobj.participants) { - BodyObject body = lookupBody(part.username); + BodyObject body = _locator.lookupBody(part.username); if (body != null) { _trobj.config.entryFee.returnFee(body); } @@ -268,38 +268,30 @@ public abstract class TourneyManager }.schedule(MINUTE * 2); } - /** - * Looks up the BodyObject for a username. - */ - protected abstract BodyObject lookupBody (Name username); - /** * Will throw an InvocationException if the user cannot join the tourney. */ protected abstract void joinTourney (BodyObject body) throws InvocationException; - /** Used to register and clear invocation services. */ - protected InvocationManager _invmgr; - - /** Provides distributed object services. */ - protected RootDObjectManager _omgr; - /** Our touney configuration. */ protected TourneyConfig _config; /** Our distributed tourney object. */ protected TourneyObject _trobj; - /** Reference to the tournies manager. */ - protected TourniesManager _tmgr; - /** The time, in milliseconds, when the tourney starts. */ protected long _startTime; /** The key this tourney is recorded under. */ protected Comparable _key; + // services on which we depend + @Inject protected RootDObjectManager _omgr; + @Inject protected InvocationManager _invmgr; + @Inject protected BodyLocator _locator; + @Inject protected TourniesManager _tmgr; + /** One minute in milliseconds. */ protected static long MINUTE = 60 * 1000L; } diff --git a/src/java/com/threerings/parlor/tourney/server/TourniesManager.java b/src/java/com/threerings/parlor/tourney/server/TourniesManager.java index 78f1d215..1394aa7d 100644 --- a/src/java/com/threerings/parlor/tourney/server/TourniesManager.java +++ b/src/java/com/threerings/parlor/tourney/server/TourniesManager.java @@ -22,41 +22,47 @@ package com.threerings.parlor.tourney.server; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; +import java.util.Map; -import com.samskivert.util.Interval; -import com.samskivert.util.RunQueue; +import com.google.common.collect.Maps; +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.Singleton; import com.samskivert.io.PersistenceException; import com.samskivert.jdbc.ConnectionProvider; - -import com.threerings.parlor.tourney.server.persist.TourneyRepository; -import com.threerings.parlor.tourney.data.Prize; -import com.threerings.parlor.tourney.data.TourneyConfig; +import com.samskivert.util.Interval; +import com.samskivert.util.RunQueue; import com.threerings.presents.client.InvocationService; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.dobj.RootDObjectManager; import com.threerings.presents.server.InvocationException; import com.threerings.presents.server.PresentsServer; import com.threerings.presents.server.ShutdownManager; -import com.threerings.presents.data.ClientObject; + +import com.threerings.parlor.tourney.data.Prize; +import com.threerings.parlor.tourney.data.TourneyConfig; +import com.threerings.parlor.tourney.server.persist.TourneyRepository; /** * An extensible tournament manager. */ +@Singleton public abstract class TourniesManager implements TourniesProvider, ShutdownManager.Shutdowner { /** * Initializes the tournies manager and starts its periodic update task. */ - public void init (ConnectionProvider conprov) + public void init (Injector injector) throws PersistenceException { - _tournrep = new TourneyRepository(conprov, getDBIdent()); loadTourneyConfigs(); - _interval = new Interval(getRunQueue()) { + _injector = injector; + _interval = new Interval(_omgr) { public void expired () { updateTournies(); } @@ -90,8 +96,13 @@ public abstract class TourniesManager protected void makeTourney (TourneyConfig config, InvocationService.ResultListener listener) { // create a new tourney manager which will run things - Integer tourneyID = Integer.valueOf(_tourneyCount++); - _tourneys.put(tourneyID, makeTourneyManager(config, tourneyID, listener)); + TourneyManager tmgr = _injector.getInstance(getTourneyManagerClass()); + int tournId = _tourneyCount++; + int tournOid = tmgr.init(config, tournId); + _tourneys.put(tournId, tmgr); + if (listener != null) { + listener.requestProcessed(tournOid); + } } /** @@ -112,7 +123,6 @@ public abstract class TourniesManager for (TourneyConfig config : tournies) { makeTourney(config, null); } - } /** @@ -124,36 +134,28 @@ public abstract class TourniesManager } /** - * Instantiates a new tourney manager. + * Returns the derivation of {@link TourneyManager} to use. */ - protected abstract TourneyManager makeTourneyManager( - TourneyConfig config, Comparable key, InvocationService.ResultListener listener); - - /** - * Returns the database identifier for our repository. - */ - protected abstract String getDBIdent (); - - /** - * Returns the RunQueue to use for our tourney interval. - */ - protected abstract RunQueue getRunQueue (); + protected abstract Class getTourneyManagerClass (); /** * Returns the tourney interval delay in milliseconds. */ protected abstract long getIntervalDelay (); - /** Holds all the current tournies in the game. */ - protected HashMap _tourneys = - new HashMap(); - - /** Reference to our tourney repository. */ - protected TourneyRepository _tournrep; + /** Used to resolve dependencies in the {@link TourneyManager}s that we create. */ + protected Injector _injector; /** The interval which updates loaded tournies. */ protected Interval _interval; /** Count to provide a unique key to tournies as they're created. */ protected int _tourneyCount; + + /** Holds all the current tournies in the game. */ + protected Map _tourneys = Maps.newHashMap(); + + // our dependencies + @Inject protected RootDObjectManager _omgr; + @Inject protected TourneyRepository _tournrep; } diff --git a/src/java/com/threerings/parlor/tourney/server/persist/TourneyRepository.java b/src/java/com/threerings/parlor/tourney/server/persist/TourneyRepository.java index 353e8921..c5fd2d89 100644 --- a/src/java/com/threerings/parlor/tourney/server/persist/TourneyRepository.java +++ b/src/java/com/threerings/parlor/tourney/server/persist/TourneyRepository.java @@ -30,6 +30,9 @@ import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; +import com.google.inject.Inject; +import com.google.inject.Singleton; + import com.samskivert.io.PersistenceException; import com.samskivert.jdbc.ConnectionProvider; @@ -49,6 +52,7 @@ import static com.threerings.parlor.Log.log; /** * The persistent store for tourney related information. */ +@Singleton public class TourneyRepository extends JORARepository { /** Contains tourney information loaded from the database. */ @@ -100,9 +104,13 @@ public class TourneyRepository extends JORARepository } } - public TourneyRepository (ConnectionProvider conprov, String dbident) + /** The database identifier used when establishing a database connection. This value being + * tourneydb. */ + public static final String TOURNEY_DB_IDENT = "tourneydb"; + + @Inject public TourneyRepository (ConnectionProvider conprov) { - super(conprov, dbident); + super(conprov, TOURNEY_DB_IDENT); } /**