View Javadoc

1   /*
2    *   @(#) $Id: ClientSessionHandler.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.IoHandler;
22  import org.apache.mina.common.IoHandlerAdapter;
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 client.
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 ClientSessionHandler extends IoHandlerAdapter
40  {
41      private final boolean useCustomCodec;
42      private final int[] values;
43      private boolean finished;
44  
45      public ClientSessionHandler( boolean useCustomCodec, int[] values )
46      {
47          this.useCustomCodec = useCustomCodec;
48          this.values = values;
49      }
50  
51      public boolean isFinished()
52      {
53          return finished;
54      }
55  
56      public void sessionCreated( IoSession session ) throws Exception
57      {
58          ProtocolCodecFactory codec;
59          if( useCustomCodec )
60          {
61              codec = new SumUpProtocolCodecFactory( false );
62          }
63          else
64          {
65              codec = new ObjectSerializationCodecFactory();
66          }
67  
68          session.getFilterChain().addLast(
69                  "protocolFilter", new ProtocolCodecFilter( codec ) );
70          session.getFilterChain().addLast(
71                  "logger", new LoggingFilter() );
72      }
73  
74      public void sessionOpened( IoSession session )
75      {
76          // send summation requests
77          for( int i = 0; i < values.length; i++ )
78          {
79              AddMessage m = new AddMessage();
80              m.setSequence( i );
81              m.setValue( values[ i ] );
82              session.write( m );
83          }
84      }
85  
86      public void messageReceived( IoSession session, Object message )
87      {
88          // server only sends ResultMessage. otherwise, we will have to identify
89          // its type using instanceof operator.
90          ResultMessage rm = ( ResultMessage ) message;
91          if( rm.isOk() )
92          {
93              // server returned OK code.
94              // if received the result message which has the last sequence
95              // number,
96              // it is time to disconnect.
97              if( rm.getSequence() == values.length - 1 )
98              {
99                  // print the sum and disconnect.
100                 SessionLog.info( session, "The sum: " + rm.getValue() );
101                 session.close();
102                 finished = true;
103             }
104         }
105         else
106         {
107             // seever returned error code because of overflow, etc.
108             SessionLog.warn( session, "Server error, disconnecting..." );
109             session.close();
110             finished = true;
111         }
112     }
113 
114     public void exceptionCaught( IoSession session, Throwable cause )
115     {
116         session.close();
117     }
118 }