My favorites | Sign in
Project Logo
                
Changes to /trunk/api/src/org/jogre/common/AbstractConnectionThread.java
r2 vs. r16   Edit
  Compare: vs.   Format:
Revision r16
Go to: 
Project members, sign in to write a code review
/trunk/api/src/org/jogre/common/AbstractConnectionThread.java   r2 /trunk/api/src/org/jogre/common/AbstractConnectionThread.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.common; 20 package org.jogre.common;
21 21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.PrintStream;
26 import java.net.Socket;
27 import java.net.SocketException;
28
29 import nanoxml.XMLElement;
30 import nanoxml.XMLParseException;
31
32 import org.jogre.common.comm.ITransmittable;
33 import org.jogre.common.util.JogreLogger;
34
35 /** 22 /**
36 * Abstract connection thread which is spawned with each client. This extends a 23 * Represents a connection which is spawned with each client. This
37 * thread and stores a Socket to the client, and a BufferedReader and PrintStream 24 * stores a way to communicate (read/write) Strings to the user/server.
38 * which can read/write Strings to the user/server. Also the username of the 25 * Also the username of the client is stored in the username String.
39 * client is stored in the username String.
40 * 26 *
41 * @author Bob Marks 27 * @author Bob Marks
42 * @version Alpha 0.2.3 28 * @version Alpha 0.2.3
43 */ 29 */
44 public abstract class AbstractConnectionThread extends Thread { 30 public abstract class AbstractConnectionThread implements MessageBus.MessageParser {
45 31
46 /** Logging */ 32 /** Communication between the server and the user. */
47 JogreLogger logger = new JogreLogger (this.getClass()); 33 private final MessageBus messageBus;
48
49 /** Socket between the server and the user. */
50 protected Socket socket;
51
52 /** Buffered input. */
53 protected BufferedReader in;
54
55 /** PrintStream for the output. */
56 private PrintStream out;
57 34
58 /** Username of the client. */ 35 /** Username of the client. */
59 protected String username; 36 private String username;
60
61 /** When the boolean loop becomes false the Thread finishes. */
62 protected boolean loop = true;
63
64 /** All clients start initially with "connected" equal to false
65 * (although they can still recieve/transfer logon information). */
66 protected boolean connected = false;
67
68 /**
69 * This abstract method must be overwritten by a child which extends this
70 * class.
71 *
72 * @param message Communication as an XML object.
73 * @throws TransmissionException This is thrown if there is a problem parsing the String.
74 */
75 public abstract void parse (XMLElement message) throws TransmissionException;
76
77 /**
78 * This method is called to properly clean up after a client.
79 */
80 public abstract void cleanup ();
81 37
82 /** 38 /**
83 * Constructor for a connection which takes a Socket and sends up the input 39 * Constructor for a connection which takes a Socket and sends up the input
84 * and output stream. 40 * and output stream.
85 * 41 *
86 * @param socket Socket connection to client / server. 42 * @param messageBus connection to client / server.
87 */ 43 */
88 public AbstractConnectionThread (Socket socket) { 44 public AbstractConnectionThread (MessageBus messageBus) {
89 try { 45 this.messageBus = messageBus;
90 logger.debug ("AbstractConnectionThread", "Creating new in/out streams."); 46 if (messageBus == null) {
91 47 throw new NullPointerException("messageBus must not be null");
92 setSocket (socket); 48 }
93 }
94 catch (IOException ioEx) {
95 logger.error ("AbstractConnectionThread", "IO Exception.");
96 logger.stacktrace (ioEx);
97 }
98 } 49 }
99 50
100 /** 51 /**
101 * @param socket 52 * @return the messaging bus that this thread uses
102 * @throws IOException
103 */ 53 */
104 protected void setSocket (Socket socket) throws IOException { 54 public MessageBus getMessageBus() {
105 this.socket = socket; 55 return messageBus;
106
107 if (socket != null) {
108 in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
109 out = new PrintStream (socket.getOutputStream());
110 }
111 }
112
113 /**
114 * Run method - runs until an exception has occured or the loop variable
115 * becomes false.
116 *
117 * @see java.lang.Runnable#run()
118 */
119 public void run () {
120 logger.debug("run", "Staring thread.");
121
122 try {
123 while (loop) {
124 // listen for input from the user
125 String inString = "";
126
127 while (loop && inString!=null && inString.equals("")) {
128 if (in == null) {
129 cleanup ();
130 return;
131 }
132 inString = in.readLine();
133 }
134
135 // Check input.
136 if (in == null) {
137 cleanup ();
138 return;
139 }
140
141 // Ensure that the communication is XML (starts with a '<' character)
142 // as any sort of client could send communication to the server.
143 if (inString != null) {
144 if (inString.startsWith("<")) {
145
146 // Starts with an '<' so try and parse this XML
147 XMLElement message = new XMLElement ();
148
149 try {
150 message.parseString (inString);
151
152 // parse this element
153 if (message != null)
154 parse (message);
155 }
156 catch (XMLParseException xmlParseEx) {
157 logger.error ("run", "problem parsing: " + inString);
158 logger.stacktrace (xmlParseEx);
159 }
160 }
161 }
162 }
163 }
164 catch (SocketException sEx) {
165 logger.debug ("run", "Connection lost");
166 logger.stacktrace (sEx);
167 }
168 catch (IOException ioEx) {
169 logger.error ("run", "IO Exception: ");
170 logger.stacktrace (ioEx);
171 }
172 catch (Exception genEx) {
173 genEx.printStackTrace();
174 logger.error ("run", "General Exception: ");
175 logger.stacktrace (genEx);
176 }
177
178 connected = false;
179 cleanup ();
180 }
181
182 /**
183 * Stop the loop.
184 */
185 public void stopLoop () {
186 this.loop = false;
187 }
188
189 /**
190 * Set boolean to specify that this client has connected sucessfully.
191 */
192 public void connect () {
193 this.connected = true;
194 }
195
196 /**
197 * Send a ITransmittable object to the output stream (could be server or
198 * client).
199 *
200 * @param transObject
201 */
202 protected void send (ITransmittable transObject) {
203 // Retrieve XMLElement from the object and flatten to a String.
204 String message = transObject.flatten().toString();
205
206 // Send down the socket to the receiving end
207 out.println (message);
208 } 56 }
209 57
210 /** 58 /**
211 * Returns the username. 59 * Returns the username.
212 * 60 *
213 * @return Username of client / server who created this thread. 61 * @return Username of client / server who created this thread.
214 */ 62 */
215 public String getUsername() { 63 public String getUsername() {
216 return username; 64 return username;
217 } 65 }
218 66
219 /** 67 /**
220 * Set the username. 68 * Set the username.
221 * 69 *
222 * @param username Username of client / server who created this thread. 70 * @param username Username of client / server who created this thread.
223 */ 71 */
224 public void setUsername (String username) { 72 public void setUsername (String username) {
225 this.username = username; 73 this.username = username;
226 } 74 }
227 } 75 }
Hosted by Google Code