View Javadoc

1   /*
2    *   @(#) $Id: Client.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 java.io.IOException;
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.common.ConnectFuture;
25  import org.apache.mina.common.IoConnector;
26  import org.apache.mina.common.IoSession;
27  import org.apache.mina.filter.ThreadPoolFilter;
28  import org.apache.mina.transport.socket.nio.SocketConnector;
29  
30  /***
31   * (<strong>Entry Point</strong>) Starts SumUp client.
32   * 
33   * @author The Apache Directory Project
34   * @version $Rev: 355016 $, $Date: 2005-12-08 16:00:30 +0900 (Thu, 08 Dec 2005) $
35   */
36  public class Client
37  {
38      private static final String HOSTNAME = "localhost";
39      private static final int PORT = 8080;
40      private static final int CONNECT_TIMEOUT = 30; // seconds
41      // Set this to false to use object serialization instead of custom codec.
42      private static final boolean USE_CUSTOM_CODEC = true;
43  
44      public static void main( String[] args ) throws Throwable
45      {
46          if( args.length == 0 )
47          {
48              System.out.println( "Please specify the list of any integers" );
49              return;
50          }
51  
52          // prepare values to sum up
53          int[] values = new int[ args.length ];
54          for( int i = 0; i < args.length; i++ )
55          {
56              values[ i ] = Integer.parseInt( args[ i ] );
57          }
58  
59          // Create I/O and Protocol thread pool filter.
60          // I/O thread pool performs encoding and decoding of messages.
61          // Protocol thread pool performs actual protocol flow.
62          ThreadPoolFilter ioThreadPoolFilter = new ThreadPoolFilter();
63          ThreadPoolFilter protocolThreadPoolFilter = new ThreadPoolFilter();
64          IoConnector connector = new SocketConnector();
65          connector.getFilterChain().addFirst(
66                  "ioThreadPool", ioThreadPoolFilter );
67          connector.getFilterChain().addLast(
68                  "protocolThreadPool", protocolThreadPoolFilter );
69  
70          // Set connect timeout.
71          connector.setConnectTimeout( CONNECT_TIMEOUT );
72          
73          IoSession session;
74          for( ;; )
75          {
76              try
77              {
78                  ConnectFuture future = connector.connect(
79                          new InetSocketAddress( HOSTNAME, PORT ),
80                          new ClientSessionHandler( USE_CUSTOM_CODEC, values ) );
81                  
82                  future.join();
83                  session = future.getSession();
84                  break;
85              }
86              catch( IOException e )
87              {
88                  System.err.println( "Failed to connect." );
89                  e.printStackTrace();
90                  Thread.sleep( 5000 );
91              }
92          }
93  
94          // wait until the summation is done
95          session.getCloseFuture().join();
96      }
97  }