001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.sumup;
021
022import org.apache.mina.core.service.IoHandler;
023import org.apache.mina.core.service.IoHandlerAdapter;
024import org.apache.mina.core.session.IdleStatus;
025import org.apache.mina.core.session.IoSession;
026import org.apache.mina.example.sumup.message.AddMessage;
027import org.apache.mina.example.sumup.message.ResultMessage;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * {@link IoHandler} for SumUp server.
033 *
034 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
035 */
036public class ServerSessionHandler extends IoHandlerAdapter {
037    
038    private static final String SUM_KEY = "sum";
039
040    private final static Logger LOGGER = LoggerFactory.getLogger(ServerSessionHandler.class);
041    
042    @Override
043    public void sessionOpened(IoSession session) {
044        // set idle time to 60 seconds
045        session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
046
047        // initial sum is zero
048        session.setAttribute(SUM_KEY, Integer.valueOf(0));
049    }
050
051    @Override
052    public void messageReceived(IoSession session, Object message) {
053        // client only sends AddMessage. otherwise, we will have to identify
054        // its type using instanceof operator.
055        AddMessage am = (AddMessage) message;
056
057        // add the value to the current sum.
058        int sum = ((Integer) session.getAttribute(SUM_KEY)).intValue();
059        int value = am.getValue();
060        long expectedSum = (long) sum + value;
061        if (expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE) {
062            // if the sum overflows or underflows, return error message
063            ResultMessage rm = new ResultMessage();
064            rm.setSequence(am.getSequence()); // copy sequence
065            rm.setOk(false);
066            session.write(rm);
067        } else {
068            // sum up
069            sum = (int) expectedSum;
070            session.setAttribute(SUM_KEY, Integer.valueOf(sum));
071
072            // return the result message
073            ResultMessage rm = new ResultMessage();
074            rm.setSequence(am.getSequence()); // copy sequence
075            rm.setOk(true);
076            rm.setValue(sum);
077            session.write(rm);
078        }
079    }
080
081    @Override
082    public void sessionIdle(IoSession session, IdleStatus status) {
083        LOGGER.info("Disconnecting the idle.");
084        // disconnect an idle client
085        session.closeNow();
086    }
087
088    @Override
089    public void exceptionCaught(IoSession session, Throwable cause) {
090        // close the connection on exceptional situation
091        session.closeNow();
092    }
093}