1 | /* |
2 | * @(#) $Id: Client.java 327113 2005-10-21 06:59:15Z 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.io.filter.IoThreadPoolFilter; |
25 | import org.apache.mina.io.socket.SocketConnector; |
26 | import org.apache.mina.protocol.ProtocolProvider; |
27 | import org.apache.mina.protocol.ProtocolSession; |
28 | import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter; |
29 | import org.apache.mina.protocol.io.IoProtocolConnector; |
30 | |
31 | /** |
32 | * (<strong>Entry Point</strong>) Starts SumUp client. |
33 | * |
34 | * @author The Apache Directory Project |
35 | * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $ |
36 | */ |
37 | public class Client |
38 | { |
39 | private static final String HOSTNAME = "localhost"; |
40 | private static final int PORT = 8080; |
41 | private static final int CONNECT_TIMEOUT = 30; // seconds |
42 | |
43 | public static void main( String[] args ) throws Throwable |
44 | { |
45 | if( args.length == 0 ) |
46 | { |
47 | System.out.println( "Please specify the list of any integers" ); |
48 | return; |
49 | } |
50 | |
51 | // prepare values to sum up |
52 | int[] values = new int[ args.length ]; |
53 | for( int i = 0; i < args.length; i++ ) |
54 | { |
55 | values[ i ] = Integer.parseInt( args[ i ] ); |
56 | } |
57 | |
58 | // Create I/O and Protocol thread pool filter. |
59 | // I/O thread pool performs encoding and decoding of messages. |
60 | // Protocol thread pool performs actual protocol flow. |
61 | IoThreadPoolFilter ioThreadPoolFilter = new IoThreadPoolFilter(); |
62 | ProtocolThreadPoolFilter protocolThreadPoolFilter = new ProtocolThreadPoolFilter(); |
63 | |
64 | // and start both. |
65 | ioThreadPoolFilter.start(); |
66 | protocolThreadPoolFilter.start(); |
67 | |
68 | IoProtocolConnector connector = new IoProtocolConnector( |
69 | new SocketConnector() ); |
70 | connector.getIoConnector().getFilterChain().addFirst( "threadPool", |
71 | ioThreadPoolFilter ); |
72 | connector.getFilterChain().addFirst( "threadPool", |
73 | protocolThreadPoolFilter ); |
74 | |
75 | ProtocolProvider protocolProvider = new ClientProtocolProvider( values ); |
76 | ProtocolSession session; |
77 | for( ;; ) |
78 | { |
79 | try |
80 | { |
81 | session = connector.connect( new InetSocketAddress( HOSTNAME, |
82 | PORT ), CONNECT_TIMEOUT, protocolProvider ); |
83 | break; |
84 | } |
85 | catch( IOException e ) |
86 | { |
87 | System.err.println( "Failed to connect." ); |
88 | e.printStackTrace(); |
89 | Thread.sleep( 5000 ); |
90 | } |
91 | } |
92 | |
93 | // wait until the summation is done |
94 | while( session.isConnected() ) |
95 | { |
96 | Thread.sleep( 100 ); |
97 | } |
98 | |
99 | ioThreadPoolFilter.stop(); |
100 | protocolThreadPoolFilter.stop(); |
101 | } |
102 | } |