Package org.apache.mina.example.sumup.message

Examples of org.apache.mina.example.sumup.message.ResultMessage


        super(Constants.RESULT);
    }

    protected void encodeBody(IoSession session, AbstractMessage message,
            ByteBuffer out) {
        ResultMessage m = (ResultMessage) message;
        if (m.isOk()) {
            out.putShort((short) Constants.RESULT_OK);
            out.putInt(m.getValue());
        } else {
            out.putShort((short) Constants.RESULT_ERROR);
        }
    }
View Full Code Here


        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 messageReceived(IoSession session, Object message) {
        // server only sends ResultMessage. otherwise, we will have to identify
        // its type using instanceof operator.
        ResultMessage rm = (ResultMessage) message;
        if (rm.isOk()) {
            // server returned OK code.
            // if received the result message which has the last sequence
            // number,
            // it is time to disconnect.
            if (rm.getSequence() == values.length - 1) {
                // print the sum and disconnect.
                SessionLog.info(session, "The sum: " + rm.getValue());
                session.close();
                finished = true;
            }
        } else {
            // seever returned error code because of overflow, etc.
View Full Code Here

        int sum = ((Integer) session.getAttribute(SUM_KEY)).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.setAttribute(SUM_KEY, 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

        if (code == Constants.RESULT_OK) {
            if (in.remaining() < Constants.RESULT_VALUE_LEN) {
                return null;
            }

            ResultMessage m = new ResultMessage();
            m.setOk(true);
            m.setValue(in.getInt());
            readCode = false;
            return m;
        } else {
            ResultMessage m = new ResultMessage();
            m.setOk(false);
            readCode = false;
            return m;
        }
    }
View Full Code Here

        int sum = ((Integer) session.getAttribute(SUM_KEY)).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.setAttribute(SUM_KEY, 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

    @Override
    public void messageReceived(IoSession session, Object message) {
        // server only sends ResultMessage. otherwise, we will have to identify
        // its type using instanceof operator.
        ResultMessage rm = (ResultMessage) message;
        if (rm.isOk()) {
            // server returned OK code.
            // if received the result message which has the last sequence
            // number,
            // it is time to disconnect.
            if (rm.getSequence() == values.length - 1) {
                // print the sum and disconnect.
                LOGGER.info("The sum: " + rm.getValue());
                session.close(true);
                finished = true;
            }
        } else {
            // seever returned error code because of overflow, etc.
View Full Code Here

        if (code == Constants.RESULT_OK) {
            if (in.remaining() < Constants.RESULT_VALUE_LEN) {
                return null;
            }

            ResultMessage m = new ResultMessage();
            m.setOk(true);
            m.setValue(in.getInt());
            readCode = false;
            return m;
        } else {
            ResultMessage m = new ResultMessage();
            m.setOk(false);
            readCode = false;
            return m;
        }
    }
View Full Code Here

        int sum = ((Integer) session.getAttribute(SUM_KEY)).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.setAttribute(SUM_KEY, 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

    @Override
    public void messageReceived(IoSession session, Object message) {
        // server only sends ResultMessage. otherwise, we will have to identify
        // its type using instanceof operator.
        ResultMessage rm = (ResultMessage) message;
        if (rm.isOk()) {
            // server returned OK code.
            // if received the result message which has the last sequence
            // number,
            // it is time to disconnect.
            if (rm.getSequence() == values.length - 1) {
                // print the sum and disconnect.
                LOGGER.info("The sum: " + rm.getValue());
                session.close(true);
                finished = true;
            }
        } else {
            // seever returned error code because of overflow, etc.
View Full Code Here

TOP

Related Classes of org.apache.mina.example.sumup.message.ResultMessage

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.