From 63013dc82cc8718e40e9557cfd83991e72d72dae Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Wed, 22 Jun 2011 01:08:54 +0200 Subject: Integrated dedicated server git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@563 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/ApplicationControl.java | 61 ++++++++++++++++++++-- src/jrummikub/control/LoginControl.java | 13 ++++- .../control/network/ConnectionControl.java | 40 ++++++++------ 3 files changed, 92 insertions(+), 22 deletions(-) (limited to 'src/jrummikub/control') diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java index 7f5cf6d..6909ce1 100644 --- a/src/jrummikub/control/ApplicationControl.java +++ b/src/jrummikub/control/ApplicationControl.java @@ -5,6 +5,8 @@ import jrummikub.control.network.NetworkControl; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IRoundState; +import jrummikub.server.DedicatedServer; +import jrummikub.server.DedicatedServer.ServerStatus; import jrummikub.util.Connection; import jrummikub.util.IListener; import jrummikub.util.IListener1; @@ -25,6 +27,7 @@ public class ApplicationControl { private SaveControl saveControl; private GameControl gameControl; private Connection tempConnection; + private DedicatedServer server; private IView view; @@ -32,7 +35,7 @@ public class ApplicationControl { * Creates a new application control * * @param view - * the view to use + * the view to use */ public ApplicationControl(final IView view) { this.view = view; @@ -52,10 +55,11 @@ public class ApplicationControl { saveControl.getLoadEvent().add( new IListener3() { @Override - public void handle(GameSettings settings, GameState gameState, - IRoundState roundState) { + public void handle(GameSettings settings, + GameState gameState, IRoundState roundState) { abortControls(); - gameControl = new GameControl(settings, saveControl, view); + gameControl = new GameControl(settings, saveControl, + view); addGameControlListeners(gameControl); gameControl.continueGame(gameState, roundState); } @@ -132,7 +136,7 @@ public class ApplicationControl { * Create a new network login control */ private void createLoginControl(boolean reset) { - loginControl = new LoginControl(view); + loginControl = new LoginControl(view, this); loginControl.getLoginEvent().add(new IListener1() { @Override public void handle(LoginData loginData) { @@ -250,4 +254,51 @@ public class ApplicationControl { networkControl.startNetwork(); } + + /** + * Ensure the dedicated server is running + * + * @param password + * password to use, if empty "jrummikub" is used + * @return whether the server could be started + */ + public boolean startDedicatedServer(String password) { + if (password == "") { + password = "jrummikub"; + } + if (server == null) { + DedicatedServer newServer = new DedicatedServer(password); + ServerStatus status = newServer.start(); + switch (status) { + case STARTED: + server = newServer; + break; + case ALREADY_RUNNING: + view.showServerStartupError(true); + return false; + case ERROR: + view.showServerStartupError(false); + return false; + } + } + server.setServerPassword(password); + view.getLoginPanel().setServer(server.getHostName()); + view.getLoginPanel().setChannel( + "jrummikub@play." + server.getHostName()); + view.getLoginPanel().setDedicatedServerRunning(true); + return true; + } + + /** + * If the login given is to our own dedicated server, update it's password + * to match + * + * @param loginData + * login data of user trying to connect + */ + public void updateDedicatedServerPassword(LoginData loginData) { + if (server != null && server.getHostName() == loginData.getServerName()) { + server.setServerPassword(loginData.getPassword()); + } + } } diff --git a/src/jrummikub/control/LoginControl.java b/src/jrummikub/control/LoginControl.java index 8908c0d..d13eb97 100644 --- a/src/jrummikub/control/LoginControl.java +++ b/src/jrummikub/control/LoginControl.java @@ -18,6 +18,7 @@ import jrummikub.view.IView; * */ public class LoginControl { + private ApplicationControl appControl; private IView view; private Event1 loginEvent = new Event1(); private Event cancelEvent = new Event(); @@ -28,14 +29,18 @@ public class LoginControl { * * @param view * for events which need handling + * @param applicationControl + * the application control */ - public LoginControl(final IView view) { + public LoginControl(final IView view, ApplicationControl applicationControl) { + this.appControl = applicationControl; this.view = view; connections.add(view.getLoginPanel().getLoginEvent() .add(new IListener1() { @Override public void handle(LoginData loginData) { abort(); + appControl.updateDedicatedServerPassword(loginData); loginEvent.emit(loginData); } })); @@ -48,6 +53,12 @@ public class LoginControl { cancelEvent.emit(); } })); + connections.add(view.getLoginPanel().getUseDedicatedServerEvent().add(new IListener1() { + @Override + public void handle(String value) { + appControl.startDedicatedServer(value); + } + })); } /** diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index b5886ca..39fabf7 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -2,6 +2,8 @@ package jrummikub.control.network; import java.awt.Color; import java.io.Serializable; +import java.util.Collection; +import java.util.Iterator; import java.util.UUID; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -88,7 +90,7 @@ public class ConnectionControl implements IConnectionControl { } } - private final LoginData loginData; + private LoginData loginData; private volatile Connection connection; private volatile MultiUserChat muc; @@ -125,7 +127,7 @@ public class ConnectionControl implements IConnectionControl { * Creates new connection control * * @param loginData - * player's login data + * player's login data */ public ConnectionControl(LoginData loginData) { this.loginData = loginData; @@ -346,7 +348,8 @@ public class ConnectionControl implements IConnectionControl { protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "change_color"); extension.setValue("uuid", uuid.toString()); - extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); + extension.setValue("color", + Base64.encodeObject(color, Base64.GZIP)); } }); } @@ -409,7 +412,8 @@ public class ConnectionControl implements IConnectionControl { protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "table_update"); extension.setValue("uuid", uuid.toString()); - extension.setValue("table", Base64.encodeObject(table, Base64.GZIP)); + extension.setValue("table", + Base64.encodeObject(table, Base64.GZIP)); } }); } @@ -423,8 +427,8 @@ public class ConnectionControl implements IConnectionControl { protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "turn_end"); extension.setValue("uuid", uuid.toString()); - extension.setValue("data", Base64.encodeObject(new TurnEndData(state, - invalidTurnInfo), Base64.GZIP)); + extension.setValue("data", Base64.encodeObject(new TurnEndData( + state, invalidTurnInfo), Base64.GZIP)); } }); } @@ -460,8 +464,8 @@ public class ConnectionControl implements IConnectionControl { protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_offer"); extension.setValue("uuid", data.getGameID().toString()); - extension.setValue("gameSettings", - Base64.encodeObject(data.getGameSettings(), Base64.GZIP)); + extension.setValue("gameSettings", Base64.encodeObject( + data.getGameSettings(), Base64.GZIP)); } }); } @@ -512,8 +516,8 @@ public class ConnectionControl implements IConnectionControl { .getExtension(ELEMENT_NAME, NAMESPACE); if (((Message) packet).getType() == Message.Type.error) { - System.err.println("Received error message from '" + packet.getFrom() - + "'"); + System.err.println("Received error message from '" + + packet.getFrom() + "'"); return; } @@ -529,14 +533,15 @@ public class ConnectionControl implements IConnectionControl { String sender, String messageType) { if (messageType.equals("game_offer")) { UUID uuid = UUID.fromString(extension.getValue("uuid")); - GameSettings settings = (GameSettings) Base64.decodeToObject(extension - .getValue("gameSettings")); + GameSettings settings = (GameSettings) Base64 + .decodeToObject(extension.getValue("gameSettings")); fixGameSettings(settings); GameData gameData = new GameData(uuid, settings, sender); gameOfferEvent.emit(gameData); } else if (messageType.equals("game_withdrawal")) { - gameWithdrawalEvent.emit(UUID.fromString(extension.getValue("uuid"))); + gameWithdrawalEvent + .emit(UUID.fromString(extension.getValue("uuid"))); } else if (messageType.equals("game_request")) { if (offeredGame != null) { sendGameOffer(); @@ -649,7 +654,8 @@ public class ConnectionControl implements IConnectionControl { XMPPError xmppError = e.getXMPPError(); if (xmppError != null) { - if (xmppError.getType() == Type.WAIT && xmppError.getCode() == 504) { + if (xmppError.getType() == Type.WAIT + && xmppError.getCode() == 504) { return LoginError.UNKNOWN_HOST; } } @@ -661,7 +667,8 @@ public class ConnectionControl implements IConnectionControl { private LoginError doLogin() { try { - connection.login(loginData.getUserName(), loginData.getPassword(), + connection.login(loginData.getUserName(), + loginData.getPassword(), "JRummikub-" + StringUtils.randomString(8)); return null; } catch (XMPPException e) { @@ -691,7 +698,8 @@ public class ConnectionControl implements IConnectionControl { continue; } else { // An unknown error has occurred, cancel connect - if (xmppError != null && xmppError.getType() == Type.CANCEL + if (xmppError != null + && xmppError.getType() == Type.CANCEL && xmppError.getCode() == 404) { return LoginError.UNKNOWN_CHANNEL; } -- cgit v1.2.3