/** * ReceivingThread.java */ package boondock.holdem.Server; import java.io.*; import java.net.*; import java.util.StringTokenizer; import boondock.holdem.Network.*; /** * ReceivingThread is a Thread extended to listen for messages from our client and delivers them to our Listener. * * @author Jonathan O'Keefe * @author Scott Semonian * @author Matt Brinza * @author Hamid R. Tahsildoost * * @author The Boondock Saints * @author No Limit Texas Holdem */ public class ReceivingThread extends Thread { private BufferedReader in; private MessageListener messageListener; private boolean listening = true; private Socket client; /** * ReceivingThread constructor. * * @param listener Listener where new messages should be sent. * @param clientSocket to read from. */ public ReceivingThread( MessageListener listener, Socket clientSocket ){ super("ReceivingThread: " + clientSocket); client = clientSocket; messageListener = listener; try { clientSocket.setSoTimeout( 10000 ); in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream() ) ); } catch (IOException ioException) { //ioException.printStackTrace(); } } /** * Listen for new messages and deliver them to the MessageListener. * This function is synchronized. */ public synchronized void run() { String text = ""; while (listening) { try { text = in.readLine(); } catch (SocketException e) {//abrupt disconnect stopListening(); server.log.info( "Disconnected from: " + client.getInetAddress() ); } catch (InterruptedIOException interruptedIOException) { continue; } catch (IOException ioException) { //ioException.printStackTrace(); break; } if (text != null) { StringTokenizer token = new StringTokenizer(text, constants.MESSAGE_SEPARATOR); if (token.countTokens() == 2) messageListener.messageReceived(token.nextToken(),token.nextToken()); else if(text.equalsIgnoreCase(constants.MESSAGE_SEPARATOR + constants.DISCONNECT_STRING)){ stopListening(); server.log.info( "Disconnected from: " + client.getInetAddress() ); } } } try { in.close(); } catch (IOException ioException) { //ioException.printStackTrace(); } } /** * Stop listening for new messages. * This function is synchronized. */ public synchronized void stopListening(){ listening = false; } }//end of RecievingThread class