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.common.ConnectFuture;
25  import org.apache.mina.common.IoSession;
26  import org.apache.mina.common.RuntimeIOException;
27  import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
28  import org.apache.mina.filter.LoggingFilter;
29  import org.apache.mina.filter.codec.ProtocolCodecFilter;
30  import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
31  import org.apache.mina.transport.socket.nio.SocketConnector;
32  import org.apache.mina.transport.socket.nio.SocketConnectorConfig;
33  
34  /**
35   * (<strong>Entry Point</strong>) Starts SumUp client.
36   * 
37   * @author The Apache Directory Project (mina-dev@directory.apache.org)
38   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
39   */
40  public class Client {
41      private static final String HOSTNAME = "localhost";
42  
43      private static final int PORT = 8080;
44  
45      private static final int CONNECT_TIMEOUT = 30; // seconds
46  
47      // Set this to false to use object serialization instead of custom codec.
48      private static final boolean USE_CUSTOM_CODEC = true;
49  
50      public static void main(String[] args) throws Throwable {
51          if (args.length == 0) {
52              System.out.println("Please specify the list of any integers");
53              return;
54          }
55  
56          // prepare values to sum up
57          int[] values = new int[args.length];
58          for (int i = 0; i < args.length; i++) {
59              values[i] = Integer.parseInt(args[i]);
60          }
61  
62          SocketConnector connector = new SocketConnector();
63  
64          // Change the worker timeout to 1 second to make the I/O thread quit soon
65          // when there's no connection to manage.
66          connector.setWorkerTimeout(1);
67  
68          // Configure the service.
69          SocketConnectorConfig cfg = new SocketConnectorConfig();
70          cfg.setConnectTimeout(CONNECT_TIMEOUT);
71          if (USE_CUSTOM_CODEC) {
72              cfg.getFilterChain().addLast(
73                      "codec",
74                      new ProtocolCodecFilter(
75                              new SumUpProtocolCodecFactory(false)));
76          } else {
77              cfg.getFilterChain().addLast(
78                      "codec",
79                      new ProtocolCodecFilter(
80                              new ObjectSerializationCodecFactory()));
81          }
82          cfg.getFilterChain().addLast("logger", new LoggingFilter());
83  
84          IoSession session;
85          for (;;) {
86              try {
87                  ConnectFuture future = connector.connect(new InetSocketAddress(
88                          HOSTNAME, PORT), new ClientSessionHandler(values), cfg);
89  
90                  future.join();
91                  session = future.getSession();
92                  break;
93              } catch (RuntimeIOException e) {
94                  System.err.println("Failed to connect.");
95                  e.printStackTrace();
96                  Thread.sleep(5000);
97              }
98          }
99  
100         // wait until the summation is done
101         session.getCloseFuture().join();
102     }
103 }