View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.sumup;
21  
22  import org.apache.mina.core.service.IoHandler;
23  import org.apache.mina.core.service.IoHandlerAdapter;
24  import org.apache.mina.core.session.IdleStatus;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.example.sumup.message.AddMessage;
27  import org.apache.mina.example.sumup.message.ResultMessage;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * {@link IoHandler} for SumUp server.
33   *
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class ServerSessionHandler extends IoHandlerAdapter {
37      
38      private static final String SUM_KEY = "sum";
39  
40      private final static Logger LOGGER = LoggerFactory.getLogger(ServerSessionHandler.class);
41      
42      @Override
43      public void sessionOpened(IoSession session) {
44          // set idle time to 60 seconds
45          session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
46  
47          // initial sum is zero
48          session.setAttribute(SUM_KEY, new Integer(0));
49      }
50  
51      @Override
52      public void messageReceived(IoSession session, Object message) {
53          // client only sends AddMessage. otherwise, we will have to identify
54          // its type using instanceof operator.
55          AddMessage am = (AddMessage) message;
56  
57          // add the value to the current sum.
58          int sum = ((Integer) session.getAttribute(SUM_KEY)).intValue();
59          int value = am.getValue();
60          long expectedSum = (long) sum + value;
61          if (expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE) {
62              // if the sum overflows or underflows, return error message
63              ResultMessage rm = new ResultMessage();
64              rm.setSequence(am.getSequence()); // copy sequence
65              rm.setOk(false);
66              session.write(rm);
67          } else {
68              // sum up
69              sum = (int) expectedSum;
70              session.setAttribute(SUM_KEY, new Integer(sum));
71  
72              // return the result message
73              ResultMessage rm = new ResultMessage();
74              rm.setSequence(am.getSequence()); // copy sequence
75              rm.setOk(true);
76              rm.setValue(sum);
77              session.write(rm);
78          }
79      }
80  
81      @Override
82      public void sessionIdle(IoSession session, IdleStatus status) {
83          LOGGER.info("Disconnecting the idle.");
84          // disconnect an idle client
85          session.close(true);
86      }
87  
88      @Override
89      public void exceptionCaught(IoSession session, Throwable cause) {
90          // close the connection on exceptional situation
91          session.close(true);
92      }
93  }