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 java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.RuntimeIoException;
25  import org.apache.mina.core.future.ConnectFuture;
26  import org.apache.mina.core.session.IoSession;
27  import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
28  import org.apache.mina.filter.codec.ProtocolCodecFilter;
29  import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
30  import org.apache.mina.filter.logging.LoggingFilter;
31  import org.apache.mina.transport.socket.nio.NioSocketConnector;
32  
33  /**
34   * (<strong>Entry Point</strong>) Starts SumUp client.
35   *
36   * @author The Apache MINA Project (dev@mina.apache.org)
37   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
38   */
39  public class Client {
40      private static final String HOSTNAME = "localhost";
41  
42      private static final int PORT = 8080;
43  
44      private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds
45  
46      // Set this to false to use object serialization instead of custom codec.
47      private static final boolean USE_CUSTOM_CODEC = true;
48  
49      public static void main(String[] args) throws Throwable {
50          if (args.length == 0) {
51              System.out.println("Please specify the list of any integers");
52              return;
53          }
54  
55          // prepare values to sum up
56          int[] values = new int[args.length];
57          for (int i = 0; i < args.length; i++) {
58              values[i] = Integer.parseInt(args[i]);
59          }
60  
61          NioSocketConnector connector = new NioSocketConnector();
62  
63          // Configure the service.
64          connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
65          if (USE_CUSTOM_CODEC) {
66              connector.getFilterChain().addLast(
67                      "codec",
68                      new ProtocolCodecFilter(
69                              new SumUpProtocolCodecFactory(false)));
70          } else {
71              connector.getFilterChain().addLast(
72                      "codec",
73                      new ProtocolCodecFilter(
74                              new ObjectSerializationCodecFactory()));
75          }
76          connector.getFilterChain().addLast("logger", new LoggingFilter());
77  
78          connector.setHandler(new ClientSessionHandler(values));
79  
80          IoSession session;
81          for (;;) {
82              try {
83                  ConnectFuture future = connector.connect(new InetSocketAddress(
84                          HOSTNAME, PORT));
85                  future.awaitUninterruptibly();
86                  session = future.getSession();
87                  break;
88              } catch (RuntimeIoException e) {
89                  System.err.println("Failed to connect.");
90                  e.printStackTrace();
91                  Thread.sleep(5000);
92              }
93          }
94  
95          // wait until the summation is done
96          session.getCloseFuture().awaitUninterruptibly();
97          
98          connector.dispose();
99      }
100 }