My favorites | Sign in
Project Logo
                
Changes to /trunk/server/src/org/jogre/server/controllers/ServerGameController.java
r2 vs. r12   Edit
  Compare: vs.   Format:
Revision r12
Go to: 
Project members, sign in to write a code review
/trunk/server/src/org/jogre/server/controllers/ServerGameController.java   r2 /trunk/server/src/org/jogre/server/controllers/ServerGameController.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.controllers; 20 package org.jogre.server.controllers;
21 21
22 import java.util.List; 22 import java.util.List;
23 23
24 import nanoxml.XMLElement; 24 import nanoxml.XMLElement;
25 25
26 import org.jogre.common.Game; 26 import org.jogre.common.Game;
27 import org.jogre.common.IError; 27 import org.jogre.common.IError;
28 import org.jogre.common.IJogre; 28 import org.jogre.common.IJogre;
29 import org.jogre.common.Table; 29 import org.jogre.common.Table;
30 import org.jogre.common.TableList; 30 import org.jogre.common.TableList;
31 import org.jogre.common.TransmissionException; 31 import org.jogre.common.TransmissionException;
32 import org.jogre.common.User; 32 import org.jogre.common.User;
33 import org.jogre.common.UserList; 33 import org.jogre.common.UserList;
34 import org.jogre.common.comm.Comm; 34 import org.jogre.common.comm.Comm;
35 import org.jogre.common.comm.CommAdminClientData; 35 import org.jogre.common.comm.CommAdminClientData;
36 import org.jogre.common.comm.CommAdminConnect; 36 import org.jogre.common.comm.CommAdminConnect;
37 import org.jogre.common.comm.CommAdminIconData; 37 import org.jogre.common.comm.CommAdminIconData;
38 import org.jogre.common.comm.CommAdminServerProperties; 38 import org.jogre.common.comm.CommAdminServerProperties;
39 import org.jogre.common.comm.CommAdminTestConnection; 39 import org.jogre.common.comm.CommAdminTestConnection;
40 import org.jogre.common.comm.CommChatClient; 40 import org.jogre.common.comm.CommChatClient;
41 import org.jogre.common.comm.CommChatPrivate; 41 import org.jogre.common.comm.CommChatPrivate;
42 import org.jogre.common.comm.CommDisconnect; 42 import org.jogre.common.comm.CommDisconnect;
43 import org.jogre.common.comm.CommError; 43 import org.jogre.common.comm.CommError;
44 import org.jogre.common.comm.CommGameConnect; 44 import org.jogre.common.comm.CommGameConnect;
45 import org.jogre.common.comm.CommInvite; 45 import org.jogre.common.comm.CommInvite;
46 import org.jogre.common.comm.CommJoinTable; 46 import org.jogre.common.comm.CommJoinTable;
47 import org.jogre.common.comm.CommNewTable; 47 import org.jogre.common.comm.CommNewTable;
48 import org.jogre.common.comm.CommRequestData; 48 import org.jogre.common.comm.CommRequestData;
49 import org.jogre.common.util.JogreLogger; 49 import org.jogre.common.util.JogreLogger;
50 import org.jogre.common.util.JogreUtils; 50 import org.jogre.common.util.JogreUtils;
51 import org.jogre.server.JogreServer; 51 import org.jogre.server.JogreServer;
52 import org.jogre.server.ServerConnectionThread; 52 import org.jogre.server.ServerConnectionThread;
53 import org.jogre.server.ServerController; 53 import org.jogre.server.ServerController;
54 import org.jogre.server.ServerProperties; 54 import org.jogre.server.ServerProperties;
55 import org.jogre.server.data.GameSummary; 55 import org.jogre.server.data.GameSummary;
56 import org.jogre.server.data.IServerData; 56 import org.jogre.server.data.IServerData;
57 import org.jogre.server.data.ServerDataException; 57 import org.jogre.server.data.ServerDataException;
58 import org.jogre.server.data.db.DBConnection; 58 import org.jogre.server.data.db.DBConnection;
59 59
60 /** 60 /**
61 * Class which parses game specific messages on the server (used to 61 * Class which parses game specific messages on the server (used to
62 * be called ServerGameParser). 62 * be called ServerGameParser).
63 * 63 *
64 * @author Bob Marks 64 * @author Bob Marks
65 * @version Beta 0.3 65 * @version Beta 0.3
66 */ 66 */
67 public class ServerGameController { 67 public class ServerGameController {
68 68
69 /** Logging. */ 69 /** Logging. */
70 JogreLogger logger = new JogreLogger (this.getClass()); 70 JogreLogger logger = new JogreLogger (this.getClass());
71 71
72 /** Convience link to the server. */ 72 /** Convience link to the server. */
73 protected JogreServer server; 73 protected JogreServer server;
74 74
75 /** 75 /**
76 * Constructor. 76 * Constructor.
77 */ 77 */
78 public ServerGameController () { 78 public ServerGameController () {
79 // Set up fields 79 // Set up fields
80 this.server = JogreServer.getInstance(); 80 this.server = JogreServer.getInstance();
81 } 81 }
82 82
83 /** 83 /**
84 * Parse this game specific message. 84 * Parse this game specific message.
85 * 85 *
86 * @param conn Connection back to client 86 * @param conn Connection back to client
87 * @param message Message to parse 87 * @param message Message to parse
88 */ 88 */
89 public synchronized void parseGameMessage (ServerConnectionThread conn, XMLElement message) { 89 public synchronized void parseGameMessage (ServerConnectionThread conn, XMLElement message) {
90 try { 90 try {
91 // 1) Implementation of Comm commands 91 // 1) Implementation of Comm commands
92 String messageType = message.getName(); 92 String messageType = message.getName();
93 93
94 // Check for a connect message first of all 94 // Check for a connect message first of all
95 if (messageType.equals (Comm.GAME_CONNECT)) { 95 if (messageType.equals (Comm.GAME_CONNECT)) {
96 gameConnect (conn, message); 96 gameConnect (conn, message);
97 } 97 }
98 else if (messageType.equals (Comm.ADMIN_CONNECT)) { 98 else if (messageType.equals (Comm.ADMIN_CONNECT)) {
99 adminConnect (conn, message); 99 adminConnect (conn, message);
100 } 100 }
101 101
102 else { 102 else {
103 // Ensure username / gameID is set. If not then send 103 // Ensure username / gameID is set. If not then send
104 // an error message back to the client and quit. 104 // an error message back to the client and quit.
105 if (conn.getUsername() == null || conn.getGameID() == null) { 105 if (conn.getUsername() == null || conn.getGameID() == null) {
106 conn.send (new CommError (CommError.USER_NOT_CONNECTED)); 106 conn.send (new CommError (CommError.USER_NOT_CONNECTED));
107 conn.stopLoop(); 107 conn.getMessageBus().close();
108 } 108 }
109 109
110 // Check if message for administrator thread. 110 // Check if message for administrator thread.
111 if (messageType.equals (Comm.ADMIN_SERVER_PROPERTIES)) { 111 if (messageType.equals (Comm.ADMIN_SERVER_PROPERTIES)) {
112 setServerProperties (conn, message); 112 setServerProperties (conn, message);
113 } 113 }
114 else if (messageType.equals (Comm.ADMIN_TEST_CONNECTION)) { 114 else if (messageType.equals (Comm.ADMIN_TEST_CONNECTION)) {
115 testConnection (conn, message); 115 testConnection (conn, message);
116 } 116 }
117 else if (messageType.equals (Comm.ADMIN_CLIENT_DATA)) { 117 else if (messageType.equals (Comm.ADMIN_CLIENT_DATA)) {
118 adminClientData (conn, message); 118 adminClientData (conn, message);
119 } 119 }
120 // Other messages 120 // Other messages
121 else { 121 else {
122 if (messageType.equals (Comm.DISCONNECT)) { 122 if (messageType.equals (Comm.DISCONNECT)) {
123 disconnect (conn, new CommDisconnect (message)); 123 disconnect (conn, new CommDisconnect (message));
124 } 124 }
125 else if (messageType.equals (Comm.INVITE)) { 125 else if (messageType.equals (Comm.INVITE)) {
126 invite (conn, new CommInvite (message)); 126 invite (conn, new CommInvite (message));
127 } 127 }
128 else if (messageType.equals (Comm.CHAT_GAME)) { 128 else if (messageType.equals (Comm.CHAT_GAME)) {
129 chatGame (conn, new CommChatClient (message)); 129 chatGame (conn, new CommChatClient (message));
130 } 130 }
131 else if (messageType.equals (Comm.CHAT_PRIVATE)) { 131 else if (messageType.equals (Comm.CHAT_PRIVATE)) {
132 chatPrivate (conn, new CommChatPrivate (message)); 132 chatPrivate (conn, new CommChatPrivate (message));
133 } 133 }
134 else if (messageType.equals (Comm.REQUEST_DATA)) { 134 else if (messageType.equals (Comm.REQUEST_DATA)) {
135 requestData (conn, new CommRequestData (message)); 135 requestData (conn, new CommRequestData (message));
136 } 136 }
137 else if (messageType.equals (Comm.NEW_TABLE)) { 137 else if (messageType.equals (Comm.NEW_TABLE)) {
138 createTable (conn, new CommNewTable (message)); 138 createTable (conn, new CommNewTable (message));
139 } 139 }
140 140
141 // Delegate to a custom controller to do extra functionality 141 // Delegate to a custom controller to do extra functionality
142 String gameID = conn.getGameID(); 142 String gameID = conn.getGameID();
143 ServerController controller = server.getControllers().getCustomController (gameID); 143 ServerController controller = server.getControllers().getCustomController (gameID);
144 if (controller != null) 144 if (controller != null)
145 controller.parseGameMessage (conn, message); 145 controller.parseGameMessage (conn, message);
146 } 146 }
147 } 147 }
148 } 148 }
149 catch (TransmissionException transEx) { 149 catch (TransmissionException transEx) {
150 transEx.printStackTrace(); 150 transEx.printStackTrace();
151 } 151 }
152 catch (ServerDataException sdEx) { 152 catch (ServerDataException sdEx) {
153 conn.send (new CommError (IError.SERVER_DATA_ERROR, sdEx.getMessage())); 153 conn.send (new CommError (IError.SERVER_DATA_ERROR, sdEx.getMessage()));
154 } 154 }
155 } 155 }
156 156
157 /** 157 /**
158 * Connect a user to a server. 158 * Connect a user to a server.
159 * 159 *
160 * @param conn 160 * @param conn
161 * @param message Connection object. 161 * @param message Connection object.
162 * @throws TransmissionException Thrown if there is a transmission error. 162 * @throws TransmissionException Thrown if there is a transmission error.
163 */ 163 */
164 private void gameConnect (ServerConnectionThread conn, XMLElement message) throws TransmissionException, ServerDataException { 164 private void gameConnect (ServerConnectionThread conn, XMLElement message) throws TransmissionException, ServerDataException {
165 ServerProperties serverProperties = ServerProperties.getInstance(); 165 ServerProperties serverProperties = ServerProperties.getInstance();
166 166
167 // Unpack game connect message 167 // Unpack game connect message
168 CommGameConnect commConnect = new CommGameConnect (message); 168 CommGameConnect commConnect = new CommGameConnect (message);
169 String username = commConnect.getUsername(); 169 String username = commConnect.getUsername();
170 String gameID = commConnect.getGameID(); 170 String gameID = commConnect.getGameID();
171 171
172 // Check server supports this particular game 172 // Check server supports this particular game
173 if (!server.getGameList().containsGame (gameID)) { 173 if (!server.getGameList().containsGame (gameID)) {
174 174
175 // Send "game not supported" error to client and kill thread 175 // Send "game not supported" error to client and kill thread
176 conn.send (new CommError (CommError.GAME_NOT_SUPPORTED)); 176 conn.send (new CommError (CommError.GAME_NOT_SUPPORTED));
177 conn.stopLoop(); 177 conn.getMessageBus().close();
178 } 178 }
179 else { // game is supported so continue 179 else { // game is supported so continue
180 // Try and retrieve game and its user list 180 // Try and retrieve game and its user list
181 Game game = server.getGameList().getGame (gameID); 181 Game game = server.getGameList().getGame (gameID);
182 UserList userList = game.getUserList(); 182 UserList userList = game.getUserList();
183 183
184 // Check now that enough users are still available for this game 184 // Check now that enough users are still available for this game
185 if (game.getUserList().size() < serverProperties.getMaxNumOfUsers()) { 185 if (game.getUserList().size() < serverProperties.getMaxNumOfUsers()) {
186 186
187 // Check user isn't already logged on in this game. 187 // Check user isn't already logged on in this game.
188 if (!userList.containsUser (username)) { 188 if (!userList.containsUser (username)) {
189 189
190 boolean userDetailsCorrect = false; 190 boolean userDetailsCorrect = false;
191 191
192 // Ensure that the user is on the user list 192 // Ensure that the user is on the user list
193 String password = commConnect.getPassword(); 193 String password = commConnect.getPassword();
194 try { 194 try {
195 IServerData userConn = server.getServerData(); 195 IServerData userConn = server.getServerData();
196 196
197 if (password != null) 197 if (password != null)
198 userDetailsCorrect = userConn.containsUser(username, password); 198 userDetailsCorrect = userConn.containsUser(username, password);
199 else 199 else
200 userDetailsCorrect = userConn.containsUser(username); 200 userDetailsCorrect = userConn.containsUser(username);
201 } 201 }
202 catch (ServerDataException sdEx) { 202 catch (ServerDataException sdEx) {
203 conn.send (new CommError (IError.SERVER_DATA_ERROR, sdEx.getMessage())); 203 conn.send (new CommError (IError.SERVER_DATA_ERROR, sdEx.getMessage()));
204 } 204 }
205 205
206 // If everything is OK - initialise this thread, add user 206 // If everything is OK - initialise this thread, add user
207 // and reply back to client 207 // and reply back to client
208 if (userDetailsCorrect) { 208 if (userDetailsCorrect) {
209 addNewUser (conn, gameID, username); 209 addNewUser (conn, gameID, username);
210 } 210 }
211 else { 211 else {
212 // Logon error 212 // Logon error
213 if (password == null && serverProperties.isUserValidationPassword()) 213 if (password == null && serverProperties.isUserValidationPassword())
214 conn.send (new CommError (CommError.SERVER_REQUIRES_PASSWORD)); 214 conn.send (new CommError (CommError.SERVER_REQUIRES_PASSWORD));
215 else 215 else
216 conn.send (new CommError (CommError.USER_LOGON_INCORRECT)); 216 conn.send (new CommError (CommError.USER_LOGON_INCORRECT));
217 217
218 conn.stopLoop(); 218 conn.getMessageBus().close();
219 } 219 }
220 } 220 }
221 else { 221 else {
222 // Send "user already connected" error to client and kill thread 222 // Send "user already connected" error to client and kill thread
223 conn.send (new CommError (CommError.USER_ALREADY_CONNECTED)); 223 conn.send (new CommError (CommError.USER_ALREADY_CONNECTED));
224 conn.stopLoop(); 224 conn.getMessageBus().close();
225 } 225 }
226 } 226 }
227 else { 227 else {
228 // Send "server full" error to client and kill thread 228 // Send "server full" error to client and kill thread
229 conn.send (new CommError (CommError.SERVER_FULL)); 229 conn.send (new CommError (CommError.SERVER_FULL));
230 conn.stopLoop (); 230 conn.getMessageBus().close ();
231 } 231 }
232 } 232 }
233 } 233 }
234 234
235 /** 235 /**
236 * Administrator connection. 236 * Administrator connection.
237 * 237 *
238 * @param conn Connection to the administrator thread. 238 * @param conn Connection to the administrator thread.
239 * @param message Message to parse. 239 * @param message Message to parse.
240 * @throws TransmissionException 240 * @throws TransmissionException
241 */ 241 */
242 private void adminConnect (ServerConnectionThread conn, XMLElement message) throws TransmissionException { 242 private void adminConnect (ServerConnectionThread conn, XMLElement message) throws TransmissionException {
243 // Unpack game connect message 243 // Unpack game connect message
244 CommAdminConnect commConnect = new CommAdminConnect (message); 244 CommAdminConnect commConnect = new CommAdminConnect (message);
245 String username = commConnect.getUsername(); 245 String username = commConnect.getUsername();
246 String password = commConnect.getPassword(); 246 String password = commConnect.getPassword();
247 247
248 // Check an admin isn't already connected (possibly support multiple in future?) 248 // Check an admin isn't already connected (possibly support multiple in future?)
249 if (server.getConnections().getAdminConnection() != null) { 249 if (server.getConnections().getAdminConnection() != null) {
250 250
251 // Send "game not supported" error to client and kill thread 251 // Send "game not supported" error to client and kill thread
252 conn.send (new CommError (CommError.USER_ALREADY_CONNECTED)); 252 conn.send (new CommError (CommError.USER_ALREADY_CONNECTED));
253 conn.stopLoop (); 253 conn.getMessageBus().close ();
254 } 254 }
255 else { 255 else {
256 // Check if the user is an administrator or not 256 // Check if the user is an administrator or not
257 if (ServerProperties.getInstance().isAdministrator (username, password)) { 257 if (ServerProperties.getInstance().isAdministrator (username, password)) {
258 // Initilise thread 258 // Initilise thread
259 conn.init (username); 259 conn.init (username);
260 260
261 // Set admin connection 261 // Set admin connection
262 server.getConnections().setAdminConnection (conn); 262 server.getConnections().setAdminConnection (conn);
263 263
264 // Reply back to the client and send server properties, icons and game state 264 // Reply back to the client and send server properties, icons and game state
265 conn.transmit (server.getGameList()); 265 conn.transmit (server.getGameList());
266 conn.transmit (new CommAdminIconData (server.getGameLoader().getIconDataHash())); 266 conn.transmit (new CommAdminIconData (server.getGameLoader().getIconDataHash()));
267 conn.transmit (ServerProperties.getInstance()); 267 conn.transmit (ServerProperties.getInstance());
268 } 268 }
269 else { 269 else {
270 conn.send (new CommError (CommError.USER_LOGON_INCORRECT)); 270 conn.send (new CommError (CommError.USER_LOGON_INCORRECT));
271 conn.stopLoop (); 271 conn.getMessageBus().close ();
272 } 272 }
273 } 273 }
274 } 274 }
275 275
276 /** 276 /**
277 * Set the server properties. 277 * Set the server properties.
278 * 278 *
279 * This may need updated at some time to do soft reboots etc. 279 * This may need updated at some time to do soft reboots etc.
280 * 280 *
281 * @param XMLElement 281 * @param XMLElement
282 */ 282 */
283 private void setServerProperties (ServerConnectionThread conn, XMLElement message) { 283 private void setServerProperties (ServerConnectionThread conn, XMLElement message) {
284 if (conn.isAdministrator()) { 284 if (conn.isAdministrator()) {
285 // Create communication admin properties object 285 // Create communication admin properties object
286 CommAdminServerProperties commServerProps = new CommAdminServerProperties (message); 286 CommAdminServerProperties commServerProps = new CommAdminServerProperties (message);
287 String serverPropsStr = commServerProps.getServerPropertiesStr(); 287 String serverPropsStr = commServerProps.getServerPropertiesStr();
288 288
289 ServerProperties.setUpFromString (serverPropsStr); 289 ServerProperties.setUpFromString (serverPropsStr);
290 ServerProperties.getInstance().saveXMLFile (); 290 ServerProperties.getInstance().saveXMLFile ();
291 } 291 }
292 } 292 }
293 293
294 /** 294 /**
295 * Test the connection. 295 * Test the connection.
296 * 296 *
297 * @param conn 297 * @param conn
298 * @param message 298 * @param message
299 */ 299 */
300 private void testConnection(ServerConnectionThread conn, XMLElement message) { 300 private void testConnection(ServerConnectionThread conn, XMLElement message) {
301 if (conn.isAdministrator()) { 301 if (conn.isAdministrator()) {
302 // Create message and retrieve attributes 302 // Create message and retrieve attributes
303 CommAdminTestConnection commTestConn = new CommAdminTestConnection (message); 303 CommAdminTestConnection commTestConn = new CommAdminTestConnection (message);
304 String driver = commTestConn.getDriver(); 304 String driver = commTestConn.getDriver();
305 String url = commTestConn.getUrl(); 305 String url = commTestConn.getUrl();
306 String username = commTestConn.getUsername(); 306 String username = commTestConn.getUsername();
307 String password = commTestConn.getPassword(); 307 String password = commTestConn.getPassword();
308 308
309 // Test connection 309 // Test connection
310 int status = DBConnection.testConnection (driver, url, username, password, false); 310 int status = DBConnection.testConnection (driver, url, username, password, false);
311 311
312 // Send reply back to user 312 // Send reply back to user
313 CommAdminTestConnection commTestConnReply = new CommAdminTestConnection (status); 313 CommAdminTestConnection commTestConnReply = new CommAdminTestConnection (status);
314 conn.send (commTestConnReply); 314 conn.send (commTestConnReply);
315 } 315 }
316 } 316 }
317 317
318 /** 318 /**
319 * The administrator client is requesting data. 319 * The administrator client is requesting data.
320 * 320 *
321 * This uses String arrays at minute - will be made better in future if required. 321 * This uses String arrays at minute - will be made better in future if required.
322 * 322 *
323 * @param conn 323 * @param conn
324 * @param message 324 * @param message
325 */ 325 */
326 private void adminClientData(ServerConnectionThread conn, XMLElement message) throws ServerDataException { 326 private void adminClientData(ServerConnectionThread conn, XMLElement message) throws ServerDataException {
327 if (conn.isAdministrator()) { 327 if (conn.isAdministrator()) {
328 // Create message and retrieve attributes 328 // Create message and retrieve attributes
329 CommAdminClientData adminDataReq = new CommAdminClientData (message); 329 CommAdminClientData adminDataReq = new CommAdminClientData (message);
330 String [][] data = null; 330 String [][] data = null;
331 String [] params = adminDataReq.getParamData(); 331 String [] params = adminDataReq.getParamData();
332 String dataType = adminDataReq.getDataType(); 332 String dataType = adminDataReq.getDataType();
333 char requestType = adminDataReq.getRequestType(); 333 char requestType = adminDataReq.getRequestType();
334 334
335 // Check data type 335 // Check data type
336 if (IServerData.DATA_USERS.equals(dataType)) { // User 336 if (IServerData.DATA_USERS.equals(dataType)) { // User
337 337
338 // Check to see if user is trying to update data first of all (only support for users at min). 338 // Check to see if user is trying to update data first of all (only support for users at min).
339 if (params != null) { 339 if (params != null) {
340 org.jogre.server.data.User user = new org.jogre.server.data.User (); 340 org.jogre.server.data.User user = new org.jogre.server.data.User ();
341 user.setUsername(params[0]); 341 user.setUsername(params[0]);
342 342
343 if (requestType == CommAdminClientData.NEW) { 343 if (requestType == CommAdminClientData.NEW) {
344 user.setPassword(params[1]); 344 user.setPassword(params[1]);
345 server.getServerData().newUser (user); 345 server.getServerData().newUser (user);
346 } 346 }
347 else if (requestType == CommAdminClientData.UPDATE) { 347 else if (requestType == CommAdminClientData.UPDATE) {
348 user.setPassword(params[1]); 348 user.setPassword(params[1]);
349 server.getServerData().updateUser (user); 349 server.getServerData().updateUser (user);
350 } 350 }
351 else if (requestType == CommAdminClientData.DELETE) { 351 else if (requestType == CommAdminClientData.DELETE) {
352 server.getServerData().deleteUser (user); 352 server.getServerData().deleteUser (user);
353 } 353 }
354 } 354 }
355 355
356 // Send list back to user. 356 // Send list back to user.
357 List users = server.getServerData().getUsers(); 357 List users = server.getServerData().getUsers();
358 data = new String [users.size()][2]; 358 data = new String [users.size()][2];
359 for (int i = 0; i < users.size(); i++) { 359 for (int i = 0; i < users.size(); i++) {
360 org.jogre.server.data.User user = (org.jogre.server.data.User)users.get(i); 360 org.jogre.server.data.User user = (org.jogre.server.data.User)users.get(i);
361 361
362 // Must be a better way of doing this ... but is it worth it? 362 // Must be a better way of doing this ... but is it worth it?
363 data [i][0] = user.getUsername(); 363 data [i][0] = user.getUsername();
364 data [i][1] = user.getPassword(); 364 data [i][1] = user.getPassword();
365 } 365 }
366 // Create item and return to user. 366 // Create item and return to user.
367 CommAdminClientData adminDataReply = new CommAdminClientData (data, dataType); 367 CommAdminClientData adminDataReply = new CommAdminClientData (data, dataType);
368 conn.send (adminDataReply); 368 conn.send (adminDataReply);
369 } 369 }
370 else if (IServerData.DATA_GAME_INFO.equals(dataType)) { // Game info 370 else if (IServerData.DATA_GAME_INFO.equals(dataType)) { // Game info
371 List gameInfos; 371 List gameInfos;
372 gameInfos = server.getServerData().getGameInfos(); 372 gameInfos = server.getServerData().getGameInfos();
373 373
374 data = new String [gameInfos.size()][7]; 374 data = new String [gameInfos.size()][7];
375 for (int i = 0; i < gameInfos.size(); i++) { 375 for (int i = 0; i < gameInfos.size(); i++) {
376 org.jogre.server.data.GameInfo gameInfo = (org.jogre.server.data.GameInfo)gameInfos.get(i); 376 org.jogre.server.data.GameInfo gameInfo = (org.jogre.server.data.GameInfo)gameInfos.get(i);
377 377
378 // Populate 2D array 378 // Populate 2D array
379 data [i][0] = gameInfo.getGameKey(); 379 data [i][0] = gameInfo.getGameKey();
380 data [i][1] = gameInfo.getPlayers(); 380 data [i][1] = gameInfo.getPlayers();
381 data [i][2] = gameInfo.getResults(); 381 data [i][2] = gameInfo.getResults();
382 data [i][3] = JogreUtils.valueOf (gameInfo.getStartTime(), IJogre.DATE_FORMAT_FULL); 382 data [i][3] = JogreUtils.valueOf (gameInfo.getStartTime(), IJogre.DATE_FORMAT_FULL);
383 data [i][4] = JogreUtils.valueOf (gameInfo.getEndTime(), IJogre.DATE_FORMAT_FULL); 383 data [i][4] = JogreUtils.valueOf (gameInfo.getEndTime(), IJogre.DATE_FORMAT_FULL);
384 data [i][5] = gameInfo.getGameScore(); 384 data [i][5] = gameInfo.getGameScore();
385 data [i][6] = gameInfo.getGameHistory(); 385 data [i][6] = gameInfo.getGameHistory();
386 } 386 }
387 // Create item and return to user. 387 // Create item and return to user.
388 CommAdminClientData adminDataReply = new CommAdminClientData (data, dataType); 388 CommAdminClientData adminDataReply = new CommAdminClientData (data, dataType);
389 conn.send (adminDataReply); 389 conn.send (adminDataReply);
390 } 390 }
391 else if (IServerData.DATA_GAME_SUMMARY.equals(dataType)) { // Game summary 391 else if (IServerData.DATA_GAME_SUMMARY.equals(dataType)) { // Game summary
392 List gameSummaries; 392 List gameSummaries;
393 gameSummaries = server.getServerData().getGameSummarys(); 393 gameSummaries = server.getServerData().getGameSummarys();
394 394
395 data = new String [gameSummaries.size()][6]; 395 data = new String [gameSummaries.size()][6];
396 for (int i = 0; i < gameSummaries.size(); i++) { 396 for (int i = 0; i < gameSummaries.size(); i++) {
397 org.jogre.server.data.GameSummary gameSummary = (org.jogre.server.data.GameSummary)gameSummaries.get(i); 397 org.jogre.server.data.GameSummary gameSummary = (org.jogre.server.data.GameSummary)gameSummaries.get(i);
398 398
399 // Populate 2D array 399 // Populate 2D array
400 data [i][0] = gameSummary.getGameKey(); 400 data [i][0] = gameSummary.getGameKey();
401 data [i][1] = gameSummary.getUsername(); 401 data [i][1] = gameSummary.getUsername();
402 data [i][2] = String.valueOf(gameSummary.getRating()); 402 data [i][2] = String.valueOf(gameSummary.getRating());
403 data [i][3] = String.valueOf(gameSummary.getWins()); 403 data [i][3] = String.valueOf(gameSummary.getWins());
404 data [i][4] = String.valueOf(gameSummary.getLoses()); 404 data [i][4] = String.valueOf(gameSummary.getLoses());
405 data [i][5] = String.valueOf(gameSummary.getDraws()); 405 data [i][5] = String.valueOf(gameSummary.getDraws());
406 } 406 }
407 // Create item and return to user. 407 // Create item and return to user.
408 CommAdminClientData adminDataReply = new CommAdminClientData (data, dataType); 408 CommAdminClientData adminDataReply = new CommAdminClientData (data, dataType);
409 conn.send (adminDataReply); 409 conn.send (adminDataReply);
410 } 410 }
411 } 411 }
412 } 412 }
413 413
414 /** 414 /**
415 * Add a new user to the userlist on the server. Also create a CommGameConnect 415 * Add a new user to the userlist on the server. Also create a CommGameConnect
416 * back to the user and broad cast the userlist and table list to ALL users. 416 * back to the user and broad cast the userlist and table list to ALL users.
417 * 417 *
418 * @param conn Link to the client. 418 * @param conn Link to the client.
419 * @param gameID GameID 419 * @param gameID GameID
420 * @param username 420 * @param username
421 * @throws TransmissionException 421 * @throws TransmissionException
422 */ 422 */
423 private void addNewUser (ServerConnectionThread conn, String gameID, String username) throws TransmissionException, ServerDataException { 423 private void addNewUser (ServerConnectionThread conn, String gameID, String username) throws TransmissionException, ServerDataException {
424 // Initilise thread 424 // Initilise thread
425 conn.init (username, gameID); 425 conn.init (username, gameID);
426 426
427 // Add this thread to the hashmap 427 // Add this thread to the hashmap
428 // (enables commuication to multiple clients) 428 // (enables commuication to multiple clients)
429 UserList userList = conn.getUserList (); 429 UserList userList = conn.getUserList ();
430 430
431 // Add connection for user 431 // Add connection for user
432 server.getConnections().addConnection (gameID, username, conn); 432 server.getConnections().addConnection (gameID, username, conn);
433 433
434 // Retrieve game summary details about this user 434 // Retrieve game summary details about this user
435 GameSummary gs = 435 GameSummary gs =
436 server.getServerData().getGameSummary (gameID, username); 436 server.getServerData().getGameSummary (gameID, username);
437 User user = new User (username, gs.getRating(), gs.getWins(), 437 User user = new User (username, gs.getRating(), gs.getWins(),
438 gs.getLoses(), gs.getDraws(), gs.getStreak()); 438 gs.getLoses(), gs.getDraws(), gs.getStreak());
439 userList.addUser (user); // Add user 439 userList.addUser (user); // Add user
440 440
441 // Reply back to the client and send game state 441 // Reply back to the client and send game state
442 conn.transmit (conn.getGame()); 442 conn.transmit (conn.getGame());
443 443
444 // Broadcast this connection to other users at this game 444 // Broadcast this connection to other users at this game
445 conn.broadcast (username, user); 445 conn.broadcast (username, user);
446 446
447 // Send message to administration client if logged on 447 // Send message to administration client if logged on
448 conn.sendDataMessageToAdmin (user); 448 conn.sendDataMessageToAdmin (user);
449 449
450 // Output the message to the console 450 // Output the message to the console
451 logger.log ("Jogre Games Server: client [" + username + "] logged on"); 451 logger.log ("Jogre Games Server: client [" + username + "] logged on");
452 452
453 // Update snapshot 453 // Update snapshot
454 server.getServerData().updateSnapshot ( 454 server.getServerData().updateSnapshot (
455 conn.getGameID(), conn.getUserList().size(), conn.getTableList().size()); 455 conn.getGameID(), conn.getUserList().size(), conn.getTableList().size());
456 } 456 }
457 457
458 /** 458 /**
459 * Disconnect a user completely from the server (remove from user list and 459 * Disconnect a user completely from the server (remove from user list and
460 * any instance of the user in the table list). 460 * any instance of the user in the table list).
461 * 461 *
462 * @param conn Connection back to client 462 * @param conn Connection back to client
463 * @param commDisconnect Unparsed disconnect String. 463 * @param commDisconnect Unparsed disconnect String.
464 * @throws TransmissionException Thrown if there is a transmission error. 464 * @throws TransmissionException Thrown if there is a transmission error.
465 */ 465 */
466 private void disconnect (ServerConnectionThread conn, CommDisconnect commDisconnect) throws TransmissionException { 466 private void disconnect (ServerConnectionThread conn, CommDisconnect commDisconnect) throws TransmissionException {
467 conn.stopLoop (); 467 conn.getMessageBus().close ();
468 } 468 }
469 469
470 /** 470 /**
471 * A client is inviting another user. 471 * A client is inviting another user.
472 * 472 *
473 * @param conn Connection to the client 473 * @param conn Connection to the client
474 * @param commInvite Invite communication object. 474 * @param commInvite Invite communication object.
475 * @throws TransmissionException Thrown if there is a transmission error. 475 * @throws TransmissionException Thrown if there is a transmission error.
476 */ 476 */
477 private void invite (ServerConnectionThread conn, CommInvite commInvite) throws TransmissionException { 477 private void invite (ServerConnectionThread conn, CommInvite commInvite) throws TransmissionException {
478 // Make sure that the table is valid. 478 // Make sure that the table is valid.
479 Table table = conn.getTableList().getTable(commInvite.getTableNum()); 479 Table table = conn.getTableList().getTable(commInvite.getTableNum());
480 if (table != null) { 480 if (table != null) {
481 switch (commInvite.getStatus()) { 481 switch (commInvite.getStatus()) {
482 case CommInvite.REQUEST: 482 case CommInvite.REQUEST:
483 doInvite(conn, commInvite, table); 483 doInvite(conn, commInvite, table);
484 break; 484 break;
485 case CommInvite.ACCEPT: 485 case CommInvite.ACCEPT:
486 doAccept(conn, table); 486 doAccept(conn, table);
487 break; 487 break;
488 case CommInvite.DECLINE: 488 case CommInvite.DECLINE:
489 doDecline(conn, commInvite, table); 489 doDecline(conn, commInvite, table);
490 break; 490 break;
491 } 491 }
492 } 492 }
493 } 493 }
494 494
495 /** 495 /**
496 * A user is inviting another user to a table. 496 * A user is inviting another user to a table.
497 * 497 *
498 * @param conn Connection to the client that is sending the invite 498 * @param conn Connection to the client that is sending the invite
499 * @param commInvite The invitation message from the sender. 499 * @param commInvite The invitation message from the sender.
500 * @param table The table the host is inviting the guest to. 500 * @param table The table the host is inviting the guest to.
501 */ 501 */
502 private void doInvite (ServerConnectionThread conn, CommInvite commInvite, Table table) throws TransmissionException { 502 private void doInvite (ServerConnectionThread conn, CommInvite commInvite, Table table) throws TransmissionException {
503 // Get the name of the inviting player and the invited player 503 // Get the name of the inviting player and the invited player
504 String host = conn.getUsername(); 504 String host = conn.getUsername();
505 String guest = commInvite.getUsernameTo(); 505 String guest = commInvite.getUsernameTo();
506 506
507 // Make sure that the table that is being invited to exists and that 507 // Make sure that the table that is being invited to exists and that
508 // the host is a member of it and that the guest is not. 508 // the host is a member of it and that the guest is not.
509 if ( table.containsPlayer (host) && 509 if ( table.containsPlayer (host) &&
510 !table.containsPlayer (guest)) { 510 !table.containsPlayer (guest)) {
511 // Send the message to the invitee 511 // Send the message to the invitee
512 commInvite.setUsername (host); 512 commInvite.setUsername (host);
513 conn.transmit (guest, commInvite); 513 conn.transmit (guest, commInvite);
514 514
515 // Attach an invitation to the table 515 // Attach an invitation to the table
516 table.addInvite(guest); 516 table.addInvite(guest);
517 } 517 }
518 } 518 }
519 519
520 /** 520 /**
521 * A user is accepting an invitation to join a table. 521 * A user is accepting an invitation to join a table.
522 * 522 *
523 * @param conn Connection to the client that is accepting. 523 * @param conn Connection to the client that is accepting.
524 * @param table The table the guest is accepting to. 524 * @param table The table the guest is accepting to.
525 */ 525 */
526 private void doAccept (ServerConnectionThread conn, Table table) throws TransmissionException { 526 private void doAccept (ServerConnectionThread conn, Table table) throws TransmissionException {
527 // Get the name of the new guest 527 // Get the name of the new guest
528 String newPlayerName = conn.getUsername(); 528 String newPlayerName = conn.getUsername();
529 User newPlayerUser = conn.getUserList ().getUser (newPlayerName); 529 User newPlayerUser = conn.getUserList ().getUser (newPlayerName);
530 530
531 // Make sure that the player had an invite to the table, and that 531 // Make sure that the player had an invite to the table, and that
532 // the player was successfully added to the table. 532 // the player was successfully added to the table.
533 if ( table.removeInvite (newPlayerName) && 533 if ( table.removeInvite (newPlayerName) &&
534 table.addPlayer (newPlayerUser)) { 534 table.addPlayer (newPlayerUser)) {
535 535
536 // Create a response for the new player that includes the entire 536 // Create a response for the new player that includes the entire
537 // table state. 537 // table state.
538 CommJoinTable commJoinTable = new CommJoinTable (newPlayerName, table); 538 CommJoinTable commJoinTable = new CommJoinTable (newPlayerName, table);
539 conn.transmit (newPlayerName, commJoinTable); 539 conn.transmit (newPlayerName, commJoinTable);
540 540
541 // Send everyone else a notice that the new player joined the table. 541 // Send everyone else a notice that the new player joined the table.
542 commJoinTable = new CommJoinTable (newPlayerName, table.getTableNum()); 542 commJoinTable = new CommJoinTable (newPlayerName, table.getTableNum());
543 conn.broadcast (newPlayerName, commJoinTable); 543 conn.broadcast (newPlayerName, commJoinTable);
544 conn.sendDataMessageToAdmin (commJoinTable); 544 conn.sendDataMessageToAdmin (commJoinTable);
545 } 545 }
546 } 546 }
547 547
548 /** 548 /**
549 * A user is declining an invitation to join a table. 549 * A user is declining an invitation to join a table.
550 * 550 *
551 * @param conn Connection to the client that is declining. 551 * @param conn Connection to the client that is declining.
552 * @param commInvite The invitation message from the guest. 552 * @param commInvite The invitation message from the guest.
553 * @param table The table the guest is declining to join. 553 * @param table The table the guest is declining to join.
554 */ 554 */
555 private void doDecline (ServerConnectionThread conn, CommInvite commInvite, Table table) throws TransmissionException { 555 private void doDecline (ServerConnectionThread conn, CommInvite commInvite, Table table) throws TransmissionException {
556 // Get the name of the inviting (host) player and the declining (guest) player 556 // Get the name of the inviting (host) player and the declining (guest) player
557 String host = commInvite.getUsername(); 557 String host = commInvite.getUsername();
558 String guest = conn.getUsername(); 558 String guest = conn.getUsername();
559 559
560 // Make sure that the player had an invite to the table 560 // Make sure that the player had an invite to the table
561 if (table.removeInvite (guest)) { 561 if (table.removeInvite (guest)) {
562 // Send the message to the host indicating declanation 562 // Send the message to the host indicating declanation
563 commInvite.setUsernameTo (guest); 563 commInvite.setUsernameTo (guest);
564 conn.transmit (host, commInvite); 564 conn.transmit (host, commInvite);
565 } 565 }
566 } 566 }
567 567
568 /** 568 /**
569 * Transmit a message. 569 * Transmit a message.
570 * 570 *
571 * @param conn Connection to the client. 571 * @param conn Connection to the client.
572 * @param commMessage Chat communication message. 572 * @param commMessage Chat communication message.
573 * @throws TransmissionException 573 * @throws TransmissionException
574 */ 574 */
575 private void chatGame (ServerConnectionThread conn, CommChatClient commMessage) throws TransmissionException { 575 private void chatGame (ServerConnectionThread conn, CommChatClient commMessage) throws TransmissionException {
576 commMessage.setUsername(conn.getUsername()); 576 commMessage.setUsername(conn.getUsername());
577 577
578 conn.broadcast (conn.getUsername(), commMessage); 578 conn.broadcast (conn.getUsername(), commMessage);
579 } 579 }
580 580
581 /** 581 /**
582 * Transmit a private chat message. 582 * Transmit a private chat message.
583 * 583 *
584 * @param conn Connection to the client. 584 * @param conn Connection to the client.
585 * @param commChatMessage Chat message. 585 * @param commChatMessage Chat message.
586 * @throws TransmissionException 586 * @throws TransmissionException
587 */ 587 */
588 private void chatPrivate (ServerConnectionThread conn, CommChatPrivate commChatMessage) throws TransmissionException { 588 private void chatPrivate (ServerConnectionThread conn, CommChatPrivate commChatMessage) throws TransmissionException {
589 String usernameTo = commChatMessage.getUsernameTo(); 589 String usernameTo = commChatMessage.getUsernameTo();
590 commChatMessage.setUsername(conn.getUsername()); // Client needs to know who sent it 590 commChatMessage.setUsername(conn.getUsername()); // Client needs to know who sent it
591 commChatMessage.setUsernameTo (null); // Don't need username to 591 commChatMessage.setUsernameTo (null); // Don't need username to
592 592
593 // Send to client 593 // Send to client
594 conn.transmit (usernameTo, commChatMessage); 594 conn.transmit (usernameTo, commChatMessage);
595 } 595 }
596 596
597 /** 597 /**
598 * Client is requesting data from the server 598 * Client is requesting data from the server
599 * 599 *
600 * @param conn Connection to the client. 600 * @param conn Connection to the client.
601 * @param commRequestData Request data communications object. 601 * @param commRequestData Request data communications object.
602 * @throws TransmissionException 602 * @throws TransmissionException
603 */ 603 */
604 private void requestData (ServerConnectionThread conn, CommRequestData commRequestData) throws TransmissionException { 604 private void requestData (ServerConnectionThread conn, CommRequestData commRequestData) throws TransmissionException {
605 // User is requesting data - transmit either Game, TableList or UserList 605 // User is requesting data - transmit either Game, TableList or UserList
606 String dataType = commRequestData.getDataType(); 606 String dataType = commRequestData.getDataType();
607 if (dataType.equals(Comm.GAME_LIST)) 607 if (dataType.equals(Comm.GAME_LIST))
608 conn.transmit (server.getGameList()); 608 conn.transmit (server.getGameList());
609 else if (dataType.equals(Comm.GAME)) 609 else if (dataType.equals(Comm.GAME))
610 conn.transmit (conn.getGame()); 610 conn.transmit (conn.getGame());
611 else if (dataType.equals(Comm.TABLE_LIST)) 611 else if (dataType.equals(Comm.TABLE_LIST))
612 conn.transmit (conn.getTableList()); 612 conn.transmit (conn.getTableList());
613 else if (dataType.equals(Comm.USER_LIST)) 613 else if (dataType.equals(Comm.USER_LIST))
614 conn.transmit (conn.getUserList ()); 614 conn.transmit (conn.getUserList ());
615 } 615 }
616 616
617 /** 617 /**
618 * Transmit a create table message. 618 * Transmit a create table message.
619 * 619 *
620 * @param conn Connection to client 620 * @param conn Connection to client
621 * @param commNewTable New table communications object. 621 * @param commNewTable New table communications object.
622 * @throws TransmissionException 622 * @throws TransmissionException
623 */ 623 */
624 private void createTable (ServerConnectionThread conn, CommNewTable commNewTable) throws TransmissionException, ServerDataException { 624 private void createTable (ServerConnectionThread conn, CommNewTable commNewTable) throws TransmissionException, ServerDataException {
625 TableList tableList = conn.getTableList (); 625 TableList tableList = conn.getTableList ();
626 626
627 // Make sure that this player is allowed to start a new table. 627 // Make sure that this player is allowed to start a new table.
628 String userName = conn.getUsername(); 628 String userName = conn.getUsername();
629 ServerProperties sProps = ServerProperties.getInstance(); 629 ServerProperties sProps = ServerProperties.getInstance();
630 630
631 if (tableList.size() >= sProps.getMaxNumOfTables()) { 631 if (tableList.size() >= sProps.getMaxNumOfTables()) {
632 conn.send (new CommError (CommError.SERVER_TABLE_LIMIT_EXCEEDED)); 632 conn.send (new CommError (CommError.SERVER_TABLE_LIMIT_EXCEEDED));
633 return; 633 return;
634 } 634 }
635 635
636 if (tableList.getNumOfTablesUserOwns (userName) >= 636 if (tableList.getNumOfTablesUserOwns (userName) >=
637 sProps.getMaxNumOfTablesPerUser()) { 637 sProps.getMaxNumOfTablesPerUser()) {
638 conn.send (new CommError (CommError.PLAYER_TABLE_LIMIT_EXCEEDED)); 638 conn.send (new CommError (CommError.PLAYER_TABLE_LIMIT_EXCEEDED));
639 return; 639 return;
640 } 640 }
641 641
642 User ownerUser = conn.getUserList().getUser (userName); 642 User ownerUser = conn.getUserList().getUser (userName);
643 // Create the new table 643 // Create the new table
644 Table table = tableList.addTable (ownerUser, 644 Table table = tableList.addTable (ownerUser,
645 commNewTable.isPublic(), 645 commNewTable.isPublic(),
646 commNewTable.getProperties()); 646 commNewTable.getProperties());
647 647
648 // Send this new table to all connected users and admin client if logged on 648 // Send this new table to all connected users and admin client if logged on
649 conn.broadcast (table); 649 conn.broadcast (table);
650 conn.sendDataMessageToAdmin (table); 650 conn.sendDataMessageToAdmin (table);
651 651
652 // Update snapshot 652 // Update snapshot
653 server.getServerData().updateSnapshot (conn.getGameID(), 653 server.getServerData().updateSnapshot (conn.getGameID(),
654 conn.getUserList().size(), 654 conn.getUserList().size(),
655 tableList.size()); 655 tableList.size());
656 } 656 }
657 } 657 }
Hosted by Google Code