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 The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
36   */
37  public class ServerSessionHandler extends IoHandlerAdapter {
38      
39      private static final String SUM_KEY = "sum";
40  
41      private final Logger logger = LoggerFactory.getLogger(getClass());    
42      
43      @Override
44      public void sessionOpened(IoSession session) {
45          // set idle time to 60 seconds
46          session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
47  
48          // initial sum is zero
49          session.setAttribute(SUM_KEY, new Integer(0));
50      }
51  
52      @Override
53      public void messageReceived(IoSession session, Object message) {
54          // client only sends AddMessage. otherwise, we will have to identify
55          // its type using instanceof operator.
56          AddMessage am = (AddMessage) message;
57  
58          // add the value to the current sum.
59          int sum = ((Integer) session.getAttribute(SUM_KEY)).intValue();
60          int value = am.getValue();
61          long expectedSum = (long) sum + value;
62          if (expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE) {
63              // if the sum overflows or underflows, return error message
64              ResultMessage rm = new ResultMessage();
65              rm.setSequence(am.getSequence()); // copy sequence
66              rm.setOk(false);
67              session.write(rm);
68          } else {
69              // sum up
70              sum = (int) expectedSum;
71              session.setAttribute(SUM_KEY, new Integer(sum));
72  
73              // return the result message
74              ResultMessage rm = new ResultMessage();
75              rm.setSequence(am.getSequence()); // copy sequence
76              rm.setOk(true);
77              rm.setValue(sum);
78              session.write(rm);
79          }
80      }
81  
82      @Override
83      public void sessionIdle(IoSession session, IdleStatus status) {
84          logger.info("Disconnecting the idle.");
85          // disconnect an idle client
86          session.close();
87      }
88  
89      @Override
90      public void exceptionCaught(IoSession session, Throwable cause) {
91          // close the connection on exceptional situation
92          session.close();
93      }
94  }