View Javadoc

1   /*
2    *   @(#) $Id: ServerSessionHandler.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.examples.sumup;
20  
21  import org.apache.mina.common.IdleStatus;
22  import org.apache.mina.examples.sumup.message.AddMessage;
23  import org.apache.mina.examples.sumup.message.ResultMessage;
24  import org.apache.mina.protocol.ProtocolHandler;
25  import org.apache.mina.protocol.ProtocolSession;
26  import org.apache.mina.protocol.filter.ProtocolLoggingFilter;
27  import org.apache.mina.util.SessionLog;
28  
29  /***
30   * {@link ProtocolHandler} for SumUp server.
31   * 
32   * @author The Apache Directory Project
33   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
34   */
35  public class ServerSessionHandler implements ProtocolHandler
36  {
37  
38      public ServerSessionHandler()
39      {
40      }
41  
42      public void sessionCreated( ProtocolSession session ) throws Exception
43      {
44          session.getFilterChain().addLast(
45                  "logger", new ProtocolLoggingFilter() );
46      }
47  
48      public void sessionOpened( ProtocolSession session )
49      {
50          // set idle time to 60 seconds
51          session.getConfig().setIdleTime( IdleStatus.BOTH_IDLE, 60 );
52  
53          // initial sum is zero
54          session.setAttachment( new Integer( 0 ) );
55      }
56  
57      public void sessionClosed( ProtocolSession session )
58      {
59      }
60  
61      public void messageReceived( ProtocolSession session, Object message )
62      {
63          // client only sends AddMessage. otherwise, we will have to identify
64          // its type using instanceof operator.
65          AddMessage am = ( AddMessage ) message;
66  
67          // add the value to the current sum.
68          int sum = ( ( Integer ) session.getAttachment() ).intValue();
69          int value = am.getValue();
70          long expectedSum = ( long ) sum + value;
71          if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
72          {
73              // if the sum overflows or underflows, return error message
74              ResultMessage rm = new ResultMessage();
75              rm.setSequence( am.getSequence() ); // copy sequence
76              rm.setOk( false );
77              session.write( rm );
78          }
79          else
80          {
81              // sum up
82              sum = ( int ) expectedSum;
83              session.setAttachment( new Integer( sum ) );
84  
85              // return the result message
86              ResultMessage rm = new ResultMessage();
87              rm.setSequence( am.getSequence() ); // copy sequence
88              rm.setOk( true );
89              rm.setValue( sum );
90              session.write( rm );
91          }
92      }
93  
94      public void messageSent( ProtocolSession session, Object message )
95      {
96      }
97  
98      public void sessionIdle( ProtocolSession session, IdleStatus status )
99      {
100         SessionLog.warn( session, "Disconnecting the idle." );
101         // disconnect an idle client
102         session.close();
103     }
104 
105     public void exceptionCaught( ProtocolSession session, Throwable cause )
106     {
107         // close the connection on exceptional situation
108         session.close();
109     }
110 }