Source of MessageReceiver.java


  1: // Fig. 24.22: MessageReceiver.java
  2: // MessageReceiver is a Runnable that listens for messages from a 
  3: // particular client and delivers messages to a MessageListener.
  4: package com.deitel.messenger.sockets.server;
  5: 
  6: import java.io.BufferedReader;
  7: import java.io.IOException;
  8: import java.io.InputStreamReader;
  9: import java.net.Socket;
 10: import java.net.SocketTimeoutException;
 11: import java.util.StringTokenizer;
 12: 
 13: import com.deitel.messenger.MessageListener;
 14: import static com.deitel.messenger.sockets.SocketMessengerConstants.*;
 15: 
 16: public class MessageReceiver implements Runnable
 17: {
 18:    private BufferedReader input; // input stream
 19:    private MessageListener messageListener; // message listener
 20:    private boolean keepListening = true; // when false, ends runnable
 21:    
 22:    // MessageReceiver constructor
 23:    public MessageReceiver( MessageListener listener, Socket clientSocket ) 
 24:    {
 25:       // set listener to which new messages should be sent
 26:       messageListener = listener;
 27:       
 28:       try 
 29:       {
 30:          // set timeout for reading from client
 31:          clientSocket.setSoTimeout( 5000 ); // five seconds
 32:          
 33:          // create BufferedReader for reading incoming messages
 34:          input = new BufferedReader( new InputStreamReader( 
 35:             clientSocket.getInputStream() ) );
 36:       } // end try
 37:       catch ( IOException ioException ) 
 38:       {
 39:          ioException.printStackTrace();
 40:       } // end catch
 41:    } // end MessageReceiver constructor
 42:    
 43:    // listen for new messages and deliver them to MessageListener
 44:    public void run() 
 45:    {    
 46:       String message; // String for incoming messages
 47:       
 48:       // listen for messages until stopped
 49:       while ( keepListening ) 
 50:       {   
 51:          try 
 52:          {            
 53:             message = input.readLine(); // read message from client
 54:          } // end try
 55:          catch ( SocketTimeoutException socketTimeoutException ) 
 56:          {
 57:             continue; // continue to next iteration to keep listening
 58:          } // end catch
 59:          catch ( IOException ioException ) 
 60:          {
 61:             ioException.printStackTrace();            
 62:             break;
 63:          } // end catch
 64: 
 65:          // ensure non-null message
 66:          if ( message != null ) 
 67:          {
 68:             // tokenize message to retrieve user name and message body
 69:             StringTokenizer tokenizer = new StringTokenizer( 
 70:                message, MESSAGE_SEPARATOR );
 71: 
 72:             // ignore messages that do not contain a user
 73:             // name and message body
 74:             if ( tokenizer.countTokens() == 2 ) 
 75:             {
 76:                // send message to MessageListener
 77:                messageListener.messageReceived( 
 78:                   tokenizer.nextToken(), // user name
 79:                   tokenizer.nextToken() ); // message body
 80:             } // end if
 81:             else
 82:             {
 83:                // if disconnect message received, stop listening
 84:                if ( message.equalsIgnoreCase(
 85:                   MESSAGE_SEPARATOR + DISCONNECT_STRING ) ) 
 86:                   stopListening();
 87:             } // end else
 88:          } // end if
 89:       } // end while  
 90:       
 91:       try
 92:       {         
 93:          input.close(); // close BufferedReader (also closes Socket)
 94:       } // end try
 95:       catch ( IOException ioException ) 
 96:       {
 97:          ioException.printStackTrace();     
 98:       } // end catch 
 99:    } // end method run
100:    
101:    // stop listening for incoming messages
102:    public void stopListening() 
103:    {
104:       keepListening = false;
105:    } // end method stopListening
106: } // end class MessageReceiver
107: 
108: 
109: /**************************************************************************
110:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
111:  * Pearson Education, Inc. All Rights Reserved.                           *
112:  *                                                                        *
113:  * DISCLAIMER: The authors and publisher of this book have used their     *
114:  * best efforts in preparing the book. These efforts include the          *
115:  * development, research, and testing of the theories and programs        *
116:  * to determine their effectiveness. The authors and publisher make       *
117:  * no warranty of any kind, expressed or implied, with regard to these    *
118:  * programs or to the documentation contained in these books. The authors *
119:  * and publisher shall not be liable in any event for incidental or       *
120:  * consequential damages in connection with, or arising out of, the       *
121:  * furnishing, performance, or use of these programs.                     *
122:  *************************************************************************/