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 */
020package org.apache.mina.example.sumup;
021
022import java.net.InetSocketAddress;
023
024import org.apache.mina.core.RuntimeIoException;
025import org.apache.mina.core.future.ConnectFuture;
026import org.apache.mina.core.session.IoSession;
027import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
028import org.apache.mina.filter.codec.ProtocolCodecFilter;
029import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
030import org.apache.mina.filter.logging.LoggingFilter;
031import org.apache.mina.transport.socket.nio.NioSocketConnector;
032
033/**
034 * (<strong>Entry Point</strong>) Starts SumUp client.
035 *
036 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
037 */
038public class Client {
039    private static final String HOSTNAME = "localhost";
040
041    private static final int PORT = 8080;
042
043    private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds
044
045    // Set this to false to use object serialization instead of custom codec.
046    private static final boolean USE_CUSTOM_CODEC = true;
047
048    public static void main(String[] args) throws Throwable {
049        if (args.length == 0) {
050            System.out.println("Please specify the list of any integers");
051            return;
052        }
053
054        // prepare values to sum up
055        int[] values = new int[args.length];
056        for (int i = 0; i < args.length; i++) {
057            values[i] = Integer.parseInt(args[i]);
058        }
059
060        NioSocketConnector connector = new NioSocketConnector();
061
062        // Configure the service.
063        connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
064        if (USE_CUSTOM_CODEC) {
065            connector.getFilterChain().addLast(
066                    "codec",
067                    new ProtocolCodecFilter(
068                            new SumUpProtocolCodecFactory(false)));
069        } else {
070            connector.getFilterChain().addLast(
071                    "codec",
072                    new ProtocolCodecFilter(
073                            new ObjectSerializationCodecFactory()));
074        }
075        connector.getFilterChain().addLast("logger", new LoggingFilter());
076
077        connector.setHandler(new ClientSessionHandler(values));
078
079        IoSession session;
080        for (;;) {
081            try {
082                ConnectFuture future = connector.connect(new InetSocketAddress(
083                        HOSTNAME, PORT));
084                future.awaitUninterruptibly();
085                session = future.getSession();
086                break;
087            } catch (RuntimeIoException e) {
088                System.err.println("Failed to connect.");
089                e.printStackTrace();
090                Thread.sleep(5000);
091            }
092        }
093
094        // wait until the summation is done
095        session.getCloseFuture().awaitUninterruptibly();
096        
097        connector.dispose();
098    }
099}