From b88b5c61e581743c4f2db5f29ef7f29c838267da Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 16 Jan 2002 02:59:08 +0000 Subject: [PATCH] Modified the simulator stuff to allow it to be extended with the intent of providing a specially configured client context for use by the code that runs in the simulator. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@861 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../simulator/client/ClientController.java | 20 +-- .../micasa/simulator/client/SimpleClient.java | 132 ++++++++++++++++ .../micasa/simulator/client/SimpleFrame.java | 62 ++++++++ .../micasa/simulator/client/SimulatorApp.java | 61 ++++++-- .../simulator/client/SimulatorClient.java | 146 +----------------- .../simulator/client/SimulatorFrame.java | 37 +---- 6 files changed, 260 insertions(+), 198 deletions(-) create mode 100644 src/java/com/threerings/micasa/simulator/client/SimpleClient.java create mode 100644 src/java/com/threerings/micasa/simulator/client/SimpleFrame.java diff --git a/src/java/com/threerings/micasa/simulator/client/ClientController.java b/src/java/com/threerings/micasa/simulator/client/ClientController.java index c11571cdc..8e9875646 100644 --- a/src/java/com/threerings/micasa/simulator/client/ClientController.java +++ b/src/java/com/threerings/micasa/simulator/client/ClientController.java @@ -1,5 +1,5 @@ // -// $Id: ClientController.java,v 1.1 2001/12/19 09:32:02 shaper Exp $ +// $Id: ClientController.java,v 1.2 2002/01/16 02:59:08 mdb Exp $ package com.threerings.micasa.simulator.client; @@ -14,9 +14,9 @@ import com.threerings.crowd.client.*; import com.threerings.crowd.data.*; import com.threerings.parlor.game.GameConfig; +import com.threerings.parlor.util.ParlorContext; import com.threerings.micasa.Log; -import com.threerings.micasa.simulator.util.SimulatorContext; import com.threerings.micasa.simulator.data.SimulatorInfo; /** @@ -30,11 +30,13 @@ public class ClientController * Creates a new client controller. The controller will set everything * up in preparation for logging on. */ - public ClientController (SimulatorContext ctx, SimulatorFrame frame) + public ClientController (ParlorContext ctx, SimulatorFrame frame, + SimulatorInfo info) { // we'll want to keep these around _ctx = ctx; _frame = frame; + _info = info; // we want to know about logon/logoff _ctx.getClient().addObserver(this); @@ -63,23 +65,20 @@ public class ClientController // keep the body object around for stuff _body = (BodyObject)client.getClientObject(); - // get a handle on the simulator info - SimulatorInfo siminfo = _ctx.getSimulatorInfo(); - // create the game config object GameConfig config = null; try { config = (GameConfig) - Class.forName(siminfo.gameConfigClass).newInstance(); + Class.forName(_info.gameConfigClass).newInstance(); } catch (Exception e) { Log.warning("Failed to instantiate game config " + - "[class=" + siminfo.gameConfigClass + "]."); + "[class=" + _info.gameConfigClass + "]."); return; } // send the game creation request SimulatorDirector.createGame( - client, config, siminfo.simClass, siminfo.playerCount); + client, config, _info.simClass, _info.playerCount); // our work here is done, as the location manager will move us // into the game room straightaway @@ -112,7 +111,8 @@ public class ClientController Log.info("Client did logoff [client=" + client + "]."); } - protected SimulatorContext _ctx; + protected ParlorContext _ctx; protected SimulatorFrame _frame; + protected SimulatorInfo _info; protected BodyObject _body; } diff --git a/src/java/com/threerings/micasa/simulator/client/SimpleClient.java b/src/java/com/threerings/micasa/simulator/client/SimpleClient.java new file mode 100644 index 000000000..a3a992aef --- /dev/null +++ b/src/java/com/threerings/micasa/simulator/client/SimpleClient.java @@ -0,0 +1,132 @@ +// +// $Id: SimpleClient.java,v 1.1 2002/01/16 02:59:08 mdb Exp $ + +package com.threerings.micasa.simulator.client; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import java.io.IOException; + +import javax.swing.JPanel; +import javax.swing.SwingUtilities; + +import com.samskivert.util.Config; + +import com.threerings.presents.client.Client; +import com.threerings.presents.dobj.DObjectManager; + +import com.threerings.crowd.client.LocationDirector; +import com.threerings.crowd.client.OccupantManager; +import com.threerings.crowd.client.PlaceView; + +import com.threerings.parlor.client.ParlorDirector; +import com.threerings.parlor.util.ParlorContext; + +import com.threerings.micasa.Log; +import com.threerings.micasa.simulator.data.SimulatorInfo; + +public class SimpleClient + implements Client.Invoker, SimulatorClient +{ + public SimpleClient (SimulatorFrame frame) + throws IOException + { + // create our context + _ctx = new ParlorContextImpl(); + + // create the handles on our various services + _config = new Config(); + _client = new Client(null, this); + + // create our managers and directors + _locdir = new LocationDirector(_ctx); + _occmgr = new OccupantManager(_ctx); + _pardtr = new ParlorDirector(_ctx); + + // for test purposes, hardcode the server info + _client.setServer("localhost", 4007); + + // keep this for later + _frame = frame; + + // log off when they close the window + _frame.getFrame().addWindowListener(new WindowAdapter() { + public void windowClosing (WindowEvent evt) { + // if we're logged on, log off + if (_client.loggedOn()) { + _client.logoff(true); + } + } + }); + } + + /** + * Returns a reference to the context in effect for this client. This + * reference is valid for the lifetime of the application. + */ + public ParlorContext getContext () + { + return _ctx; + } + + // documentation inherited + public void invokeLater (Runnable run) + { + // queue it on up on the swing thread + SwingUtilities.invokeLater(run); + } + + /** + * The context implementation. This provides access to all of the + * objects and services that are needed by the operating client. + */ + protected class ParlorContextImpl implements ParlorContext + { + public Config getConfig () + { + return _config; + } + + public Client getClient () + { + return _client; + } + + public DObjectManager getDObjectManager () + { + return _client.getDObjectManager(); + } + + public LocationDirector getLocationDirector () + { + return _locdir; + } + + public OccupantManager getOccupantManager () + { + return _occmgr; + } + + public ParlorDirector getParlorDirector () + { + return _pardtr; + } + + public void setPlaceView (PlaceView view) + { + // stick the place view into our frame + _frame.setPanel((JPanel)view); + } + } + + protected ParlorContext _ctx; + protected SimulatorFrame _frame; + + protected Config _config; + protected Client _client; + protected LocationDirector _locdir; + protected OccupantManager _occmgr; + protected ParlorDirector _pardtr; +} + diff --git a/src/java/com/threerings/micasa/simulator/client/SimpleFrame.java b/src/java/com/threerings/micasa/simulator/client/SimpleFrame.java new file mode 100644 index 000000000..ad96acf68 --- /dev/null +++ b/src/java/com/threerings/micasa/simulator/client/SimpleFrame.java @@ -0,0 +1,62 @@ +// +// $Id: SimpleFrame.java,v 1.1 2002/01/16 02:59:08 mdb Exp $ + +package com.threerings.micasa.simulator.client; + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import com.samskivert.swing.Controller; + +/** + * Contains the user interface for the Simulator client application. + */ +public class SimpleFrame + extends JFrame implements SimulatorFrame +{ + /** + * Constructs the top-level Simulator client frame. + */ + public SimpleFrame () + { + super("Simulator"); + } + + // documentation inherited + public JFrame getFrame () + { + return this; + } + + /** + * Sets the panel that makes up the entire client display. + */ + public void setPanel (JPanel panel) + { + // remove the old panel + getContentPane().removeAll(); + // add the new one + getContentPane().add(panel, BorderLayout.CENTER); + // swing doesn't properly repaint after adding/removing children + validate(); + } + + /** + * Sets the controller for the outermost scope. This controller will + * handle all actions that aren't handled by controllers of tigher + * scope. + */ + public void setController (Controller controller) + { + _controller = controller; + } + + // documentation inherited + public Controller getController () + { + return _controller; + } + + protected Controller _controller; +} diff --git a/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java b/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java index 2c327aa6e..1d6eddd37 100644 --- a/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java +++ b/src/java/com/threerings/micasa/simulator/client/SimulatorApp.java @@ -1,8 +1,11 @@ // -// $Id: SimulatorApp.java,v 1.1 2001/12/19 09:32:02 shaper Exp $ +// $Id: SimulatorApp.java,v 1.2 2002/01/16 02:59:08 mdb Exp $ package com.threerings.micasa.simulator.client; +import javax.swing.JFrame; + +import com.samskivert.swing.Controller; import com.samskivert.swing.util.SwingUtil; import com.threerings.presents.client.Client; @@ -28,7 +31,7 @@ public class SimulatorApp _serverThread = new ServerThread(server); // create a frame - _frame = new SimulatorFrame(); + _frame = createSimulatorFrame(); // create the simulator info object SimulatorInfo siminfo = new SimulatorInfo(); @@ -38,7 +41,23 @@ public class SimulatorApp System.getProperty("playercount"), DEFAULT_PLAYER_COUNT); // create our client instance - _client = new SimulatorClient(_frame, siminfo); + _client = createSimulatorClient(_frame); + + // set up the top-level client controller + Controller ctrl = new ClientController( + _client.getContext(), _frame, siminfo); + _frame.setController(ctrl); + } + + protected SimulatorFrame createSimulatorFrame () + { + return new SimpleFrame(); + } + + protected SimulatorClient createSimulatorClient (SimulatorFrame frame) + throws Exception + { + return new SimpleClient(_frame); } public void run () @@ -46,9 +65,10 @@ public class SimulatorApp // size and display the window int wid = getInt(System.getProperty("width"), 800); int hei = getInt(System.getProperty("height"), 600); - _frame.setSize(wid, hei); - SwingUtil.centerWindow(_frame); - _frame.show(); + JFrame frame = _frame.getFrame(); + frame.setSize(wid, hei); + SwingUtil.centerWindow(frame); + frame.show(); // start up the server _serverThread.start(); @@ -84,16 +104,7 @@ public class SimulatorApp public static void main (String[] args) { - if (args.length < 2) { - String msg = "Usage:\n" + - " java com.threerings.simulator.SimulatorApp " + - " \n" + - "Optional properties:\n" + - " -Dusername=\n" + - " -Dplayercount=\n" + - " -Dwidth=\n" + - " -Dheight="; - System.out.println(msg); + if (!checkArgs(args)) { return; } @@ -108,6 +119,24 @@ public class SimulatorApp app.run(); } + protected static boolean checkArgs (String[] args) + { + if (args.length < 2) { + String msg = "Usage:\n" + + " java com.threerings.simulator.SimulatorApp " + + " \n" + + "Optional properties:\n" + + " -Dusername=\n" + + " -Dplayercount=\n" + + " -Dwidth=\n" + + " -Dheight="; + System.out.println(msg); + return false; + } + + return true; + } + protected int getInt (String value, int defval) { try { diff --git a/src/java/com/threerings/micasa/simulator/client/SimulatorClient.java b/src/java/com/threerings/micasa/simulator/client/SimulatorClient.java index 7e41fb44a..586844705 100644 --- a/src/java/com/threerings/micasa/simulator/client/SimulatorClient.java +++ b/src/java/com/threerings/micasa/simulator/client/SimulatorClient.java @@ -1,149 +1,11 @@ // -// $Id: SimulatorClient.java,v 1.1 2001/12/19 09:32:02 shaper Exp $ +// $Id: SimulatorClient.java,v 1.2 2002/01/16 02:59:08 mdb Exp $ package com.threerings.micasa.simulator.client; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; +import com.threerings.parlor.util.ParlorContext; -import java.io.IOException; - -import javax.swing.JPanel; -import javax.swing.SwingUtilities; - -import com.samskivert.swing.Controller; -import com.samskivert.util.Config; - -import com.threerings.presents.client.Client; -import com.threerings.presents.dobj.DObjectManager; - -import com.threerings.crowd.client.LocationDirector; -import com.threerings.crowd.client.OccupantManager; -import com.threerings.crowd.client.PlaceView; - -import com.threerings.parlor.client.ParlorDirector; - -import com.threerings.micasa.Log; -import com.threerings.micasa.simulator.data.SimulatorInfo; -import com.threerings.micasa.simulator.util.SimulatorContext; - -public class SimulatorClient - implements Client.Invoker +public interface SimulatorClient { - public SimulatorClient (SimulatorFrame frame, SimulatorInfo siminfo) - throws IOException - { - // create our context - _ctx = new SimulatorContextImpl(); - - // create the handles on our various services - _config = new Config(); - _client = new Client(null, this); - - // create our managers and directors - _locdir = new LocationDirector(_ctx); - _occmgr = new OccupantManager(_ctx); - _pardtr = new ParlorDirector(_ctx); - - // for test purposes, hardcode the server info - _client.setServer("localhost", 4007); - - // keep this for later - _frame = frame; - _siminfo = siminfo; - - // log off when they close the window - _frame.addWindowListener(new WindowAdapter() { - public void windowClosing (WindowEvent evt) { - // if we're logged on, log off - if (_client.loggedOn()) { - _client.logoff(true); - } - } - }); - - // create our client controller and stick it in the frame - Controller ctrl = new ClientController(_ctx, _frame); - _frame.setController(ctrl); - } - - /** - * Returns a reference to the context in effect for this client. This - * reference is valid for the lifetime of the application. - */ - public SimulatorContext getContext () - { - return _ctx; - } - - // documentation inherited - public void invokeLater (Runnable run) - { - // queue it on up on the swing thread - SwingUtilities.invokeLater(run); - } - - /** - * The context implementation. This provides access to all of the - * objects and services that are needed by the operating client. - */ - protected class SimulatorContextImpl implements SimulatorContext - { - public Config getConfig () - { - return _config; - } - - public Client getClient () - { - return _client; - } - - public DObjectManager getDObjectManager () - { - return _client.getDObjectManager(); - } - - public LocationDirector getLocationDirector () - { - return _locdir; - } - - public OccupantManager getOccupantManager () - { - return _occmgr; - } - - public ParlorDirector getParlorDirector () - { - return _pardtr; - } - - public void setPlaceView (PlaceView view) - { - // stick the place view into our frame - _frame.setPanel((JPanel)view); - } - - public SimulatorFrame getFrame () - { - return _frame; - } - - public SimulatorInfo getSimulatorInfo () - { - return _siminfo; - } - } - - protected SimulatorContext _ctx; - protected SimulatorFrame _frame; - protected SimulatorInfo _siminfo; - - protected Config _config; - protected Client _client; - protected LocationDirector _locdir; - protected OccupantManager _occmgr; - protected ParlorDirector _pardtr; + public ParlorContext getContext (); } - diff --git a/src/java/com/threerings/micasa/simulator/client/SimulatorFrame.java b/src/java/com/threerings/micasa/simulator/client/SimulatorFrame.java index 9ef5f414f..a4eeb1320 100644 --- a/src/java/com/threerings/micasa/simulator/client/SimulatorFrame.java +++ b/src/java/com/threerings/micasa/simulator/client/SimulatorFrame.java @@ -1,9 +1,8 @@ // -// $Id: SimulatorFrame.java,v 1.1 2001/12/19 09:32:02 shaper Exp $ +// $Id: SimulatorFrame.java,v 1.2 2002/01/16 02:59:08 mdb Exp $ package com.threerings.micasa.simulator.client; -import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; @@ -13,45 +12,23 @@ import com.samskivert.swing.ControllerProvider; /** * Contains the user interface for the Simulator client application. */ -public class SimulatorFrame - extends JFrame implements ControllerProvider +public interface SimulatorFrame extends ControllerProvider { /** - * Constructs the top-level Simulator client frame. + * Returns a reference to the top-level frame that the simulator will + * use to display everything. */ - public SimulatorFrame () - { - super("Simulator"); - } + public JFrame getFrame (); /** * Sets the panel that makes up the entire client display. */ - public void setPanel (JPanel panel) - { - // remove the old panel - getContentPane().removeAll(); - // add the new one - getContentPane().add(panel, BorderLayout.CENTER); - // swing doesn't properly repaint after adding/removing children - validate(); - } + public void setPanel (JPanel panel); /** * Sets the controller for the outermost scope. This controller will * handle all actions that aren't handled by controllers of tigher * scope. */ - public void setController (Controller controller) - { - _controller = controller; - } - - // documentation inherited - public Controller getController () - { - return _controller; - } - - protected Controller _controller; + public void setController (Controller controller); }