| /trunk/server/src/org/jogre/server/JogreServer.java r2 | /trunk/server/src/org/jogre/server/JogreServer.java r12 | ||
| 1 | /* | 1 | /* |
|---|---|---|---|
| 2 | * JOGRE (Java Online Gaming Real-time Engine) - Server | 2 | * JOGRE (Java Online Gaming Real-time Engine) - Server |
| 3 | * Copyright (C) 2004 Bob Marks (marksie531@yahoo.com) | 3 | * Copyright (C) 2004 Bob Marks (marksie531@yahoo.com) |
| 4 | * http://jogre.sourceforge.org | 4 | * http://jogre.sourceforge.org |
| 5 | * | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License | 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 | 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. | 9 | * of the License, or (at your option) any later version. |
| 10 | * | 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. | 14 | * GNU General Public License for more details. |
| 15 | * | 15 | * |
| 16 | * You should have received a copy of the GNU General Public License | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software | 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ | 19 | */ |
| 20 | package org.jogre.server; | 20 | package org.jogre.server; |
| 21 | 21 | ||
| 22 | import java.io.IOException; | 22 | import java.io.IOException; |
| 23 | import java.net.BindException; | 23 | import java.net.BindException; |
| 24 | import java.net.ServerSocket; | 24 | import java.net.ServerSocket; |
| 25 | import java.net.Socket; | 25 | import java.net.Socket; |
| 26 | import java.text.NumberFormat; | 26 | import java.text.NumberFormat; |
| 27 | import java.util.Locale; | 27 | import java.util.Locale; |
| 28 | 28 | ||
| 29 | import org.jogre.common.GameList; | 29 | import org.jogre.common.GameList; |
| 30 | import org.jogre.common.IError; | 30 | import org.jogre.common.IError; |
| 31 | import org.jogre.common.IJogre; | 31 | import org.jogre.common.IJogre; |
| 32 | import org.jogre.common.JogreGlobals; | 32 | import org.jogre.common.JogreGlobals; |
| 33 | import org.jogre.common.util.JogreLabels; | 33 | import org.jogre.common.util.JogreLabels; |
| 34 | import org.jogre.common.util.JogreLogger; | 34 | import org.jogre.common.util.JogreLogger; |
| 35 | import org.jogre.server.controllers.ServerControllerList; | 35 | import org.jogre.server.controllers.ServerControllerList; |
| 36 | import org.jogre.server.data.IServerData; | 36 | import org.jogre.server.data.IServerData; |
| 37 | import org.jogre.server.data.ServerDataException; | 37 | import org.jogre.server.data.ServerDataException; |
| 38 | import org.jogre.server.data.ServerDataFactory; | 38 | import org.jogre.server.data.ServerDataFactory; |
| 39 | import org.jogre.server.data.db.DBConnection; | 39 | import org.jogre.server.data.db.DBConnection; |
| 40 | 40 | ||
| 41 | /** | 41 | /** |
| 42 | * <p>This is the all important JogreServer class.</p> | 42 | * <p>This is the all important JogreServer class.</p> |
| 43 | * | 43 | * |
| 44 | * <p>When a client connects to a <code>JogreServer</code> a new | 44 | * <p>When a client connects to a <code>JogreServer</code> a new |
| 45 | * <code>ServerConnectionThread</code> is created in its own Thread which will | 45 | * <code>ServerConnectionThread</code> is created in its own Thread which will |
| 46 | * handle all communciation between the server and the client. The connect | 46 | * handle all communciation between the server and the client. The connect |
| 47 | * method returns the correct ServerConnectionThread to the server.</p> | 47 | * method returns the correct ServerConnectionThread to the server.</p> |
| 48 | * | 48 | * |
| 49 | * <p>If a server keeps state then the abstract <code>getJogreModel()</code> method should | 49 | * <p>If a server keeps state then the abstract <code>getJogreModel()</code> method should |
| 50 | * return the correct implementation of the <code>JogreModel</code>. If a game should keep | 50 | * return the correct implementation of the <code>JogreModel</code>. If a game should keep |
| 51 | * state (this allows demanding games to run faster but means other users can't | 51 | * state (this allows demanding games to run faster but means other users can't |
| 52 | * join a table and watch a game) then this method should simply return null.</p> | 52 | * join a table and watch a game) then this method should simply return null.</p> |
| 53 | * | 53 | * |
| 54 | * @author Bob Marks | 54 | * @author Bob Marks |
| 55 | * @version Beta 0.3 | 55 | * @version Beta 0.3 |
| 56 | */ | 56 | */ |
| 57 | public class JogreServer { | 57 | public class JogreServer { |
| 58 | 58 | ||
| 59 | /** Logging */ | 59 | /** Logging */ |
| 60 | JogreLogger logger = new JogreLogger (this.getClass()); | 60 | JogreLogger logger = new JogreLogger (this.getClass()); |
| 61 | 61 | ||
| 62 | // Singleton instance of this server | 62 | // Singleton instance of this server |
| 63 | private static JogreServer instance; | 63 | private static JogreServer instance; |
| 64 | 64 | ||
| 65 | /** Default server port. */ | 65 | /** Default server port. */ |
| 66 | public static final int DEFAULT_SERVER_PORT = 1790; | 66 | public static final int DEFAULT_SERVER_PORT = 1790; |
| 67 | 67 | ||
| 68 | /** Server port we are listening on. */ | 68 | /** Server port we are listening on. */ |
| 69 | protected int serverPort; | 69 | protected int serverPort; |
| 70 | 70 | ||
| 71 | /** Boolean to specify if this server tries to link to a master server. */ | 71 | /** Boolean to specify if this server tries to link to a master server. */ |
| 72 | protected boolean linkToMasterServer = false; | 72 | protected boolean linkToMasterServer = false; |
| 73 | 73 | ||
| 74 | /** List of server connenction objects. */ | 74 | /** List of server connenction objects. */ |
| 75 | protected ConnectionList connections; | 75 | protected ConnectionList connections; |
| 76 | 76 | ||
| 77 | /** Link to the various games, and tables within games. */ | 77 | /** Link to the various games, and tables within games. */ |
| 78 | protected GameList gameList; | 78 | protected GameList gameList; |
| 79 | 79 | ||
| 80 | /** Link to the various parsers. */ | 80 | /** Link to the various parsers. */ |
| 81 | protected ServerControllerList serverControllerList; | 81 | protected ServerControllerList serverControllerList; |
| 82 | 82 | ||
| 83 | /** Game loader. */ | 83 | /** Game loader. */ |
| 84 | protected GameLoader gameLoader; | 84 | protected GameLoader gameLoader; |
| 85 | 85 | ||
| 86 | /** Declare how a user connection. */ | 86 | /** Declare how a user connection. */ |
| 87 | protected IServerData dataConnection = null; | 87 | protected IServerData dataConnection = null; |
| 88 | 88 | ||
| 89 | private ServerLabels labels = ServerLabels.getInstance(); // convience link to server labels | 89 | private ServerLabels labels = ServerLabels.getInstance(); // convience link to server labels |
| 90 | private long startTime; | 90 | private long startTime; |
| 91 | 91 | ||
| 92 | /** | 92 | /** |
| 93 | * Default server constructor. | 93 | * Default server constructor. |
| 94 | */ | 94 | */ |
| 95 | private JogreServer () { | 95 | private JogreServer () { |
| 96 | 96 | ||
| 97 | // Take note of start time | 97 | // Take note of start time |
| 98 | startTime = System.currentTimeMillis(); | 98 | startTime = System.currentTimeMillis(); |
| 99 | 99 | ||
| 100 | // Type initial games server info. | 100 | // Type initial games server info. |
| 101 | System.out.println ("------------------------------------------------------------------"); | 101 | System.out.println ("------------------------------------------------------------------"); |
| 102 | System.out.println (" J O G R E G A M E S S E R V E R"); | 102 | System.out.println (" J O G R E G A M E S S E R V E R"); |
| 103 | System.out.println ("------------------------------------------------------------------"); | 103 | System.out.println ("------------------------------------------------------------------"); |
| 104 | System.out.println (labels.get("author") + ":\t\t\tBob Marks"); | 104 | System.out.println (labels.get("author") + ":\t\t\tBob Marks"); |
| 105 | System.out.println (labels.get("version") + ":\t\t" + IJogre.VERSION); | 105 | System.out.println (labels.get("version") + ":\t\t" + IJogre.VERSION); |
| 106 | 106 | ||
| 107 | // Set default port | 107 | // Set default port |
| 108 | serverPort = DEFAULT_SERVER_PORT; | 108 | serverPort = DEFAULT_SERVER_PORT; |
| 109 | 109 | ||
| 110 | // Setup server properties (read "server.xml" file) | 110 | // Setup server properties (read "server.xml" file) |
| 111 | ServerProperties.setUpFromFile(); | 111 | ServerProperties.setUpFromFile(); |
| 112 | 112 | ||
| 113 | System.out.println (labels.get("server.properties") + ":\t" + ServerProperties.getInstance().getServerFile().getAbsolutePath()); | 113 | System.out.println (labels.get("server.properties") + ":\t" + ServerProperties.getInstance().getServerFile().getAbsolutePath()); |
| 114 | 114 | ||
| 115 | // Set default values such as ports | 115 | // Set default values such as ports |
| 116 | setServerPort (ServerProperties.getInstance().getServerPort()); | 116 | setServerPort (ServerProperties.getInstance().getServerPort()); |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | /** | 119 | /** |
| 120 | * Return the single instance of this server. | 120 | * Return the single instance of this server. |
| 121 | * | 121 | * |
| 122 | * @return | 122 | * @return |
| 123 | */ | 123 | */ |
| 124 | public static JogreServer getInstance () { | 124 | public static JogreServer getInstance () { |
| 125 | if (instance == null) | 125 | if (instance == null) |
| 126 | instance = new JogreServer (); | 126 | instance = new JogreServer (); |
| 127 | return instance; | 127 | return instance; |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | /** | 130 | /** |
| 131 | * Initialise the JOGRE server. | 131 | * Initialise the JOGRE server. |
| 132 | * | 132 | * |
| 133 | * @throws ServerDataException | 133 | * @throws ServerDataException |
| 134 | */ | 134 | */ |
| 135 | public void init () { | 135 | public void init () { |
| 136 | // Setup fields | 136 | // Setup fields |
| 137 | this.connections = new ConnectionList (); | 137 | this.connections = new ConnectionList (); |
| 138 | this.serverControllerList = new ServerControllerList (); | 138 | this.serverControllerList = new ServerControllerList (); |
| 139 | this.gameList = new GameList (); | 139 | this.gameList = new GameList (); |
| 140 | this.gameLoader = new GameLoader (gameList, serverControllerList); | 140 | this.gameLoader = new GameLoader (gameList, serverControllerList); |
| 141 | 141 | ||
| 142 | // Load the correct type of data connection | 142 | // Load the correct type of data connection |
| 143 | this.dataConnection = ServerDataFactory.getInstance (); | 143 | this.dataConnection = ServerDataFactory.getInstance (); |
| 144 | 144 | ||
| 145 | // Reset the server snapshot | 145 | // Reset the server snapshot |
| 146 | try { | 146 | try { |
| 147 | // Reset the database snapshot table | 147 | // Reset the database snapshot table |
| 148 | dataConnection.resetSnapshot (gameList.getGameKeys ()); | 148 | dataConnection.resetSnapshot (gameList.getGameKeys ()); |
| 149 | } catch (ServerDataException e) { | 149 | } catch (ServerDataException e) { |
| 150 | // If we get here it shows the database connection is not work so exit program | 150 | // If we get here it shows the database connection is not work so exit program |
| 151 | e.printStackTrace(); | 151 | e.printStackTrace(); |
| 152 | System.exit(-1); | 152 | System.exit(-1); |
| 153 | } | 153 | } |
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | /** | 156 | /** |
| 157 | * <p>This method parses the commands handed in from the command prompt. | 157 | * <p>This method parses the commands handed in from the command prompt. |
| 158 | * For example:</p> | 158 | * For example:</p> |
| 159 | * | 159 | * |
| 160 | * <code>-port=1234</code> | 160 | * <code>-port=1234</code> |
| 161 | * | 161 | * |
| 162 | * <p>This sets the port for the server to listen on to 1234. More complex | 162 | * <p>This sets the port for the server to listen on to 1234. More complex |
| 163 | * servers should over write this method and handle its specific commands | 163 | * servers should over write this method and handle its specific commands |
| 164 | * here.</p> | 164 | * here.</p> |
| 165 | * | 165 | * |
| 166 | * @param args Additional arguments from the command line. | 166 | * @param args Additional arguments from the command line. |
| 167 | */ | 167 | */ |
| 168 | public void parseCommandLineArguments (String [] args) { | 168 | public void parseCommandLineArguments (String [] args) { |
| 169 | if (args != null) { | 169 | if (args != null) { |
| 170 | for (int i = 0; i < args.length; i++) { | 170 | for (int i = 0; i < args.length; i++) { |
| 171 | String argument = args [i]; | 171 | String argument = args [i]; |
| 172 | 172 | ||
| 173 | // Read the port if specified | 173 | // Read the port if specified |
| 174 | if (argument.startsWith("-port=")) { | 174 | if (argument.startsWith("-port=")) { |
| 175 | int pos = argument.indexOf ("="); | 175 | int pos = argument.indexOf ("="); |
| 176 | try { | 176 | try { |
| 177 | int portNum = Integer.parseInt (argument.substring(pos + 1)); | 177 | int portNum = Integer.parseInt (argument.substring(pos + 1)); |
| 178 | setServerPort (portNum); | 178 | setServerPort (portNum); |
| 179 | } catch (NumberFormatException nfEx) { | 179 | } catch (NumberFormatException nfEx) { |
| 180 | usage (); | 180 | usage (); |
| 181 | } | 181 | } |
| 182 | } | 182 | } |
| 183 | // Read the language | 183 | // Read the language |
| 184 | else if (argument.startsWith("-lang=")) { | 184 | else if (argument.startsWith("-lang=")) { |
| 185 | int pos = argument.indexOf ("="); | 185 | int pos = argument.indexOf ("="); |
| 186 | try { | 186 | try { |
| 187 | String lang = argument.substring(pos + 1); | 187 | String lang = argument.substring(pos + 1); |
| 188 | JogreGlobals.setLocale(lang); | 188 | JogreGlobals.setLocale(lang); |
| 189 | } catch (Exception nfEx) { | 189 | } catch (Exception nfEx) { |
| 190 | usage (); | 190 | usage (); |
| 191 | } | 191 | } |
| 192 | } | 192 | } |
| 193 | } | 193 | } |
| 194 | } | 194 | } |
| 195 | 195 | ||
| 196 | // Print port and language | 196 | // Print port and language |
| 197 | Locale l = JogreGlobals.getLocale(); | 197 | Locale l = JogreGlobals.getLocale(); |
| 198 | System.out.println (labels.get("server.port") + ":\t\t" + getServerPort()); | 198 | System.out.println (labels.get("server.port") + ":\t\t" + getServerPort()); |
| 199 | System.out.println (labels.get("language") + ":\t\t" + l.getLanguage() + " (" + | 199 | System.out.println (labels.get("language") + ":\t\t" + l.getLanguage() + " (" + |
| 200 | l.getDisplayLanguage() + ")"); | 200 | l.getDisplayLanguage() + ")"); |
| 201 | } | 201 | } |
| 202 | 202 | ||
| 203 | /** | 203 | /** |
| 204 | * Prints out the usage of the server. | 204 | * Prints out the usage of the server. |
| 205 | */ | 205 | */ |
| 206 | private void usage () { | 206 | private void usage () { |
| 207 | labels = ServerLabels.getInstance(); | 207 | labels = ServerLabels.getInstance(); |
| 208 | System.out.println (labels.get ("jogre.server.version") + ": " + IJogre.VERSION); | 208 | System.out.println (labels.get ("jogre.server.version") + ": " + IJogre.VERSION); |
| 209 | System.out.println ("\n" + labels.get ("usage") + ":"); | 209 | System.out.println ("\n" + labels.get ("usage") + ":"); |
| 210 | System.out.println ("\n\tjava org.jogre.server.JogreServer [" + labels.get("additional.arguments") + "]"); | 210 | System.out.println ("\n\tjava org.jogre.server.JogreServer [" + labels.get("additional.arguments") + "]"); |
| 211 | System.out.println ("\n" + labels.get ("arguments") + ":"); | 211 | System.out.println ("\n" + labels.get ("arguments") + ":"); |
| 212 | System.out.println ("\t-port=x x=" + labels.get ("port.number.default.1790")); | 212 | System.out.println ("\t-port=x x=" + labels.get ("port.number.default.1790")); |
| 213 | System.out.println ("\t-lang=x x=" + JogreGlobals.SUPPORTED_LANGS); | 213 | System.out.println ("\t-lang=x x=" + JogreGlobals.SUPPORTED_LANGS); |
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | /** | 216 | /** |
| 217 | * Run method which runs a new server and listens for clients on the | 217 | * Run method which runs a new server and listens for clients on the |
| 218 | * specified port. | 218 | * specified port. |
| 219 | */ | 219 | */ |
| 220 | public void run () { | 220 | public void run () { |
| 221 | // Output welcome message to the console | 221 | // Output welcome message to the console |
| 222 | System.out.println ("------------------------------------------------------------------"); | 222 | System.out.println ("------------------------------------------------------------------"); |
| 223 | System.out.println (labels.get ("games.being.served") + " (" + gameList.size() + "):"); | 223 | System.out.println (labels.get ("games.being.served") + " (" + gameList.size() + "):"); |
| 224 | 224 | ||
| 225 | // Load the various games | 225 | // Load the various games |
| 226 | System.out.print (gameLoader); | 226 | System.out.print (gameLoader); |
| 227 | 227 | ||
| 228 | // End of welcome message | 228 | // End of welcome message |
| 229 | System.out.println ("------------------------------------------------------------------"); | 229 | System.out.println ("------------------------------------------------------------------"); |
| 230 | 230 | ||
| 231 | // Declare server socket to listen for client connections | 231 | // Declare server socket to listen for client connections |
| 232 | ServerSocket listenSocket = null; | 232 | ServerSocket listenSocket = null; |
| 233 | 233 | ||
| 234 | try { | 234 | try { |
| 235 | // Set up the server first of all | 235 | // Set up the server first of all |
| 236 | listenSocket = new ServerSocket (serverPort); | 236 | listenSocket = new ServerSocket (serverPort); |
| 237 | 237 | ||
| 238 | System.out.println (labels.get("jogre.games.server.listening.on.port") + ": " + serverPort); | 238 | System.out.println (labels.get("jogre.games.server.listening.on.port") + ": " + serverPort); |
| 239 | 239 | ||
| 240 | // Show time server has started | 240 | // Show time server has started |
| 241 | NumberFormat nf = NumberFormat.getInstance(); | 241 | NumberFormat nf = NumberFormat.getInstance(); |
| 242 | nf.setGroupingUsed(true); | 242 | nf.setGroupingUsed(true); |
| 243 | long timeStarted = System.currentTimeMillis() - startTime; | 243 | long timeStarted = System.currentTimeMillis() - startTime; |
| 244 | System.out.println ("\n" + labels.get("started.in", new String [] {nf.format(timeStarted)})); | 244 | System.out.println ("\n" + labels.get("started.in", new String [] {nf.format(timeStarted)})); |
| 245 | 245 | ||
| 246 | while (true) { | 246 | while (true) { |
| 247 | // listen for and accept the connection | 247 | // listen for and accept the connection |
| 248 | Socket clientSocket = listenSocket.accept (); | 248 | Socket clientSocket = listenSocket.accept (); |
| 249 | 249 | ||
| 250 | // Try to connect client to this server | 250 | // Try to connect client to this server |
| 251 | ServerConnectionThread conn = new ServerConnectionThread (clientSocket); | 251 | ServerConnectionThread conn = new ServerConnectionThread (clientSocket); |
| 252 | 252 | ||
| 253 | conn.start(); | 253 | conn.getMessageBus().open(conn); |
| 254 | } | 254 | } |
| 255 | } | 255 | } |
| 256 | catch (BindException bindEx) { | 256 | catch (BindException bindEx) { |
| 257 | System.out.println (labels.get("jogre.server.already.running.on.port") + ": " + serverPort); | 257 | System.out.println (labels.get("jogre.server.already.running.on.port") + ": " + serverPort); |
| 258 | System.exit (0); | 258 | System.exit (0); |
| 259 | } | 259 | } |
| 260 | catch (Exception genEx) { | 260 | catch (Exception genEx) { |
| 261 | genEx.printStackTrace(); | 261 | genEx.printStackTrace(); |
| 262 | } | 262 | } |
| 263 | 263 | ||
| 264 | // Close the server down again | 264 | // Close the server down again |
| 265 | try { | 265 | try { |
| 266 | System.out.println (labels.get("jogre.games.server.closing") + ": " + serverPort); | 266 | System.out.println (labels.get("jogre.games.server.closing") + ": " + serverPort); |
| 267 | 267 | ||
| 268 | if (listenSocket != null) | 268 | if (listenSocket != null) |
| 269 | listenSocket.close(); | 269 | listenSocket.close(); |
| 270 | } | 270 | } |
| 271 | catch (IOException ioEx) {} | 271 | catch (IOException ioEx) {} |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | /** | 274 | /** |
| 275 | * Return the current list of games on this server. | 275 | * Return the current list of games on this server. |
| 276 | * | 276 | * |
| 277 | * @return | 277 | * @return |
| 278 | */ | 278 | */ |
| 279 | public GameList getGameList () { | 279 | public GameList getGameList () { |
| 280 | return gameList; | 280 | return gameList; |
| 281 | } | 281 | } |
| 282 | 282 | ||
| 283 | /** | 283 | /** |
| 284 | * Set the server port. | 284 | * Set the server port. |
| 285 | * | 285 | * |
| 286 | * @param serverPort | 286 | * @param serverPort |
| 287 | */ | 287 | */ |
| 288 | public void setServerPort (int serverPort) { | 288 | public void setServerPort (int serverPort) { |
| 289 | this.serverPort = serverPort; | 289 | this.serverPort = serverPort; |
| 290 | } | 290 | } |
| 291 | 291 | ||
| 292 | /** | 292 | /** |
| 293 | * Return the server port. | 293 | * Return the server port. |
| 294 | * | 294 | * |
| 295 | * @return | 295 | * @return |
| 296 | */ | 296 | */ |
| 297 | public int getServerPort () { | 297 | public int getServerPort () { |
| 298 | return serverPort; | 298 | return serverPort; |
| 299 | } | 299 | } |
| 300 | 300 | ||
| 301 | /** | 301 | /** |
| 302 | * Set the link to the master server. | 302 | * Set the link to the master server. |
| 303 | * | 303 | * |
| 304 | * @param linkToMasterServer | 304 | * @param linkToMasterServer |
| 305 | */ | 305 | */ |
| 306 | public void setLinkToMasterServer (boolean linkToMasterServer) { | 306 | public void setLinkToMasterServer (boolean linkToMasterServer) { |
| 307 | this.linkToMasterServer = linkToMasterServer; | 307 | this.linkToMasterServer = linkToMasterServer; |
| 308 | } | 308 | } |
| 309 | 309 | ||
| 310 | /** | 310 | /** |
| 311 | * Return true if this server wishes to try and link to the | 311 | * Return true if this server wishes to try and link to the |
| 312 | * master JOGRE game server. | 312 | * master JOGRE game server. |
| 313 | * | 313 | * |
| 314 | * @return | 314 | * @return |
| 315 | */ | 315 | */ |
| 316 | public boolean isLinkToMasterServer () { | 316 | public boolean isLinkToMasterServer () { |
| 317 | return linkToMasterServer; | 317 | return linkToMasterServer; |
| 318 | } | 318 | } |
| 319 | 319 | ||
| 320 | /** | 320 | /** |
| 321 | * Return the ConnectionList object of connections to the server. | 321 | * Return the ConnectionList object of connections to the server. |
| 322 | * | 322 | * |
| 323 | * @return | 323 | * @return |
| 324 | */ | 324 | */ |
| 325 | public ConnectionList getConnections () { | 325 | public ConnectionList getConnections () { |
| 326 | return connections; | 326 | return connections; |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | /** | 329 | /** |
| 330 | * Return the server parser list which includes the standard base, game | 330 | * Return the server parser list which includes the standard base, game |
| 331 | * and table parser. Also includes parsers for each specific game. | 331 | * and table parser. Also includes parsers for each specific game. |
| 332 | * | 332 | * |
| 333 | * @return List of server parsers | 333 | * @return List of server parsers |
| 334 | */ | 334 | */ |
| 335 | public ServerControllerList getControllers () { | 335 | public ServerControllerList getControllers () { |
| 336 | return serverControllerList; | 336 | return serverControllerList; |
| 337 | } | 337 | } |
| 338 | 338 | ||
| 339 | /** | 339 | /** |
| 340 | * Return the connection to the users. | 340 | * Return the connection to the users. |
| 341 | * | 341 | * |
| 342 | * @return | 342 | * @return |
| 343 | */ | 343 | */ |
| 344 | public IServerData getServerData () { | 344 | public IServerData getServerData () { |
| 345 | return dataConnection; | 345 | return dataConnection; |
| 346 | } | 346 | } |
| 347 | 347 | ||
| 348 | /** | 348 | /** |
| 349 | * Return the game loader. | 349 | * Return the game loader. |
| 350 | * | 350 | * |
| 351 | * @return | 351 | * @return |
| 352 | */ | 352 | */ |
| 353 | public GameLoader getGameLoader() { | 353 | public GameLoader getGameLoader() { |
| 354 | return gameLoader; | 354 | return gameLoader; |
| 355 | } | 355 | } |
| 356 | 356 | ||
| 357 | /** | 357 | /** |
| 358 | * Perform some basic tests before server starts up properly e.g. check the server | 358 | * Perform some basic tests before server starts up properly e.g. check the server |
| 359 | * port | 359 | * port |
| 360 | */ | 360 | */ |
| 361 | public void initialTests () { | 361 | public void initialTests () { |
| 362 | ServerProperties serverProps = ServerProperties.getInstance(); | 362 | ServerProperties serverProps = ServerProperties.getInstance(); |
| 363 | String dataType = serverProps.getCurrentServerData(); | 363 | String dataType = serverProps.getCurrentServerData(); |
| 364 | System.out.println (labels.get("persistent.data") + ":\t" + dataType); | 364 | System.out.println (labels.get("persistent.data") + ":\t" + dataType); |
| 365 | if (dataType.equals (IServerData.DATABASE)) { | 365 | if (dataType.equals (IServerData.DATABASE)) { |
| 366 | String curDatabaseConn = serverProps.getCurrentDatabaseConnection(); | 366 | String curDatabaseConn = serverProps.getCurrentDatabaseConnection(); |
| 367 | System.out.println (labels.get("database.connection") + ":\t" + curDatabaseConn); | 367 | System.out.println (labels.get("database.connection") + ":\t" + curDatabaseConn); |
| 368 | System.out.println (labels.get("database.url") + ":\t\t" + serverProps.getConnectionURL(curDatabaseConn)); | 368 | System.out.println (labels.get("database.url") + ":\t\t" + serverProps.getConnectionURL(curDatabaseConn)); |
| 369 | System.out.println (labels.get("database.driver") + ":\t" + serverProps.getConnectionDriver(curDatabaseConn)); | 369 | System.out.println (labels.get("database.driver") + ":\t" + serverProps.getConnectionDriver(curDatabaseConn)); |
| 370 | } | 370 | } |
| 371 | 371 | ||
| 372 | // TEST 1: Ensure port is available | 372 | // TEST 1: Ensure port is available |
| 373 | try { | 373 | try { |
| 374 | System.out.println ("------------------------------------------------------------------"); | 374 | System.out.println ("------------------------------------------------------------------"); |
| 375 | System.out.println (labels.get("inital.tests") + ":"); | 375 | System.out.println (labels.get("inital.tests") + ":"); |
| 376 | ServerSocket testSocket = new ServerSocket (serverPort); | 376 | ServerSocket testSocket = new ServerSocket (serverPort); |
| 377 | System.out.println ("\t" + labels.get("server.port.is.available")); | 377 | System.out.println ("\t" + labels.get("server.port.is.available")); |
| 378 | testSocket.close(); | 378 | testSocket.close(); |
| 379 | } | 379 | } |
| 380 | catch (BindException bindEx) { | 380 | catch (BindException bindEx) { |
| 381 | System.out.println (labels.get("jogre.server.already.running.on.port") + ": " + serverPort); | 381 | System.out.println (labels.get("jogre.server.already.running.on.port") + ": " + serverPort); |
| 382 | System.exit (-1); | 382 | System.exit (-1); |
| 383 | } catch (IOException e) { | 383 | } catch (IOException e) { |
| 384 | e.printStackTrace(); | 384 | e.printStackTrace(); |
| 385 | } | 385 | } |
| 386 | 386 | ||
| 387 | // TEST 2: Test if using database that connection exists | 387 | // TEST 2: Test if using database that connection exists |
| 388 | if (dataType.equals(IServerData.DATABASE)) { | 388 | if (dataType.equals(IServerData.DATABASE)) { |
| 389 | int error = IError.NO_ERROR; | 389 | int error = IError.NO_ERROR; |
| 390 | 390 | ||
| 391 | error = DBConnection.testConnection(serverProps.getDBDriver(), | 391 | error = DBConnection.testConnection(serverProps.getDBDriver(), |
| 392 | serverProps.getDBConnURL(), | 392 | serverProps.getDBConnURL(), |
| 393 | serverProps.getDBUsername(), | 393 | serverProps.getDBUsername(), |
| 394 | serverProps.getDBPassword(), | 394 | serverProps.getDBPassword(), |
| 395 | true); | 395 | true); |
| 396 | // Check error code | 396 | // Check error code |
| 397 | if (error != IError.NO_ERROR) { | 397 | if (error != IError.NO_ERROR) { |
| 398 | System.out.println (JogreLabels.getError(error) + " " + serverProps.getDBConnURL()); | 398 | System.out.println (JogreLabels.getError(error) + " " + serverProps.getDBConnURL()); |
| 399 | System.exit (-1); | 399 | System.exit (-1); |
| 400 | } | 400 | } |
| 401 | } | 401 | } |
| 402 | } | 402 | } |
| 403 | 403 | ||
| 404 | /** | 404 | /** |
| 405 | * Main method which creates a single instance of the server, | 405 | * Main method which creates a single instance of the server, |
| 406 | * parses the commandline arguments and then runs the server. | 406 | * parses the commandline arguments and then runs the server. |
| 407 | * | 407 | * |
| 408 | * @param args Additional arguments from command line. | 408 | * @param args Additional arguments from command line. |
| 409 | */ | 409 | */ |
| 410 | public static void main (String [] args) { | 410 | public static void main (String [] args) { |
| 411 | // Initialise the server | 411 | // Initialise the server |
| 412 | JogreServer server = getInstance (); | 412 | JogreServer server = getInstance (); |
| 413 | 413 | ||
| 414 | // Parse the command line arguments | 414 | // Parse the command line arguments |
| 415 | server.parseCommandLineArguments (args); | 415 | server.parseCommandLineArguments (args); |
| 416 | 416 | ||
| 417 | // Perform basic tests to ensure server can run | 417 | // Perform basic tests to ensure server can run |
| 418 | server.initialTests (); | 418 | server.initialTests (); |
| 419 | 419 | ||
| 420 | // Setup server and run the server | 420 | // Setup server and run the server |
| 421 | server.init(); | 421 | server.init(); |
| 422 | server.run (); | 422 | server.run (); |
| 423 | } | 423 | } |
| 424 | } | 424 | } |