protected void processMessagesOnConnection() {
        try {
            logger.log(INFO, "Connected to " + getConnectionName());
            state.set(StreamState.CONNECTED);
            notifyConnection();
            inputBuffer.flip();

            while (!Thread.currentThread().isInterrupted() && isConnected()) {
                try {
                    // Find the start of message
                    byte byStart = 0;
                    while(byStart != START_OF_MSG && !Thread.currentThread().isInterrupted() && isConnected()) {
                        byStart = nextByte(inputBuffer);
                    }

                    // and then make sure there are enough bytes and read the protocol
                    readCompleteMessage(inputBuffer);
                    if(inputBuffer.get() != protocol.getKeyIdentifier()) throw new TcProtocolException("Bad protocol");

                    logByteBuffer("Line read from stream", inputBuffer);

                    // now we take a shallow buffer copy and process the message
                    MenuCommand mc = protocol.fromChannel(inputBuffer);
                    logger.log(INFO, "Command received: " + mc);
                    notifyListeners(mc);
                }
                catch(TcProtocolException ex) {
                    // a protocol problem shouldn't drop the connection
                    logger.log(WARNING, "Probable Bad message reason='{0}' Remote={1} ",
                               ex.getMessage(), getConnectionName());
                }
            }
            logger.log(INFO, "Disconnected from " + getConnectionName());
        } catch (Exception e) {
            logger.log(ERROR, "Problem with connectivity on " + getConnectionName(), e);
        }
        finally {
            close();
        }
    }
protected void processMessagesOnConnection ( ) { try { logger . log ( INFO , "Connected to " + getConnectionName ( ) ) ; state . set ( StreamState . CONNECTED ) ; notifyConnection ( ) ; inputBuffer . flip ( ) ; while ( ! Thread . currentThread ( ) . isInterrupted ( ) && isConnected ( ) ) { try { byte byStart = 0 ; while ( byStart != START_OF_MSG && ! Thread . currentThread ( ) . isInterrupted ( ) && isConnected ( ) ) { byStart = nextByte ( inputBuffer ) ; } readCompleteMessage ( inputBuffer ) ; if ( inputBuffer . get ( ) != protocol . getKeyIdentifier ( ) ) throw new TcProtocolException ( "Bad protocol" ) ; logByteBuffer ( "Line read from stream" , inputBuffer ) ; MenuCommand mc = protocol . fromChannel ( inputBuffer ) ; logger . log ( INFO , "Command received: " + mc ) ; notifyListeners ( mc ) ; } catch ( TcProtocolException ex ) { logger . log ( WARNING , "Probable Bad message reason='{0}' Remote={1} " , ex . getMessage ( ) , getConnectionName ( ) ) ; } } logger . log ( INFO , "Disconnected from " + getConnectionName ( ) ) ; } catch ( Exception e ) { logger . log ( ERROR , "Problem with connectivity on " + getConnectionName ( ) , e ) ; } finally { close ( ) ; } }
This is the loop that handles a connection by reading messages until there's an IOException, or the transport
or thread change to an end state.