001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.sumup;
021:
022: import java.net.InetSocketAddress;
023:
024: import org.apache.mina.common.ConnectFuture;
025: import org.apache.mina.common.IoSession;
026: import org.apache.mina.common.RuntimeIoException;
027: import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
028: import org.apache.mina.filter.codec.ProtocolCodecFilter;
029: import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
030: import org.apache.mina.filter.logging.LoggingFilter;
031: import org.apache.mina.transport.socket.nio.NioSocketConnector;
032:
033: /**
034: * (<strong>Entry Point</strong>) Starts SumUp client.
035: *
036: * @author The Apache MINA Project (dev@mina.apache.org)
037: * @version $Rev: 593019 $, $Date: 2007-11-07 22:42:30 -0700 (Wed, 07 Nov 2007) $
038: */
039: public class Client {
040: private static final String HOSTNAME = "localhost";
041:
042: private static final int PORT = 8080;
043:
044: private static final int CONNECT_TIMEOUT = 30; // seconds
045:
046: // Set this to false to use object serialization instead of custom codec.
047: private static final boolean USE_CUSTOM_CODEC = true;
048:
049: public static void main(String[] args) throws Throwable {
050: if (args.length == 0) {
051: System.out
052: .println("Please specify the list of any integers");
053: return;
054: }
055:
056: // prepare values to sum up
057: int[] values = new int[args.length];
058: for (int i = 0; i < args.length; i++) {
059: values[i] = Integer.parseInt(args[i]);
060: }
061:
062: NioSocketConnector connector = new NioSocketConnector();
063:
064: // Configure the service.
065: connector.setConnectTimeout(CONNECT_TIMEOUT);
066: if (USE_CUSTOM_CODEC) {
067: connector.getFilterChain().addLast(
068: "codec",
069: new ProtocolCodecFilter(
070: new SumUpProtocolCodecFactory(false)));
071: } else {
072: connector.getFilterChain().addLast(
073: "codec",
074: new ProtocolCodecFilter(
075: new ObjectSerializationCodecFactory()));
076: }
077: connector.getFilterChain().addLast("logger",
078: new LoggingFilter());
079:
080: connector.setHandler(new ClientSessionHandler(values));
081:
082: IoSession session;
083: for (;;) {
084: try {
085: ConnectFuture future = connector
086: .connect(new InetSocketAddress(HOSTNAME, PORT));
087: future.awaitUninterruptibly();
088: session = future.getSession();
089: break;
090: } catch (RuntimeIoException e) {
091: System.err.println("Failed to connect.");
092: e.printStackTrace();
093: Thread.sleep(5000);
094: }
095: }
096:
097: // wait until the summation is done
098: session.getCloseFuture().awaitUninterruptibly();
099:
100: connector.dispose();
101: }
102: }
|