View Javadoc

1   /*
2    *   @(#) $Id: ClientSessionHandler.java 264677 2005-08-30 02:44:35Z 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 java.util.logging.Level;
22  
23  import org.apache.mina.examples.sumup.message.AddMessage;
24  import org.apache.mina.examples.sumup.message.ResultMessage;
25  import org.apache.mina.protocol.ProtocolHandler;
26  import org.apache.mina.protocol.ProtocolHandlerAdapter;
27  import org.apache.mina.protocol.ProtocolSession;
28  import org.apache.mina.protocol.filter.ProtocolLoggingFilter;
29  import org.apache.mina.util.SessionLog;
30  
31  /***
32   * {@link ProtocolHandler} for SumUp client.
33   * 
34   * @author The Apache Directory Project
35   * @version $Rev: 264677 $, $Date: 2005-08-30 11:44:35 +0900 $
36   */
37  public class ClientSessionHandler extends ProtocolHandlerAdapter
38  {
39      private final int[] values;
40  
41      private boolean finished;
42  
43      public ClientSessionHandler( int[] values )
44      {
45          this.values = values;
46      }
47  
48      public boolean isFinished()
49      {
50          return finished;
51      }
52  
53      public void sessionCreated( ProtocolSession session )
54      {
55          session.getFilterChain().addLast(
56                  "logger", new ProtocolLoggingFilter() );
57      }
58  
59      public void sessionOpened( ProtocolSession session )
60      {
61          // send summation requests
62          for( int i = 0; i < values.length; i++ )
63          {
64              AddMessage m = new AddMessage();
65              m.setSequence( i );
66              m.setValue( values[ i ] );
67              session.write( m );
68          }
69      }
70  
71      public void messageReceived( ProtocolSession session, Object message )
72      {
73          // server only sends ResultMessage. otherwise, we will have to identify
74          // its type using instanceof operator.
75          ResultMessage rm = ( ResultMessage ) message;
76          if( rm.isOk() )
77          {
78              // server returned OK code.
79              // if received the result message which has the last sequence
80              // number,
81              // it is time to disconnect.
82              if( rm.getSequence() == values.length - 1 )
83              {
84                  // print the sum and disconnect.
85                  SessionLog.log( Level.INFO, session, "The sum: " + rm.getValue() );
86                  session.close();
87                  finished = true;
88              }
89          }
90          else
91          {
92              // seever returned error code because of overflow, etc.
93              SessionLog.log( Level.WARNING, session, "Server error, disconnecting..." );
94              session.close();
95              finished = true;
96          }
97      }
98  
99      public void exceptionCaught( ProtocolSession session, Throwable cause )
100     {
101         session.close();
102     }
103 }