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.IoSession;
25  import org.apache.mina.example.sumup.message.AddMessage;
26  import org.apache.mina.example.sumup.message.ResultMessage;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * {@link IoHandler} for SumUp client.
32   *
33   * @author The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
35   */
36  public class ClientSessionHandler extends IoHandlerAdapter {
37  
38      private final Logger logger = LoggerFactory.getLogger(getClass());
39      
40      private final int[] values;
41  
42      private boolean finished;
43  
44      public ClientSessionHandler(int[] values) {
45          this.values = values;
46      }
47  
48      public boolean isFinished() {
49          return finished;
50      }
51  
52      @Override
53      public void sessionOpened(IoSession session) {
54          // send summation requests
55          for (int i = 0; i < values.length; i++) {
56              AddMessage m = new AddMessage();
57              m.setSequence(i);
58              m.setValue(values[i]);
59              session.write(m);
60          }
61      }
62  
63      @Override
64      public void messageReceived(IoSession session, Object message) {
65          // server only sends ResultMessage. otherwise, we will have to identify
66          // its type using instanceof operator.
67          ResultMessage rm = (ResultMessage) message;
68          if (rm.isOk()) {
69              // server returned OK code.
70              // if received the result message which has the last sequence
71              // number,
72              // it is time to disconnect.
73              if (rm.getSequence() == values.length - 1) {
74                  // print the sum and disconnect.
75                  logger.info("The sum: " + rm.getValue());
76                  session.close();
77                  finished = true;
78              }
79          } else {
80              // seever returned error code because of overflow, etc.
81              logger.warn("Server error, disconnecting...");
82              session.close();
83              finished = true;
84          }
85      }
86  
87      @Override
88      public void exceptionCaught(IoSession session, Throwable cause) {
89          session.close();
90      }
91  }