| /trunk/server/src/org/jogre/server/ServerConnectionThread.java r2 | /trunk/server/src/org/jogre/server/ServerConnectionThread.java r16 | ||
| 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.net.Socket; | 22 | import java.net.Socket; |
| 23 | import java.util.Vector; | 23 | import java.util.Vector; |
| 24 | 24 | ||
| 25 | import nanoxml.XMLElement; | 25 | import nanoxml.XMLElement; |
| 26 | 26 | ||
| 27 | import org.jogre.common.AbstractConnectionThread; | 27 | import org.jogre.common.AbstractConnectionThread; |
| 28 | import org.jogre.common.Game; | 28 | import org.jogre.common.Game; |
| 29 | import org.jogre.common.IJogre; | 29 | import org.jogre.common.IJogre; |
| 30 | import org.jogre.common.Player; | 30 | import org.jogre.common.Player; |
| 31 | import org.jogre.common.PlayerList; | 31 | import org.jogre.common.PlayerList; |
| 32 | import org.jogre.common.SocketBasedMessageBus; | ||
| 32 | import org.jogre.common.Table; | 33 | import org.jogre.common.Table; |
| 33 | import org.jogre.common.TableList; | 34 | import org.jogre.common.TableList; |
| 34 | import org.jogre.common.TransmissionException; | 35 | import org.jogre.common.TransmissionException; |
| 35 | import org.jogre.common.UserList; | 36 | import org.jogre.common.UserList; |
| 36 | import org.jogre.common.comm.CommAdminMessage; | 37 | import org.jogre.common.comm.CommAdminMessage; |
| 37 | import org.jogre.common.comm.CommAdminGameMessage; | 38 | import org.jogre.common.comm.CommAdminGameMessage; |
| 38 | import org.jogre.common.comm.CommDisconnect; | 39 | import org.jogre.common.comm.CommDisconnect; |
| 39 | import org.jogre.common.comm.CommTableMessage; | 40 | import org.jogre.common.comm.CommTableMessage; |
| 40 | import org.jogre.common.comm.ITransmittable; | 41 | import org.jogre.common.comm.ITransmittable; |
| 41 | import org.jogre.common.playerstate.PlayerStateGameStarted; | 42 | import org.jogre.common.playerstate.PlayerStateGameStarted; |
| 42 | import org.jogre.common.util.JogreLogger; | 43 | import org.jogre.common.util.JogreLogger; |
| 43 | import org.jogre.server.controllers.ServerControllerList; | 44 | import org.jogre.server.controllers.ServerControllerList; |
| 44 | import org.jogre.server.data.ServerDataException; | 45 | import org.jogre.server.data.ServerDataException; |
| 45 | 46 | ||
| 46 | /** | 47 | /** |
| 47 | * <p>Every time a client connections to a JogreServer this class (which extends | 48 | * <p>Every time a client connections to a JogreServer this class (which extends |
| 48 | * Thread) is run. This ensures that a JogreServer is multi-threaded.</p> | 49 | * Thread) is run. This ensures that a JogreServer is multi-threaded.</p> |
| 49 | * | 50 | * |
| 50 | * @author Bob Marks | 51 | * @author Bob Marks |
| 51 | * @version Beta 0.3 | 52 | * @version Beta 0.3 |
| 52 | * @see org.jogre.server.JogreServer | 53 | * @see org.jogre.server.JogreServer |
| 53 | */ | 54 | */ |
| 54 | public class ServerConnectionThread extends AbstractConnectionThread | 55 | public class ServerConnectionThread extends AbstractConnectionThread |
| 55 | implements IJogre { | 56 | implements IJogre { |
| 56 | 57 | ||
| 57 | /** Logging */ | 58 | /** Logging */ |
| 58 | JogreLogger logger = new JogreLogger (this.getClass()); | 59 | JogreLogger logger = new JogreLogger (this.getClass()); |
| 59 | 60 | ||
| 60 | /** link to the JogreServer */ | 61 | /** link to the JogreServer */ |
| 61 | protected JogreServer server; | 62 | protected JogreServer server; |
| 62 | 63 | ||
| 63 | /** Link to the various parsers. */ | 64 | /** Link to the various parsers. */ |
| 64 | private ServerControllerList controllers = null; | 65 | private ServerControllerList controllers = null; |
| 65 | 66 | ||
| 66 | /** Game ID which is set to a particular game, master server or admin. */ | 67 | /** Game ID which is set to a particular game, master server or admin. */ |
| 67 | private String gameID = null; | 68 | private String gameID = null; |
| 68 | 69 | ||
| 69 | /** Convience link to the userlist. */ | 70 | /** Convience link to the userlist. */ |
| 70 | private UserList userList = null; | 71 | private UserList userList = null; |
| 71 | 72 | ||
| 72 | /** Convience link to the tablelist. */ | 73 | /** Convience link to the tablelist. */ |
| 73 | private TableList tableList = null; | 74 | private TableList tableList = null; |
| 74 | 75 | ||
| 75 | /** Convience link to the game. */ | 76 | /** Convience link to the game. */ |
| 76 | private Game game = null; | 77 | private Game game = null; |
| 77 | 78 | ||
| 78 | /** Connection list. */ | 79 | /** Connection list. */ |
| 79 | private ConnectionList connections = null; | 80 | private ConnectionList connections = null; |
| 80 | 81 | ||
| 81 | private boolean isAdministrator = false; | 82 | private boolean isAdministrator = false; |
| 82 | 83 | ||
| 83 | /** | 84 | /** |
| 84 | * Constructor which takes a socket connection and a link to the server. | 85 | * Constructor which takes a socket connection and a link to the server. |
| 85 | * | 86 | * |
| 86 | * @param clientSocket Client connection. | 87 | * @param clientSocket Client connection. |
| 87 | */ | 88 | */ |
| 88 | public ServerConnectionThread (Socket clientSocket) { | 89 | public ServerConnectionThread (Socket clientSocket) { |
| 89 | super (clientSocket); | 90 | super (new SocketBasedMessageBus(clientSocket)); |
| 90 | 91 | ||
| 91 | // Get link to the server (singleton) and the various parsers | 92 | // Get link to the server (singleton) and the various parsers |
| 92 | this.server = JogreServer.getInstance(); | 93 | this.server = JogreServer.getInstance(); |
| 93 | this.controllers = server.getControllers(); | 94 | this.controllers = server.getControllers(); |
| 94 | 95 | ||
| 95 | // and also set up links to server objects which are often used. | 96 | // and also set up links to server objects which are often used. |
| 96 | this.connections = server.getConnections(); // link to connections | 97 | this.connections = server.getConnections(); // link to connections |
| 97 | } | 98 | } |
| 98 | 99 | ||
| 99 | /** | 100 | /** |
| 100 | * Initilise this server connection thread with a username and a | 101 | * Initilise this server connection thread with a username and a |
| 101 | * game key. | 102 | * game key. |
| 102 | * | 103 | * |
| 103 | * @param username Username of the client connecting to this thread. | 104 | * @param username Username of the client connecting to this thread. |
| 104 | * @param gameID Gamekey of the client connecting. | 105 | * @param gameID Gamekey of the client connecting. |
| 105 | */ | 106 | */ |
| 106 | public void init (String username, String gameID) { | 107 | public void init (String username, String gameID) { |
| 107 | this.username = username; | 108 | setUsername(username); |
| 108 | this.gameID = gameID; | 109 | this.gameID = gameID; |
| 109 | 110 | ||
| 110 | // Set convience fields | 111 | // Set convience fields |
| 111 | this.game = server.getGameList().getGame (gameID); | 112 | this.game = server.getGameList().getGame (gameID); |
| 112 | this.userList = game.getUserList(); // link to userlist | 113 | this.userList = game.getUserList(); // link to userlist |
| 113 | this.tableList = game.getTableList(); // link to table list | 114 | this.tableList = game.getTableList(); // link to table list |
| 114 | } | 115 | } |
| 115 | 116 | ||
| 116 | /** | 117 | /** |
| 117 | * Initilise this server connection thread for an administrator user. | 118 | * Initilise this server connection thread for an administrator user. |
| 118 | * | 119 | * |
| 119 | * @param admin_username Administrator username. | 120 | * @param admin_username Administrator username. |
| 120 | */ | 121 | */ |
| 121 | public void init (String admin_username) { | 122 | public void init (String admin_username) { |
| 122 | this.username = admin_username; | 123 | setUsername(admin_username); |
| 123 | this.gameID = ADMINISTRATOR; | 124 | this.gameID = ADMINISTRATOR; |
| 124 | this.isAdministrator = true; | 125 | this.isAdministrator = true; |
| 125 | } | 126 | } |
| 126 | 127 | ||
| 127 | /** | 128 | /** |
| 128 | * Implementation of the parse method on the server side. This method | 129 | * Implementation of the parse method on the server side. This method |
| 129 | * recieves XML communication from a client. | 130 | * recieves XML communication from a client. |
| 130 | * | 131 | * |
| 131 | * @see org.jogre.common.AbstractConnectionThread#parse (XMLElement element) | 132 | * @see org.jogre.common.AbstractConnectionThread#parse (XMLElement element) |
| 132 | */ | 133 | */ |
| 133 | public void parse (XMLElement message) throws TransmissionException { | 134 | public void parse (XMLElement message) throws TransmissionException { |
| 134 | if (message != null) { | 135 | if (message != null) { |
| 135 | // Retrieve table number | 136 | // Retrieve table number |
| 136 | String sTableNum = message.getStringAttribute (CommTableMessage.XML_ATT_TABLE_NUM); | 137 | String sTableNum = message.getStringAttribute (CommTableMessage.XML_ATT_TABLE_NUM); |
| 137 | 138 | ||
| 138 | // If message contains a "table" attribute then delegate to a table message | 139 | // If message contains a "table" attribute then delegate to a table message |
| 139 | if (sTableNum == null) { | 140 | if (sTableNum == null) { |
| 140 | // Parse game message | 141 | // Parse game message |
| 141 | controllers.getGameController().parseGameMessage (this, message); | 142 | controllers.getGameController().parseGameMessage (this, message); |
| 142 | } | 143 | } |
| 143 | else { | 144 | else { |
| 144 | int tableNum = Integer.parseInt (sTableNum); | 145 | int tableNum = Integer.parseInt (sTableNum); |
| 145 | controllers.getTableController().parseTableMessage (this, message, tableNum); | 146 | controllers.getTableController().parseTableMessage (this, message, tableNum); |
| 146 | } | 147 | } |
| 147 | 148 | ||
| 148 | // Send a copy to the administrator thread if it is listening. | 149 | // Send a copy to the administrator thread if it is listening. |
| 149 | sendGameMessageToAdmin (message, true); | 150 | sendGameMessageToAdmin (message, true); |
| 150 | } | 151 | } |
| 151 | } | 152 | } |
| 152 | 153 | ||
| 153 | /** | 154 | /** |
| 154 | * Send a ITransmittable object to the output stream (could be server or | 155 | * Send a ITransmittable object to the output stream (could be server or |
| 155 | * client). | 156 | * client). |
| 156 | * | 157 | * |
| 157 | * @param transObject | 158 | * @param transObject |
| 158 | * @param sendCopyToAdmin If this is true then a copy is sent to the administrator. | 159 | * @param sendCopyToAdmin If this is true then a copy is sent to the administrator. |
| 159 | */ | 160 | */ |
| 160 | protected void send (ITransmittable transObject, boolean sendCopyToAdmin) { | 161 | protected void send (ITransmittable transObject, boolean sendCopyToAdmin) { |
| 161 | super.send (transObject); | 162 | getMessageBus().send (transObject); |
| 162 | 163 | ||
| 163 | // Send to administrator if the administrator is listening. | 164 | // Send to administrator if the administrator is listening. |
| 164 | if (sendCopyToAdmin) | 165 | if (sendCopyToAdmin) |
| 165 | sendGameMessageToAdmin (transObject.flatten(), false); | 166 | sendGameMessageToAdmin (transObject.flatten(), false); |
| 166 | } | 167 | } |
| 167 | 168 | ||
| 168 | /** | 169 | /** |
| 169 | * Send game message to administration connection. The game message will | 170 | * Send game message to administration connection. The game message will |
| 170 | * be wrapped in in a CommAdminGameMessage. | 171 | * be wrapped in in a CommAdminGameMessage. |
| 171 | * | 172 | * |
| 172 | * @param message Game message as an XML message. | 173 | * @param message Game message as an XML message. |
| 173 | * @param isReceivingMessage If true then going to | 174 | * @param isReceivingMessage If true then going to |
| 174 | */ | 175 | */ |
| 175 | public void sendGameMessageToAdmin (XMLElement message, boolean isReceivingMessage) { | 176 | public void sendGameMessageToAdmin (XMLElement message, boolean isReceivingMessage) { |
| 176 | // Wrap the message inside an <admin_message> which details | 177 | // Wrap the message inside an <admin_message> which details |
| 177 | // additional info about the message such as which user thread has sent it | 178 | // additional info about the message such as which user thread has sent it |
| 178 | // and what game. | 179 | // and what game. |
| 179 | ServerConnectionThread conn = connections.getAdminConnection(); | 180 | ServerConnectionThread conn = connections.getAdminConnection(); |
| 180 | 181 | ||
| 181 | if (conn != null) { // ensure we're not sending same message twice | 182 | if (conn != null) { // ensure we're not sending same message twice |
| 182 | CommAdminGameMessage adminMessage = new CommAdminGameMessage (isReceivingMessage, gameID, username, message); | 183 | CommAdminGameMessage adminMessage = new CommAdminGameMessage (isReceivingMessage, gameID, getUsername(), message); |
| 183 | conn.send (adminMessage, false); | 184 | conn.send (adminMessage, false); |
| 184 | } | 185 | } |
| 185 | } | 186 | } |
| 186 | 187 | ||
| 187 | /** | 188 | /** |
| 188 | * Send data update message to administration connection. The update message | 189 | * Send data update message to administration connection. The update message |
| 189 | * will be wrapped in a CommAdminDataMessage object. | 190 | * will be wrapped in a CommAdminDataMessage object. |
| 190 | * | 191 | * |
| 191 | * @param message | 192 | * @param message |
| 192 | * @param isReceivingMessage | 193 | * @param isReceivingMessage |
| 193 | */ | 194 | */ |
| 194 | public void sendDataMessageToAdmin (ITransmittable message) { | 195 | public void sendDataMessageToAdmin (ITransmittable message) { |
| 195 | ServerConnectionThread conn = connections.getAdminConnection(); | 196 | ServerConnectionThread conn = connections.getAdminConnection(); |
| 196 | 197 | ||
| 197 | if (conn != null) { // ensure we're not sending same message twice | 198 | if (conn != null) { // ensure we're not sending same message twice |
| 198 | CommAdminMessage adminMessage = new CommAdminMessage (gameID, username, message.flatten()); | 199 | CommAdminMessage adminMessage = new CommAdminMessage (gameID, getUsername(), message.flatten()); |
| 199 | conn.send (adminMessage, false); | 200 | conn.send (adminMessage, false); |
| 200 | } | 201 | } |
| 201 | } | 202 | } |
| 202 | 203 | ||
| 203 | /** | 204 | /** |
| 204 | * Convience method to send a message - assumes that message is also being | 205 | * Convience method to send a message - assumes that message is also being |
| 205 | * sent to the admin client. | 206 | * sent to the admin client. |
| 206 | * | 207 | * |
| 207 | * @see org.jogre.common.AbstractConnectionThread#send(org.jogre.common.comm.ITransmittable) | 208 | * @see org.jogre.common.AbstractConnectionThread#send(org.jogre.common.comm.ITransmittable) |
| 208 | */ | 209 | */ |
| 209 | public void send (ITransmittable transObject) { | 210 | public void send (ITransmittable transObject) { |
| 210 | send (transObject, true); | 211 | send (transObject, true); |
| 211 | } | 212 | } |
| 212 | 213 | ||
| 213 | /** | 214 | /** |
| 214 | * Return the game object currently tied to this connection thread.. | 215 | * Return the game object currently tied to this connection thread.. |
| 215 | * | 216 | * |
| 216 | * @return Game object (which contains the userlist and table list). | 217 | * @return Game object (which contains the userlist and table list). |
| 217 | */ | 218 | */ |
| 218 | public Game getGame () { | 219 | public Game getGame () { |
| 219 | return game; | 220 | return game; |
| 220 | } | 221 | } |
| 221 | 222 | ||
| 222 | /** | 223 | /** |
| 223 | * Return the gameID. | 224 | * Return the gameID. |
| 224 | * | 225 | * |
| 225 | * @return Game ID (e.g. chess-0.2). | 226 | * @return Game ID (e.g. chess-0.2). |
| 226 | */ | 227 | */ |
| 227 | public String getGameID () { | 228 | public String getGameID () { |
| 228 | return this.gameID; | 229 | return this.gameID; |
| 229 | } | 230 | } |
| 230 | 231 | ||
| 231 | /** | 232 | /** |
| 232 | * Return the user list for this game. | 233 | * Return the user list for this game. |
| 233 | * | 234 | * |
| 234 | * @return Userlist. | 235 | * @return Userlist. |
| 235 | */ | 236 | */ |
| 236 | public UserList getUserList () { | 237 | public UserList getUserList () { |
| 237 | return this.userList; | 238 | return this.userList; |
| 238 | } | 239 | } |
| 239 | 240 | ||
| 240 | /** | 241 | /** |
| 241 | * Return the table list. | 242 | * Return the table list. |
| 242 | * | 243 | * |
| 243 | * @return | 244 | * @return |
| 244 | */ | 245 | */ |
| 245 | public TableList getTableList () { | 246 | public TableList getTableList () { |
| 246 | return this.tableList; | 247 | return this.tableList; |
| 247 | } | 248 | } |
| 248 | 249 | ||
| 249 | /** | 250 | /** |
| 250 | * Return true/false if this user is administrator. | 251 | * Return true/false if this user is administrator. |
| 251 | * | 252 | * |
| 252 | * @return True if user is an administrator. | 253 | * @return True if user is an administrator. |
| 253 | */ | 254 | */ |
| 254 | public boolean isAdministrator () { | 255 | public boolean isAdministrator () { |
| 255 | return this.isAdministrator; | 256 | return this.isAdministrator; |
| 256 | } | 257 | } |
| 257 | 258 | ||
| 258 | /** | 259 | /** |
| 259 | * Client has exitted so clean everything up. | 260 | * Client has exitted so clean everything up. |
| 260 | * | 261 | * |
| 261 | * @see org.jogre.common.AbstractConnectionThread#cleanup() | 262 | * @see org.jogre.common.AbstractConnectionThread#cleanup() |
| 262 | */ | 263 | */ |
| 263 | public void cleanup () { | 264 | public void cleanup () { |
| 264 | logger.debug ("cleanup", "Cleaning up user details"); | 265 | logger.debug ("cleanup", "Cleaning up user details"); |
| 265 | 266 | ||
| 266 | // Remove connection from the connection list (game & admin users) | 267 | // Remove connection from the connection list (game & admin users) |
| 267 | if (isAdministrator ()) | 268 | if (isAdministrator ()) |
| 268 | server.getConnections().removeAdminConnection (); | 269 | server.getConnections().removeAdminConnection (); |
| 269 | else | 270 | else |
| 270 | server.getConnections().removeConnection (gameID, username); | 271 | server.getConnections().removeConnection (gameID, getUsername()); |
| 271 | 272 | ||
| 272 | // Game users only | 273 | // Game users only |
| 273 | if (userList != null && tableList != null) { | 274 | if (userList != null && tableList != null) { |
| 274 | logger.debug ("cleanup", "remove user"); | 275 | logger.debug ("cleanup", "remove user"); |
| 275 | userList.removeUser (username); | 276 | userList.removeUser (getUsername()); |
| 276 | 277 | ||
| 277 | // remove any instance of the user from the table list | 278 | // remove any instance of the user from the table list |
| 278 | logger.debug ("cleanup", "removing table"); | 279 | logger.debug ("cleanup", "removing table"); |
| 279 | tableList.removeUserFromTables (username); | 280 | tableList.removeUserFromTables (getUsername()); |
| 280 | 281 | ||
| 281 | // Inform all connected clients that user has disconnected from game | 282 | // Inform all connected clients that user has disconnected from game |
| 282 | CommDisconnect commDisconnect = new CommDisconnect (username); | 283 | CommDisconnect commDisconnect = new CommDisconnect (getUsername()); |
| 283 | broadcast (commDisconnect); | 284 | broadcast (commDisconnect); |
| 284 | sendDataMessageToAdmin (commDisconnect); | 285 | sendDataMessageToAdmin (commDisconnect); |
| 285 | 286 | ||
| 286 | // Log message | 287 | // Log message |
| 287 | logger.log ("Jogre Games Server: client [" + username + "] has logged off"); | 288 | logger.log ("Jogre Games Server: client [" + getUsername() + "] has logged off"); |
| 288 | 289 | ||
| 289 | // Update snapshot | 290 | // Update snapshot |
| 290 | try { | 291 | try { |
| 291 | server.getServerData().updateSnapshot ( | 292 | server.getServerData().updateSnapshot ( |
| 292 | getGameID(), getUserList().size(), getTableList().size()); | 293 | getGameID(), getUserList().size(), getTableList().size()); |
| 293 | } catch (ServerDataException e) { | 294 | } catch (ServerDataException e) { |
| 294 | e.printStackTrace(); // proper logging at some stage | 295 | e.printStackTrace(); // proper logging at some stage |
| 295 | } | 296 | } |
| 296 | } | 297 | } |
| 297 | } | 298 | } |
| 298 | 299 | ||
| 299 | /** | 300 | /** |
| 300 | * Transmit a message to the specified table and specified player. | 301 | * Transmit a message to the specified table and specified player. |
| 301 | * | 302 | * |
| 302 | * @param usernameTo Username to transmit to | 303 | * @param usernameTo Username to transmit to |
| 303 | * @param tableNum Table number to transmit to | 304 | * @param tableNum Table number to transmit to |
| 304 | * @param transObject Transmittable object | 305 | * @param transObject Transmittable object |
| 305 | */ | 306 | */ |
| 306 | public void transmitToTablePlayer (String usernameTo, int tableNum, ITransmittable transObject) { | 307 | public void transmitToTablePlayer (String usernameTo, int tableNum, ITransmittable transObject) { |
| 307 | if (transObject instanceof CommTableMessage) | 308 | if (transObject instanceof CommTableMessage) |
| 308 | ((CommTableMessage)transObject).setTableNum (tableNum); | 309 | ((CommTableMessage)transObject).setTableNum (tableNum); |
| 309 | 310 | ||
| 310 | // Insure that that user is actually in this table | 311 | // Insure that that user is actually in this table |
| 311 | Table table = tableList.getTable(tableNum); | 312 | Table table = tableList.getTable(tableNum); |
| 312 | if (table != null) { | 313 | if (table != null) { |
| 313 | PlayerList playerList = table.getPlayerList(); | 314 | PlayerList playerList = table.getPlayerList(); |
| 314 | Player player = playerList.getPlayer(usernameTo); | 315 | Player player = playerList.getPlayer(usernameTo); |
| 315 | if (player != null) | 316 | if (player != null) |
| 316 | transmit (usernameTo, transObject); | 317 | transmit (usernameTo, transObject); |
| 317 | } | 318 | } |
| 318 | } | 319 | } |
| 319 | 320 | ||
| 320 | /** | 321 | /** |
| 321 | * Transmit a message to the specified table. | 322 | * Transmit a message to the specified table. |
| 322 | * | 323 | * |
| 323 | * @param tableNum | 324 | * @param tableNum |
| 324 | * @param transObject | 325 | * @param transObject |
| 325 | */ | 326 | */ |
| 326 | public void transmitToTablePlayers (int tableNum, ITransmittable transObject) { | 327 | public void transmitToTablePlayers (int tableNum, ITransmittable transObject) { |
| 327 | if (transObject instanceof CommTableMessage) | 328 | if (transObject instanceof CommTableMessage) |
| 328 | ((CommTableMessage)transObject).setTableNum (tableNum); | 329 | ((CommTableMessage)transObject).setTableNum (tableNum); |
| 329 | 330 | ||
| 330 | Table table = tableList.getTable(tableNum); | 331 | Table table = tableList.getTable(tableNum); |
| 331 | if (table != null) { | 332 | if (table != null) { |
| 332 | Vector players = table.getPlayerList().getPlayers(); | 333 | Vector players = table.getPlayerList().getPlayers(); |
| 333 | 334 | ||
| 334 | for (int i = 0; i < players.size(); i++) { | 335 | for (int i = 0; i < players.size(); i++) { |
| 335 | Player player = (Player)players.get(i); | 336 | Player player = (Player)players.get(i); |
| 336 | String username = player.getPlayerName(); | 337 | String username = player.getPlayerName(); |
| 337 | transmit (username, transObject); | 338 | transmit (username, transObject); |
| 338 | } | 339 | } |
| 339 | } | 340 | } |
| 340 | } | 341 | } |
| 341 | 342 | ||
| 342 | /** | 343 | /** |
| 343 | * Transmit a messgae to a table but omit a user (this user will generally | 344 | * Transmit a messgae to a table but omit a user (this user will generally |
| 344 | * be the player who has created the message in the first place). | 345 | * be the player who has created the message in the first place). |
| 345 | * | 346 | * |
| 346 | * @param omitUser Username to omit from the list (usually the sender). | 347 | * @param omitUser Username to omit from the list (usually the sender). |
| 347 | * @param tableNum Table number to transmit message to. | 348 | * @param tableNum Table number to transmit message to. |
| 348 | * @param transObject Object which implements the ITransmittable interface. | 349 | * @param transObject Object which implements the ITransmittable interface. |
| 349 | */ | 350 | */ |
| 350 | public void transmitToTablePlayers (String omitUser, int tableNum, ITransmittable transObject) { | 351 | public void transmitToTablePlayers (String omitUser, int tableNum, ITransmittable transObject) { |
| 351 | transmitToTablePlayers (omitUser, tableNum, transObject, false); | 352 | transmitToTablePlayers (omitUser, tableNum, transObject, false); |
| 352 | } | 353 | } |
| 353 | 354 | ||
| 354 | /** | 355 | /** |
| 355 | * Overloaded version which only transmits to players in a game in progress i.e. transmits | 356 | * Overloaded version which only transmits to players in a game in progress i.e. transmits |
| 356 | * to players whose state is "PlayerStateGameStarted". | 357 | * to players whose state is "PlayerStateGameStarted". |
| 357 | * | 358 | * |
| 358 | * @param omitUser Username to omit from the list (usually the sender). | 359 | * @param omitUser Username to omit from the list (usually the sender). |
| 359 | * @param tableNum Table number to transmit message to. | 360 | * @param tableNum Table number to transmit message to. |
| 360 | * @param transObject Object which implements the ITransmittable interface. | 361 | * @param transObject Object which implements the ITransmittable interface. |
| 361 | * @param gameInProgress If true only transmits to players if they are playing. | 362 | * @param gameInProgress If true only transmits to players if they are playing. |
| 362 | */ | 363 | */ |
| 363 | public void transmitToTablePlayers (String omitUser, int tableNum, ITransmittable transObject, boolean gameInProgress) { | 364 | public void transmitToTablePlayers (String omitUser, int tableNum, ITransmittable transObject, boolean gameInProgress) { |
| 364 | if (transObject instanceof CommTableMessage) | 365 | if (transObject instanceof CommTableMessage) |
| 365 | ((CommTableMessage)transObject).setTableNum (tableNum); | 366 | ((CommTableMessage)transObject).setTableNum (tableNum); |
| 366 | 367 | ||
| 367 | Table table = tableList.getTable(tableNum); | 368 | Table table = tableList.getTable(tableNum); |
| 368 | if (table != null) { | 369 | if (table != null) { |
| 369 | Vector players = table.getPlayerList().getPlayers(); | 370 | Vector players = table.getPlayerList().getPlayers(); |
| 370 | 371 | ||
| 371 | // Loop throught the various players and transmit the message | 372 | // Loop throught the various players and transmit the message |
| 372 | for (int i = 0; i < players.size(); i++) { | 373 | for (int i = 0; i < players.size(); i++) { |
| 373 | Player player = (Player)players.get(i); | 374 | Player player = (Player)players.get(i); |
| 374 | String username = player.getPlayerName(); | 375 | String username = player.getPlayerName(); |
| 375 | 376 | ||
| 376 | if (!username.equals(omitUser)) { | 377 | if (!username.equals(omitUser)) { |
| 377 | if (gameInProgress) { | 378 | if (gameInProgress) { |
| 378 | if (player.getState() instanceof PlayerStateGameStarted) | 379 | if (player.getState() instanceof PlayerStateGameStarted) |
| 379 | transmit (username, transObject); | 380 | transmit (username, transObject); |
| 380 | } | 381 | } |
| 381 | else | 382 | else |
| 382 | transmit (username, transObject); | 383 | transmit (username, transObject); |
| 383 | } | 384 | } |
| 384 | } | 385 | } |
| 385 | } | 386 | } |
| 386 | } | 387 | } |
| 387 | 388 | ||
| 388 | /** | 389 | /** |
| 389 | * Broadcast a transmittable object to all the clients. | 390 | * Broadcast a transmittable object to all the clients. |
| 390 | * | 391 | * |
| 391 | * @param transmittableObject | 392 | * @param transmittableObject |
| 392 | */ | 393 | */ |
| 393 | public void broadcast (ITransmittable transmittableObject) { | 394 | public void broadcast (ITransmittable transmittableObject) { |
| 394 | // retrieve all the clients | 395 | // retrieve all the clients |
| 395 | Vector users = userList.getUsers(); | 396 | Vector users = userList.getUsers(); |
| 396 | for (int i = 0; i < users.size(); i++) { | 397 | for (int i = 0; i < users.size(); i++) { |
| 397 | transmit((String)users.get(i), transmittableObject); | 398 | transmit((String)users.get(i), transmittableObject); |
| 398 | } | 399 | } |
| 399 | } | 400 | } |
| 400 | 401 | ||
| 401 | /** | 402 | /** |
| 402 | * Broadcast a transmittable object to all the clients. | 403 | * Broadcast a transmittable object to all the clients. |
| 403 | * | 404 | * |
| 404 | * @param omitUser Omit this user (usually the person who sent the message). | 405 | * @param omitUser Omit this user (usually the person who sent the message). |
| 405 | * @param transmittableObject Object to transmit. | 406 | * @param transmittableObject Object to transmit. |
| 406 | */ | 407 | */ |
| 407 | public void broadcast (String omitUser, ITransmittable transmittableObject) { | 408 | public void broadcast (String omitUser, ITransmittable transmittableObject) { |
| 408 | // retrieve all the clients | 409 | // retrieve all the clients |
| 409 | Vector users = userList.getUsers(); | 410 | Vector users = userList.getUsers(); |
| 410 | for (int i = 0; i < users.size(); i++) { | 411 | for (int i = 0; i < users.size(); i++) { |
| 411 | String currentUsername = (String)users.get(i); | 412 | String currentUsername = (String)users.get(i); |
| 412 | if (!omitUser.equals(currentUsername)) | 413 | if (!omitUser.equals(currentUsername)) |
| 413 | transmit(currentUsername, transmittableObject); | 414 | transmit(currentUsername, transmittableObject); |
| 414 | } | 415 | } |
| 415 | } | 416 | } |
| 416 | 417 | ||
| 417 | /** | 418 | /** |
| 418 | * Overloaded version which takes a CommGameMessage object. | 419 | * Overloaded version which takes a CommGameMessage object. |
| 419 | * | 420 | * |
| 420 | * @param username Username to send message to. | 421 | * @param username Username to send message to. |
| 421 | * @param transObject Transmittable object. | 422 | * @param transObject Transmittable object. |
| 422 | */ | 423 | */ |
| 423 | public void transmit (String username, ITransmittable transObject) { | 424 | public void transmit (String username, ITransmittable transObject) { |
| 424 | ServerConnectionThread conn = connections.getServerConnectionThread (getGameID(), username); | 425 | ServerConnectionThread conn = connections.getServerConnectionThread (getGameID(), username); |
| 425 | 426 | ||
| 426 | // Send the object to the user | 427 | // Send the object to the user |
| 427 | conn.send (transObject); | 428 | conn.send (transObject); |
| 428 | } | 429 | } |
| 429 | 430 | ||
| 430 | /** | 431 | /** |
| 431 | * Send a ITransmittable object to user connected to this thread only. | 432 | * Send a ITransmittable object to user connected to this thread only. |
| 432 | * | 433 | * |
| 433 | * @param transObject Transmittable object. | 434 | * @param transObject Transmittable object. |
| 434 | */ | 435 | */ |
| 435 | public void transmit (ITransmittable transObject) { | 436 | public void transmit (ITransmittable transObject) { |
| 436 | // Send the object to the user | 437 | // Send the object to the user |
| 437 | send (transObject); | 438 | send (transObject); |
| 438 | } | 439 | } |
| 439 | 440 | ||
| 440 | /** | 441 | /** |
| 441 | * This metohd returns a server controller associated with this | 442 | * This metohd returns a server controller associated with this |
| 442 | * server connection thread. | 443 | * server connection thread. |
| 443 | * | 444 | * |
| 444 | * @return Server connection. | 445 | * @return Server connection. |
| 445 | */ | 446 | */ |
| 446 | public ServerController getServerController () { | 447 | public ServerController getServerController () { |
| 447 | return JogreServer.getInstance().getControllers().getCustomController (gameID); | 448 | return JogreServer.getInstance().getControllers().getCustomController (gameID); |
| 448 | } | 449 | } |
| 449 | } | 450 | } |