Clover coverage report -
Coverage timestamp: Sun Oct 12 2003 22:57:21 PDT
file stats: LOC: 155   Methods: 4
NCLOC: 67   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ClientReader.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
 
 3   
 VRMoo Server - Virtual Reality Object Oriented MUD Server
 4   
 Copyright (C) 2001 - 2003  VRMoo Development Team
 5   
 
 6   
 
 7   
 This program is free software; you can redistribute it and/or modify
 8   
 it under the terms of the GNU General Public License as published by
 9   
 the Free Software Foundation; either version 2 of the License, or
 10   
 (at your option) any later version.
 11   
 
 12   
 This program is distributed in the hope that it will be useful,
 13   
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   
 GNU General Public License for more details.
 16   
 
 17   
 You should have received a copy of the GNU General Public License
 18   
 along with this program; if not, write to the Free Software
 19   
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20   
 
 21   
 
 22   
 For information about VRMoo and its authors, please visit the website:
 23   
 http://www.vrmoo.org/
 24   
 
 25   
 */
 26   
 
 27   
 package org.vrmoo.server.client;
 28   
 
 29   
 import java.io.IOException;
 30   
 import java.io.InputStream;
 31   
 
 32   
 /**
 33   
  * The <code>ClientReader</code> class is responsible for reading data from a
 34   
  * client socket connected to the server.
 35   
  * The constructor starts up a thread whose sole responsibility is to read data
 36   
  * from the client socket.
 37   
  *
 38   
  * @author Jeff Weston
 39   
  */
 40   
 public class ClientReader
 41   
 {
 42   
     /**
 43   
      * The <code>InputStream</code> attached to the client socket.
 44   
      */
 45   
     private InputStream in;
 46   
 
 47   
     /**
 48   
      * The <code>ClientHandler</code> associated with this
 49   
      * <code>ClientReader</code>.
 50   
      */
 51   
     private ClientHandler clientHandler;
 52   
 
 53   
     /**
 54   
      * The stop flag is set to false when running, true when we need to stop.
 55   
      */
 56   
     private boolean stopFlag = false;
 57   
 
 58   
     /**
 59   
      * Stores the string of text that the client connected to this socket
 60   
      * is currently typing.
 61   
      */
 62   
     private StringBuffer input;
 63   
 
 64   
     /**
 65   
      * Creates a thread whose sole responsibility is to read data from the
 66   
      * specified <code>InputStream</code>.
 67   
      *
 68   
      * @param streamIn        the <code>InputStream</code> to read data from
 69   
      * @param clientHandler   the <code>ClientHandler</code> associated with
 70   
      *                          this reader
 71   
      */
 72  0
     public ClientReader( InputStream streamIn, ClientHandler clientHandler )
 73   
     {
 74  0
         in                 = streamIn;
 75  0
         this.clientHandler = clientHandler;
 76  0
         String threadName  = "ClientReader" + clientHandler.getClientID( );
 77   
 
 78  0
         new Thread( new Loop( ), threadName ).start( );
 79   
     }
 80   
 
 81   
     /**
 82   
      * Specifies that the <code>ClientReader</code> thread must stop by setting
 83   
      * the <code>stopFlag</code> to true.
 84   
      */
 85  0
     public synchronized void stop( )
 86   
     {
 87  0
         stopFlag = true;
 88   
     }
 89   
 
 90   
     /**
 91   
      * Checks the <code>stopFlag</code> to see if we are supposed to stop.
 92   
      *
 93   
      * @return true if we are supposed to stop, otherwise return false
 94   
      */
 95  0
     private synchronized boolean checkStopFlag( )
 96   
     {
 97  0
         return stopFlag;
 98   
     }
 99   
 
 100   
     /**
 101   
      * Private inner class implementing Runnable to make this class thread-safe.
 102   
      * The thread that is started can only be called from the constructor.
 103   
      */
 104   
     private class Loop implements Runnable
 105   
     {
 106   
         /**
 107   
          * Continuously reads data from the <code>InputStream</code> bound to
 108   
          * this class. This method is called by the thread started from the
 109   
          * constructor.
 110   
          */
 111  0
         public void run( )
 112   
         {
 113  0
             int iData;
 114   
 
 115  0
             input = new StringBuffer( );
 116   
 
 117  0
             try
 118   
             {
 119  0
                 iData = in.read( );
 120  0
                 while ( ( iData >= 0 ) && ( ! checkStopFlag( ) ) )
 121   
                 {
 122  0
                     char c = ( char ) iData;
 123  0
                     if ( ( c == '\r' ) || ( c == '\n' ) )
 124   
                     {
 125  0
                         if ( ( input.length( ) > 0 ) && ( c == '\n' ) )
 126   
                         {
 127  0
                             try
 128   
                             {
 129  0
                                 clientHandler.processCommand(
 130   
                                         input.toString( ) );
 131   
                             }
 132   
                             catch ( Exception e )
 133   
                             {
 134  0
                                 e.printStackTrace( );
 135   
                             }
 136   
 
 137  0
                             input = new StringBuffer( );
 138   
                         }
 139   
                     }
 140   
                     else
 141   
                     {
 142  0
                         input.append( c );
 143   
                     }
 144  0
                     iData = in.read( );
 145   
                 }
 146  0
                 clientHandler.clientDisconnected( );
 147   
             }
 148   
             catch ( IOException e )
 149   
             {
 150  0
                 clientHandler.clientDisconnected( );
 151   
             }
 152   
         }
 153   
     }
 154   
 }
 155