| /trunk/api/src/org/jogre/client/ClientConnectionThread.java r2 | /trunk/api/src/org/jogre/client/ClientConnectionThread.java r16 | ||
| 1 | /* | 1 | /* |
|---|---|---|---|
| 2 | * JOGRE (Java Online Gaming Real-time Engine) - API | 2 | * JOGRE (Java Online Gaming Real-time Engine) - API |
| 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.client; | 20 | package org.jogre.client; |
| 21 | 21 | ||
| 22 | import java.net.Socket; | 22 | import java.net.Socket; |
| 23 | 23 | ||
| 24 | import nanoxml.XMLElement; | 24 | import nanoxml.XMLElement; |
| 25 | 25 | ||
| 26 | import org.jogre.common.AbstractConnectionThread; | 26 | import org.jogre.common.AbstractConnectionThread; |
| 27 | import org.jogre.common.ClientCommDataReceiver; | 27 | import org.jogre.common.ClientCommDataReceiver; |
| 28 | import org.jogre.common.Game; | 28 | import org.jogre.common.Game; |
| 29 | import org.jogre.common.SocketBasedMessageBus; | ||
| 29 | import org.jogre.common.TableList; | 30 | import org.jogre.common.TableList; |
| 30 | import org.jogre.common.UserList; | 31 | import org.jogre.common.UserList; |
| 31 | import org.jogre.common.comm.CommGameMessage; | 32 | import org.jogre.common.comm.CommGameMessage; |
| 32 | import org.jogre.common.comm.CommTableMessage; | 33 | import org.jogre.common.comm.CommTableMessage; |
| 33 | 34 | ||
| 34 | /** | 35 | /** |
| 35 | * <p>Client connection which is spawned from the client machine and receives/ | 36 | * <p>Client connection which is spawned from the client machine and receives/ |
| 36 | * sends communication to/from the server. This class also holds a mirrored | 37 | * sends communication to/from the server. This class also holds a mirrored |
| 37 | * copy of the TableList and UserList objects which are stored on the | 38 | * copy of the TableList and UserList objects which are stored on the |
| 38 | * JogreServer.</p> | 39 | * JogreServer.</p> |
| 39 | * | 40 | * |
| 40 | * <p>The communication between this connection thread and the GUI (e.g. | 41 | * <p>The communication between this connection thread and the GUI (e.g. |
| 41 | * an implementation of the JogreClientFrame) must goes through | 42 | * an implementation of the JogreClientFrame) must goes through |
| 42 | * an interface IClient to ensure maximum abstraction.</p> | 43 | * an interface IClient to ensure maximum abstraction.</p> |
| 43 | * | 44 | * |
| 44 | * @author Bob Marks | 45 | * @author Bob Marks |
| 45 | * @version Beta 0.3 | 46 | * @version Beta 0.3 |
| 46 | */ | 47 | */ |
| 47 | public class ClientConnectionThread extends AbstractConnectionThread { | 48 | public class ClientConnectionThread extends AbstractConnectionThread { |
| 48 | 49 | ||
| 49 | /** Interface betweem this thread and the Frame */ | 50 | /** Interface betweem this thread and the Frame */ |
| 50 | protected IClient clientInterface; | 51 | protected IClient clientInterface; |
| 51 | 52 | ||
| 52 | /** Current game ID. */ | 53 | /** Current game ID. */ |
| 53 | protected String gameID = null; | 54 | protected String gameID = null; |
| 54 | 55 | ||
| 55 | /** Game associated with this connection thread (will be | 56 | /** Game associated with this connection thread (will be |
| 56 | * mirrored to that on the server. */ | 57 | * mirrored to that on the server. */ |
| 57 | protected Game game = null; | 58 | protected Game game = null; |
| 58 | 59 | ||
| 59 | /** Link to data communication receiver. */ | 60 | /** Link to data communication receiver. */ |
| 60 | private ClientCommDataReceiver commDataReceiver = null; | 61 | private ClientCommDataReceiver commDataReceiver = null; |
| 61 | 62 | ||
| 62 | /** | 63 | /** |
| 63 | * Default constructor which takes a Socket connection to the server, a | 64 | * Default constructor which takes a Socket connection to the server, a |
| 64 | * username and an IClient which sits between this class and the GUI. | 65 | * username and an IClient which sits between this class and the GUI. |
| 65 | * | 66 | * |
| 66 | * @param connection Socket connection to server. | 67 | * @param connection Socket connection to server. |
| 67 | * @param username Username | 68 | * @param username Username |
| 68 | * @param clientInterface Interface between this class and GUI. | 69 | * @param clientInterface Interface between this class and GUI. |
| 69 | */ | 70 | */ |
| 70 | public ClientConnectionThread (Socket connection, String username, IClient clientInterface) { | 71 | public ClientConnectionThread (Socket connection, String username, IClient clientInterface) { |
| 71 | super (connection); | 72 | super (new SocketBasedMessageBus(connection)); |
| 72 | 73 | ||
| 73 | this.username = username; | 74 | setUsername(username); |
| 74 | this.clientInterface = clientInterface; | 75 | this.clientInterface = clientInterface; |
| 75 | this.game = null; | 76 | this.game = null; |
| 76 | } | 77 | } |
| 77 | 78 | ||
| 78 | /** | 79 | /** |
| 79 | * Set the client interface. | 80 | * Set the client interface. |
| 80 | * | 81 | * |
| 81 | * @param clientInterface Client interface | 82 | * @param clientInterface Client interface |
| 82 | */ | 83 | */ |
| 83 | public void setClientInterface (IClient clientInterface) { | 84 | public void setClientInterface (IClient clientInterface) { |
| 84 | this.clientInterface = clientInterface; | 85 | this.clientInterface = clientInterface; |
| 85 | } | 86 | } |
| 86 | 87 | ||
| 87 | /** | 88 | /** |
| 88 | * Parse method which reads the first token of the message and delegate | 89 | * Parse method which reads the first token of the message and delegate |
| 89 | * to the implementating client. | 90 | * to the implementating client. |
| 90 | * | 91 | * |
| 91 | * @see org.jogre.common.AbstractConnectionThread#parse(nanoxml.XMLElement) | 92 | * @see org.jogre.common.AbstractConnectionThread#parse(nanoxml.XMLElement) |
| 92 | */ | 93 | */ |
| 93 | public void parse (XMLElement message) { | 94 | public void parse (XMLElement message) { |
| 94 | // Update data first of all | 95 | // Update data first of all |
| 95 | String sTableNum = message.getStringAttribute (CommTableMessage.XML_ATT_TABLE_NUM); | 96 | String sTableNum = message.getStringAttribute (CommTableMessage.XML_ATT_TABLE_NUM); |
| 96 | 97 | ||
| 97 | // If message contains a "table" attribute then delegate to a table message | 98 | // If message contains a "table" attribute then delegate to a table message |
| 98 | if (sTableNum == null) { | 99 | if (sTableNum == null) { |
| 99 | 100 | ||
| 100 | // Update data tree first | 101 | // Update data tree first |
| 101 | if (commDataReceiver != null) | 102 | if (commDataReceiver != null) |
| 102 | commDataReceiver.receiveGameMessage (message, username); | 103 | commDataReceiver.receiveGameMessage (message, getUsername()); |
| 103 | 104 | ||
| 104 | // Delegate to client interface for further processing | 105 | // Delegate to client interface for further processing |
| 105 | this.clientInterface.receiveGameMessage (message); | 106 | this.clientInterface.receiveGameMessage (message); |
| 106 | } | 107 | } |
| 107 | else { | 108 | else { |
| 108 | int tableNum = Integer.parseInt(sTableNum); | 109 | int tableNum = Integer.parseInt(sTableNum); |
| 109 | 110 | ||
| 110 | // Update data tree first | 111 | // Update data tree first |
| 111 | if (commDataReceiver != null) | 112 | if (commDataReceiver != null) |
| 112 | commDataReceiver.receiveTableMessage (message, username, tableNum); | 113 | commDataReceiver.receiveTableMessage (message, getUsername(), tableNum); |
| 113 | 114 | ||
| 114 | this.clientInterface.receiveTableMessage (message, tableNum); | 115 | this.clientInterface.receiveTableMessage (message, tableNum); |
| 115 | } | 116 | } |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 118 | /** | 119 | /** |
| 119 | * Send a ITransmittable object from a client to the server. | 120 | * Send a ITransmittable object from a client to the server. |
| 120 | * | 121 | * |
| 121 | * @param message | 122 | * @param message |
| 122 | */ | 123 | */ |
| 123 | public void send (CommGameMessage message) { | 124 | public void send (CommGameMessage message) { |
| 124 | super.send (message); | 125 | getMessageBus().send(message); |
| 125 | } | 126 | } |
| 126 | 127 | ||
| 127 | /** | 128 | /** |
| 128 | * Return the user list (should be the same as the server user | 129 | * Return the user list (should be the same as the server user |
| 129 | * list object). | 130 | * list object). |
| 130 | * | 131 | * |
| 131 | * @return List of users. | 132 | * @return List of users. |
| 132 | */ | 133 | */ |
| 133 | public UserList getUserList () { | 134 | public UserList getUserList () { |
| 134 | return this.game.getUserList(); | 135 | return this.game.getUserList(); |
| 135 | } | 136 | } |
| 136 | 137 | ||
| 137 | /** | 138 | /** |
| 138 | * Update the user list. | 139 | * Update the user list. |
| 139 | * | 140 | * |
| 140 | * @param userList List of users | 141 | * @param userList List of users |
| 141 | */ | 142 | */ |
| 142 | public void setUserList (UserList userList) { | 143 | public void setUserList (UserList userList) { |
| 143 | this.game.setUserList (userList); | 144 | this.game.setUserList (userList); |
| 144 | } | 145 | } |
| 145 | 146 | ||
| 146 | /** | 147 | /** |
| 147 | * Return the gameID. | 148 | * Return the gameID. |
| 148 | * | 149 | * |
| 149 | * @return Game ID. | 150 | * @return Game ID. |
| 150 | */ | 151 | */ |
| 151 | public String getGameID () { | 152 | public String getGameID () { |
| 152 | return this.gameID; | 153 | return this.gameID; |
| 153 | } | 154 | } |
| 154 | 155 | ||
| 155 | /** | 156 | /** |
| 156 | * Return the game object which should be mirrored to that | 157 | * Return the game object which should be mirrored to that |
| 157 | * on the server. | 158 | * on the server. |
| 158 | * | 159 | * |
| 159 | * @return Game object consisting of user and table lists. | 160 | * @return Game object consisting of user and table lists. |
| 160 | */ | 161 | */ |
| 161 | public Game getGame () { | 162 | public Game getGame () { |
| 162 | return this.game; | 163 | return this.game; |
| 163 | } | 164 | } |
| 164 | 165 | ||
| 165 | /** | 166 | /** |
| 166 | * Set the game object from the server. | 167 | * Set the game object from the server. |
| 167 | * | 168 | * |
| 168 | * @param game | 169 | * @param game |
| 169 | */ | 170 | */ |
| 170 | public void setGame (Game game) { | 171 | public void setGame (Game game) { |
| 171 | this.game = game; | 172 | this.game = game; |
| 172 | 173 | ||
| 173 | this.commDataReceiver = new ClientCommDataReceiver (game); | 174 | this.commDataReceiver = new ClientCommDataReceiver (game); |
| 174 | } | 175 | } |
| 175 | 176 | ||
| 176 | /** | 177 | /** |
| 177 | * Return the table list object (should be the same as the server table | 178 | * Return the table list object (should be the same as the server table |
| 178 | * list). | 179 | * list). |
| 179 | * | 180 | * |
| 180 | * @return TableList object (contains number of Table objects). | 181 | * @return TableList object (contains number of Table objects). |
| 181 | */ | 182 | */ |
| 182 | public TableList getTableList () { | 183 | public TableList getTableList () { |
| 183 | return this.game.getTableList(); | 184 | return this.game.getTableList(); |
| 184 | } | 185 | } |
| 185 | 186 | ||
| 186 | /** | 187 | /** |
| 187 | * Update the table list. | 188 | * Update the table list. |
| 188 | * | 189 | * |
| 189 | * @param tableList TableList object containing Table objects. | 190 | * @param tableList TableList object containing Table objects. |
| 190 | */ | 191 | */ |
| 191 | public void setTableList (TableList tableList) { | 192 | public void setTableList (TableList tableList) { |
| 192 | this.game.setTableList (tableList); | 193 | this.game.setTableList (tableList); |
| 193 | } | 194 | } |
| 194 | 195 | ||
| 195 | /** | 196 | /** |
| 196 | * Stop the thread. | 197 | * Stop the thread. |
| 197 | */ | 198 | */ |
| 198 | protected void disconnect () { | 199 | protected void disconnect () { |
| 199 | super.loop = false; // End thread | 200 | getMessageBus().close(); |
| 200 | } | 201 | } |
| 201 | 202 | ||
| 202 | /** | 203 | /** |
| 203 | * Client has exitted so clean everything up. | 204 | * Client has exitted so clean everything up. |
| 204 | * | 205 | * |
| 205 | * @see org.jogre.common.AbstractConnectionThread#cleanup() | 206 | * @see org.jogre.common.AbstractConnectionThread#cleanup() |
| 206 | */ | 207 | */ |
| 207 | public void cleanup () { | 208 | public void cleanup () { |
| 208 | // Clean up | 209 | // Clean up |
| 209 | } | 210 | } |
| 210 | } | 211 | } |