Package org.apache.mina.examples.sumup.message

Examples of org.apache.mina.examples.sumup.message.AddMessage


    public void messageReceived( ProtocolSession session, Object message )
    {
        // client only sends AddMessage. otherwise, we will have to identify
        // its type using instanceof operator.
        AddMessage am = ( AddMessage ) message;

        // add the value to the current sum.
        int sum = ( ( Integer ) session.getAttachment() ).intValue();
        int value = am.getValue();
        long expectedSum = ( long ) sum + value;
        if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
        {
            // if the sum overflows or underflows, return error message
            ResultMessage rm = new ResultMessage();
            rm.setSequence( am.getSequence() ); // copy sequence
            rm.setOk( false );
            session.write( rm );
        }
        else
        {
            // sum up
            sum = ( int ) expectedSum;
            session.setAttachment( new Integer( sum ) );

            // return the result message
            ResultMessage rm = new ResultMessage();
            rm.setSequence( am.getSequence() ); // copy sequence
            rm.setOk( true );
            rm.setValue( sum );
            session.write( rm );
        }
    }
View Full Code Here


    public void sessionOpened( ProtocolSession session )
    {
        // send summation requests
        for( int i = 0; i < values.length; i++ )
        {
            AddMessage m = new AddMessage();
            m.setSequence( i );
            m.setValue( values[ i ] );
            session.write( m );
        }
    }
View Full Code Here

        if( in.remaining() < Constants.ADD_BODY_LEN )
        {
            return null;
        }

        AddMessage m = new AddMessage();
        m.setValue( in.getInt() );
        return m;
    }
View Full Code Here

        super( Constants.ADD );
    }

    protected void encodeBody( IoSession session, AbstractMessage message, ByteBuffer out )
    {
        AddMessage m = ( AddMessage ) message;
        out.putInt( m.getValue() );
    }
View Full Code Here

        if( in.remaining() < Constants.ADD_BODY_LEN )
        {
            return null;
        }

        AddMessage m = new AddMessage();
        m.setValue( in.getInt() );
        return m;
    }
View Full Code Here

        super( Constants.ADD );
    }

    protected void encodeBody( IoSession session, AbstractMessage message, ByteBuffer out )
    {
        AddMessage m = ( AddMessage ) message;
        out.putInt( m.getValue() );
    }
View Full Code Here

    public void messageReceived( IoSession session, Object message )
    {
        // client only sends AddMessage. otherwise, we will have to identify
        // its type using instanceof operator.
        AddMessage am = ( AddMessage ) message;

        // add the value to the current sum.
        int sum = ( ( Integer ) session.getAttachment() ).intValue();
        int value = am.getValue();
        long expectedSum = ( long ) sum + value;
        if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
        {
            // if the sum overflows or underflows, return error message
            ResultMessage rm = new ResultMessage();
            rm.setSequence( am.getSequence() ); // copy sequence
            rm.setOk( false );
            session.write( rm );
        }
        else
        {
            // sum up
            sum = ( int ) expectedSum;
            session.setAttachment( new Integer( sum ) );

            // return the result message
            ResultMessage rm = new ResultMessage();
            rm.setSequence( am.getSequence() ); // copy sequence
            rm.setOk( true );
            rm.setValue( sum );
            session.write( rm );
        }
    }
View Full Code Here

    public void sessionOpened( IoSession session )
    {
        // send summation requests
        for( int i = 0; i < values.length; i++ )
        {
            AddMessage m = new AddMessage();
            m.setSequence( i );
            m.setValue( values[ i ] );
            session.write( m );
        }
    }
View Full Code Here

        super( Constants.ADD );
    }

    protected void encodeBody( ProtocolSession session, AbstractMessage message, ByteBuffer out )
    {
        AddMessage m = ( AddMessage ) message;
        out.putInt( m.getValue() );
    }
View Full Code Here

    public void messageReceived( ProtocolSession session, Object message )
    {
        // client only sends AddMessage. otherwise, we will have to identify
        // its type using instanceof operator.
        AddMessage am = ( AddMessage ) message;

        // add the value to the current sum.
        int sum = ( ( Integer ) session.getAttachment() ).intValue();
        int value = am.getValue();
        long expectedSum = ( long ) sum + value;
        if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
        {
            // if the sum overflows or underflows, return error message
            ResultMessage rm = new ResultMessage();
            rm.setSequence( am.getSequence() ); // copy sequence
            rm.setOk( false );
            session.write( rm );
        }
        else
        {
            // sum up
            sum = ( int ) expectedSum;
            session.setAttachment( new Integer( sum ) );

            // return the result message
            ResultMessage rm = new ResultMessage();
            rm.setSequence( am.getSequence() ); // copy sequence
            rm.setOk( true );
            rm.setValue( sum );
            session.write( rm );
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.mina.examples.sumup.message.AddMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.