View Javadoc

1   /*
2    *   @(#) $Id: ServerSessionHandler.java 355016 2005-12-08 07:00:30Z 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.common.IoHandler;
23  import org.apache.mina.common.IoSession;
24  import org.apache.mina.examples.sumup.codec.SumUpProtocolCodecFactory;
25  import org.apache.mina.examples.sumup.message.AddMessage;
26  import org.apache.mina.examples.sumup.message.ResultMessage;
27  import org.apache.mina.filter.LoggingFilter;
28  import org.apache.mina.filter.codec.ProtocolCodecFactory;
29  import org.apache.mina.filter.codec.ProtocolCodecFilter;
30  import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
31  import org.apache.mina.util.SessionLog;
32  
33  /***
34   * {@link IoHandler} for SumUp server.
35   * 
36   * @author The Apache Directory Project
37   * @version $Rev: 355016 $, $Date: 2005-12-08 16:00:30 +0900 (Thu, 08 Dec 2005) $
38   */
39  public class ServerSessionHandler implements IoHandler
40  {
41      private final boolean useCustomCodec;
42  
43      public ServerSessionHandler( boolean useCustomCodec )
44      {
45          this.useCustomCodec = useCustomCodec;
46      }
47  
48      public void sessionCreated( IoSession session ) throws Exception
49      {
50          ProtocolCodecFactory codec;
51          if( useCustomCodec )
52          {
53              codec = new SumUpProtocolCodecFactory( true );
54          }
55          else
56          {
57              codec = new ObjectSerializationCodecFactory();
58          }
59  
60          session.getFilterChain().addFirst(
61                  "protocolFilter", new ProtocolCodecFilter( codec ) );
62          session.getFilterChain().addLast(
63                  "logger", new LoggingFilter() );
64      }
65  
66      public void sessionOpened( IoSession session )
67      {
68          // set idle time to 60 seconds
69          session.setIdleTime( IdleStatus.BOTH_IDLE, 60 );
70  
71          // initial sum is zero
72          session.setAttachment( new Integer( 0 ) );
73      }
74  
75      public void sessionClosed( IoSession session )
76      {
77      }
78  
79      public void messageReceived( IoSession session, Object message )
80      {
81          // client only sends AddMessage. otherwise, we will have to identify
82          // its type using instanceof operator.
83          AddMessage am = ( AddMessage ) message;
84  
85          // add the value to the current sum.
86          int sum = ( ( Integer ) session.getAttachment() ).intValue();
87          int value = am.getValue();
88          long expectedSum = ( long ) sum + value;
89          if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
90          {
91              // if the sum overflows or underflows, return error message
92              ResultMessage rm = new ResultMessage();
93              rm.setSequence( am.getSequence() ); // copy sequence
94              rm.setOk( false );
95              session.write( rm );
96          }
97          else
98          {
99              // sum up
100             sum = ( int ) expectedSum;
101             session.setAttachment( new Integer( sum ) );
102 
103             // return the result message
104             ResultMessage rm = new ResultMessage();
105             rm.setSequence( am.getSequence() ); // copy sequence
106             rm.setOk( true );
107             rm.setValue( sum );
108             session.write( rm );
109         }
110     }
111 
112     public void messageSent( IoSession session, Object message )
113     {
114     }
115 
116     public void sessionIdle( IoSession session, IdleStatus status )
117     {
118         SessionLog.info( session, "Disconnecting the idle." );
119         // disconnect an idle client
120         session.close();
121     }
122 
123     public void exceptionCaught( IoSession session, Throwable cause )
124     {
125         // close the connection on exceptional situation
126         session.close();
127     }
128 }